Repository navigation
Expand file tree
/
Copy pathUpdateScript.ts
More file actions
84 lines (68 loc) · 2.44 KB
/
Copy pathUpdateScript.ts
File metadata and controls
84 lines (68 loc) · 2.44 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
import * as cp from 'child_process'
import { Log, Logger, Project, Updater } from './index'
export abstract class UpdateScript implements Updater {
protected readonly log: Log
private readonly _name: string
constructor(name: string) {
this._name = name
this.log = Logger(`chest:${name}`)
}
public get name(): string {
return this._name
}
public exec(project: Project): Promise<Project> {
const rootProject = project.children && project.children.length
? Promise.all(project.children.map(child => this.workspace(child).then(proj => this.log.task(proj.name)))).then(() => project)
: Promise.resolve(project)
return rootProject
.then(() => this.log.start(this.name, project.name))
.then(() => project)
}
protected workspace(project: Project): Promise<Project> {
return Promise.resolve(project)
.then(() => this.log.task(this.name, project.name))
.then(() => project)
}
protected run(project: Project, command: string, ...args: string[]): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.log.debug('run', project.name, command, ...args)
const child = cp.exec(`${command} ${args.join(' ')}`, { cwd: project.path }, error => {
/* istanbul ignore next */
if (error) {
this.log.error(error)
}
})
if (!child.stderr || !child.stdout) {
throw new Error('could not get stdout or stderr')
}
child.stderr.on('data', data =>
this.stream(project, process.stderr, data)
.map(lines => lines)
.forEach(args => this.log.error(...args))
)
child.stdout.on('data', data =>
this.stream(project, process.stdout, data)
.map(lines => lines)
.forEach(args => this.log.task(...args))
)
child.addListener('exit', (code: number, signal: string) => {
/* istanbul ignore next */
if (code === 0) {
resolve()
} else {
reject(signal)
}
})
})
}
private stream(project: Project, stream: NodeJS.WriteStream, data: string | Buffer): string[][] {
const format = (lines: string[]): string[][] => {
return lines.filter(line => line.trim()).map(line => [project.name, '>', line])
}
/* istanbul ignore next */
if (data instanceof Buffer) {
return format(data.toString().replace('\r', '').split('\n'))
}
return format(data.replace('\r', '').split('\n'))
}
}