From d077e79a8a300b23e4074c66af7d9af14f554444 Mon Sep 17 00:00:00 2001
From: Christopher Willis-Ford <cwillisf@media.mit.edu>
Date: Wed, 11 Sep 2019 18:46:28 -0700
Subject: [PATCH] skip code signing for AppX builds

---
 package.json                        |  2 +-
 scripts/electron-builder-wrapper.js | 56 +++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 scripts/electron-builder-wrapper.js

diff --git a/package.json b/package.json
index 5d58af6..fa3d994 100644
--- a/package.json
+++ b/package.json
@@ -12,7 +12,7 @@
     "clean": "rimraf ./dist/ ./static/assets/",
     "compile": "rimraf ./dist/ && electron-webpack --bail --display-error-details --env.minify=false",
     "fetch": "rimraf ./static/assets/ && mkdirp ./static/assets/ && node ./scripts/fetchMediaLibraryAssets.js",
-    "dist": "npm run build-gui && npm run fetch && npm run compile -p && electron-builder",
+    "dist": "npm run build-gui && npm run fetch && npm run compile -p && node ./scripts/electron-builder-wrapper.js",
     "dist:dir": "npm run dist -- --dir -c.compression=store -c.mac.identity=null",
     "lint": "eslint --cache --color --ext .jsx,.js ."
   },
diff --git a/scripts/electron-builder-wrapper.js b/scripts/electron-builder-wrapper.js
new file mode 100644
index 0000000..2f30dfc
--- /dev/null
+++ b/scripts/electron-builder-wrapper.js
@@ -0,0 +1,56 @@
+const {spawnSync} = require('child_process');
+
+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;
+};
+
+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}`);
+}
+
+const runBuilder = function (targetGroup) {
+    // the appx build fails if CSC_* or WIN_CSC_* variables are set
+    const shouldStripCSC = (targetGroup === 'appx');
+    const childEnvironment = shouldStripCSC ? stripCSC(process.env) : process.env;
+    if ((targetGroup === 'nsis') && !(childEnvironment.CSC_LINK || childEnvironment.WIN_CSC_LINK)) {
+        throw new Error(`NSIS build requires CSC_LINK or WIN_CSC_LINK`);
+    }
+    const platformFlag = getPlatformFlag();
+    const command = `electron-builder ${platformFlag} ${targetGroup}`;
+    console.log(`running: ${command}`);
+    spawnSync(command, {
+        env: childEnvironment,
+        shell: true,
+        stdio: 'inherit'
+    });
+};
+
+const calculateTargets = function () {
+    switch (process.platform) {
+    case 'win32':
+        // run in two passes so we can skip signing the appx
+        return ['nsis', 'appx'];
+    case 'darwin':
+        // run in one pass for slightly better speed
+        return ['dmg mas'];
+    }
+    throw new Error(`Could not determine targets for platform: ${process.platform}`);
+};
+
+// TODO: allow user to specify targets? We could theoretically build NSIS on Mac, for example.
+const targets = calculateTargets();
+for (const targetGroup of targets) {
+    runBuilder(targetGroup);
+}