Skip to content

Commit

Permalink
fix(api): run npm install with --ignore-scripts (#793)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

---------

Co-authored-by: Kanad Gupta <[email protected]>
  • Loading branch information
erunion and kanadgupta authored Oct 26, 2023
1 parent 0b88a3b commit f93f152
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 55 deletions.
65 changes: 28 additions & 37 deletions packages/api/src/codegen/languages/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -61,6 +62,22 @@ interface OperationTypeHousing {
const REF_PLACEHOLDER = '::convert::';
const REF_PLACEHOLDER_REGEX = /"::convert::([a-zA-Z_$\\d]*)"/g;

function handleExecSuccess(res: ExecaReturnValue<string>, 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;

Expand Down Expand Up @@ -146,12 +163,7 @@ export default class TSGenerator extends CodeGenerator {
// adding `@api/<sdk identifier>` as a dependency there so you can load it with
// `require('@api/<sdk identifier>)`.
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
Expand All @@ -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);
});
}

Expand All @@ -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));
}

/**
Expand All @@ -197,23 +197,15 @@ export default class TSGenerator extends CodeGenerator {
async compile(storage: Storage, opts: InstallerOptions = {}): Promise<void> {
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));
}

/**
Expand Down Expand Up @@ -634,7 +626,6 @@ dist/
files: ['dist', 'openapi.json'],
scripts: {
lint: 'tsc --noEmit',
prepare: 'tsup',
},
dependencies,
devDependencies,
Expand Down
3 changes: 1 addition & 2 deletions packages/test-utils/sdks/alby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"openapi.json"
],
"scripts": {
"lint": "tsc --noEmit",
"prepare": "tsup"
"lint": "tsc --noEmit"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
Expand Down
3 changes: 1 addition & 2 deletions packages/test-utils/sdks/metrotransit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"openapi.json"
],
"scripts": {
"lint": "tsc --noEmit",
"prepare": "tsup"
"lint": "tsc --noEmit"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
Expand Down
3 changes: 1 addition & 2 deletions packages/test-utils/sdks/operationid-quirks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"openapi.json"
],
"scripts": {
"lint": "tsc --noEmit",
"prepare": "tsup"
"lint": "tsc --noEmit"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
Expand Down
3 changes: 1 addition & 2 deletions packages/test-utils/sdks/optional-payload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"openapi.json"
],
"scripts": {
"lint": "tsc --noEmit",
"prepare": "tsup"
"lint": "tsc --noEmit"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
Expand Down
3 changes: 1 addition & 2 deletions packages/test-utils/sdks/petstore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"openapi.json"
],
"scripts": {
"lint": "tsc --noEmit",
"prepare": "tsup"
"lint": "tsc --noEmit"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
Expand Down
3 changes: 1 addition & 2 deletions packages/test-utils/sdks/readme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"openapi.json"
],
"scripts": {
"lint": "tsc --noEmit",
"prepare": "tsup"
"lint": "tsc --noEmit"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
Expand Down
3 changes: 1 addition & 2 deletions packages/test-utils/sdks/response-title-quirks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"openapi.json"
],
"scripts": {
"lint": "tsc --noEmit",
"prepare": "tsup"
"lint": "tsc --noEmit"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
Expand Down
3 changes: 1 addition & 2 deletions packages/test-utils/sdks/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"openapi.json"
],
"scripts": {
"lint": "tsc --noEmit",
"prepare": "tsup"
"lint": "tsc --noEmit"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
Expand Down
3 changes: 1 addition & 2 deletions packages/test-utils/sdks/star-trek/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"openapi.json"
],
"scripts": {
"lint": "tsc --noEmit",
"prepare": "tsup"
"lint": "tsc --noEmit"
},
"dependencies": {
"@readme/api-core": "file:../../../../../core",
Expand Down

0 comments on commit f93f152

Please sign in to comment.