diff --git a/package.json b/package.json index 433333604ba..9d522a0cce6 100644 --- a/package.json +++ b/package.json @@ -46,13 +46,14 @@ "dev:webpack": "NODE_ENV=development webpack-dev-server --hot", "dev:typed-scss": "yarn build:typed-scss -w", "dev:storybook": "SIGNAL_ENV=storybook start-storybook -p 6006 -s ./", - "build": "run-s --print-label build:grunt build:typed-scss build:webpack build:release", + "build": "run-s --print-label build:grunt build:typed-scss build:webpack build:release build:zip", "build:dev": "run-s --print-label build:grunt build:typed-scss build:webpack", "build:grunt": "yarn grunt", "build:typed-scss": "tsm sticker-creator", "build:webpack": "cross-env NODE_ENV=production webpack", "build:electron": "electron-builder --config.extraMetadata.environment=$SIGNAL_ENV", "build:release": "cross-env SIGNAL_ENV=production npm run build:electron -- --config.directories.output=release", + "build:zip": "node scripts/zip-macos-release.js", "preverify:ts": "yarn build:typed-scss", "verify": "run-p --print-label verify:*", "verify:ts": "tsc --noEmit" diff --git a/scripts/zip-macos-release.js b/scripts/zip-macos-release.js new file mode 100644 index 00000000000..75736677186 --- /dev/null +++ b/scripts/zip-macos-release.js @@ -0,0 +1,3 @@ +const { zipMacOSRelease } = require('../ts/scripts/zip-macos-release'); + +zipMacOSRelease(); diff --git a/ts/scripts/zip-macos-release.ts b/ts/scripts/zip-macos-release.ts new file mode 100644 index 00000000000..168779c4c24 --- /dev/null +++ b/ts/scripts/zip-macos-release.ts @@ -0,0 +1,78 @@ +/* tslint:disable no-console non-literal-fs-path */ +import fs from 'fs'; +import path from 'path'; +import rimraf from 'rimraf'; +import { execSync } from 'child_process'; +import packageJSON from '../../package.json'; + +export function zipMacOSRelease() { + if (process.platform !== 'darwin') { + return; + } + + const files = fs + .readdirSync('release') + .filter(file => path.extname(file) === '.zip'); + if (!files.length) { + throw new Error( + 'No zip file found. Maybe the release did not complete properly?' + ); + } + if (files.length > 1) { + throw new Error( + 'More than one zip file found, release directory was not cleared.' + ); + } + const zipFile = files[0]; + const zipPath = path.join('release', zipFile); + + console.log('Removing current zip file'); + rimraf.sync(zipPath); + + const appName = `${packageJSON.productName}.app`; + const appPath = path.join('release', 'mac', appName); + + const tmpPath = path.join('release', 'tmp'); + const appDir = path.dirname(appPath); + const tmpZip = path.join(appDir, zipFile); + console.log('Creating temporary zip file at', tmpZip); + try { + execSync(`cd ${appDir} && zip -ro ${zipFile} "${appName}"`); + console.log( + 'Unzipping to remove duplicate electron references from', + tmpZip + ); + execSync(`unzip ${tmpZip} -d ${tmpPath}`); + } catch (err) { + console.log('stdout:', String(err.stdout)); + console.log('stderr:', String(err.stderr)); + throw err; + } + console.log('Removing temporary zip file'); + rimraf.sync(tmpZip); + + const electronFrameworkPath = path.join( + tmpPath, + appName, + 'Contents', + 'Frameworks', + 'Electron Framework.framework', + 'Versions' + ); + console.log('Removing duplicate electron framework', electronFrameworkPath); + rimraf.sync(electronFrameworkPath); + + try { + console.log('Creating final zip'); + execSync(`cd ${tmpPath} && zip -ro ${zipFile} "${appName}"`); + } catch (err) { + console.log('stdout:', String(err.stdout)); + console.log('stderr:', String(err.stderr)); + throw err; + } + console.log('Moving into the final destination', zipPath); + fs.renameSync(path.join(tmpPath, zipFile), zipPath); + rimraf.sync(tmpPath); + + console.log('zip-macos-release is done'); +}