Renamed js files to ts

This commit is contained in:
mrfry 2020-11-23 15:10:24 +01:00
parent 0bddef2b78
commit 7fcb15da88
54 changed files with 8521 additions and 60 deletions

View file

@ -1,146 +0,0 @@
module.exports = {
ReadFile: ReadFile,
ReadJSON: ReadJSON,
WriteFile: WriteFile,
writeFileAsync: WriteFileAsync,
AppendToFile: AppendToFile,
FileExists: FileExists,
CreatePath: CreatePath,
WatchFile: WatchFile,
ReadDir: ReadDir,
CopyFile: CopyFile,
GetDateString: GetDateString,
}
const fs = require('fs')
const logger = require('../utils/logger.js')
function GetDateString(noTime) {
const date = new Date()
if (noTime) {
return (
date.getFullYear() +
'-' +
('0' + (date.getMonth() + 1)).slice(-2) +
'-' +
('0' + date.getDate()).slice(-2)
)
} else {
return (
date.getFullYear() +
'-' +
('0' + (date.getMonth() + 1)).slice(-2) +
'-' +
('0' + date.getDate()).slice(-2) +
' ' +
('0' + date.getHours()).slice(-2) +
':' +
('0' + date.getMinutes()).slice(-2) +
':' +
('0' + date.getSeconds()).slice(-2)
)
}
}
function CopyFile(from, to) {
CreatePath(to)
fs.copyFileSync(from, to)
}
function ReadDir(path) {
return fs.readdirSync(path)
}
function ReadJSON(name) {
try {
return JSON.parse(ReadFile(name))
} catch (err) {
console.error(err)
throw new Error('Coulndt parse JSON in "ReadJSON", file: ' + name)
}
}
function ReadFile(name) {
if (!FileExists(name)) {
throw new Error('No such file: ' + name)
}
return fs.readFileSync(name, 'utf8')
}
function FileExists(path) {
return fs.existsSync(path)
}
function WatchFile(file, callback) {
if (FileExists(file)) {
fs.watchFile(file, () => {
fs.readFile(file, 'utf8', (err, data) => {
if (err) {
// console.log(err)
} else {
callback(data)
}
})
})
} else {
console.log(file + ' does not eadjsalék')
setTimeout(() => {
WatchFile(file)
}, 1000)
}
}
function CreatePath(path, onlyPath) {
if (FileExists(path)) {
return
}
var spath = path.split('/')
var currDir = spath[0]
for (var i = 1; i < spath.length; i++) {
if (currDir !== '' && !fs.existsSync(currDir)) {
try {
fs.mkdirSync(currDir)
} catch (err) {
console.log('Failed to make ' + currDir + ' directory... ')
console.error(err)
}
}
currDir += '/' + spath[i]
}
if (onlyPath) {
fs.mkdirSync(path)
}
}
function WriteFile(content, path) {
CreatePath(path)
fs.writeFileSync(path, content)
}
function WriteFileAsync(content, path) {
CreatePath(path)
fs.writeFile(path, content, function(err) {
if (err) {
logger.Log(
'Error writing file: ' + path + ' (sync)',
logger.GetColor('redbg')
)
}
})
}
function AppendToFile(data, file) {
CreatePath(file)
try {
fs.appendFileSync(file, '\n' + data)
} catch (err) {
logger.Log(
'Error appendig to file log file: ' + file + ' (sync)',
logger.GetColor('redbg')
)
logger.Log(data)
console.error(err)
}
}