|
1 | 1 | import { spawnSync } from 'child_process'; |
2 | 2 | import path from 'path'; |
3 | 3 | import type { CommandContext } from './types'; |
| 4 | +import { scriptName } from './utils/constants'; |
| 5 | +import { t } from './utils/i18n'; |
4 | 6 | import { getInstallCommand } from './utils/runtime'; |
5 | 7 |
|
| 8 | +// package names / version specs only — rejects shell metacharacters since |
| 9 | +// Windows needs shell: true to run package manager .cmd shims |
| 10 | +const SAFE_PACKAGE_ARG = /^[@a-z0-9^~._/-]+$/i; |
| 11 | + |
6 | 12 | export const installCommands = { |
7 | 13 | install: async ({ args }: CommandContext) => { |
8 | 14 | if (args.length === 0) { |
9 | | - return; |
| 15 | + throw new Error(t('installPackageRequired', { scriptName })); |
| 16 | + } |
| 17 | + for (const arg of args) { |
| 18 | + if (!SAFE_PACKAGE_ARG.test(arg)) { |
| 19 | + throw new Error(t('installPackageRequired', { scriptName })); |
| 20 | + } |
10 | 21 | } |
11 | 22 |
|
12 | 23 | const cliDir = path.resolve(__dirname, '..'); |
13 | 24 | const installCommand = getInstallCommand(args, cliDir); |
14 | 25 |
|
15 | | - spawnSync(installCommand.command, installCommand.args, { |
| 26 | + const result = spawnSync(installCommand.command, installCommand.args, { |
16 | 27 | cwd: cliDir, |
17 | 28 | stdio: 'inherit', |
| 29 | + // package manager executables are .cmd shims on Windows |
| 30 | + shell: process.platform === 'win32', |
18 | 31 | }); |
| 32 | + |
| 33 | + if (result.error) { |
| 34 | + throw new Error( |
| 35 | + t('installFailed', { |
| 36 | + packages: args.join(' '), |
| 37 | + error: result.error.message, |
| 38 | + }), |
| 39 | + ); |
| 40 | + } |
| 41 | + if (result.status !== 0) { |
| 42 | + throw new Error( |
| 43 | + t('installFailed', { |
| 44 | + packages: args.join(' '), |
| 45 | + error: `${installCommand.command} exited with code ${result.status}`, |
| 46 | + }), |
| 47 | + ); |
| 48 | + } |
19 | 49 | }, |
20 | 50 | }; |
0 commit comments