forked from backstage/backstage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request backstage#304 from spotify/rugvip/cli-helpers
cli: add helpers for running child processes and error handling in commands
- Loading branch information
Showing
10 changed files
with
151 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2020 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import chalk from 'chalk'; | ||
|
||
export class CustomError extends Error { | ||
get name(): string { | ||
return this.constructor.name; | ||
} | ||
} | ||
|
||
export class ExitCodeError extends CustomError { | ||
readonly code: number; | ||
|
||
constructor(code: number, command?: string) { | ||
if (command) { | ||
super(`Command '${command}' exited with code ${code}`); | ||
} else { | ||
super(`Child exited with code ${code}`); | ||
} | ||
this.code = code; | ||
} | ||
} | ||
|
||
export function exitWithError(error: Error): never { | ||
if (error instanceof ExitCodeError) { | ||
process.stderr.write(`${chalk.red(error.message)}\n`); | ||
process.exit(error.code); | ||
} else { | ||
process.stderr.write(`${chalk.red(`${error}`)}\n`); | ||
process.exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2020 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { SpawnSyncOptions, spawn, ChildProcess } from 'child_process'; | ||
import { ExitCodeError } from './errors'; | ||
|
||
// Runs a child command, returning a promise that is only resolved if the child exits with code 0. | ||
export async function run( | ||
name: string, | ||
args: string[] = [], | ||
options: SpawnSyncOptions = {}, | ||
) { | ||
const env: NodeJS.ProcessEnv = { | ||
...process.env, | ||
FORCE_COLOR: 'true', | ||
...(options.env ?? {}), | ||
}; | ||
|
||
const child = spawn(name, args, { | ||
stdio: 'inherit', | ||
shell: true, | ||
...options, | ||
env, | ||
}); | ||
|
||
await waitForExit(child); | ||
} | ||
|
||
export async function waitForExit( | ||
child: ChildProcess & { exitCode?: number }, | ||
): Promise<void> { | ||
if (typeof child.exitCode === 'number') { | ||
if (child.exitCode) { | ||
throw new ExitCodeError(child.exitCode, name); | ||
} | ||
return; | ||
} | ||
|
||
await new Promise((resolve, reject) => { | ||
child.once('error', error => reject(error)); | ||
child.once('exit', code => { | ||
if (code) { | ||
reject(new ExitCodeError(code, name)); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters