Print process id when running

This commit is contained in:
Jerko Steiner 2019-11-01 12:54:01 -04:00
parent e27fdfb212
commit 8c7b537a99
2 changed files with 18 additions and 5 deletions

View File

@ -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()
})
})

View File

@ -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 => {