@fuzdev/fuz_util 0.48.3 → 0.49.0
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/dist/async.d.ts +11 -0
- package/dist/async.d.ts.map +1 -1
- package/dist/async.js +30 -0
- package/dist/dag.d.ts +80 -0
- package/dist/dag.d.ts.map +1 -0
- package/dist/dag.js +156 -0
- package/dist/diff.d.ts +61 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +185 -0
- package/dist/path.d.ts +11 -0
- package/dist/path.d.ts.map +1 -1
- package/dist/path.js +15 -0
- package/dist/sort.d.ts +38 -0
- package/dist/sort.d.ts.map +1 -0
- package/dist/sort.js +123 -0
- package/dist/source_json.d.ts +4 -4
- package/dist/string.d.ts +16 -0
- package/dist/string.d.ts.map +1 -1
- package/dist/string.js +33 -0
- package/dist/svelte_preprocess_helpers.d.ts +134 -0
- package/dist/svelte_preprocess_helpers.d.ts.map +1 -0
- package/dist/svelte_preprocess_helpers.js +243 -0
- package/package.json +15 -6
- package/src/lib/async.ts +33 -0
- package/src/lib/dag.ts +240 -0
- package/src/lib/diff.ts +234 -0
- package/src/lib/path.ts +20 -0
- package/src/lib/sort.ts +160 -0
- package/src/lib/string.ts +36 -0
- package/src/lib/svelte_preprocess_helpers.ts +270 -0
package/src/lib/async.ts
CHANGED
|
@@ -249,3 +249,36 @@ export const map_concurrent_settled = async <T, R>(
|
|
|
249
249
|
run_next();
|
|
250
250
|
});
|
|
251
251
|
};
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Async semaphore for concurrency limiting.
|
|
255
|
+
*
|
|
256
|
+
* With `Infinity` permits, `acquire()` always resolves immediately.
|
|
257
|
+
*/
|
|
258
|
+
export class AsyncSemaphore {
|
|
259
|
+
#permits: number;
|
|
260
|
+
#waiters: Array<() => void> = [];
|
|
261
|
+
|
|
262
|
+
constructor(permits: number) {
|
|
263
|
+
this.#permits = permits;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async acquire(): Promise<void> {
|
|
267
|
+
if (this.#permits > 0) {
|
|
268
|
+
this.#permits--;
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
return new Promise<void>((resolve) => {
|
|
272
|
+
this.#waiters.push(resolve);
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
release(): void {
|
|
277
|
+
const next = this.#waiters.shift();
|
|
278
|
+
if (next) {
|
|
279
|
+
next();
|
|
280
|
+
} else {
|
|
281
|
+
this.#permits++;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
package/src/lib/dag.ts
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic concurrent DAG executor.
|
|
3
|
+
*
|
|
4
|
+
* Executes nodes in a dependency graph with configurable concurrency,
|
|
5
|
+
* failure handling, and skip semantics. Nodes without dependency edges
|
|
6
|
+
* run in parallel (up to max_concurrency). Dependencies are respected
|
|
7
|
+
* via per-node deferreds.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {AsyncSemaphore, create_deferred, type Deferred} from './async.js';
|
|
13
|
+
import {topological_sort, type Sortable} from './sort.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Minimum shape for a DAG node.
|
|
17
|
+
*/
|
|
18
|
+
export interface DagNode extends Sortable {
|
|
19
|
+
id: string;
|
|
20
|
+
depends_on?: Array<string>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Options for running a DAG.
|
|
25
|
+
*/
|
|
26
|
+
export interface DagOptions<T extends DagNode> {
|
|
27
|
+
/** Nodes to execute. */
|
|
28
|
+
nodes: Array<T>;
|
|
29
|
+
/** Execute a node. Throw on failure. */
|
|
30
|
+
execute: (node: T) => Promise<void>;
|
|
31
|
+
/** Called after a node fails. For observability — the error is already recorded. */
|
|
32
|
+
on_error?: (node: T, error: Error) => Promise<void>;
|
|
33
|
+
/** Called when a node is skipped (pre-skip or dependency failure). */
|
|
34
|
+
on_skip?: (node: T, reason: string) => Promise<void>;
|
|
35
|
+
/** Return true to skip a node without executing. Dependents still proceed. */
|
|
36
|
+
should_skip?: (node: T) => boolean;
|
|
37
|
+
/** Maximum concurrent executions. Default: Infinity. */
|
|
38
|
+
max_concurrency?: number;
|
|
39
|
+
/** Stop starting new nodes on first failure. Default: true. */
|
|
40
|
+
stop_on_failure?: boolean;
|
|
41
|
+
/** Skip internal graph validation (caller already validated). */
|
|
42
|
+
skip_validation?: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Result for a single node.
|
|
47
|
+
*/
|
|
48
|
+
export interface DagNodeResult {
|
|
49
|
+
id: string;
|
|
50
|
+
status: 'completed' | 'failed' | 'skipped';
|
|
51
|
+
error?: string;
|
|
52
|
+
duration_ms: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Result of a DAG execution.
|
|
57
|
+
*/
|
|
58
|
+
export interface DagResult {
|
|
59
|
+
/** Whether all executed nodes succeeded. */
|
|
60
|
+
success: boolean;
|
|
61
|
+
/** Per-node results. */
|
|
62
|
+
results: Map<string, DagNodeResult>;
|
|
63
|
+
/** Number of nodes that completed successfully. */
|
|
64
|
+
completed: number;
|
|
65
|
+
/** Number of nodes that failed. */
|
|
66
|
+
failed: number;
|
|
67
|
+
/** Number of nodes that were skipped. */
|
|
68
|
+
skipped: number;
|
|
69
|
+
/** Total execution time in milliseconds. */
|
|
70
|
+
duration_ms: number;
|
|
71
|
+
/** Error message if any nodes failed. */
|
|
72
|
+
error?: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Execute nodes in a dependency graph concurrently.
|
|
77
|
+
*
|
|
78
|
+
* Independent nodes (no unmet dependencies) run in parallel up to
|
|
79
|
+
* `max_concurrency`. When a node completes, its dependents become
|
|
80
|
+
* eligible to start. Failure cascading and stop-on-failure are handled
|
|
81
|
+
* per the options.
|
|
82
|
+
*
|
|
83
|
+
* @param options - DAG execution options.
|
|
84
|
+
* @returns Aggregated result with per-node details.
|
|
85
|
+
*/
|
|
86
|
+
export const run_dag = async <T extends DagNode>(options: DagOptions<T>): Promise<DagResult> => {
|
|
87
|
+
const {
|
|
88
|
+
nodes,
|
|
89
|
+
execute,
|
|
90
|
+
on_error,
|
|
91
|
+
on_skip,
|
|
92
|
+
should_skip,
|
|
93
|
+
max_concurrency = Infinity,
|
|
94
|
+
stop_on_failure = true,
|
|
95
|
+
skip_validation = false,
|
|
96
|
+
} = options;
|
|
97
|
+
|
|
98
|
+
const start_time = Date.now();
|
|
99
|
+
|
|
100
|
+
// Empty graph
|
|
101
|
+
if (nodes.length === 0) {
|
|
102
|
+
return {
|
|
103
|
+
success: true,
|
|
104
|
+
results: new Map(),
|
|
105
|
+
completed: 0,
|
|
106
|
+
failed: 0,
|
|
107
|
+
skipped: 0,
|
|
108
|
+
duration_ms: 0,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Validate graph (cycle detection, duplicate IDs, missing deps)
|
|
113
|
+
if (!skip_validation) {
|
|
114
|
+
const sort_result = topological_sort(nodes, 'node');
|
|
115
|
+
if (!sort_result.ok) {
|
|
116
|
+
return {
|
|
117
|
+
success: false,
|
|
118
|
+
results: new Map(),
|
|
119
|
+
completed: 0,
|
|
120
|
+
failed: 0,
|
|
121
|
+
skipped: 0,
|
|
122
|
+
duration_ms: Date.now() - start_time,
|
|
123
|
+
error: sort_result.error,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Build deferreds and tracking maps
|
|
129
|
+
const deferreds: Map<string, Deferred<void>> = new Map();
|
|
130
|
+
const outcomes: Map<string, 'ok' | 'fail'> = new Map();
|
|
131
|
+
const results: Map<string, DagNodeResult> = new Map();
|
|
132
|
+
|
|
133
|
+
for (const node of nodes) {
|
|
134
|
+
deferreds.set(node.id, create_deferred());
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let stopping = false;
|
|
138
|
+
const semaphore = new AsyncSemaphore(max_concurrency);
|
|
139
|
+
|
|
140
|
+
// Skip a node, record outcome, notify dependents
|
|
141
|
+
const skip_node = async (node: T, outcome: 'ok' | 'fail', reason: string): Promise<void> => {
|
|
142
|
+
outcomes.set(node.id, outcome);
|
|
143
|
+
results.set(node.id, {id: node.id, status: 'skipped', duration_ms: 0});
|
|
144
|
+
if (on_skip) await on_skip(node, reason);
|
|
145
|
+
deferreds.get(node.id)!.resolve();
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// Per-node async task
|
|
149
|
+
const run_node = async (node: T): Promise<void> => {
|
|
150
|
+
const deps = node.depends_on ?? [];
|
|
151
|
+
|
|
152
|
+
// Wait for all dependencies to resolve
|
|
153
|
+
if (deps.length > 0) {
|
|
154
|
+
await Promise.all(deps.map((d) => deferreds.get(d)!.promise));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Pre-skip check (e.g., pipeline step.skip or change.action === 'skip')
|
|
158
|
+
if (should_skip?.(node)) {
|
|
159
|
+
return skip_node(node, 'ok', 'pre-skipped');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Check if any dependency failed — skip this node too
|
|
163
|
+
if (deps.some((d) => outcomes.get(d) === 'fail')) {
|
|
164
|
+
return skip_node(node, 'fail', 'dependency failed');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Check if we're stopping (some other node failed with stop_on_failure)
|
|
168
|
+
if (stopping) {
|
|
169
|
+
return skip_node(node, 'fail', 'stopped');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Acquire concurrency slot
|
|
173
|
+
await semaphore.acquire();
|
|
174
|
+
|
|
175
|
+
// Double-check stopping after acquiring slot
|
|
176
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
177
|
+
if (stopping) {
|
|
178
|
+
semaphore.release();
|
|
179
|
+
return skip_node(node, 'fail', 'stopped');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Execute
|
|
183
|
+
const exec_start = Date.now();
|
|
184
|
+
try {
|
|
185
|
+
await execute(node);
|
|
186
|
+
outcomes.set(node.id, 'ok');
|
|
187
|
+
results.set(node.id, {
|
|
188
|
+
id: node.id,
|
|
189
|
+
status: 'completed',
|
|
190
|
+
duration_ms: Date.now() - exec_start,
|
|
191
|
+
});
|
|
192
|
+
} catch (err) {
|
|
193
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
194
|
+
outcomes.set(node.id, 'fail');
|
|
195
|
+
results.set(node.id, {
|
|
196
|
+
id: node.id,
|
|
197
|
+
status: 'failed',
|
|
198
|
+
error: error.message,
|
|
199
|
+
duration_ms: Date.now() - exec_start,
|
|
200
|
+
});
|
|
201
|
+
if (stop_on_failure) stopping = true;
|
|
202
|
+
if (on_error) await on_error(node, error);
|
|
203
|
+
} finally {
|
|
204
|
+
semaphore.release();
|
|
205
|
+
deferreds.get(node.id)!.resolve();
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// Launch all nodes — they naturally wait for their deps via deferreds
|
|
210
|
+
await Promise.all(nodes.map(run_node));
|
|
211
|
+
|
|
212
|
+
// Aggregate results
|
|
213
|
+
let completed = 0;
|
|
214
|
+
let failed = 0;
|
|
215
|
+
let skipped = 0;
|
|
216
|
+
for (const result of results.values()) {
|
|
217
|
+
switch (result.status) {
|
|
218
|
+
case 'completed':
|
|
219
|
+
completed++;
|
|
220
|
+
break;
|
|
221
|
+
case 'failed':
|
|
222
|
+
failed++;
|
|
223
|
+
break;
|
|
224
|
+
case 'skipped':
|
|
225
|
+
skipped++;
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const success = failed === 0;
|
|
231
|
+
return {
|
|
232
|
+
success,
|
|
233
|
+
results,
|
|
234
|
+
completed,
|
|
235
|
+
failed,
|
|
236
|
+
skipped,
|
|
237
|
+
duration_ms: Date.now() - start_time,
|
|
238
|
+
error: success ? undefined : `${failed} node(s) failed`,
|
|
239
|
+
};
|
|
240
|
+
};
|
package/src/lib/diff.ts
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Line-based diff utilities using LCS (Longest Common Subsequence).
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {string_is_binary} from './string.js';
|
|
8
|
+
|
|
9
|
+
/** Line diff result */
|
|
10
|
+
export interface DiffLine {
|
|
11
|
+
type: 'same' | 'add' | 'remove';
|
|
12
|
+
line: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Generate a line-based diff between two strings using LCS algorithm.
|
|
17
|
+
*
|
|
18
|
+
* @param a - The original/current content.
|
|
19
|
+
* @param b - The new/desired content.
|
|
20
|
+
* @returns Array of diff lines with type annotations.
|
|
21
|
+
*/
|
|
22
|
+
export const diff_lines = (a: string, b: string): Array<DiffLine> => {
|
|
23
|
+
const a_lines = a.split('\n');
|
|
24
|
+
const b_lines = b.split('\n');
|
|
25
|
+
const result: Array<DiffLine> = [];
|
|
26
|
+
|
|
27
|
+
const lcs = compute_lcs(a_lines, b_lines);
|
|
28
|
+
let ai = 0;
|
|
29
|
+
let bi = 0;
|
|
30
|
+
let li = 0;
|
|
31
|
+
|
|
32
|
+
while (ai < a_lines.length || bi < b_lines.length) {
|
|
33
|
+
if (li < lcs.length && ai < a_lines.length && a_lines[ai] === lcs[li]) {
|
|
34
|
+
if (bi < b_lines.length && b_lines[bi] === lcs[li]) {
|
|
35
|
+
result.push({type: 'same', line: a_lines[ai]!});
|
|
36
|
+
ai++;
|
|
37
|
+
bi++;
|
|
38
|
+
li++;
|
|
39
|
+
} else {
|
|
40
|
+
result.push({type: 'add', line: b_lines[bi]!});
|
|
41
|
+
bi++;
|
|
42
|
+
}
|
|
43
|
+
} else if (ai < a_lines.length && (li >= lcs.length || a_lines[ai] !== lcs[li])) {
|
|
44
|
+
result.push({type: 'remove', line: a_lines[ai]!});
|
|
45
|
+
ai++;
|
|
46
|
+
} else if (bi < b_lines.length) {
|
|
47
|
+
result.push({type: 'add', line: b_lines[bi]!});
|
|
48
|
+
bi++;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return result;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Compute longest common subsequence of two string arrays.
|
|
57
|
+
*
|
|
58
|
+
* Uses dynamic programming with O(m*n) time and space complexity.
|
|
59
|
+
*/
|
|
60
|
+
const compute_lcs = (a: Array<string>, b: Array<string>): Array<string> => {
|
|
61
|
+
const m = a.length;
|
|
62
|
+
const n = b.length;
|
|
63
|
+
const dp: Array<Array<number>> = Array.from({length: m + 1}, () => Array(n + 1).fill(0));
|
|
64
|
+
|
|
65
|
+
for (let i = 1; i <= m; i++) {
|
|
66
|
+
for (let j = 1; j <= n; j++) {
|
|
67
|
+
if (a[i - 1] === b[j - 1]) {
|
|
68
|
+
dp[i]![j] = dp[i - 1]![j - 1]! + 1;
|
|
69
|
+
} else {
|
|
70
|
+
dp[i]![j] = Math.max(dp[i - 1]![j]!, dp[i]![j - 1]!);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Backtrack to find LCS
|
|
76
|
+
const lcs: Array<string> = [];
|
|
77
|
+
let i = m;
|
|
78
|
+
let j = n;
|
|
79
|
+
while (i > 0 && j > 0) {
|
|
80
|
+
if (a[i - 1] === b[j - 1]) {
|
|
81
|
+
lcs.unshift(a[i - 1]!);
|
|
82
|
+
i--;
|
|
83
|
+
j--;
|
|
84
|
+
} else if (dp[i - 1]![j]! > dp[i]![j - 1]!) {
|
|
85
|
+
i--;
|
|
86
|
+
} else {
|
|
87
|
+
j--;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return lcs;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Filter diff to only include lines within N lines of context around changes.
|
|
96
|
+
*
|
|
97
|
+
* @param diff - The full diff lines.
|
|
98
|
+
* @param context_lines - Number of context lines to show around changes (default: 3).
|
|
99
|
+
* @returns Filtered diff with ellipsis markers for skipped regions.
|
|
100
|
+
*/
|
|
101
|
+
export const filter_diff_context = (diff: Array<DiffLine>, context_lines = 3): Array<DiffLine> => {
|
|
102
|
+
if (diff.length === 0) return [];
|
|
103
|
+
|
|
104
|
+
// Find indices of all changed lines
|
|
105
|
+
const changed_indices: Array<number> = [];
|
|
106
|
+
for (let i = 0; i < diff.length; i++) {
|
|
107
|
+
if (diff[i]!.type !== 'same') {
|
|
108
|
+
changed_indices.push(i);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (changed_indices.length === 0) return [];
|
|
113
|
+
|
|
114
|
+
// Build set of indices to include (changed lines + context)
|
|
115
|
+
const include_indices: Set<number> = new Set();
|
|
116
|
+
for (const idx of changed_indices) {
|
|
117
|
+
for (
|
|
118
|
+
let i = Math.max(0, idx - context_lines);
|
|
119
|
+
i <= Math.min(diff.length - 1, idx + context_lines);
|
|
120
|
+
i++
|
|
121
|
+
) {
|
|
122
|
+
include_indices.add(i);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Build result with ellipsis markers for gaps
|
|
127
|
+
const result: Array<DiffLine> = [];
|
|
128
|
+
let last_included = -1;
|
|
129
|
+
|
|
130
|
+
for (let i = 0; i < diff.length; i++) {
|
|
131
|
+
if (include_indices.has(i)) {
|
|
132
|
+
// Add ellipsis if there's a gap
|
|
133
|
+
if (last_included >= 0 && i > last_included + 1) {
|
|
134
|
+
result.push({type: 'same', line: '...'});
|
|
135
|
+
}
|
|
136
|
+
result.push(diff[i]!);
|
|
137
|
+
last_included = i;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return result;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/** ANSI color codes */
|
|
145
|
+
const colors = {
|
|
146
|
+
red: '\x1b[31m',
|
|
147
|
+
green: '\x1b[32m',
|
|
148
|
+
reset: '\x1b[0m',
|
|
149
|
+
} as const;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Format options for diff output.
|
|
153
|
+
*/
|
|
154
|
+
export interface FormatDiffOptions {
|
|
155
|
+
/** Prefix for each line (for indentation in plan output). */
|
|
156
|
+
prefix?: string;
|
|
157
|
+
/** Whether to use ANSI colors. */
|
|
158
|
+
use_color?: boolean;
|
|
159
|
+
/** Maximum number of diff lines to show (0 = unlimited). */
|
|
160
|
+
max_lines?: number;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Format a diff for display.
|
|
165
|
+
*
|
|
166
|
+
* @param diff - The diff lines to format.
|
|
167
|
+
* @param current_path - Path label for "current" content.
|
|
168
|
+
* @param desired_path - Path label for "desired" content.
|
|
169
|
+
* @param options - Formatting options.
|
|
170
|
+
* @returns Formatted diff string.
|
|
171
|
+
*/
|
|
172
|
+
export const format_diff = (
|
|
173
|
+
diff: Array<DiffLine>,
|
|
174
|
+
current_path: string,
|
|
175
|
+
desired_path: string,
|
|
176
|
+
options: FormatDiffOptions = {},
|
|
177
|
+
): string => {
|
|
178
|
+
const {prefix = '', use_color = true, max_lines = 50} = options;
|
|
179
|
+
|
|
180
|
+
const lines: Array<string> = [
|
|
181
|
+
`${prefix}--- ${current_path} (current)`,
|
|
182
|
+
`${prefix}+++ ${desired_path} (desired)`,
|
|
183
|
+
];
|
|
184
|
+
|
|
185
|
+
let count = 0;
|
|
186
|
+
for (const d of diff) {
|
|
187
|
+
if (max_lines > 0 && count >= max_lines) {
|
|
188
|
+
const remaining = diff.length - count;
|
|
189
|
+
lines.push(`${prefix}... (${remaining} more lines)`);
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const line_prefix = d.type === 'add' ? '+' : d.type === 'remove' ? '-' : ' ';
|
|
194
|
+
|
|
195
|
+
if (use_color && d.type !== 'same') {
|
|
196
|
+
const color = d.type === 'add' ? colors.green : colors.red;
|
|
197
|
+
lines.push(`${prefix}${color}${line_prefix}${d.line}${colors.reset}`);
|
|
198
|
+
} else {
|
|
199
|
+
lines.push(`${prefix}${line_prefix}${d.line}`);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
count++;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return lines.join('\n');
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Generate a formatted diff between two strings.
|
|
210
|
+
*
|
|
211
|
+
* Combines diff_lines, filter_diff_context, and format_diff for convenience.
|
|
212
|
+
* Returns null if content is binary.
|
|
213
|
+
*
|
|
214
|
+
* @param current - Current content.
|
|
215
|
+
* @param desired - Desired content.
|
|
216
|
+
* @param path - File path for labels.
|
|
217
|
+
* @param options - Formatting options.
|
|
218
|
+
* @returns Formatted diff string, or null if binary.
|
|
219
|
+
*/
|
|
220
|
+
export const generate_diff = (
|
|
221
|
+
current: string,
|
|
222
|
+
desired: string,
|
|
223
|
+
path: string,
|
|
224
|
+
options: FormatDiffOptions = {},
|
|
225
|
+
): string | null => {
|
|
226
|
+
// Skip binary files
|
|
227
|
+
if (string_is_binary(current) || string_is_binary(desired)) {
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const diff = diff_lines(current, desired);
|
|
232
|
+
const filtered = filter_diff_context(diff);
|
|
233
|
+
return format_diff(filtered, path, path, options);
|
|
234
|
+
};
|
package/src/lib/path.ts
CHANGED
|
@@ -94,6 +94,26 @@ export const parse_path_pieces = (raw_path: string): Array<PathPiece> => {
|
|
|
94
94
|
return pieces;
|
|
95
95
|
};
|
|
96
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Checks if a filename matches any exclusion pattern.
|
|
99
|
+
*
|
|
100
|
+
* Returns `false` when `filename` is `undefined`, empty string, or `exclude` is empty.
|
|
101
|
+
* String patterns use substring matching. RegExp patterns use `.test()`.
|
|
102
|
+
*
|
|
103
|
+
* @param filename The file path to check, or `undefined` for virtual files.
|
|
104
|
+
* @param exclude Array of string or RegExp exclusion patterns.
|
|
105
|
+
* @returns `true` if the file should be excluded from processing.
|
|
106
|
+
*/
|
|
107
|
+
export const should_exclude_path = (
|
|
108
|
+
filename: string | undefined,
|
|
109
|
+
exclude: Array<string | RegExp>,
|
|
110
|
+
): boolean => {
|
|
111
|
+
if (!filename || exclude.length === 0) return false;
|
|
112
|
+
return exclude.some((pattern) =>
|
|
113
|
+
typeof pattern === 'string' ? filename.includes(pattern) : pattern.test(filename),
|
|
114
|
+
);
|
|
115
|
+
};
|
|
116
|
+
|
|
97
117
|
/**
|
|
98
118
|
* Converts a string into a URL-compatible slug.
|
|
99
119
|
* @param str the string to convert
|
package/src/lib/sort.ts
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic topological sort using Kahn's algorithm.
|
|
3
|
+
*
|
|
4
|
+
* Orders items so that dependencies come before dependents.
|
|
5
|
+
* Works with any item type that has `id` and optional `depends_on`.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Minimum shape required for topological sorting.
|
|
12
|
+
*/
|
|
13
|
+
export interface Sortable {
|
|
14
|
+
id: string;
|
|
15
|
+
depends_on?: Array<string>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Result of topological sort.
|
|
20
|
+
*/
|
|
21
|
+
export type TopologicalSortResult<T extends Sortable> =
|
|
22
|
+
| {ok: true; sorted: Array<T>}
|
|
23
|
+
| {ok: false; error: string; cycle?: Array<string>};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Sort items by their dependencies using Kahn's algorithm.
|
|
27
|
+
*
|
|
28
|
+
* Returns items ordered so that dependencies come before dependents.
|
|
29
|
+
* If a cycle is detected, returns an error with the cycle path.
|
|
30
|
+
*
|
|
31
|
+
* @param items - Array of items to sort.
|
|
32
|
+
* @param label - Label for error messages (e.g. "resource", "step").
|
|
33
|
+
* @returns Sorted items or error if cycle detected.
|
|
34
|
+
*/
|
|
35
|
+
export const topological_sort = <T extends Sortable>(
|
|
36
|
+
items: Array<T>,
|
|
37
|
+
label: string = 'item',
|
|
38
|
+
): TopologicalSortResult<T> => {
|
|
39
|
+
// Build id -> item map
|
|
40
|
+
const item_map: Map<string, T> = new Map();
|
|
41
|
+
for (const item of items) {
|
|
42
|
+
if (item_map.has(item.id)) {
|
|
43
|
+
return {ok: false, error: `duplicate ${label} id: ${item.id}`};
|
|
44
|
+
}
|
|
45
|
+
item_map.set(item.id, item);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Validate all dependencies exist and count dependents per item
|
|
49
|
+
// dependents_count[X] = how many items list X in their depends_on
|
|
50
|
+
const dependents_count: Map<string, number> = new Map();
|
|
51
|
+
for (const item of items) {
|
|
52
|
+
dependents_count.set(item.id, 0);
|
|
53
|
+
}
|
|
54
|
+
for (const item of items) {
|
|
55
|
+
const deps = item.depends_on ?? [];
|
|
56
|
+
for (const dep of deps) {
|
|
57
|
+
if (!item_map.has(dep)) {
|
|
58
|
+
return {ok: false, error: `${label} "${item.id}" depends on unknown ${label} "${dep}"`};
|
|
59
|
+
}
|
|
60
|
+
dependents_count.set(dep, dependents_count.get(dep)! + 1);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Start from leaf items (nothing depends on them), work toward roots
|
|
65
|
+
const queue: Array<string> = [];
|
|
66
|
+
for (const [id, count] of dependents_count) {
|
|
67
|
+
if (count === 0) {
|
|
68
|
+
queue.push(id);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Process leaves first, then items whose dependents are all processed
|
|
73
|
+
const sorted: Array<T> = [];
|
|
74
|
+
const visited: Set<string> = new Set();
|
|
75
|
+
|
|
76
|
+
while (queue.length > 0) {
|
|
77
|
+
const id = queue.shift()!;
|
|
78
|
+
if (visited.has(id)) continue;
|
|
79
|
+
visited.add(id);
|
|
80
|
+
|
|
81
|
+
sorted.push(item_map.get(id)!);
|
|
82
|
+
|
|
83
|
+
// This item is processed — decrement its dependencies' dependent counts
|
|
84
|
+
const deps = item_map.get(id)!.depends_on ?? [];
|
|
85
|
+
for (const dep of deps) {
|
|
86
|
+
const new_count = dependents_count.get(dep)! - 1;
|
|
87
|
+
dependents_count.set(dep, new_count);
|
|
88
|
+
if (new_count === 0) {
|
|
89
|
+
queue.push(dep);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Check for cycle
|
|
95
|
+
if (sorted.length !== items.length) {
|
|
96
|
+
const unvisited = items.filter((item) => !visited.has(item.id)).map((item) => item.id);
|
|
97
|
+
const cycle = find_cycle(item_map, unvisited);
|
|
98
|
+
return {
|
|
99
|
+
ok: false,
|
|
100
|
+
error: `dependency cycle detected: ${cycle.join(' -> ')}`,
|
|
101
|
+
cycle,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Reverse: leaves were processed first, but dependencies must come first in output
|
|
106
|
+
sorted.reverse();
|
|
107
|
+
|
|
108
|
+
return {ok: true, sorted};
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Find a cycle in the dependency graph starting from unvisited nodes.
|
|
113
|
+
* Used for error reporting when a cycle is detected.
|
|
114
|
+
*/
|
|
115
|
+
const find_cycle = <T extends Sortable>(
|
|
116
|
+
item_map: Map<string, T>,
|
|
117
|
+
unvisited: Array<string>,
|
|
118
|
+
): Array<string> => {
|
|
119
|
+
const unvisited_set = new Set(unvisited);
|
|
120
|
+
|
|
121
|
+
// DFS to find cycle
|
|
122
|
+
const path: Array<string> = [];
|
|
123
|
+
const in_path: Set<string> = new Set();
|
|
124
|
+
|
|
125
|
+
const dfs = (id: string): boolean => {
|
|
126
|
+
if (in_path.has(id)) {
|
|
127
|
+
path.push(id);
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
if (!unvisited_set.has(id)) return false;
|
|
131
|
+
|
|
132
|
+
in_path.add(id);
|
|
133
|
+
path.push(id);
|
|
134
|
+
|
|
135
|
+
const item = item_map.get(id);
|
|
136
|
+
const deps = item?.depends_on ?? [];
|
|
137
|
+
for (const dep of deps) {
|
|
138
|
+
if (dfs(dep)) return true;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
in_path.delete(id);
|
|
142
|
+
path.pop();
|
|
143
|
+
return false;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
if (unvisited.length > 0) {
|
|
147
|
+
dfs(unvisited[0]!);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Extract just the cycle portion
|
|
151
|
+
if (path.length > 0) {
|
|
152
|
+
const last = path[path.length - 1]!;
|
|
153
|
+
const cycle_start = path.indexOf(last);
|
|
154
|
+
if (cycle_start < path.length - 1) {
|
|
155
|
+
return path.slice(cycle_start);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return unvisited;
|
|
160
|
+
};
|