-
Notifications
You must be signed in to change notification settings - Fork 73
/
createTargetGraph.ts
84 lines (73 loc) · 2.47 KB
/
createTargetGraph.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import type { Logger } from "@lage-run/logger";
import { WorkspaceTargetGraphBuilder } from "@lage-run/target-graph";
import type { PackageInfos } from "workspace-tools";
import { getBranchChanges, getDefaultRemoteBranch, getStagedChanges, getUnstagedChanges, getUntrackedChanges } from "workspace-tools";
import { getFilteredPackages } from "../../filter/getFilteredPackages.js";
import type { PipelineDefinition } from "@lage-run/config";
import { hasRepoChanged } from "../../filter/hasRepoChanged.js";
interface CreateTargetGraphOptions {
logger: Logger;
root: string;
dependencies: boolean;
dependents: boolean;
since?: string;
scope?: string[];
ignore: string[];
repoWideChanges: string[];
pipeline: PipelineDefinition;
outputs: string[];
tasks: string[];
packageInfos: PackageInfos;
}
function getChangedFiles(since: string, cwd: string) {
const targetBranch = since || getDefaultRemoteBranch({ cwd });
const changes = [
...new Set([
...(getUntrackedChanges(cwd) || []),
...(getUnstagedChanges(cwd) || []),
...(getBranchChanges(targetBranch, cwd) || []),
...(getStagedChanges(cwd) || []),
]),
];
return changes;
}
export async function createTargetGraph(options: CreateTargetGraphOptions) {
const { logger, root, dependencies, dependents, since, scope, repoWideChanges, ignore, pipeline, outputs, tasks, packageInfos } = options;
const builder = new WorkspaceTargetGraphBuilder(root, packageInfos);
const packages = getFilteredPackages({
root,
logger,
packageInfos,
includeDependencies: dependencies,
includeDependents: dependents,
since,
scope,
repoWideChanges,
sinceIgnoreGlobs: ignore,
});
let changedFiles: string[] = [];
// TODO: enhancement would be for workspace-tools to implement a "getChangedPackageFromChangedFiles()" type function
// TODO: optimize this so that we don't double up the work to determine if repo has changed
if (since) {
if (!hasRepoChanged(since, root, repoWideChanges, logger)) {
changedFiles = getChangedFiles(since, root);
}
}
for (const [id, definition] of Object.entries(pipeline)) {
if (Array.isArray(definition)) {
builder.addTargetConfig(
id,
{
cache: true,
dependsOn: definition,
options: {},
outputs,
},
changedFiles
);
} else {
builder.addTargetConfig(id, definition, changedFiles);
}
}
return await builder.build(tasks, packages);
}