|
1 | 1 | import {spawn, exec, SpawnOptionsWithoutStdio} from 'child_process'; |
| 2 | +import {RunCommandResult} from '@project-types/common'; |
| 3 | +import {CommandResultCode} from 'src/constants'; |
2 | 4 |
|
3 | 5 | /** |
4 | 6 | * Run command with text output in terminal |
5 | 7 | */ |
6 | | -export const runCommand = (command: string, options?: SpawnOptionsWithoutStdio): Promise<string> => { |
| 8 | +export const runCommand = (command: string, options?: SpawnOptionsWithoutStdio): Promise<RunCommandResult> => { |
7 | 9 | const [commandName, ...args] = command.split(' '); |
8 | 10 |
|
9 | 11 | return new Promise((resolve, reject) => { |
10 | 12 | const spawnCommand = spawn(commandName, args, options); |
11 | 13 | let result = ''; |
| 14 | + let errorMessage = ''; |
12 | 15 |
|
13 | 16 | spawnCommand.stdout.on('data', (data) => { |
14 | 17 | result += data.toString(); |
15 | 18 | }); |
16 | 19 |
|
| 20 | + spawnCommand.stderr.on('data', (data) => { |
| 21 | + errorMessage += data.toString(); |
| 22 | + }); |
| 23 | + |
17 | 24 | spawnCommand.on('error', (error) => { |
18 | 25 | console.log('error', error); |
19 | | - reject(error); |
| 26 | + reject({code: CommandResultCode.Error, error: error.message}); |
20 | 27 | }); |
21 | 28 |
|
22 | 29 | spawnCommand.on('close', (code) => { |
23 | | - resolve(result); |
| 30 | + if (code) { |
| 31 | + resolve({code: CommandResultCode.Error, error: errorMessage}); |
| 32 | + } else { |
| 33 | + resolve({code: CommandResultCode.Success, result}); |
| 34 | + } |
24 | 35 |
|
25 | 36 | return code; |
26 | 37 | }); |
|
0 commit comments