@ucdjs/pipelines-graph 0.0.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-PRESENT Lucas Nørgård
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # @ucdjs/pipelines-graph
2
+
3
+ [![npm version][npm-version-src]][npm-version-href]
4
+ [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
+ [![codecov][codecov-src]][codecov-href]
6
+
7
+ > [!IMPORTANT]
8
+ > This is an internal package. It may change without warning and is not subject to semantic versioning. Use at your own risk.
9
+
10
+ A collection of core pipeline functionalities for the UCD project.
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install @ucdjs/pipelines-graph
16
+ ```
17
+
18
+ ## 📄 License
19
+
20
+ Published under [MIT License](./LICENSE).
21
+
22
+ [npm-version-src]: https://img.shields.io/npm/v/@ucdjs/pipelines-graph?style=flat&colorA=18181B&colorB=4169E1
23
+ [npm-version-href]: https://npmjs.com/package/@ucdjs/pipelines-graph
24
+ [npm-downloads-src]: https://img.shields.io/npm/dm/@ucdjs/pipelines-graph?style=flat&colorA=18181B&colorB=4169E1
25
+ [npm-downloads-href]: https://npmjs.com/package/@ucdjs/pipelines-graph
26
+ [codecov-src]: https://img.shields.io/codecov/c/gh/ucdjs/ucd?style=flat&colorA=18181B&colorB=4169E1
27
+ [codecov-href]: https://codecov.io/gh/ucdjs/ucd
@@ -0,0 +1,50 @@
1
+ import { DAG, FileContext, PipelineDefinition, PipelineGraph, PipelineGraphEdge } from "@ucdjs/pipelines-core";
2
+
3
+ //#region src/builder.d.ts
4
+ interface GraphBuilderOptions {
5
+ includeArtifacts?: boolean;
6
+ }
7
+ declare class PipelineGraphBuilder {
8
+ #private;
9
+ addSourceNode(version: string): string;
10
+ addFileNode(file: FileContext): string;
11
+ addRouteNode(routeId: string, version: string): string;
12
+ addArtifactNode(artifactId: string, version: string): string;
13
+ addOutputNode(outputIndex: number, version: string, property?: string): string;
14
+ addEdge(from: string, to: string, type: PipelineGraphEdge["type"]): void;
15
+ build(): PipelineGraph;
16
+ clear(): void;
17
+ }
18
+ declare function createPipelineGraphBuilder(): PipelineGraphBuilder;
19
+ //#endregion
20
+ //#region src/graph-utils.d.ts
21
+ /**
22
+ * Build a pipeline graph from a pipeline definition and its DAG.
23
+ * @param {PipelineDefinition} pipeline The pipeline definition to build the graph from.
24
+ * @param {DAG} dag The DAG representing the dependencies between routes and artifacts in the pipeline.
25
+ * @returns {PipelineGraph} The constructed pipeline graph.
26
+ */
27
+ declare function buildRouteGraph(pipeline: PipelineDefinition, dag: DAG): PipelineGraph;
28
+ /**
29
+ * Convert a pipeline graph into a visual tree representation.
30
+ * @param {PipelineGraph} graph The pipeline graph to convert.
31
+ * @returns {string} A visual tree representation of the graph.
32
+ */
33
+ declare function toVisualTree(graph: PipelineGraph): string;
34
+ /**
35
+ * Find a node in the graph by its ID.
36
+ * @param {PipelineGraph} graph The pipeline graph to search within.
37
+ * @param {string} nodeId The ID of the node to find.
38
+ * @returns {PipelineGraph["nodes"][number] | undefined} The node with the specified ID, or undefined if not found.
39
+ */
40
+ declare function find(graph: PipelineGraph, nodeId: string): PipelineGraph["nodes"][number] | undefined;
41
+ /**
42
+ * Find edges between two nodes in the graph.
43
+ * @param {PipelineGraph} graph The pipeline graph to search within.
44
+ * @param {string} from The ID of the source node.
45
+ * @param {string} to The ID of the target node.
46
+ * @returns {PipelineGraph["edges"]} An array of edges from the source node to the target node.
47
+ */
48
+ declare function findEdges(graph: PipelineGraph, from: string, to: string): PipelineGraph["edges"];
49
+ //#endregion
50
+ export { type GraphBuilderOptions, PipelineGraphBuilder, buildRouteGraph, createPipelineGraphBuilder, find, findEdges, toVisualTree };
package/dist/index.mjs ADDED
@@ -0,0 +1,169 @@
1
+ //#region src/builder.ts
2
+ var PipelineGraphBuilder = class {
3
+ #nodes = /* @__PURE__ */ new Map();
4
+ #edges = [];
5
+ #edgeIndex = /* @__PURE__ */ new Set();
6
+ addSourceNode(version) {
7
+ const id = `source:${version}`;
8
+ if (!this.#nodes.has(id)) this.#nodes.set(id, {
9
+ id,
10
+ type: "source",
11
+ version
12
+ });
13
+ return id;
14
+ }
15
+ addFileNode(file) {
16
+ const id = `file:${file.version}:${file.path}`;
17
+ if (!this.#nodes.has(id)) this.#nodes.set(id, {
18
+ id,
19
+ type: "file",
20
+ file
21
+ });
22
+ return id;
23
+ }
24
+ addRouteNode(routeId, version) {
25
+ const id = `route:${version}:${routeId}`;
26
+ if (!this.#nodes.has(id)) this.#nodes.set(id, {
27
+ id,
28
+ type: "route",
29
+ routeId
30
+ });
31
+ return id;
32
+ }
33
+ addArtifactNode(artifactId, version) {
34
+ const id = `artifact:${version}:${artifactId}`;
35
+ if (!this.#nodes.has(id)) this.#nodes.set(id, {
36
+ id,
37
+ type: "artifact",
38
+ artifactId
39
+ });
40
+ return id;
41
+ }
42
+ addOutputNode(outputIndex, version, property) {
43
+ const id = `output:${version}:${outputIndex}`;
44
+ if (!this.#nodes.has(id)) this.#nodes.set(id, {
45
+ id,
46
+ type: "output",
47
+ outputIndex,
48
+ property
49
+ });
50
+ return id;
51
+ }
52
+ addEdge(from, to, type) {
53
+ const key = `${from}|${to}|${type}`;
54
+ if (this.#edgeIndex.has(key)) return;
55
+ this.#edgeIndex.add(key);
56
+ this.#edges.push({
57
+ from,
58
+ to,
59
+ type
60
+ });
61
+ }
62
+ build() {
63
+ return {
64
+ nodes: Array.from(this.#nodes.values()),
65
+ edges: [...this.#edges]
66
+ };
67
+ }
68
+ clear() {
69
+ this.#nodes.clear();
70
+ this.#edges.length = 0;
71
+ this.#edgeIndex.clear();
72
+ }
73
+ };
74
+ function createPipelineGraphBuilder() {
75
+ return new PipelineGraphBuilder();
76
+ }
77
+
78
+ //#endregion
79
+ //#region src/graph-utils.ts
80
+ /**
81
+ * Build a pipeline graph from a pipeline definition and its DAG.
82
+ * @param {PipelineDefinition} pipeline The pipeline definition to build the graph from.
83
+ * @param {DAG} dag The DAG representing the dependencies between routes and artifacts in the pipeline.
84
+ * @returns {PipelineGraph} The constructed pipeline graph.
85
+ */
86
+ function buildRouteGraph(pipeline, dag) {
87
+ const builder = new PipelineGraphBuilder();
88
+ for (const route of pipeline.routes) {
89
+ const routeNode = dag.nodes.get(route.id);
90
+ if (!routeNode) continue;
91
+ const currentRouteId = builder.addRouteNode(route.id, "static");
92
+ for (const depId of routeNode.dependencies) {
93
+ const depNodeId = builder.addRouteNode(depId, "static");
94
+ builder.addEdge(depNodeId, currentRouteId, "provides");
95
+ }
96
+ for (const artifactId of routeNode.emittedArtifacts) {
97
+ const artifactNodeId = builder.addArtifactNode(artifactId, "static");
98
+ builder.addEdge(currentRouteId, artifactNodeId, "resolved");
99
+ }
100
+ }
101
+ return builder.build();
102
+ }
103
+ const END_LINE = "└─ ";
104
+ const MID_LINE = "├─ ";
105
+ const VERTICAL_LINE = "│ ";
106
+ const EMPTY_SPACE = " ";
107
+ /**
108
+ * Convert a pipeline graph into a visual tree representation.
109
+ * @param {PipelineGraph} graph The pipeline graph to convert.
110
+ * @returns {string} A visual tree representation of the graph.
111
+ */
112
+ function toVisualTree(graph) {
113
+ const adjacencyList = /* @__PURE__ */ new Map();
114
+ const incomingCount = /* @__PURE__ */ new Map();
115
+ graph.nodes.forEach((node) => {
116
+ incomingCount.set(node.id, 0);
117
+ });
118
+ for (const edge of graph.edges) {
119
+ if (!adjacencyList.has(edge.from)) adjacencyList.set(edge.from, []);
120
+ adjacencyList.get(edge.from).push(edge.to);
121
+ incomingCount.set(edge.to, (incomingCount.get(edge.to) ?? 0) + 1);
122
+ if (!incomingCount.has(edge.from)) incomingCount.set(edge.from, 0);
123
+ }
124
+ const nodeIds = graph.nodes.map((node) => node.id);
125
+ const rootIds = nodeIds.filter((id) => (incomingCount.get(id) ?? 0) === 0);
126
+ const orderedRoots = rootIds.length > 0 ? rootIds : nodeIds;
127
+ const lines = [];
128
+ const visited = /* @__PURE__ */ new Set();
129
+ function buildTree(nodeId, prefix, isLast) {
130
+ if (visited.has(nodeId)) return;
131
+ visited.add(nodeId);
132
+ lines.push(`${prefix}${isLast ? END_LINE : MID_LINE}${nodeId}`);
133
+ const children = adjacencyList.get(nodeId) ?? [];
134
+ const nextPrefix = prefix + (isLast ? EMPTY_SPACE : VERTICAL_LINE);
135
+ children.forEach((childId, index) => {
136
+ buildTree(childId, nextPrefix, index === children.length - 1);
137
+ });
138
+ }
139
+ orderedRoots.forEach((rootId, index) => {
140
+ buildTree(rootId, "", index === orderedRoots.length - 1);
141
+ });
142
+ const remaining = nodeIds.filter((id) => !visited.has(id));
143
+ remaining.forEach((nodeId, index) => {
144
+ buildTree(nodeId, "", index === remaining.length - 1);
145
+ });
146
+ return lines.join("\n");
147
+ }
148
+ /**
149
+ * Find a node in the graph by its ID.
150
+ * @param {PipelineGraph} graph The pipeline graph to search within.
151
+ * @param {string} nodeId The ID of the node to find.
152
+ * @returns {PipelineGraph["nodes"][number] | undefined} The node with the specified ID, or undefined if not found.
153
+ */
154
+ function find(graph, nodeId) {
155
+ return graph.nodes.find((node) => node.id === nodeId);
156
+ }
157
+ /**
158
+ * Find edges between two nodes in the graph.
159
+ * @param {PipelineGraph} graph The pipeline graph to search within.
160
+ * @param {string} from The ID of the source node.
161
+ * @param {string} to The ID of the target node.
162
+ * @returns {PipelineGraph["edges"]} An array of edges from the source node to the target node.
163
+ */
164
+ function findEdges(graph, from, to) {
165
+ return graph.edges.filter((edge) => edge.from === from && edge.to === to);
166
+ }
167
+
168
+ //#endregion
169
+ export { PipelineGraphBuilder, buildRouteGraph, createPipelineGraphBuilder, find, findEdges, toVisualTree };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@ucdjs/pipelines-graph",
3
+ "version": "0.0.1-beta.1",
4
+ "type": "module",
5
+ "author": {
6
+ "name": "Lucas Nørgård",
7
+ "email": "lucasnrgaard@gmail.com",
8
+ "url": "https://luxass.dev"
9
+ },
10
+ "license": "MIT",
11
+ "homepage": "https://github.com/ucdjs/ucd",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/ucdjs/ucd.git",
15
+ "directory": "packages/pipelines/pipeline-graph"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/ucdjs/ucd/issues"
19
+ },
20
+ "exports": {
21
+ ".": "./dist/index.mjs",
22
+ "./package.json": "./package.json"
23
+ },
24
+ "types": "./dist/index.d.mts",
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "engines": {
29
+ "node": ">=22.18"
30
+ },
31
+ "dependencies": {
32
+ "@ucdjs/pipelines-core": "0.0.1-beta.1"
33
+ },
34
+ "devDependencies": {
35
+ "@luxass/eslint-config": "7.2.0",
36
+ "eslint": "10.0.0",
37
+ "publint": "0.3.17",
38
+ "tsdown": "0.20.3",
39
+ "typescript": "5.9.3",
40
+ "@ucdjs-tooling/tsdown-config": "1.0.0",
41
+ "@ucdjs-tooling/tsconfig": "1.0.0"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "scripts": {
47
+ "build": "tsdown --tsconfig=./tsconfig.build.json",
48
+ "dev": "tsdown --watch",
49
+ "clean": "git clean -xdf dist node_modules",
50
+ "lint": "eslint .",
51
+ "typecheck": "tsc --noEmit -p tsconfig.build.json"
52
+ }
53
+ }