add afterAllArtifactBuild function where generate and include checksum files

This commit is contained in:
jjavierdguezas 2020-08-29 16:40:03 +02:00
parent 43d83c09c6
commit a506d63276
3 changed files with 69 additions and 0 deletions

View file

@ -63,6 +63,7 @@
"main": "./app/main.prod.js",
"build": {
"afterSign": "scripts/notarize.js",
"afterAllArtifactBuild": "scripts/extraPublishFiles.js",
"productName": "ResponsivelyApp",
"appId": "app.responsively",
"files": [

View file

@ -0,0 +1,13 @@
const {generateChecksums} = require('./generate-checksums');
exports.default = function() {
return generateChecksums()
.then(files => {
// add *.zip file
return files;
})
.catch(e => {
console.log('Something went wrong generating checksum files');
return [];
});
};

View file

@ -0,0 +1,55 @@
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
function hashFile(file, algorithm = 'sha512', encoding = 'base64', options) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash(algorithm);
hash.on('error', reject).setEncoding(encoding);
fs.createReadStream(
file,
Object.assign({}, options, {
highWaterMark: 1024 * 1024,
/* better to use more memory but hash faster */
})
)
.on('error', reject)
.on('end', () => {
hash.end();
resolve(hash.read());
})
.pipe(hash, {
end: false,
});
});
}
const RELATIVE_FOLDER_PATH = '../release';
const CHECKSUM_SUFFIX = '.checksum.sha512';
const SKIP_SUFFIX_LIST = [CHECKSUM_SUFFIX, '.yml', '.yaml', '.txt'];
const generateChecksums = async () => {
const result = [];
const installerPath = path.resolve(__dirname, RELATIVE_FOLDER_PATH);
const files = await fs.promises.readdir(installerPath);
console.log("Generating checksum files for files in: '%s'", installerPath);
for (const file of files) {
if (SKIP_SUFFIX_LIST.some(s => file.endsWith(s))) continue;
const filePath = path.join(installerPath, file);
const stat = await fs.promises.stat(filePath);
if (stat.isFile()) {
const checksumFile = `${file}${CHECKSUM_SUFFIX}`;
const checksumFilePath = path.join(installerPath, checksumFile);
const checksum = await hashFile(filePath);
await fs.promises.writeFile(checksumFilePath, checksum);
console.log("'%s' -> '%s'", checksum, checksumFile);
result.push(checksumFilePath);
}
}
return result;
};
module.exports = {generateChecksums};