Skip to content

Commit

Permalink
fix: forward exit code of external package commands (#412)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben McCann <[email protected]>
  • Loading branch information
rChaoz and benmccann authored Jan 22, 2025
1 parent 8e76bbf commit ea45ddb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-apples-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': patch
---

fix: forward exit code of external package commands
5 changes: 4 additions & 1 deletion packages/cli/commands/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Command } from 'commander';
import * as resolve from 'empathic/resolve';
import { resolveCommand } from 'package-manager-detector/commands';
import { getUserAgent } from '../utils/package-manager.ts';
import { forwardExitCode } from '../utils/common.js';

export const check = new Command('check')
.description('a CLI for checking your Svelte code')
Expand Down Expand Up @@ -42,5 +43,7 @@ function runCheck(cwd: string, args: string[]) {
try {
const cmd = resolveCommand(pm, 'execute-local', ['svelte-check', ...args])!;
execSync(`${cmd.command} ${cmd.args.join(' ')}`, { stdio: 'inherit', cwd });
} catch {}
} catch (error) {
forwardExitCode(error);
}
}
5 changes: 4 additions & 1 deletion packages/cli/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import process from 'node:process';
import { Command } from 'commander';
import { resolveCommand } from 'package-manager-detector';
import { getUserAgent } from '../utils/package-manager.ts';
import { forwardExitCode } from '../utils/common.js';

export const migrate = new Command('migrate')
.description('a CLI for migrating Svelte(Kit) codebases')
Expand Down Expand Up @@ -31,5 +32,7 @@ function runMigrate(cwd: string, args: string[]) {

const cmd = resolveCommand(pm, 'execute', cmdArgs)!;
execSync(`${cmd.command} ${cmd.args.join(' ')}`, { stdio: 'inherit', cwd });
} catch {}
} catch (error) {
forwardExitCode(error);
}
}
9 changes: 9 additions & 0 deletions packages/cli/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import pkg from '../package.json' with { type: 'json' };
import * as p from '@sveltejs/clack-prompts';
import type { Argument, HelpConfiguration, Option } from 'commander';
import { UnsupportedError } from './errors.ts';
import process from 'node:process';

const NO_PREFIX = '--no-';
let options: readonly Option[] = [];
Expand Down Expand Up @@ -96,3 +97,11 @@ export function getPadding(lines: string[]) {
const lengths = lines.map((s) => s.length);
return Math.max(...lengths);
}

export function forwardExitCode(error: unknown) {
if (error && typeof error === 'object' && 'status' in error && typeof error.status == 'number') {
process.exit(error.status);
} else {
process.exit(1);
}
}

0 comments on commit ea45ddb

Please sign in to comment.