2021-11-05 14:51:43 +03:00
|
|
|
const crypto = require('crypto')
|
|
|
|
const fs = require('fs')
|
2022-05-23 05:16:04 +03:00
|
|
|
const path = require('path')
|
2021-02-05 03:44:21 +03:00
|
|
|
|
|
|
|
// =================
|
|
|
|
// === Constants ===
|
|
|
|
// =================
|
|
|
|
|
|
|
|
const CHECKSUM_TYPE = 'sha256'
|
|
|
|
|
|
|
|
// ================
|
|
|
|
// === Checksum ===
|
|
|
|
// ================
|
|
|
|
|
|
|
|
/// The `type` argument can be one of `md5`, `sha1`, or `sha256`.
|
2021-11-05 14:51:43 +03:00
|
|
|
function getChecksum(path, type) {
|
2021-02-05 03:44:21 +03:00
|
|
|
return new Promise(function (resolve, reject) {
|
2021-11-05 14:51:43 +03:00
|
|
|
const hash = crypto.createHash(type)
|
|
|
|
const input = fs.createReadStream(path)
|
|
|
|
input.on('error', reject)
|
2021-02-05 03:44:21 +03:00
|
|
|
input.on('data', function (chunk) {
|
2021-11-05 14:51:43 +03:00
|
|
|
hash.update(chunk)
|
|
|
|
})
|
2021-02-05 03:44:21 +03:00
|
|
|
input.on('close', function () {
|
2021-11-05 14:51:43 +03:00
|
|
|
resolve(hash.digest('hex'))
|
|
|
|
})
|
|
|
|
})
|
2021-02-05 03:44:21 +03:00
|
|
|
}
|
|
|
|
|
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}`)
|
|
|
|
}
|
|
|
|
|
2021-11-05 14:51:43 +03:00
|
|
|
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}.`)
|
2021-11-05 14:51:43 +03:00
|
|
|
fs.writeFile(targetPath, checksum, 'utf8', err => {
|
|
|
|
if (err) {
|
|
|
|
throw err
|
|
|
|
}
|
2021-02-05 03:44:21 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ================
|
|
|
|
// === 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) {
|
2021-02-05 03:44:21 +03:00
|
|
|
console.log(`Generating ${CHECKSUM_TYPE} checksum for ${file}.`)
|
2021-11-05 14:51:43 +03:00
|
|
|
await writeFileChecksum(file, CHECKSUM_TYPE)
|
2021-02-05 03:44:21 +03:00
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|