-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
65 lines (57 loc) · 2.15 KB
/
index.ts
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
import { Step, Diff, Plugin, Trigger, Effect } from "@shanzhai/interfaces";
import { generateStepForTrigger } from "./generate-step-for-trigger";
import { listTriggerOrderingConstraints } from "./list-trigger-ordering-constraints";
import { listTriggers } from "./list-triggers";
import { orderSteps } from "./order-steps";
/**
* Queries a given set of {@link Plugin}s to produce a hierarchy of
* {@link Step}s.
* @param plugins The {@link Plugin}s to query.
* @param firstRun True when this is the first run, false when a subsequent run.
* @param diff The diff of filenames for which a build is being performed.
* @returns A hierarchy of {@link Step}s to execute.
*/
export async function plan(
plugins: {
readonly [name: string]: Plugin<{ readonly [name: string]: Trigger }>;
},
firstRun: boolean,
diff: Diff<string>
): Promise<{
readonly unmatchedAddedFiles: ReadonlyArray<string>;
readonly step: Step;
}> {
let triggers = listTriggers(plugins);
if (!firstRun) {
triggers = triggers.filter((trigger) => trigger.trigger.type !== `oneTime`);
}
const triggerOrderingConstraints = listTriggerOrderingConstraints(
triggers.map((trigger) => trigger.trigger)
);
const effects: Effect[] = [];
const triggerSteps: { readonly trigger: Trigger; readonly step: Step }[] = [];
for (const trigger of triggers) {
const result = await generateStepForTrigger(diff, effects, trigger);
effects.push(...result.step.effects);
diff = result.unclaimedFiles;
triggerSteps.push({ trigger: trigger.trigger, step: result.step });
}
return {
unmatchedAddedFiles: diff.added,
step: orderSteps(
triggerSteps.map((triggerStep) => triggerStep.step),
triggerOrderingConstraints.map((orderingConstraint) => [
(
triggerSteps.find(
(triggerStep) => triggerStep.trigger === orderingConstraint[0]
) as { readonly trigger: Trigger; readonly step: Step }
).step,
(
triggerSteps.find(
(triggerStep) => triggerStep.trigger === orderingConstraint[1]
) as { readonly trigger: Trigger; readonly step: Step }
).step,
])
),
};
}