mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Prettied all js in src/
This commit is contained in:
parent
3f081d8dff
commit
ee0f0a9f3b
17 changed files with 1012 additions and 688 deletions
|
@ -11,7 +11,7 @@ module.exports = {
|
|||
WatchFile: WatchFile,
|
||||
ReadDir: ReadDir,
|
||||
CopyFile: CopyFile,
|
||||
GetDateString: GetDateString
|
||||
GetDateString: GetDateString,
|
||||
}
|
||||
|
||||
var fs = require('fs')
|
||||
|
@ -20,26 +20,33 @@ var logger = require('../utils/logger.js')
|
|||
|
||||
const dataFile = './qminingPublic/data.json'
|
||||
|
||||
function GetDateString () {
|
||||
function GetDateString() {
|
||||
const m = new Date()
|
||||
return m.getFullYear() + '-' +
|
||||
('0' + (m.getMonth() + 1)).slice(-2) + '-' +
|
||||
('0' + m.getDate()).slice(-2) + ' ' +
|
||||
('0' + m.getHours()).slice(-2) + ':' +
|
||||
('0' + m.getMinutes()).slice(-2) + ':' +
|
||||
return (
|
||||
m.getFullYear() +
|
||||
'-' +
|
||||
('0' + (m.getMonth() + 1)).slice(-2) +
|
||||
'-' +
|
||||
('0' + m.getDate()).slice(-2) +
|
||||
' ' +
|
||||
('0' + m.getHours()).slice(-2) +
|
||||
':' +
|
||||
('0' + m.getMinutes()).slice(-2) +
|
||||
':' +
|
||||
('0' + m.getSeconds()).slice(-2)
|
||||
)
|
||||
}
|
||||
|
||||
function CopyFile (from, to) {
|
||||
function CopyFile(from, to) {
|
||||
CreatePath(to)
|
||||
fs.copyFileSync(from, to)
|
||||
}
|
||||
|
||||
function ReadDir (path) {
|
||||
function ReadDir(path) {
|
||||
return fs.readdirSync(path)
|
||||
}
|
||||
|
||||
function ReadJSON (name) {
|
||||
function ReadJSON(name) {
|
||||
try {
|
||||
return JSON.parse(ReadFile(name))
|
||||
} catch (e) {
|
||||
|
@ -48,16 +55,18 @@ function ReadJSON (name) {
|
|||
}
|
||||
}
|
||||
|
||||
function ReadFile (name) {
|
||||
if (!FileExists(name)) { throw new Error('No such file: ' + name) }
|
||||
function ReadFile(name) {
|
||||
if (!FileExists(name)) {
|
||||
throw new Error('No such file: ' + name)
|
||||
}
|
||||
return fs.readFileSync(name, 'utf8')
|
||||
}
|
||||
|
||||
function FileExists (path) {
|
||||
function FileExists(path) {
|
||||
return fs.existsSync(path)
|
||||
}
|
||||
|
||||
function WatchFile (file, callback) {
|
||||
function WatchFile(file, callback) {
|
||||
if (FileExists(file)) {
|
||||
fs.watchFile(file, (curr, prev) => {
|
||||
fs.readFile(file, 'utf8', (err, data) => {
|
||||
|
@ -76,8 +85,10 @@ function WatchFile (file, callback) {
|
|||
}
|
||||
}
|
||||
|
||||
function CreatePath (path, onlyPath) {
|
||||
if (FileExists(path)) { return }
|
||||
function CreatePath(path, onlyPath) {
|
||||
if (FileExists(path)) {
|
||||
return
|
||||
}
|
||||
|
||||
var p = path.split('/')
|
||||
var currDir = p[0]
|
||||
|
@ -85,7 +96,9 @@ function CreatePath (path, onlyPath) {
|
|||
if (currDir !== '' && !fs.existsSync(currDir)) {
|
||||
try {
|
||||
fs.mkdirSync(currDir)
|
||||
} catch (e) { console.log('Failed to make ' + currDir + ' directory... ') }
|
||||
} catch (e) {
|
||||
console.log('Failed to make ' + currDir + ' directory... ')
|
||||
}
|
||||
}
|
||||
currDir += '/' + p[i]
|
||||
}
|
||||
|
@ -95,32 +108,38 @@ function CreatePath (path, onlyPath) {
|
|||
}
|
||||
}
|
||||
|
||||
function WriteFile (content, path) {
|
||||
function WriteFile(content, path) {
|
||||
CreatePath(path)
|
||||
fs.writeFileSync(path, content)
|
||||
}
|
||||
|
||||
function WriteFileAsync (content, path) {
|
||||
function WriteFileAsync(content, path) {
|
||||
CreatePath(path)
|
||||
fs.writeFile(path, content, function (err) {
|
||||
fs.writeFile(path, content, function(err) {
|
||||
if (err) {
|
||||
logger.Log('Error writing file: ' + path + ' (sync)', logger.GetColor('redbg'))
|
||||
logger.Log(
|
||||
'Error writing file: ' + path + ' (sync)',
|
||||
logger.GetColor('redbg')
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function AppendToFile (data, file) {
|
||||
function AppendToFile(data, file) {
|
||||
CreatePath(file)
|
||||
try {
|
||||
fs.appendFileSync(file, '\n' + data)
|
||||
} catch (e) {
|
||||
logger.Log('Error appendig to file log file: ' + file + ' (sync)', logger.GetColor('redbg'))
|
||||
logger.Log(
|
||||
'Error appendig to file log file: ' + file + ' (sync)',
|
||||
logger.GetColor('redbg')
|
||||
)
|
||||
logger.Log(data)
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
function Beep () {
|
||||
function Beep() {
|
||||
try {
|
||||
process.stdout.write('\x07')
|
||||
} catch (e) {
|
||||
|
@ -128,9 +147,12 @@ function Beep () {
|
|||
}
|
||||
}
|
||||
|
||||
function WriteBackup () {
|
||||
function WriteBackup() {
|
||||
try {
|
||||
WriteFileAsync(ReadFile(dataFile), 'public/backs/data_' + new Date().toString())
|
||||
WriteFileAsync(
|
||||
ReadFile(dataFile),
|
||||
'public/backs/data_' + new Date().toString()
|
||||
)
|
||||
} catch (e) {
|
||||
logger.Log('Error backing up data json file!', logger.GetColor('redbg'))
|
||||
console.log(e)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue