|
| 1 | +/** |
| 2 | + * @overview This script runs `electron-builder` with special management of code signing configuration on Windows. |
| 3 | + * Running this script with no command line parameters should build all targets for the current platform. |
| 4 | + * On Windows, make sure to set CSC_* or WIN_CSC_* environment variables or the NSIS build will fail. |
| 5 | + * On Mac, the CSC_* variables are optional but will be respected if present. |
| 6 | + * See also: https://www.electron.build/code-signing |
| 7 | + */ |
| 8 | + |
| 9 | +const {spawnSync} = require('child_process'); |
| 10 | + |
| 11 | +/** |
| 12 | + * Strip any code signing configuration (CSC) from a set of environment variables. |
| 13 | + * @param {object} environment - a collection of environment variables which might include code signing configuration. |
| 14 | + * @returns {object} - a collection of environment variables which does not include code signing configuration. |
| 15 | + */ |
| 16 | +const stripCSC = function (environment) { |
| 17 | + const { |
| 18 | + CSC_LINK: _CSC_LINK, |
| 19 | + CSC_KEY_PASSWORD: _CSC_KEY_PASSWORD, |
| 20 | + WIN_CSC_LINK: _WIN_CSC_LINK, |
| 21 | + WIN_CSC_KEY_PASSWORD: _WIN_CSC_KEY_PASSWORD, |
| 22 | + ...strippedEnvironment |
| 23 | + } = environment; |
| 24 | + return strippedEnvironment; |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * @returns {string} - an `electron-builder` flag to build for the current platform, based on `process.platform`. |
| 29 | + */ |
| 30 | +const getPlatformFlag = function () { |
| 31 | + switch (process.platform) { |
| 32 | + case 'win32': return '--windows'; |
| 33 | + case 'darwin': return '--macos'; |
| 34 | + case 'linux': return '--linux'; |
| 35 | + } |
| 36 | + throw new Error(`Could not determine platform flag for platform: ${process.platform}`); |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * Run `electron-builder` once to build one or more target(s). |
| 41 | + * @param {string} targetGroup - the target(s) to build in this pass. |
| 42 | + * If the `targetGroup` is `'nsis'` then the environment must contain code-signing config (CSC_* or WIN_CSC_*). |
| 43 | + * If the `targetGroup` is `'appx'` then code-signing config will be stripped from the environment if present. |
| 44 | + */ |
| 45 | +const runBuilder = function (targetGroup) { |
| 46 | + // the appx build fails if CSC_* or WIN_CSC_* variables are set |
| 47 | + const shouldStripCSC = (targetGroup === 'appx'); |
| 48 | + const childEnvironment = shouldStripCSC ? stripCSC(process.env) : process.env; |
| 49 | + if ((targetGroup === 'nsis') && !(childEnvironment.CSC_LINK || childEnvironment.WIN_CSC_LINK)) { |
| 50 | + throw new Error(`NSIS build requires CSC_LINK or WIN_CSC_LINK`); |
| 51 | + } |
| 52 | + const platformFlag = getPlatformFlag(); |
| 53 | + const command = `electron-builder ${platformFlag} ${targetGroup}`; |
| 54 | + console.log(`running: ${command}`); |
| 55 | + spawnSync(command, { |
| 56 | + env: childEnvironment, |
| 57 | + shell: true, |
| 58 | + stdio: 'inherit' |
| 59 | + }); |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * @returns {Array.<string>} - the default list of target groups on this platform. Each item in the array represents |
| 64 | + * one call to `runBuilder` for one or more build target(s). |
| 65 | + */ |
| 66 | +const calculateTargets = function () { |
| 67 | + switch (process.platform) { |
| 68 | + case 'win32': |
| 69 | + // run in two passes so we can skip signing the appx |
| 70 | + return ['nsis', 'appx']; |
| 71 | + case 'darwin': |
| 72 | + // run in one pass for slightly better speed |
| 73 | + return ['dmg mas']; |
| 74 | + } |
| 75 | + throw new Error(`Could not determine targets for platform: ${process.platform}`); |
| 76 | +}; |
| 77 | + |
| 78 | +// TODO: allow user to specify targets? We could theoretically build NSIS on Mac, for example. |
| 79 | +const targets = calculateTargets(); |
| 80 | +for (const targetGroup of targets) { |
| 81 | + runBuilder(targetGroup); |
| 82 | +} |
0 commit comments