|
1 | 1 | import confirm from '@inquirer/confirm' |
2 | 2 | import select from '@inquirer/select' |
3 | | -import { execa } from 'execa' |
| 3 | +import spawn, { SubprocessError } from 'nano-spawn' |
4 | 4 | import { createSpinner } from 'nanospinner' |
5 | 5 | import * as picocolor from 'picocolors' |
6 | | -import { exec } from 'node:child_process' |
7 | 6 | import type { EventEmitter } from 'node:events' |
8 | | -import { chdir, exit } from 'node:process' |
| 7 | +import { exit } from 'node:process' |
9 | 8 | import { projectDependenciesHook } from '../hook' |
10 | 9 |
|
11 | 10 | type PackageManager = 'npm' | 'bun' | 'deno' | 'pnpm' | 'yarn' |
@@ -57,7 +56,7 @@ const registerInstallationHook = ( |
57 | 56 | if (installedPackageManagerNames.includes('deno')) { |
58 | 57 | let isVersion1 = false |
59 | 58 | try { |
60 | | - const { stdout } = await execa('deno', ['-v']) |
| 59 | + const { stdout } = await spawn('deno', ['-v']) |
61 | 60 | isVersion1 = stdout.split(' ')[1].split('.')[0] === '1' |
62 | 61 | } catch { |
63 | 62 | isVersion1 = true |
@@ -101,29 +100,31 @@ const registerInstallationHook = ( |
101 | 100 | emitter.emit('packageManager', packageManager) |
102 | 101 |
|
103 | 102 | emitter.on('dependencies', async () => { |
104 | | - chdir(directoryPath) |
105 | | - |
106 | 103 | if (!knownPackageManagers[packageManager]) { |
107 | 104 | exit(1) |
108 | 105 | } |
109 | 106 |
|
110 | 107 | const spinner = createSpinner('Installing project dependencies').start() |
111 | | - const proc = exec(knownPackageManagers[packageManager]) |
112 | 108 |
|
113 | | - const procExit: number = await new Promise((res) => { |
114 | | - proc.on('exit', (code) => res(code == null ? 0xff : code)) |
115 | | - }) |
| 109 | + const [command, ...args] = knownPackageManagers[packageManager].split(' ') |
116 | 110 |
|
117 | | - if (procExit === 0) { |
118 | | - spinner.success() |
119 | | - } else { |
120 | | - spinner.stop({ |
121 | | - mark: picocolor.red('×'), |
122 | | - text: 'Failed to install project dependencies', |
| 111 | + try { |
| 112 | + await spawn(command, args, { |
| 113 | + cwd: directoryPath, |
123 | 114 | }) |
124 | | - exit(procExit) |
| 115 | + } catch (error: unknown) { |
| 116 | + if (error instanceof SubprocessError) { |
| 117 | + spinner.stop({ |
| 118 | + mark: picocolor.red('×'), |
| 119 | + text: 'Failed to install project dependencies', |
| 120 | + }) |
| 121 | + exit(error.exitCode ?? 1) |
| 122 | + } |
| 123 | + throw error |
125 | 124 | } |
126 | 125 |
|
| 126 | + spinner.success() |
| 127 | + |
127 | 128 | emitter.emit('completed') |
128 | 129 | }) |
129 | 130 |
|
@@ -152,7 +153,7 @@ function getCurrentPackageManager(): PackageManager { |
152 | 153 |
|
153 | 154 | function checkPackageManagerInstalled(packageManager: string) { |
154 | 155 | return new Promise<boolean>((resolve) => { |
155 | | - execa(packageManager, ['--version']) |
| 156 | + spawn(packageManager, ['--version']) |
156 | 157 | .then(() => resolve(true)) |
157 | 158 | .catch(() => resolve(false)) |
158 | 159 | }) |
|
0 commit comments