2020-01-10 15:55:35 +03:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
|
2021-04-02 12:59:49 +03:00
|
|
|
function readFile(filePath, parseJson = true) {
|
2020-01-10 15:55:35 +03:00
|
|
|
try {
|
2021-02-26 05:39:48 +03:00
|
|
|
const content = fs.readFileSync(filePath)
|
2021-04-02 12:59:49 +03:00
|
|
|
if (!parseJson) return content
|
2020-01-10 15:55:35 +03:00
|
|
|
const json = JSON.parse(content)
|
|
|
|
const timeDiff = Math.floor(Date.now() / 1000) - json.lastChecked
|
|
|
|
return Object.assign({}, json, { timeDiff })
|
2021-06-12 00:24:20 +03:00
|
|
|
} catch (_) {
|
|
|
|
console.error(`File ${filePath} does not exist`)
|
2020-01-10 15:55:35 +03:00
|
|
|
return {
|
|
|
|
error: 'the bridge statistics are not available'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 12:59:49 +03:00
|
|
|
function writeFile(filePath, object, paramOptions = {}) {
|
|
|
|
const defaultOptions = {
|
|
|
|
useCwd: true,
|
|
|
|
stringify: true
|
|
|
|
}
|
|
|
|
const { useCwd, stringify } = Object.assign({}, defaultOptions, paramOptions)
|
|
|
|
|
2020-05-07 20:42:27 +03:00
|
|
|
const fullPath = useCwd ? path.join(process.cwd(), filePath) : filePath
|
2021-04-02 12:59:49 +03:00
|
|
|
fs.writeFileSync(fullPath, stringify ? JSON.stringify(object, null, 4) : object)
|
2020-01-10 15:55:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function createDir(dirPath) {
|
|
|
|
try {
|
|
|
|
fs.mkdirSync(path.join(process.cwd(), dirPath), { recursive: true })
|
|
|
|
} catch (e) {
|
|
|
|
if (!e.message.includes('exists')) {
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 20:42:27 +03:00
|
|
|
function readCacheFile(filePath) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(fs.readFileSync(filePath))
|
|
|
|
} catch (_) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 14:24:42 +03:00
|
|
|
function writeCacheFile(filePath, object) {
|
|
|
|
fs.mkdirSync(path.dirname(filePath), { recursive: true })
|
|
|
|
fs.writeFileSync(filePath, JSON.stringify(object))
|
|
|
|
}
|
|
|
|
|
2020-10-29 11:25:43 +03:00
|
|
|
function readAccessListFile(filePath) {
|
|
|
|
const data = fs.readFileSync(filePath)
|
|
|
|
return data
|
|
|
|
.toString()
|
|
|
|
.split('\n')
|
|
|
|
.map(addr => addr.trim().toLowerCase())
|
|
|
|
.filter(addr => addr.length === 42)
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:55:35 +03:00
|
|
|
module.exports = {
|
|
|
|
readFile,
|
|
|
|
writeFile,
|
2020-05-07 20:42:27 +03:00
|
|
|
createDir,
|
2020-10-29 11:25:43 +03:00
|
|
|
readCacheFile,
|
2020-11-04 14:24:42 +03:00
|
|
|
writeCacheFile,
|
2020-10-29 11:25:43 +03:00
|
|
|
readAccessListFile
|
2020-01-10 15:55:35 +03:00
|
|
|
}
|