scratch-desktop/scripts/electron-builder-wrapper.js

204 lines
7.4 KiB
JavaScript
Raw Normal View History

/**
* @overview This script runs `electron-builder` with special management of code signing configuration on Windows.
* Running this script with no command line parameters should build all targets for the current platform.
* On Windows, make sure to set CSC_* or WIN_CSC_* environment variables or the NSIS build will fail.
* On Mac, the CSC_* variables are optional but will be respected if present.
* See also: https://www.electron.build/code-signing
*/
2019-09-11 21:46:28 -04:00
const {spawnSync} = require('child_process');
2020-07-10 18:53:59 -04:00
const fs = require('fs');
2019-09-11 21:46:28 -04:00
/**
* Strip any code signing configuration (CSC) from a set of environment variables.
* @param {object} environment - a collection of environment variables which might include code signing configuration.
* @returns {object} - a collection of environment variables which does not include code signing configuration.
*/
2019-09-11 21:46:28 -04:00
const stripCSC = function (environment) {
const {
CSC_LINK: _CSC_LINK,
CSC_KEY_PASSWORD: _CSC_KEY_PASSWORD,
WIN_CSC_LINK: _WIN_CSC_LINK,
WIN_CSC_KEY_PASSWORD: _WIN_CSC_KEY_PASSWORD,
...strippedEnvironment
} = environment;
return strippedEnvironment;
};
/**
* @returns {string} - an `electron-builder` flag to build for the current platform, based on `process.platform`.
*/
2019-09-11 21:46:28 -04:00
const getPlatformFlag = function () {
switch (process.platform) {
case 'win32': return '--windows';
case 'darwin': return '--macos';
case 'linux': return '--linux';
}
throw new Error(`Could not determine platform flag for platform: ${process.platform}`);
};
2019-09-11 21:46:28 -04:00
/**
* Run `electron-builder` once to build one or more target(s).
* @param {object} wrapperConfig - overall configuration object for the wrapper script.
* @param {object} target - the target to build in this call.
* If the `target.name` is `'nsis'` then the environment must contain code-signing config (CSC_* or WIN_CSC_*).
* If the `target.name` is `'appx'` then code-signing config will be stripped from the environment if present.
*/
const runBuilder = function (wrapperConfig, target) {
// the AppX build fails if CSC_* or WIN_CSC_* variables are set
2020-06-29 15:23:22 -04:00
const shouldStripCSC = (target.name === 'appx') || (!wrapperConfig.doSign);
2019-09-11 21:46:28 -04:00
const childEnvironment = shouldStripCSC ? stripCSC(process.env) : process.env;
2020-06-29 15:23:22 -04:00
if (wrapperConfig.doSign &&
(target.name.indexOf('nsis') === 0) &&
!(childEnvironment.CSC_LINK || childEnvironment.WIN_CSC_LINK)) {
throw new Error(`Signing NSIS build requires CSC_LINK or WIN_CSC_LINK`);
2019-09-11 21:46:28 -04:00
}
const platformFlag = getPlatformFlag();
let allArgs = [platformFlag, target.name];
if (target.platform === 'darwin') {
allArgs.push(`--c.mac.type=${wrapperConfig.mode === 'dist' ? 'distribution' : 'development'}`);
if (target.name === 'mas-dev') {
allArgs.push('--c.mac.provisioningProfile=mas-dev.provisionprofile');
}
2020-06-29 15:23:22 -04:00
if (wrapperConfig.doSign) {
// really this is "notarize only if we also sign"
allArgs.push('--c.afterSign=scripts/afterSign.js');
} else {
allArgs.push('--c.mac.identity=null');
}
}
if (!wrapperConfig.doPackage) {
allArgs.push('--dir', '--c.compression=store');
}
allArgs = allArgs.concat(wrapperConfig.builderArgs);
2020-03-12 17:23:35 -04:00
console.log(`running electron-builder with arguments: ${allArgs}`);
const result = spawnSync('electron-builder', allArgs, {
2019-09-11 21:46:28 -04:00
env: childEnvironment,
shell: true,
stdio: 'inherit'
});
if (result.error) {
throw result.error;
}
if (result.signal) {
throw new Error(`Child process terminated due to signal ${result.signal}`);
}
if (result.status) {
throw new Error(`Child process returned status code ${result.status}`);
}
2019-09-11 21:46:28 -04:00
};
/**
2020-06-29 15:23:22 -04:00
* @param {object} wrapperConfig - overall configuration object for the wrapper script.
* @returns {Array.<object>} - the default list of targets on this platform. Each item in the array represents one
* call to `runBuilder` for exactly one build target. In theory electron-builder can build two or more targets at the
* same time but doing so limits has unwanted side effects on both macOS and Windows (see function body).
*/
2020-06-29 15:23:22 -04:00
const calculateTargets = function (wrapperConfig) {
2020-07-10 18:53:59 -04:00
const masDevProfile = 'mas-dev.provisionprofile';
2020-06-29 15:23:22 -04:00
const availableTargets = {
macAppStore: {
name: 'mas',
platform: 'darwin'
},
macAppStoreDev: {
name: 'mas-dev',
platform: 'darwin'
},
macDirectDownload: {
name: 'dmg',
platform: 'darwin'
},
microsoftStore: {
name: 'appx',
platform: 'win32'
},
windowsDirectDownload: {
name: 'nsis:ia32',
platform: 'win32'
}
};
2020-06-29 15:23:22 -04:00
const targets = [];
2019-09-11 21:46:28 -04:00
switch (process.platform) {
case 'win32':
// Run in two passes so we can skip signing the AppX for distribution through the MS Store.
2020-06-29 15:23:22 -04:00
targets.push(availableTargets.microsoftStore);
targets.push(availableTargets.windowsDirectDownload);
break;
2019-09-11 21:46:28 -04:00
case 'darwin':
// Running 'dmg' and 'mas' in the same pass causes electron-builder to skip signing the non-MAS app copy.
// Running them as separate passes means they can both get signed.
// Seems like a bug in electron-builder...
2020-05-13 20:32:30 -04:00
// Running the 'mas' build first means that its output is available while we wait for 'dmg' notarization.
// Add macAppStoreDev here to test a MAS-like build locally. You'll need a Mac Developer provisioning profile.
2020-07-10 18:53:59 -04:00
if (fs.existsSync(masDevProfile)) {
targets.push(availableTargets.macAppStoreDev);
} else {
console.log(`skipping target "${availableTargets.macAppStoreDev.name}": ${masDevProfile} missing`);
}
2020-06-29 15:23:22 -04:00
if (wrapperConfig.doSign) {
targets.push(availableTargets.macAppStore);
} else {
// electron-builder doesn't seem to support this configuration even if mac.type is "development"
console.log(`skipping target "${availableTargets.macAppStore.name}" because code-signing is disabled`);
}
targets.push(availableTargets.macDirectDownload);
break;
default:
throw new Error(`Could not determine targets for platform: ${process.platform}`);
2019-09-11 21:46:28 -04:00
}
2020-06-29 15:23:22 -04:00
return targets;
2019-09-11 21:46:28 -04:00
};
const parseArgs = function () {
const scriptArgs = process.argv.slice(2); // remove `node` and `this-script.js`
const builderArgs = [];
let mode = 'dev'; // default
for (const arg of scriptArgs) {
const modeSplit = arg.split(/--mode(\s+|=)/);
if (modeSplit.length === 3) {
mode = modeSplit[2];
} else {
builderArgs.push(arg);
}
}
let doPackage;
let doSign;
switch (mode) {
case 'dev':
doPackage = true;
doSign = false;
break;
case 'dir':
doPackage = false;
doSign = false;
break;
case 'dist':
doPackage = true;
doSign = true;
}
return {
builderArgs,
doPackage, // false = build to directory
doSign,
2020-06-29 15:23:22 -04:00
mode
};
};
const main = function () {
const wrapperConfig = parseArgs();
2020-06-29 15:23:22 -04:00
// TODO: allow user to specify targets? We could theoretically build NSIS on Mac, for example.
wrapperConfig.targets = calculateTargets(wrapperConfig);
for (const target of wrapperConfig.targets) {
runBuilder(wrapperConfig, target);
}
};
main();