element-desktop/scripts/set-version.js
David Baker 67a4c99907 Fix the set-version script
Call the right function to set package version and await
2020-02-27 17:34:34 +00:00

81 lines
2.2 KiB
JavaScript
Executable file

#!/usr/bin/env node
/*
* Checks for the presence of a webapp, inspects its version and sets the
* version metadata of the package to match.
*/
const fs = require('fs').promises;
const asar = require('asar');
const childProcess = require('child_process');
async function versionFromAsar() {
try {
await fs.stat('webapp.asar');
} catch (e) {
console.log("No 'webapp.asar' found. Run 'yarn run fetch'");
return 1;
}
return asar.extractFile('webapp.asar', 'version').toString().trim();
}
async function setPackageVersion(ver) {
// set version in package.json: electron-builder will use this to populate
// all the various version fields
await new Promise((resolve, reject) => {
childProcess.execFile(process.platform === 'win32' ? 'yarn.cmd' : 'yarn', [
'version',
'-s',
'--no-git-tag-version', // This also means "don't commit to git" as it turns out
'--new-version',
ver,
], (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
async function setDebVersion(ver) {
// Also create a debian package control file with the version.
// We use a custom control file so we need to do this ourselves
const outFile = await fs.open('pkg/control', 'w');
const template = await fs.readFile('pkg/control.template');
await outFile.write(template);
await outFile.write('Version: ' + ver + "\n");
await outFile.close();
console.log("Version set to " + ver);
}
async function main(args) {
let setDeb = false;
let setPkg = false;
let version;
for (const arg of args) {
if (arg === '--deb') {
setDeb = true;
} else if (arg === '--pkg') {
setPkg = true;
} else {
version = arg;
}
}
if (version === undefined) version = await versionFromAsar();
if (setPkg) await setPackageVersion(version);
if (setDeb) await setDebVersion(version);
}
if (require.main === module) {
main(process.argv.slice(2)).then((ret) => process.exit(ret));
}
module.exports = {versionFromAsar, setPackageVersion, setDebVersion};