Skip to content

Commit

Permalink
Merge pull request #22039 from storybookjs/valentin/fix-npm-ls-unexpe…
Browse files Browse the repository at this point in the history
…cted-error-thrown-1

CLI: Catch errors thrown on sanity check of SB installs
  • Loading branch information
shilman committed Apr 12, 2023
2 parents eb3c849 + 86de42e commit 95ae92b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 3 additions & 2 deletions code/lib/cli/src/js-package-manager/JsPackageManager.ts
Expand Up @@ -416,7 +416,8 @@ export abstract class JsPackageManager {
command: string,
args: string[],
stdio?: 'pipe' | 'inherit',
cwd?: string
cwd?: string,
ignoreError?: boolean
): string {
const commandResult = spawnSync(command, args, {
cwd: cwd ?? this.cwd,
Expand All @@ -425,7 +426,7 @@ export abstract class JsPackageManager {
shell: true,
});

if (commandResult.status !== 0) {
if (commandResult.status !== 0 && ignoreError !== true) {
throw new Error(commandResult.stderr ?? '');
}

Expand Down
11 changes: 9 additions & 2 deletions code/lib/cli/src/js-package-manager/NPMProxy.ts
Expand Up @@ -48,12 +48,19 @@ export class NPMProxy extends JsPackageManager {
}

public runPackageCommand(command: string, args: string[], cwd?: string): string {
return this.executeCommand(`npm`, ['exec', '--', command, ...args], undefined, cwd);
return this.executeCommand('npm', ['exec', '--', command, ...args], undefined, cwd);
}

public findInstallations() {
const pipeToNull = platform() === 'win32' ? '2>NUL' : '2>/dev/null';
const commandResult = this.executeCommand('npm', ['ls', '--json', '--depth=99', pipeToNull]);
const commandResult = this.executeCommand(
'npm',
['ls', '--json', '--depth=99', pipeToNull],
undefined,
undefined,
// ignore errors, because npm ls will exit with code 1 if there are e.g. unmet peer dependencies
true
);

try {
const parsedOutput = JSON.parse(commandResult);
Expand Down

0 comments on commit 95ae92b

Please sign in to comment.