-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
computeManifest.js
162 lines (137 loc) · 4.86 KB
/
computeManifest.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
import path, { join } from 'path'
import pc from 'picocolors'
import { createTimer } from '../createTimer.js'
import { existsSync, mkdirSync, statSync } from '../fs.js'
import { uniq } from '../uniq.js'
import { ManifestConstructor } from './ManifestConstructor.js'
import { getInputFiles } from './getInputFiles.js'
import { hashFile, hashString } from './hash.js'
const types = {
upstreamTaskInputs: 'upstream task inputs',
dependencyTaskInputs: 'dependency task inputs',
envVar: 'env var',
file: 'file',
}
const order = [types.upstreamTaskInputs, types.dependencyTaskInputs, types.envVar, types.file]
/**
*
* @param {string} a
* @param {string} b
* @returns
*/
export const compareManifestTypes = (a, b) => {
const aIndex = order.indexOf(a)
const bIndex = order.indexOf(b)
if (aIndex === bIndex) {
return 0
}
return aIndex < bIndex ? -1 : 1
}
/**
* @param {{ task: import('../types.js').ScheduledTask, tasks: import('../TaskGraph.js').TaskGraph }} param0
* @returns
*/
export async function computeManifest({ tasks, task }) {
if (task.taskConfig.cache === 'none') return null
const manifestPath = task.taskConfig.getManifestPath()
const nextManifestPath = task.taskConfig.getNextManifestPath()
const diffPath = task.taskConfig.getDiffPath()
if (!existsSync(path.dirname(manifestPath))) {
mkdirSync(path.dirname(manifestPath), { recursive: true })
}
if (!existsSync(path.dirname(nextManifestPath))) {
mkdirSync(path.dirname(nextManifestPath), { recursive: true })
}
if (diffPath && !existsSync(path.dirname(diffPath))) {
mkdirSync(path.dirname(diffPath), { recursive: true })
}
const manifestConstructor = new ManifestConstructor({
diffPath,
previousManifestPath: manifestPath,
nextManifestPath,
})
const extraFiles = []
for (const [otherTaskName, depConfig] of task.taskConfig.runsAfterEntries) {
if (!depConfig.inheritsInput && depConfig.usesOutput === false) continue
const isTopLevel =
tasks.config.getTaskConfig(task.workspace.dir, otherTaskName).execution === 'top-level'
const key = tasks.config.getTaskKey(
isTopLevel ? tasks.config.project.root.dir : task.workspace.dir,
otherTaskName,
)
const depTask = tasks.allTasks[key]
if (isTopLevel && !depTask) throw new Error(`Missing task: ${key}.`)
if (!depTask) continue
if (depConfig.inheritsInput) {
if (!depTask.inputManifestCacheKey) {
throw new Error(`Missing inputManifestCacheKey for task: ${key}.`)
}
manifestConstructor.update('upstream task inputs', key, depTask.inputManifestCacheKey)
}
if (depConfig.usesOutput !== false) {
extraFiles.push(depTask.outputFiles)
}
}
if (
task.taskConfig.execution !== 'independent' &&
(task.taskConfig.cache?.inheritsInputFromDependencies ?? true)
) {
// TODO: test that localDeps is always sorted
const upstreamTaskKeys = task.workspace.localDependencyWorkspaceNames
.map((packageName) => {
const depPackage = tasks.config.project.getWorkspaceByName(packageName)
const key = tasks.config.getTaskKey(depPackage.dir, task.taskName)
return key
})
.sort()
if (upstreamTaskKeys) {
for (const key of upstreamTaskKeys) {
const depTask = tasks.allTasks[key]
if (!depTask) continue
if (!depTask.inputManifestCacheKey) {
throw new Error(`Missing inputManifestCacheKey for task: ${key}.`)
}
manifestConstructor.update('upstream package inputs', key, depTask.inputManifestCacheKey)
}
}
}
const allEnvVars = uniq(
(tasks.config.getBaseCacheConfig().envInputs ?? []).concat(
task.taskConfig.cache?.envInputs ?? [],
),
).sort()
for (const envVar of allEnvVars) {
const hash = hashString(process.env[envVar] ?? '')
manifestConstructor.update('env var', envVar, hash)
}
let numSkipped = 0
let numHashed = 0
// getInputFiles returns null for cache=none
// TODO: make it clearer that's what's happening. Result type or something
const files = getInputFiles(tasks, task, extraFiles.flat())
if (!files) return null
const timer = createTimer()
for (const file of files.sort()) {
const fullPath = join(tasks.config.project.root.dir, file)
const stat = statSync(fullPath)
const timestamp = String(stat.mtimeMs)
if (manifestConstructor.copyLineOverIfMetaIsSame('file', file, timestamp)) {
numSkipped++
continue
}
numHashed++
const hash = hashFile(fullPath, stat.size)
manifestConstructor.update('file', file, hash, timestamp)
}
const { didChange, hash } = await manifestConstructor.end()
// todo: always log this if verbose
if (timer.getElapsedMs() > 100) {
task.logger.note(
`Hashed ${numHashed}/${numSkipped + numHashed} files in ${pc.cyan(
timer.formatElapsedTime(),
)}`,
)
}
task.inputManifestCacheKey = hash
return didChange
}