From f93f152e3abaf9e6483e42a2c4a75bc3047b5306 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Thu, 26 Oct 2023 10:35:34 -0700 Subject: [PATCH] fix(api): run `npm install` with `--ignore-scripts` (#793) * fix(api): run `npm install` with `--ignore-scripts` * fix: pr feedback * Update packages/api/src/codegen/languages/typescript/index.ts Co-authored-by: Kanad Gupta <8854718+kanadgupta@users.noreply.github.com> --------- Co-authored-by: Kanad Gupta <8854718+kanadgupta@users.noreply.github.com> --- .../src/codegen/languages/typescript/index.ts | 65 ++++++++----------- packages/test-utils/sdks/alby/package.json | 3 +- .../test-utils/sdks/metrotransit/package.json | 3 +- .../sdks/operationid-quirks/package.json | 3 +- .../sdks/optional-payload/package.json | 3 +- .../test-utils/sdks/petstore/package.json | 3 +- packages/test-utils/sdks/readme/package.json | 3 +- .../sdks/response-title-quirks/package.json | 3 +- packages/test-utils/sdks/simple/package.json | 3 +- .../test-utils/sdks/star-trek/package.json | 3 +- 10 files changed, 37 insertions(+), 55 deletions(-) diff --git a/packages/api/src/codegen/languages/typescript/index.ts b/packages/api/src/codegen/languages/typescript/index.ts index 71507ee7..a96a6b0a 100644 --- a/packages/api/src/codegen/languages/typescript/index.ts +++ b/packages/api/src/codegen/languages/typescript/index.ts @@ -1,4 +1,5 @@ import type { InstallerOptions } from '../../factory.js'; +import type { ExecaReturnValue } from 'execa'; import type Oas from 'oas'; import type Operation from 'oas/operation'; import type { HttpMethods, SchemaObject } from 'oas/rmoas.types'; @@ -61,6 +62,22 @@ interface OperationTypeHousing { const REF_PLACEHOLDER = '::convert::'; const REF_PLACEHOLDER_REGEX = /"::convert::([a-zA-Z_$\\d]*)"/g; +function handleExecSuccess(res: ExecaReturnValue, opts: InstallerOptions = {}) { + if (opts.dryRun) { + (opts.logger ? opts.logger : logger)(res.command); + (opts.logger ? opts.logger : logger)(res.stdout); + } +} + +function handleExecFailure(err: Error, opts: InstallerOptions = {}) { + if (opts.dryRun) { + (opts.logger ? opts.logger : logger)(err.message); + return; + } + + throw err; +} + export default class TSGenerator extends CodeGenerator { project: Project; @@ -146,12 +163,7 @@ export default class TSGenerator extends CodeGenerator { // adding `@api/` as a dependency there so you can load it with // `require('@api/)`. return execa('npm', [...npmInstall, installDir].filter(Boolean)) - .then(res => { - if (opts.dryRun) { - (opts.logger ? opts.logger : logger)(res.command); - (opts.logger ? opts.logger : logger)(res.stdout); - } - }) + .then(res => handleExecSuccess(res, opts)) .catch(err => { // If `npm install` throws this error it always happens **after** our dependencies have been // installed and is an annoying quirk that sometimes occurs when installing a package within @@ -164,12 +176,7 @@ export default class TSGenerator extends CodeGenerator { return; } - if (opts.dryRun) { - (opts.logger ? opts.logger : logger)(err.message); - return; - } - - throw err; + handleExecFailure(err, opts); }); } @@ -178,15 +185,8 @@ export default class TSGenerator extends CodeGenerator { const args = ['uninstall', pkgName, opts.dryRun ? '--dry-run' : ''].filter(Boolean); return execa('npm', args) - .then(res => { - if (opts.dryRun) { - (opts.logger ? opts.logger : logger)(res.command); - (opts.logger ? opts.logger : logger)(res.stdout); - } - }) - .catch(err => { - throw err; - }); + .then(res => handleExecSuccess(res, opts)) + .catch(err => handleExecFailure(err, opts)); } /** @@ -197,23 +197,15 @@ export default class TSGenerator extends CodeGenerator { async compile(storage: Storage, opts: InstallerOptions = {}): Promise { const installDir = storage.getIdentifierStorageDir(); - await execa('npx', ['tsup'], { + await execa('npm', ['pkg', 'set', 'scripts.prepare=tsup'], { cwd: installDir }) + .then(res => handleExecSuccess(res, opts)) + .catch(err => handleExecFailure(err, opts)); + + await execa('npm', ['run', 'prepare'], { cwd: installDir, }) - .then(res => { - if (opts.dryRun) { - (opts.logger ? opts.logger : logger)(res.command); - (opts.logger ? opts.logger : logger)(res.stdout); - } - }) - .catch(err => { - if (opts.dryRun) { - (opts.logger ? opts.logger : logger)(err.message); - return; - } - - throw err; - }); + .then(res => handleExecSuccess(res, opts)) + .catch(err => handleExecFailure(err, opts)); } /** @@ -634,7 +626,6 @@ dist/ files: ['dist', 'openapi.json'], scripts: { lint: 'tsc --noEmit', - prepare: 'tsup', }, dependencies, devDependencies, diff --git a/packages/test-utils/sdks/alby/package.json b/packages/test-utils/sdks/alby/package.json index bb9e5442..f8a82697 100644 --- a/packages/test-utils/sdks/alby/package.json +++ b/packages/test-utils/sdks/alby/package.json @@ -19,8 +19,7 @@ "openapi.json" ], "scripts": { - "lint": "tsc --noEmit", - "prepare": "tsup" + "lint": "tsc --noEmit" }, "dependencies": { "@readme/api-core": "file:../../../../../core", diff --git a/packages/test-utils/sdks/metrotransit/package.json b/packages/test-utils/sdks/metrotransit/package.json index 2a5b5363..904af22e 100644 --- a/packages/test-utils/sdks/metrotransit/package.json +++ b/packages/test-utils/sdks/metrotransit/package.json @@ -19,8 +19,7 @@ "openapi.json" ], "scripts": { - "lint": "tsc --noEmit", - "prepare": "tsup" + "lint": "tsc --noEmit" }, "dependencies": { "@readme/api-core": "file:../../../../../core", diff --git a/packages/test-utils/sdks/operationid-quirks/package.json b/packages/test-utils/sdks/operationid-quirks/package.json index 21b625bd..37b6ec9b 100644 --- a/packages/test-utils/sdks/operationid-quirks/package.json +++ b/packages/test-utils/sdks/operationid-quirks/package.json @@ -15,8 +15,7 @@ "openapi.json" ], "scripts": { - "lint": "tsc --noEmit", - "prepare": "tsup" + "lint": "tsc --noEmit" }, "dependencies": { "@readme/api-core": "file:../../../../../core", diff --git a/packages/test-utils/sdks/optional-payload/package.json b/packages/test-utils/sdks/optional-payload/package.json index 1a049ed6..78f5b2ad 100644 --- a/packages/test-utils/sdks/optional-payload/package.json +++ b/packages/test-utils/sdks/optional-payload/package.json @@ -19,8 +19,7 @@ "openapi.json" ], "scripts": { - "lint": "tsc --noEmit", - "prepare": "tsup" + "lint": "tsc --noEmit" }, "dependencies": { "@readme/api-core": "file:../../../../../core", diff --git a/packages/test-utils/sdks/petstore/package.json b/packages/test-utils/sdks/petstore/package.json index b228b87d..e2b60992 100644 --- a/packages/test-utils/sdks/petstore/package.json +++ b/packages/test-utils/sdks/petstore/package.json @@ -20,8 +20,7 @@ "openapi.json" ], "scripts": { - "lint": "tsc --noEmit", - "prepare": "tsup" + "lint": "tsc --noEmit" }, "dependencies": { "@readme/api-core": "file:../../../../../core", diff --git a/packages/test-utils/sdks/readme/package.json b/packages/test-utils/sdks/readme/package.json index e55cb8d8..d6283c74 100644 --- a/packages/test-utils/sdks/readme/package.json +++ b/packages/test-utils/sdks/readme/package.json @@ -19,8 +19,7 @@ "openapi.json" ], "scripts": { - "lint": "tsc --noEmit", - "prepare": "tsup" + "lint": "tsc --noEmit" }, "dependencies": { "@readme/api-core": "file:../../../../../core", diff --git a/packages/test-utils/sdks/response-title-quirks/package.json b/packages/test-utils/sdks/response-title-quirks/package.json index 3b8a3bf8..268c71e3 100644 --- a/packages/test-utils/sdks/response-title-quirks/package.json +++ b/packages/test-utils/sdks/response-title-quirks/package.json @@ -19,8 +19,7 @@ "openapi.json" ], "scripts": { - "lint": "tsc --noEmit", - "prepare": "tsup" + "lint": "tsc --noEmit" }, "dependencies": { "@readme/api-core": "file:../../../../../core", diff --git a/packages/test-utils/sdks/simple/package.json b/packages/test-utils/sdks/simple/package.json index b272a189..7d8ca96b 100644 --- a/packages/test-utils/sdks/simple/package.json +++ b/packages/test-utils/sdks/simple/package.json @@ -19,8 +19,7 @@ "openapi.json" ], "scripts": { - "lint": "tsc --noEmit", - "prepare": "tsup" + "lint": "tsc --noEmit" }, "dependencies": { "@readme/api-core": "file:../../../../../core", diff --git a/packages/test-utils/sdks/star-trek/package.json b/packages/test-utils/sdks/star-trek/package.json index 8dfa926d..ffb7649a 100644 --- a/packages/test-utils/sdks/star-trek/package.json +++ b/packages/test-utils/sdks/star-trek/package.json @@ -19,8 +19,7 @@ "openapi.json" ], "scripts": { - "lint": "tsc --noEmit", - "prepare": "tsup" + "lint": "tsc --noEmit" }, "dependencies": { "@readme/api-core": "file:../../../../../core",