chains/scripts/verify.js

53 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-07-02 10:00:13 +00:00
const fs = require('fs')
const path = require('path')
const {
CHAINS_DIRECTORY,
2019-07-02 10:17:25 +00:00
startSpinner,
stopSpinner,
2019-07-02 10:00:13 +00:00
tableLog,
stat,
writeJson,
toNumber,
verifyJson
} = require('./shared')
fs.readdir(CHAINS_DIRECTORY, async function (err, files) {
if (err) {
console.error('Could not list the directory.', err)
process.exit(1)
}
2019-07-02 10:18:44 +00:00
startSpinner(`Verifying ${CHAINS_DIRECTORY} files`)
2019-07-02 10:00:13 +00:00
let result = []
await Promise.all(
files.map(async function (file, index) {
const filePath = path.join(CHAINS_DIRECTORY, file)
const fileStat = await stat(filePath)
const ext = path.extname(file)
if (fileStat.isFile() && ext === '.json') {
let json = require(filePath)
const fileName = file.replace(ext, '')
if (toNumber(fileName)) {
json.chainId = toNumber(fileName)
}
json = await verifyJson(json)
await writeJson(filePath, json)
result.push(json)
}
return fileStat
})
)
2019-07-02 10:17:25 +00:00
stopSpinner()
2019-07-02 10:00:13 +00:00
tableLog(result)
console.log(
2019-07-02 10:18:44 +00:00
`Successfully verified and wrote ${files.length} file${
files.length > 1 ? 's' : ''
}`
2019-07-02 10:00:13 +00:00
)
})