Skip to content

Instantly share code, notes, and snippets.

@Karbust
Created April 5, 2021 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Karbust/14bbaba7910b72023e0229abf53e8d54 to your computer and use it in GitHub Desktop.
Save Karbust/14bbaba7910b72023e0229abf53e8d54 to your computer and use it in GitHub Desktop.
Script to generate webserver JSON file
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!')
})
})
@JohnTonS
Copy link

JohnTonS commented Jul 3, 2022

Hola, how do I use this Script?

@Fuzkyn
Copy link

Fuzkyn commented Dec 30, 2022

how do we run the script?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment