Print process id when running
This commit is contained in:
parent
e27fdfb212
commit
8c7b537a99
@ -32,7 +32,8 @@ describe('Subprocess', () => {
|
||||
// })
|
||||
|
||||
it('resolves on successful invocation', async () => {
|
||||
await new Subprocess('ls', [], {}, StdioOptions.IGNORE).run()
|
||||
await new Subprocess(
|
||||
process.argv[0], ['-e', '1 + 1'], {}, StdioOptions.IGNORE).run()
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
@ -8,16 +8,22 @@ export enum StdioOptions {
|
||||
|
||||
export class Subprocess {
|
||||
|
||||
static count = 0
|
||||
private readonly i: number
|
||||
|
||||
constructor(
|
||||
public readonly command: string,
|
||||
public readonly args: readonly string[],
|
||||
public readonly environment: Record<string, string | undefined>,
|
||||
public readonly stdio: StdioOptions = StdioOptions.PIPE,
|
||||
) {}
|
||||
) {
|
||||
this.i = ++Subprocess.count
|
||||
}
|
||||
|
||||
async run(cwd?: string) {
|
||||
const {i} = this
|
||||
return new Promise((resolve, reject) => {
|
||||
process.stderr.write(`> ${this.command} ${this.args.join(' ')}\n`)
|
||||
process.stderr.write(`[${i}]: ${this.command} ${this.args.join(' ')}\n`)
|
||||
const subprocess = spawn(this.command, this.args, {
|
||||
shell: false,
|
||||
stdio: this.stdio,
|
||||
@ -26,8 +32,14 @@ export class Subprocess {
|
||||
})
|
||||
|
||||
if (this.stdio === StdioOptions.PIPE) {
|
||||
subprocess.stdout!.on('data', data => process.stdout.write(data))
|
||||
subprocess.stderr!.on('data', data => process.stderr.write(data))
|
||||
subprocess.stdout!.on('data', data => {
|
||||
process.stdout.write(`${i}> `)
|
||||
process.stdout.write(data)
|
||||
})
|
||||
subprocess.stderr!.on('data', data => {
|
||||
process.stdout.write(`${i}> `)
|
||||
process.stderr.write(data)
|
||||
})
|
||||
}
|
||||
|
||||
subprocess.on('close', code => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user