enso/app/ide-desktop/lib/client/tasks/computeHashes.cjs

59 lines
1.6 KiB
JavaScript
Raw Normal View History

const crypto = require('crypto')
const fs = require('fs')
2022-05-23 05:16:04 +03:00
const path = require('path')
// =================
// === Constants ===
// =================
const CHECKSUM_TYPE = 'sha256'
// ================
// === Checksum ===
// ================
/// The `type` argument can be one of `md5`, `sha1`, or `sha256`.
function getChecksum(path, type) {
return new Promise(function (resolve, reject) {
const hash = crypto.createHash(type)
const input = fs.createReadStream(path)
input.on('error', reject)
input.on('data', function (chunk) {
hash.update(chunk)
})
input.on('close', function () {
resolve(hash.digest('hex'))
})
})
}
2022-05-23 05:16:04 +03:00
// Based on https://stackoverflow.com/a/57371333
function changeExtension(file, extension) {
const basename = path.basename(file, path.extname(file))
return path.join(path.dirname(file), `${basename}.${extension}`)
}
async function writeFileChecksum(path, type) {
let checksum = await getChecksum(path, type)
2022-05-23 05:16:04 +03:00
let targetPath = changeExtension(path, type)
console.log(`Writing ${targetPath}.`)
fs.writeFile(targetPath, checksum, 'utf8', err => {
if (err) {
throw err
}
})
}
// ================
// === Callback ===
// ================
2022-05-23 05:16:04 +03:00
exports.default = async function (context) {
// `context` is BuildResult, see https://www.electron.build/configuration/configuration.html#buildresult
for (let file of context.artifactPaths) {
console.log(`Generating ${CHECKSUM_TYPE} checksum for ${file}.`)
await writeFileChecksum(file, CHECKSUM_TYPE)
}
return []
}