export default { ReadFile, ReadJSON, WriteFile, writeFileAsync, AppendToFile, FileExists, CreatePath, WatchFile, ReadDir, CopyFile, GetDateString, formatUrl, deleteFile: deleteFile, } import fs from 'fs' import logger from '../utils/logger' interface URLFormatOptions { pathname?: string query?: any } function formatUrl(options: URLFormatOptions): string { const path = options.pathname || '' if (!options.query || Object.keys(options.query).length === 0) { return path } const queryString = '?' + Object.keys(options.query) .map((key) => { return `${key}=${encodeURIComponent(options.query[key])}` }) .join('&') return path + queryString } function GetDateString(noTime?: boolean): string { 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: string, to: string): void { CreatePath(to) fs.copyFileSync(from, to) } function ReadDir(path: string): Array { return fs.readdirSync(path) } function ReadJSON(name: string): any { try { return JSON.parse(ReadFile(name)) } catch (err) { console.error(err) throw new Error('Coulndt parse JSON in "ReadJSON", file: ' + name) } } function ReadFile(name: string): string { if (!FileExists(name)) { throw new Error('No such file: ' + name) } return fs.readFileSync(name, 'utf8') } function FileExists(path: string): boolean { return fs.existsSync(path) } function WatchFile(file: string, callback: Function): void { if (FileExists(file)) { fs.watchFile(file, () => { fs.readFile(file, 'utf8', (err, data) => { if (err) { // console.log(err) } else { callback(data) } }) }) } else { throw new Error(`${file} does not exits to watch`) } } function CreatePath(path: string, onlyPath?: boolean): void { if (FileExists(path)) { return } const spath = path.split('/') let currDir = spath[0] for (let 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: string, path: string): void { CreatePath(path) fs.writeFileSync(path, content) } function writeFileAsync(content: string, path: string): void { CreatePath(path) fs.writeFile(path, content, function(err) { if (err) { logger.Log( 'Error writing file: ' + path + ' (sync)', logger.GetColor('redbg') ) } }) } function AppendToFile(data: string, file: string): void { 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) } } function deleteFile(fname: string): Boolean { if (FileExists(fname)) { fs.unlinkSync(fname) return true } return false }