From 11b76efaef4dae2db23a507117da4f26dae6447a Mon Sep 17 00:00:00 2001 From: Opender Singh Date: Thu, 30 Sep 2021 17:50:33 +1300 Subject: [PATCH] improve tar structure and package from script --- .github/workflows/test.yml | 4 +- packages/insomnia-inso/package.json | 15 ++-- packages/insomnia-inso/src/scripts/pkg.ts | 74 +++++++++++++++++++ .../insomnia-inso/src/scripts/verify-pkg.ts | 48 ++---------- 4 files changed, 90 insertions(+), 51 deletions(-) create mode 100644 packages/insomnia-inso/src/scripts/pkg.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b74206a105..2aeaffda29 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -40,7 +40,9 @@ jobs: run: npm run inso-package - name: Tar Inso CLI binaries if: success() - run: tar -zcvf ${{ matrix.os }}-inso-${{ github.run_number }}.tar packages/insomnia-inso/binaries + run: + cd packages/insomnia-inso/binaries + tar -zcvf ${{ matrix.os }}-inso-${{ github.run_number }}.tar . - name: Upload Inso CLI tar uses: actions/upload-artifact@v2 if: success() diff --git a/packages/insomnia-inso/package.json b/packages/insomnia-inso/package.json index 9155a5c498..5687df15b0 100644 --- a/packages/insomnia-inso/package.json +++ b/packages/insomnia-inso/package.json @@ -23,23 +23,20 @@ "lint": "eslint . --ext .js,.ts,.tsx", "lint:fix": "npm run lint -- --fix", "clean": "tsc --build tsconfig.build.json --clean", - "postclean": "rimraf dist", + "postclean": "rimraf dist && rimraf binaries", "test": "jest --runInBand", "test:watch": "npm run test -- --watch", "test:snapshots": "npm run build && npm run test -- -u", "prebuild": "npm run clean", "build": "webpack --config webpack/webpack.config.development.js", + "prebuild:production": "npm run clean", "build:production": "webpack --config webpack/webpack.config.production.js --display errors-only", "start": "npm run build -- --watch", "prepare": "npm run build:production", - "package": "rimraf binaries && npm run build:production && pkg .", - "package:verify": "ts-node src/scripts/verify-pkg.ts" - }, - "pkg": { - "targets": [ - "node12-x64" - ], - "outputPath": "binaries" + "prepackage": "npm run build:production", + "package": "ts-node src/scripts/pkg.ts", + "pkg": "pkg .", + "postpackage": "ts-node src/scripts/verify-pkg.ts" }, "devDependencies": { "@babel/core": "^7.9.0", diff --git a/packages/insomnia-inso/src/scripts/pkg.ts b/packages/insomnia-inso/src/scripts/pkg.ts new file mode 100644 index 0000000000..ce386f1a76 --- /dev/null +++ b/packages/insomnia-inso/src/scripts/pkg.ts @@ -0,0 +1,74 @@ +import { spawn } from 'child_process'; +import path from 'path'; + +const prefixPkgInso = (msg: string) => `[pkg-inso] ${msg}`; + +const getPlatform = () => process.platform; +const isMac = () => getPlatform() === 'darwin'; +const isLinux = () => getPlatform() === 'linux'; +const isWindows = () => getPlatform() === 'win32'; + +const getTargets = () => { + if (isMac()) { + return ['node12-macos-x64']; + } else if (isLinux()) { + return ['node12-linux-x64']; + } else if (isWindows()) { + return ['node12-win-x64']; + } else { + return []; + } +}; + +const pkg = async () => { + return new Promise((resolve, reject) => { + const targets = getTargets(); + + if (targets.length === 0) { + reject(new Error(prefixPkgInso(`Unsupported OS ${getPlatform()}`))); + } + + const rootDir = path.join(__dirname, '../..'); + + const process = spawn('npm', + [ + 'run', + 'pkg', + '--', + '--targets', + targets.join(','), + '--output', + 'binaries/inso', + ], { + cwd: rootDir, + shell: true, + }); + + process.stdout.on('data', data => { + console.log(data.toString()); + }); + + process.stderr.on('data', data => { + console.log(data.toString()); + }); + + process.on('exit', code => { + if (code === 0) { + resolve(); + } else { + console.log(prefixPkgInso(`exited with code ${code}`)); + reject(new Error(prefixPkgInso('failed to package'))); + } + }); + }); + +}; + +pkg() + .then(() => { + process.exit(0); + }) + .catch(error => { + console.error(error); + process.exit(1); + }); diff --git a/packages/insomnia-inso/src/scripts/verify-pkg.ts b/packages/insomnia-inso/src/scripts/verify-pkg.ts index 790994daa6..7a6534ef13 100644 --- a/packages/insomnia-inso/src/scripts/verify-pkg.ts +++ b/packages/insomnia-inso/src/scripts/verify-pkg.ts @@ -1,54 +1,22 @@ import { promises } from 'fs'; -import { basename, join, resolve } from 'path'; -import { difference, map } from 'ramda'; +import { join, resolve } from 'path'; + +const prefixPkgVerify = (msg: string) => `[pkg-inso-verify] ${msg}`; const { readdir, stat } = promises; -const fail = (message: string) => { - throw new Error(`[inso-pkg]: ${message}`); -}; - const verifyFile = (basePath: string) => async (fileName: string) => { const filePath = join(basePath, fileName); try { const stats = await stat(filePath); if (!stats) { - fail(`failed to find file ${filePath}`); + throw new Error(prefixPkgVerify(`failed to find file ${filePath}`)); } if (stats.size === 0) { - fail(`the file ${filePath} is unexpectedly empty`); + throw new Error(prefixPkgVerify(`the file ${filePath} is unexpectedly empty`)); } } catch (error: unknown) { - if (error instanceof Error) { - fail(error.message); - } - fail(`unexpected error: ${error}`); - } - -}; - -/** this list of binary artifiacts that we intended to create */ -const desiredNames = [ - 'insomnia-inso-alpine', // needed for CI use-cases - 'insomnia-inso-linux', - 'insomnia-inso-win.exe', - 'insomnia-inso-macos', // OSX binary (not M1-specific) - // 'insomnia-inso-macos-arm64', // would be nice to have Apple M1 support here (`node12-mac-arm64`). see: https://github.com/vercel/pkg/issues/1023 -]; - -const verifyAllFilesArePresent = (filePaths: string[]) => { - const actualNames = map(basename, filePaths); - - const missingFiles = difference(desiredNames, actualNames); - if (missingFiles.length > 0) { - const singular = missingFiles.length === 1; - fail(`missing ${singular ? 'a binary' : 'binaries'} for the expected file${singular ? '' : 's'}: ${missingFiles.join(', ')}`); - } - - const extraFiles = difference(actualNames, desiredNames); - if (extraFiles.length === 1) { - const singular = extraFiles.length === 1; - fail(`${singular ? 'an ' : ''}extra (and unexpected) ${singular ? 'binary was' : 'binaries were'} found: ${extraFiles.join(', ')}`); + throw error; } }; @@ -57,11 +25,9 @@ const verifyPkg = async () => { const files = await readdir(basePath); if (files.length === 0) { - fail('executable files found'); + throw new Error(prefixPkgVerify('no executable binaries found')); } - verifyAllFilesArePresent(files); - return Promise.all(files.map(verifyFile(basePath))); };