2021-11-05 14:51:43 +03:00
|
|
|
const crypto = require('crypto')
|
|
|
|
const fs = require('fs')
|
|
|
|
const glob = require('glob')
|
|
|
|
const paths = require('../../../../../build/paths')
|
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
|
|
|
}
|
|
|
|
|
2021-11-05 14:51:43 +03:00
|
|
|
async function writeFileChecksum(path, type) {
|
|
|
|
let checksum = await getChecksum(path, type)
|
2021-02-05 03:44:21 +03:00
|
|
|
let targetPath = `${path}.${type}`
|
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 ===
|
|
|
|
// ================
|
|
|
|
|
|
|
|
exports.default = async function () {
|
|
|
|
let files = glob.sync(paths.dist.client + '/*.{dmg,exe,AppImage}')
|
|
|
|
for (let file of files) {
|
|
|
|
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 []
|
|
|
|
}
|