Repository navigation
Expand file tree
/
Copy pathTaskExecutor.ts
More file actions
118 lines (95 loc) · 3.25 KB
/
Copy pathTaskExecutor.ts
File metadata and controls
118 lines (95 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import execa from 'execa'
import { Subject } from 'rxjs'
import { Merge } from '@nnode/common'
import { Logger } from '../Logging'
import { TaskEntry } from '../Models/TaskEntry'
import { TaskRunnerResult } from './TaskRunnerResult'
import { TaskEntryType } from '../Models/TaskEntryType'
import { TaskRunnerOptions } from './TaskRunnerOptions'
import { TaskResultError } from '../Errors/TaskResultError'
export class TaskExecutor extends Subject<TaskRunnerResult> {
private readonly log = Logger.extend('task-executor')
async execute(entry: TaskEntry, options: TaskRunnerOptions): Promise<TaskRunnerResult> {
try {
const execaOpts: execa.Options<string> = {
cwd: options.cwd,
detached: entry.type === TaskEntryType.detached,
env: options.env,
shell: entry.type === TaskEntryType.shell ? '/bin/bash' : false,
}
const command = [entry.name, ...entry.args].join(' ')
this.log.info('execute', command, entry)
if (options.streaming) {
const stream = this.stream(entry, execaOpts)
const results = await stream
this.complete()
return this.createResult(entry, results)
}
const result = await this.sync(entry, execaOpts)
this.complete()
return result
} catch (error) {
return this.bail(error, entry)
}
}
private bail(error: execa.ExecaError, entry: TaskEntry): TaskRunnerResult {
this.error(error)
if (entry.type === TaskEntryType.nobail) {
return {
command: entry.command,
entry,
exitCode: 0,
stderr: error.stderr ? error.stderr.split('\n') : [],
stdout: error.stdout ? error.stdout.split('\n') : [],
}
}
process.exit(error.exitCode)
}
private broadcast(result: TaskRunnerResult): TaskRunnerResult {
if (result.exitCode !== 0) {
this.error(new TaskResultError(result))
} else {
this.next(result)
}
this.log.trace('broadcast', result)
return result
}
private createResult(entry: TaskEntry, results: execa.ExecaReturnValue): TaskRunnerResult {
const result = {
entry,
command: [entry.name, ...entry.args].join(' '),
exitCode: results.exitCode,
stderr: results.stderr ? results.stderr.split('\n') : [],
stdout: results.stdout ? results.stdout.split('\n') : [],
}
return this.broadcast(result)
}
private stream(entry: TaskEntry, options: execa.Options): execa.ExecaChildProcess {
const child = execa(entry.name, entry.args, {
...options,
...{ stderr: process.stderr, stdin: process.stdin, stdout: process.stdout },
})
if (child.stderr) {
child.stderr.pipe(process.stderr)
}
if (child.stdout) {
child.stdout.pipe(process.stdout)
}
return child
}
private async sync(entry: TaskEntry, options: execa.Options): Promise<TaskRunnerResult> {
const command = [entry.name].concat(entry.args).join(' ')
const results = await execa.command(
command,
Merge<execa.Options>(options, {
stderr: process.stderr,
stdin: process.stdin,
stdout: process.stdout,
}),
)
if (results.exitCode !== 0) {
throw new Error(`executor failed when running: ${command}`)
}
return this.createResult(entry, results)
}
}