From 4216478a1a53325e73e36bbfcf41a42031e0a89f Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Sun, 29 Aug 2021 19:42:49 -0700 Subject: [PATCH 1/7] build: add promisify typings for sudo-prompt exec --- typings/sudo-prompt/index.d.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 typings/sudo-prompt/index.d.ts diff --git a/typings/sudo-prompt/index.d.ts b/typings/sudo-prompt/index.d.ts new file mode 100644 index 0000000000..2eb3b11f1b --- /dev/null +++ b/typings/sudo-prompt/index.d.ts @@ -0,0 +1,15 @@ +import { PromiseWithChild } from 'child_process'; + +// Copied from https://github.com/jorangreef/sudo-prompt/pull/124 +// TODO: Remove this if/when that PR gets merged/released +declare module 'sudo-prompt' { + namespace exec { + function __promisify__(command: string): PromiseWithChild<{ stdout: string; stderr: string }> + function __promisify__( + command: string, + options: + | ((error?: Error, stdout?: TBuffer, stderr?: TBuffer) => void) + | { name?: string; icns?: string; env?: Record } + ): PromiseWithChild<{ stdout: TBuffer; stderr: TBuffer }> + } +} From 583d0aa0875ad693e7b48f8b01ddf482feae7647 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Sun, 29 Aug 2021 19:45:04 -0700 Subject: [PATCH 2/7] build(deps-dev): upgrade typescript to ~4.4.2 --- package.json | 2 +- yarn.lock | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 609cdc72d2..e672ba4ad3 100644 --- a/package.json +++ b/package.json @@ -138,7 +138,7 @@ "sinon-chai": "^3.6.0", "ts-node": "^10.0.0", "typedoc": "^0.21.0", - "typescript": "~4.0.2", + "typescript": "~4.4.2", "xvfb-maybe": "^0.2.1" }, "optionalDependencies": { diff --git a/yarn.lock b/yarn.lock index 214f7bc199..f28bf9f6bb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9588,16 +9588,16 @@ typedoc@^0.21.0: shiki "^0.9.3" typedoc-default-themes "^0.12.10" -typescript@~4.0.2: - version "4.0.8" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.8.tgz#5739105541db80a971fdbd0d56511d1a6f17d37f" - integrity sha512-oz1765PN+imfz1MlZzSZPtC/tqcwsCyIYA8L47EkRnRW97ztRk83SzMiWLrnChC0vqoYxSU1fcFUDA5gV/ZiPg== - typescript@~4.2.4: version "4.2.4" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.2.4.tgz#8610b59747de028fda898a8aef0e103f156d0961" integrity sha512-V+evlYHZnQkaz8TRBuxTA92yZBPotr5H+WhQ7bD3hZUndx5tGOa1fuCgeSjxAzM1RiN5IzvadIXTVefuuwZCRg== +typescript@~4.4.2: + version "4.4.2" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.2.tgz#6d618640d430e3569a1dfb44f7d7e600ced3ee86" + integrity sha512-gzP+t5W4hdy4c+68bfcv0t400HVJMMd2+H9B7gae1nQlBzCqvrXX+6GL/b3GAgyTH966pzrZ70/fRjwAtZksSQ== + uglify-js@^3.1.4: version "3.14.1" resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.14.1.tgz#e2cb9fe34db9cb4cf7e35d1d26dfea28e09a7d06" From ebc6fa9d877dd4e172923c02154b2a2da8635fc1 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Sun, 29 Aug 2021 19:46:26 -0700 Subject: [PATCH 3/7] build(deps-dev): upgrade typedoc to 0.21.9 --- yarn.lock | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/yarn.lock b/yarn.lock index f28bf9f6bb..8e0aec992d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6577,10 +6577,10 @@ map-visit@^1.0.0: dependencies: object-visit "^1.0.0" -marked@^2.1.1: - version "2.1.3" - resolved "https://registry.yarnpkg.com/marked/-/marked-2.1.3.tgz#bd017cef6431724fd4b27e0657f5ceb14bff3753" - integrity sha512-/Q+7MGzaETqifOMWYEA7HVMaZb4XbcRfaOzcSsHZEith83KGlvaSG33u0SKu89Mj5h+T8V2hM+8O45Qc5XTgwA== +marked@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/marked/-/marked-3.0.2.tgz#60ce97d6aec34dd882ab4bb4df82494666854e17" + integrity sha512-TMJQQ79Z0e3rJYazY0tIoMsFzteUGw9fB3FD+gzuIT3zLuG9L9ckIvUfF51apdJkcqc208jJN2KbtPbOvXtbjA== matcher@^3.0.0: version "3.0.0" @@ -8690,10 +8690,10 @@ shebang-regex@^3.0.0: resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172" integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A== -shiki@^0.9.3: - version "0.9.7" - resolved "https://registry.yarnpkg.com/shiki/-/shiki-0.9.7.tgz#9c760254798a9bbc6df52bbd26f888486f780079" - integrity sha512-rOoAmwRWDiGKjQ1GaSKmbp1J5CamCera+I+DMM3wG/phbwNYQPt1mrjBBZbK66v80Vl1/A9TTLgXVHMbgtOCIQ== +shiki@^0.9.8: + version "0.9.8" + resolved "https://registry.yarnpkg.com/shiki/-/shiki-0.9.8.tgz#7dcd1af28c9a7804afb9ee3c3a5ee3987c21e86e" + integrity sha512-499zQUTjcNTVwwiaPrWldUTXIV3T9HZWxDwE82bY+9GE7P2uD6hpHUTXNbTof3iOG6WT+/062+OMbl0lDoG8WQ== dependencies: json5 "^2.2.0" onigasm "^2.2.5" @@ -9575,17 +9575,17 @@ typedoc-default-themes@^0.12.10: integrity sha512-fIS001cAYHkyQPidWXmHuhs8usjP5XVJjWB8oZGqkTowZaz3v7g3KDZeeqE82FBrmkAnIBOY3jgy7lnPnqATbA== typedoc@^0.21.0: - version "0.21.6" - resolved "https://registry.yarnpkg.com/typedoc/-/typedoc-0.21.6.tgz#854bfa2d6b3ac818ac70aa4734a4d1ba93695595" - integrity sha512-+4u3PEBjQdaL5/yfus5WJbjIdQHv7E/FpZq3cNki9BBdGmZhqnTF6JLIXDQ2EfVggojOJG9/soB5QVFgXRYnIw== + version "0.21.9" + resolved "https://registry.yarnpkg.com/typedoc/-/typedoc-0.21.9.tgz#6fbdc7152024a00f03af53a0ca40f44e91f0f129" + integrity sha512-VRo7aII4bnYaBBM1lhw4bQFmUcDQV8m8tqgjtc7oXl87jc1Slbhfw2X5MccfcR2YnEClHDWgsiQGgNB8KJXocA== dependencies: glob "^7.1.7" handlebars "^4.7.7" lunr "^2.3.9" - marked "^2.1.1" + marked "^3.0.2" minimatch "^3.0.0" progress "^2.0.3" - shiki "^0.9.3" + shiki "^0.9.8" typedoc-default-themes "^0.12.10" typescript@~4.2.4: From ed7b9d95bcd8028c3d2d6441c4950fbc3716d36a Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Sun, 29 Aug 2021 19:47:39 -0700 Subject: [PATCH 4/7] build: remove typescript from the update blocklist --- .github/dependabot.yml | 2 -- tools/update-dependencies.js | 1 - 2 files changed, 3 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 3e2f85b625..6dc8393682 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -13,8 +13,6 @@ updates: - dependency-name: find-up - dependency-name: log-symbols - dependency-name: ora - - dependency-name: typescript - versions: ['4.1.x', '4.2.x', '4.3.x'] - dependency-name: username - package-ecosystem: "npm" directory: "/packages/template/typescript/tmpl" diff --git a/tools/update-dependencies.js b/tools/update-dependencies.js index 75b806a348..92b9d2a98c 100755 --- a/tools/update-dependencies.js +++ b/tools/update-dependencies.js @@ -11,7 +11,6 @@ const DO_NOT_UPGRADE = [ 'find-up', // Requires ESM 'log-symbols', // Requires ESM 'ora', // Requires ESM - 'typescript', // Promisify issues, see https://github.com/DefinitelyTyped/DefinitelyTyped/pull/49699 'username' // Requires ESM ] From e3f548c97d7b12b4949884aae7bbf07a879f0dd3 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Sun, 29 Aug 2021 20:46:26 -0700 Subject: [PATCH 5/7] refactor: fix up error typechecks per TypeScript errors --- .../core/src/api/init-scripts/find-template.ts | 2 +- packages/api/core/src/api/make.ts | 4 +++- .../api/core/src/util/install-dependencies.ts | 7 ++++++- packages/api/core/src/util/require-search.ts | 16 +++++++++++++--- packages/api/core/src/util/resolve-dir.ts | 4 +++- packages/publisher/github/src/PublisherGithub.ts | 6 +++--- .../github/src/util/no-release-error.ts | 10 ++++++++++ packages/utils/test-utils/src/index.ts | 4 ++-- 8 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 packages/publisher/github/src/util/no-release-error.ts diff --git a/packages/api/core/src/api/init-scripts/find-template.ts b/packages/api/core/src/api/init-scripts/find-template.ts index 6e841405e9..45c0d05d02 100644 --- a/packages/api/core/src/api/init-scripts/find-template.ts +++ b/packages/api/core/src/api/init-scripts/find-template.ts @@ -29,7 +29,7 @@ export default async (dir: string, template: string): Promise => foundTemplate = true; break; } catch (err) { - d(`Error: ${err.message || err}`); + d(`Error: ${err instanceof Error ? err.message : err}`); } } if (!foundTemplate) { diff --git a/packages/api/core/src/api/make.ts b/packages/api/core/src/api/make.ts index f812f0656e..8db1baa0d3 100644 --- a/packages/api/core/src/api/make.ts +++ b/packages/api/core/src/api/make.ts @@ -231,12 +231,14 @@ export default async ({ arch: targetArch, }); } catch (err) { - if (err) { + if (err instanceof Error) { // eslint-disable-next-line no-throw-literal throw { message: `An error occured while making for target: ${maker.name}`, stack: `${err.message}\n${err.stack}`, }; + } else if (err) { + throw err; } else { throw new Error(`An unknown error occured while making for target: ${maker.name}`); } diff --git a/packages/api/core/src/util/install-dependencies.ts b/packages/api/core/src/util/install-dependencies.ts index 1e0e818009..5ca3f70210 100644 --- a/packages/api/core/src/util/install-dependencies.ts +++ b/packages/api/core/src/util/install-dependencies.ts @@ -1,4 +1,5 @@ import debug from 'debug'; +import { ExitError } from '@malept/cross-spawn-promise'; import { yarnOrNpmSpawn, hasYarn } from './yarn-or-npm'; const d = debug('electron-forge:dependency-installer'); @@ -41,6 +42,10 @@ export default async ( stdio: 'pipe', }); } catch (err) { - throw new Error(`Failed to install modules: ${JSON.stringify(deps)}\n\nWith output: ${err.message}\n${err.stderr ? err.stderr.toString() : ''}`); + if (err instanceof ExitError) { + throw new Error(`Failed to install modules: ${JSON.stringify(deps)}\n\nWith output: ${err.message}\n${err.stderr ? err.stderr.toString() : ''}`); + } else { + throw err; + } } }; diff --git a/packages/api/core/src/util/require-search.ts b/packages/api/core/src/util/require-search.ts index 7916185738..ab97b3dd3c 100644 --- a/packages/api/core/src/util/require-search.ts +++ b/packages/api/core/src/util/require-search.ts @@ -3,6 +3,13 @@ import path from 'path'; const d = debug('electron-forge:require-search'); +// https://github.com/nodejs/node/blob/da0ede1ad55a502a25b4139f58aab3fb1ee3bf3f/lib/internal/modules/cjs/loader.js#L353-L359 +type RequireError = Error & { + code: string; + path: string; + requestPath: string | undefined; +} + export function requireSearchRaw(relativeTo: string, paths: string[]): T | null { const testPaths = paths .concat(paths.map((mapPath) => path.resolve(relativeTo, mapPath))) @@ -14,9 +21,12 @@ export function requireSearchRaw(relativeTo: string, paths: string[]): T | nu // eslint-disable-next-line global-require, import/no-dynamic-require return require(testPath); } catch (err) { - // Ignore require-related errors - if (err.code !== 'MODULE_NOT_FOUND' || ![undefined, testPath].includes(err.requestPath)) { - throw err; + if (err instanceof Error) { + const requireErr = err as RequireError; + // Ignore require-related errors + if (requireErr.code !== 'MODULE_NOT_FOUND' || ![undefined, testPath].includes(requireErr.requestPath)) { + throw err; + } } } } diff --git a/packages/api/core/src/util/resolve-dir.ts b/packages/api/core/src/util/resolve-dir.ts index b6bccc295e..91768c9bda 100644 --- a/packages/api/core/src/util/resolve-dir.ts +++ b/packages/api/core/src/util/resolve-dir.ts @@ -27,7 +27,9 @@ export default async (dir: string) => { try { await getElectronVersion(mDir, packageJSON); } catch (err) { - lastError = err.message; + if (err instanceof Error) { + lastError = err.message; + } } if (packageJSON.config && packageJSON.config.forge) { diff --git a/packages/publisher/github/src/PublisherGithub.ts b/packages/publisher/github/src/PublisherGithub.ts index 64081184fc..ad9e657443 100644 --- a/packages/publisher/github/src/PublisherGithub.ts +++ b/packages/publisher/github/src/PublisherGithub.ts @@ -7,6 +7,7 @@ import PublisherBase, { PublisherOptions } from '@electron-forge/publisher-base' import { GetResponseDataTypeFromEndpointMethod } from '@octokit/types'; import GitHub from './util/github'; +import NoReleaseError from './util/no-release-error'; import { PublisherGitHubConfig } from './Config'; interface GitHubRelease { @@ -62,11 +63,10 @@ export default class PublisherGithub extends PublisherBase testRelease.tag_name === `v${releaseName}`); if (!release) { - // eslint-disable-next-line no-throw-literal - throw { code: 404 }; + throw new NoReleaseError(404); } } catch (err) { - if (err.code === 404) { + if (err instanceof NoReleaseError && err.code === 404) { // Release does not exist, let's make it release = (await github.getGitHub().repos.createRelease({ owner: config.repository.owner, diff --git a/packages/publisher/github/src/util/no-release-error.ts b/packages/publisher/github/src/util/no-release-error.ts new file mode 100644 index 0000000000..8ee65473a1 --- /dev/null +++ b/packages/publisher/github/src/util/no-release-error.ts @@ -0,0 +1,10 @@ +class NoReleaseError extends Error { + code: number; + + constructor(code: number) { + super('No GitHub Release found'); + this.code = code; + } +} + +export { NoReleaseError as default }; diff --git a/packages/utils/test-utils/src/index.ts b/packages/utils/test-utils/src/index.ts index ff05b1fa58..4a839511eb 100644 --- a/packages/utils/test-utils/src/index.ts +++ b/packages/utils/test-utils/src/index.ts @@ -2,7 +2,7 @@ import { expect } from 'chai'; import fs from 'fs-extra'; import os from 'os'; import path from 'path'; -import { spawn } from '@malept/cross-spawn-promise'; +import { ExitError, spawn } from '@malept/cross-spawn-promise'; async function runNPM(dir: string, ...args: string[]) { await spawn('npm', args, { cwd: dir }); @@ -31,7 +31,7 @@ export async function expectLintToPass(dir: string) { try { await runNPM(dir, 'run', 'lint'); } catch (err) { - if (err.stdout) { + if (err instanceof ExitError) { // eslint-disable-next-line no-console console.error('STDOUT:', err.stdout.toString()); // eslint-disable-next-line no-console From bf782c6cf7f49863cd55414b0997df58e4978c0b Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Sun, 29 Aug 2021 20:46:59 -0700 Subject: [PATCH 6/7] refactor(core): prefer undefined to null --- packages/api/core/src/api/package.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api/core/src/api/package.ts b/packages/api/core/src/api/package.ts index cb5d424cd8..456e0f5cfc 100644 --- a/packages/api/core/src/api/package.ts +++ b/packages/api/core/src/api/package.ts @@ -108,7 +108,7 @@ export default async ({ } const calculatedOutDir = outDir || getCurrentOutDir(dir, forgeConfig); - let packagerSpinner: OraImpl | null = null; + let packagerSpinner: OraImpl | undefined; const pruneEnabled = !('prune' in forgeConfig.packagerConfig) || forgeConfig.packagerConfig.prune; From 096332872e4a65ccb682529bedb97fff900f7908 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Sun, 29 Aug 2021 20:54:12 -0700 Subject: [PATCH 7/7] refactor: specify type when instantiating Promises --- packages/api/cli/src/electron-forge-start.ts | 2 +- packages/api/core/src/api/init-scripts/init-git.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/api/cli/src/electron-forge-start.ts b/packages/api/cli/src/electron-forge-start.ts index ff036daa99..35de300ec7 100644 --- a/packages/api/cli/src/electron-forge-start.ts +++ b/packages/api/cli/src/electron-forge-start.ts @@ -57,7 +57,7 @@ import workingDir from './util/working-dir'; const spawned = await api.start(opts); - await new Promise((resolve) => { + await new Promise((resolve) => { const listenForExit = (child: ElectronProcess) => { let onExit: NodeJS.ExitListener; let onRestart: (newChild: ElectronProcess) => void; diff --git a/packages/api/core/src/api/init-scripts/init-git.ts b/packages/api/core/src/api/init-scripts/init-git.ts index 64f26f3468..3ae980ad06 100644 --- a/packages/api/core/src/api/init-scripts/init-git.ts +++ b/packages/api/core/src/api/init-scripts/init-git.ts @@ -6,7 +6,7 @@ const d = debug('electron-forge:init:git'); export default async (dir: string) => { await asyncOra('Initializing Git Repository', async () => { - await new Promise((resolve, reject) => { + await new Promise((resolve, reject) => { exec('git rev-parse --show-toplevel', { cwd: dir, }, (err) => {