2023-03-15 06:42:14 +03:00
|
|
|
/** @file Definition of hash computing functions. */
|
|
|
|
|
|
|
|
// Eslint is not (and should not be) set up to recognize CommonJS imports.
|
|
|
|
/* eslint-disable no-restricted-syntax */
|
|
|
|
const cryptoModule = require('crypto')
|
2021-11-05 14:51:43 +03:00
|
|
|
const fs = require('fs')
|
2023-03-15 06:42:14 +03:00
|
|
|
const pathModule = require('path')
|
|
|
|
/* eslint-enable no-restricted-syntax */
|
2021-02-05 03:44:21 +03:00
|
|
|
|
|
|
|
// =================
|
|
|
|
// === Constants ===
|
|
|
|
// =================
|
2023-03-15 06:42:14 +03:00
|
|
|
/** @typedef {"md5" | "sha1" | "sha256"} ChecksumType */
|
2021-02-05 03:44:21 +03:00
|
|
|
const CHECKSUM_TYPE = 'sha256'
|
|
|
|
|
|
|
|
// ================
|
|
|
|
// === Checksum ===
|
|
|
|
// ================
|
|
|
|
|
2023-05-19 22:55:29 +03:00
|
|
|
/** The `type` argument can be one of `md5`, `sha1`, or `sha256`.
|
2023-03-15 06:42:14 +03:00
|
|
|
* @param {string} path - Path to the file.
|
2023-04-05 08:13:18 +03:00
|
|
|
* @param {ChecksumType} type - The checksum algorithm to use.
|
|
|
|
* @returns {Promise<string>} A promise that resolves to the checksum. */
|
2021-11-05 14:51:43 +03:00
|
|
|
function getChecksum(path, type) {
|
2023-03-15 06:42:14 +03:00
|
|
|
return new Promise(
|
|
|
|
// This JSDoc annotation is required for correct types that are also type-safe.
|
|
|
|
/** @param {(value: string) => void} resolve - Fulfill the promise with the given value. */
|
2023-03-20 12:35:16 +03:00
|
|
|
(resolve, reject) => {
|
2023-03-15 06:42:14 +03:00
|
|
|
const hash = cryptoModule.createHash(type)
|
|
|
|
const input = fs.createReadStream(path)
|
|
|
|
input.on('error', reject)
|
2023-03-20 12:35:16 +03:00
|
|
|
input.on('data', chunk => {
|
2023-03-15 06:42:14 +03:00
|
|
|
hash.update(chunk)
|
|
|
|
})
|
2023-03-20 12:35:16 +03:00
|
|
|
input.on('close', () => {
|
2023-03-15 06:42:14 +03:00
|
|
|
resolve(hash.digest('hex'))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2021-02-05 03:44:21 +03:00
|
|
|
}
|
|
|
|
|
2023-03-15 06:42:14 +03:00
|
|
|
/** Based on https://stackoverflow.com/a/57371333.
|
|
|
|
* @param {string} file - The path to the file.
|
|
|
|
* @param {string} extension - The new extension of the file.
|
|
|
|
* @returns A path with the new exension. */
|
2022-05-23 05:16:04 +03:00
|
|
|
function changeExtension(file, extension) {
|
2023-03-15 06:42:14 +03:00
|
|
|
const basename = pathModule.basename(file, pathModule.extname(file))
|
|
|
|
return pathModule.join(pathModule.dirname(file), `${basename}.${extension}`)
|
2022-05-23 05:16:04 +03:00
|
|
|
}
|
|
|
|
|
2023-03-15 06:42:14 +03:00
|
|
|
/** Write the file checksum to the provided path.
|
|
|
|
* @param {string} path - The path to the file.
|
|
|
|
* @param {ChecksumType} type - The checksum algorithm to use. */
|
2021-11-05 14:51:43 +03:00
|
|
|
async function writeFileChecksum(path, type) {
|
2023-06-19 02:02:08 +03:00
|
|
|
const checksum = await getChecksum(path, type)
|
|
|
|
const targetPath = changeExtension(path, type)
|
2023-04-05 08:13:18 +03:00
|
|
|
console.log(`Writing ${targetPath}. Checksum is ${checksum}.`)
|
|
|
|
await fs.promises.writeFile(targetPath, checksum, 'utf8')
|
2021-02-05 03:44:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ================
|
|
|
|
// === Callback ===
|
|
|
|
// ================
|
|
|
|
|
2023-03-15 06:42:14 +03:00
|
|
|
/** Generates checksums for all build artifacts.
|
|
|
|
* @param {import('electron-builder').BuildResult} context - Build information. */
|
2022-05-23 05:16:04 +03:00
|
|
|
exports.default = async function (context) {
|
2023-05-19 22:55:29 +03:00
|
|
|
// `context` is BuildResult, see
|
|
|
|
// https://www.electron.build/configuration/configuration.html#buildresult
|
2023-03-15 06:42:14 +03:00
|
|
|
for (const 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 []
|
|
|
|
}
|