-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
runTask.js
174 lines (151 loc) · 5.05 KB
/
runTask.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { spawn } from 'child_process'
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'fs'
import kleur from 'kleur'
import { getDiffPath, getManifestPath, getTask } from './config.js'
import { log } from './log.js'
import path from 'path'
import stripAnsi from 'strip-ansi'
import { computeManifest } from './manifest/computeManifest.js'
import { workspaceRoot } from './workspaceRoot.js'
/**
* @param {import('./types.js').ScheduledTask} task
* @param {import('./TaskGraph.js').TaskGraph} tasks
* @returns {Promise<boolean>}
*/
export async function runTaskIfNeeded(task, tasks) {
const manifestPath = getManifestPath(task)
/**
* @param {string} msg
*/
const print = (msg) => console.log(task.terminalPrefix, msg)
const didHaveManifest = existsSync(manifestPath)
const didChange = await computeManifest({
task,
tasks,
})
let didRunTask = false
if (task.force) {
print('cache miss, --force flag used')
await runTask(task)
didRunTask = true
} else if (didChange === null) {
print('cache disabled')
await runTask(task)
didRunTask = true
} else if (didChange) {
const diffPath = getDiffPath(task)
const diff = existsSync(diffPath) ? readFileSync(diffPath, 'utf-8').toString() : null
if (diff?.length) {
const allLines = diff.split('\n')
const diffPath = getDiffPath(task)
if (!existsSync(path.dirname(diffPath))) {
mkdirSync(path.dirname(diffPath), { recursive: true })
}
writeFileSync(diffPath, stripAnsi(allLines.join('\n')))
print('cache miss, changes since last run:')
allLines.slice(0, 10).forEach((line) => print(kleur.gray(line)))
if (allLines.length > 10) {
print(kleur.gray(`... and ${allLines.length - 10} more. See ${diffPath} for full diff.`))
}
} else if (!didHaveManifest) {
print('cache miss, no previous manifest found')
}
await runTask(task)
didRunTask = true
} else {
print(`cache hit ⚡️`)
}
print(kleur.gray('input manifest saved: ' + path.relative(workspaceRoot, manifestPath)))
return didRunTask
}
/**
* @param {import('./types.js').ScheduledTask} task
* @returns {Promise<void>}
*/
async function runTask(task) {
const packageJson = JSON.parse(readFileSync(`${task.cwd}/package.json`, 'utf8'))
let command = packageJson.scripts[task.taskName]
if (command.startsWith('lazy :inherit')) {
const { defaultCommand } = await getTask({ taskName: task.taskName })
if (!defaultCommand) {
// TODO: evaluate this stuff ahead-of-time
log.fail(
`Encountered 'lazy :inherit' for scripts#${task.taskName} in ${task.cwd}/package.json, but there is defaultCommand configured for the task '${task.taskName}'`,
)
process.exit(1)
}
command = defaultCommand + ' ' + command.slice('lazy :inherit'.length)
command = command.trim()
}
const extraArgs = process.argv.slice(3)
const start = Date.now()
console.log(task.terminalPrefix + kleur.bold(' RUN ') + kleur.green().bold(command))
try {
await new Promise((resolve, reject) => {
const proc = spawn(command, extraArgs, {
cwd: task.cwd,
shell: true,
env: {
...process.env,
PATH: `${process.env.PATH}:${workspaceRoot}/node_modules/.bin:./node_modules/.bin`,
FORCE_COLOR: '1',
npm_lifecycle_event: task.taskName,
},
})
// forward all output to the terminal without losing color
let outData = ''
// save stdout to buffer
proc.stdout.on('data', (data) => {
outData += data
const lastLineFeed = outData.lastIndexOf('\n')
if (lastLineFeed === -1) {
return
}
const lines = outData.replaceAll('\r', '').split('\n')
outData = lines.pop() || ''
for (const line of lines) {
process.stdout.write(task.terminalPrefix + ' ' + line + '\n')
}
})
let errData = ''
proc.stderr.on('data', (data) => {
errData += data
const lastLineFeed = errData.lastIndexOf('\n')
if (lastLineFeed === -1) {
return
}
const lines = errData.replaceAll('\r', '').split('\n')
errData = lines.pop() || ''
for (const line of lines) {
process.stderr.write(task.terminalPrefix + ' ' + line + '\n')
}
})
proc.on('exit', (code) => {
if (outData) {
process.stdout.write(task.terminalPrefix + ' ' + outData + '\n')
}
if (errData) {
process.stderr.write(task.terminalPrefix + ' ' + errData + '\n')
}
if (code === 0) {
resolve(null)
} else {
reject(new Error(`Command '${command}' exited with code ${code}`))
}
})
proc.on('error', (err) => {
reject(err)
})
})
} catch (e) {
const manifestPath = getManifestPath(task)
if (existsSync(manifestPath)) {
unlinkSync(manifestPath)
}
throw e
}
log.log(
task.terminalPrefix,
`done in ${kleur.cyan(((Date.now() - start) / 1000).toFixed(2) + 's')}`,
)
}