-
-
Save Karbust/14bbaba7910b72023e0229abf53e8d54 to your computer and use it in GitHub Desktop.
Script to generate webserver JSON file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs") | |
const path = require("path") | |
const crypto = require('crypto') | |
const getFileChecksum = (filePath) => { | |
return new Promise((resolve, reject) => { | |
const hash = crypto.createHash('sha256') | |
const input = fs.createReadStream(filePath) | |
input.on('error', reject) | |
input.on('data', (chunk) => hash.update(chunk)) | |
input.on('close', () => { | |
resolve(hash.digest('hex')) | |
}) | |
}) | |
} | |
const getAllFiles = (dirPath, arrayOfFiles) => { | |
files = fs.readdirSync(dirPath) | |
arrayOfFiles = arrayOfFiles || [] | |
files.forEach(async (file) => { | |
const filePath = path.join(__dirname, dirPath, "/", file) | |
if (fs.statSync(dirPath + "/" + file).isDirectory()) { | |
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles) | |
} else { | |
arrayOfFiles.push({ | |
fileName: filePath, | |
size: fs.statSync(filePath).size, | |
}) | |
} | |
}) | |
return arrayOfFiles | |
} | |
const result = getAllFiles("./files").map(file => { | |
return getFileChecksum(file.fileName).then((data) => { | |
file.fileName = file.fileName.split(path.join(__dirname, "./files/")).pop() | |
file.hash = data | |
return file | |
}) | |
}) | |
Promise.all(result).then((data) => { | |
fs.writeFile("./files.json", JSON.stringify(data), (err) => { | |
if (err) throw err | |
console.log(data.length) | |
console.log('The file has been saved!') | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hola, how do I use this Script?