ts-repo-utils 3.0.1 → 3.1.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.
Files changed (43) hide show
  1. package/dist/functions/index.d.mts +1 -0
  2. package/dist/functions/index.d.mts.map +1 -1
  3. package/dist/functions/index.mjs +4 -0
  4. package/dist/functions/index.mjs.map +1 -1
  5. package/dist/functions/should-run.d.mts +14 -0
  6. package/dist/functions/should-run.d.mts.map +1 -1
  7. package/dist/functions/should-run.mjs +14 -0
  8. package/dist/functions/should-run.mjs.map +1 -1
  9. package/dist/functions/workspace-utils/execute-parallel.d.mts +25 -0
  10. package/dist/functions/workspace-utils/execute-parallel.d.mts.map +1 -0
  11. package/dist/functions/workspace-utils/execute-parallel.mjs +162 -0
  12. package/dist/functions/workspace-utils/execute-parallel.mjs.map +1 -0
  13. package/dist/functions/workspace-utils/get-workspace-packages.d.mts +11 -0
  14. package/dist/functions/workspace-utils/get-workspace-packages.d.mts.map +1 -0
  15. package/dist/functions/workspace-utils/get-workspace-packages.mjs +73 -0
  16. package/dist/functions/workspace-utils/get-workspace-packages.mjs.map +1 -0
  17. package/dist/functions/workspace-utils/index.d.mts +6 -0
  18. package/dist/functions/workspace-utils/index.d.mts.map +1 -0
  19. package/dist/functions/workspace-utils/index.mjs +5 -0
  20. package/dist/functions/workspace-utils/index.mjs.map +1 -0
  21. package/dist/functions/workspace-utils/run-cmd-in-parallel.d.mts +17 -0
  22. package/dist/functions/workspace-utils/run-cmd-in-parallel.d.mts.map +1 -0
  23. package/dist/functions/workspace-utils/run-cmd-in-parallel.mjs +30 -0
  24. package/dist/functions/workspace-utils/run-cmd-in-parallel.mjs.map +1 -0
  25. package/dist/functions/workspace-utils/run-cmd-in-stages.d.mts +19 -0
  26. package/dist/functions/workspace-utils/run-cmd-in-stages.d.mts.map +1 -0
  27. package/dist/functions/workspace-utils/run-cmd-in-stages.mjs +32 -0
  28. package/dist/functions/workspace-utils/run-cmd-in-stages.mjs.map +1 -0
  29. package/dist/functions/workspace-utils/types.d.mts +7 -0
  30. package/dist/functions/workspace-utils/types.d.mts.map +1 -0
  31. package/dist/functions/workspace-utils/types.mjs +2 -0
  32. package/dist/functions/workspace-utils/types.mjs.map +1 -0
  33. package/dist/index.mjs +4 -0
  34. package/dist/index.mjs.map +1 -1
  35. package/package.json +13 -14
  36. package/src/functions/index.mts +1 -0
  37. package/src/functions/should-run.mts +14 -0
  38. package/src/functions/workspace-utils/execute-parallel.mts +247 -0
  39. package/src/functions/workspace-utils/get-workspace-packages.mts +118 -0
  40. package/src/functions/workspace-utils/index.mts +5 -0
  41. package/src/functions/workspace-utils/run-cmd-in-parallel.mts +43 -0
  42. package/src/functions/workspace-utils/run-cmd-in-stages.mts +45 -0
  43. package/src/functions/workspace-utils/types.mts +7 -0
@@ -6,4 +6,5 @@ export * from './exec-async.mjs';
6
6
  export * from './format.mjs';
7
7
  export * from './gen-index.mjs';
8
8
  export * from './should-run.mjs';
9
+ export * from './workspace-utils/index.mjs';
9
10
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/functions/index.mts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,0BAA0B,CAAC;AACzC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/functions/index.mts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,0BAA0B,CAAC;AACzC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,6BAA6B,CAAC"}
@@ -6,4 +6,8 @@ export { $ } from './exec-async.mjs';
6
6
  export { formatDiffFrom, formatFiles, formatFilesList, formatUntracked } from './format.mjs';
7
7
  export { genIndex } from './gen-index.mjs';
8
8
  export { checkShouldRunTypeChecks } from './should-run.mjs';
9
+ export { executeParallel, executeStages } from './workspace-utils/execute-parallel.mjs';
10
+ export { getWorkspacePackages } from './workspace-utils/get-workspace-packages.mjs';
11
+ export { runCmdInParallelAcrossWorkspaces } from './workspace-utils/run-cmd-in-parallel.mjs';
12
+ export { runCmdInStagesAcrossWorkspaces } from './workspace-utils/run-cmd-in-stages.mjs';
9
13
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;"}
@@ -1,3 +1,17 @@
1
1
  import '../node-global.mjs';
2
+ /**
3
+ * Checks if TypeScript type checks should run based on the diff from origin/main.
4
+ * Skips type checks if all changed files are documentation files, spell check config,
5
+ * or other non-TypeScript files that don't affect type checking.
6
+ *
7
+ * Ignored file patterns:
8
+ * - '.cspell.json'
9
+ * - '**.md'
10
+ * - '**.txt'
11
+ * - 'docs/**'
12
+ *
13
+ * @returns A promise that resolves when the check is complete. Sets GITHUB_OUTPUT
14
+ * environment variable with should_run=true/false if running in GitHub Actions.
15
+ */
2
16
  export declare const checkShouldRunTypeChecks: () => Promise<void>;
3
17
  //# sourceMappingURL=should-run.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"should-run.d.mts","sourceRoot":"","sources":["../../src/functions/should-run.mts"],"names":[],"mappings":"AACA,OAAO,oBAAoB,CAAC;AAG5B,eAAO,MAAM,wBAAwB,QAAa,OAAO,CAAC,IAAI,CA4B7D,CAAC"}
1
+ {"version":3,"file":"should-run.d.mts","sourceRoot":"","sources":["../../src/functions/should-run.mts"],"names":[],"mappings":"AACA,OAAO,oBAAoB,CAAC;AAG5B;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,wBAAwB,QAAa,OAAO,CAAC,IAAI,CA4B7D,CAAC"}
@@ -2,6 +2,20 @@ import { Result, pipe } from 'ts-data-forge';
2
2
  import '../node-global.mjs';
3
3
  import { getDiffFrom } from './diff.mjs';
4
4
 
5
+ /**
6
+ * Checks if TypeScript type checks should run based on the diff from origin/main.
7
+ * Skips type checks if all changed files are documentation files, spell check config,
8
+ * or other non-TypeScript files that don't affect type checking.
9
+ *
10
+ * Ignored file patterns:
11
+ * - '.cspell.json'
12
+ * - '**.md'
13
+ * - '**.txt'
14
+ * - 'docs/**'
15
+ *
16
+ * @returns A promise that resolves when the check is complete. Sets GITHUB_OUTPUT
17
+ * environment variable with should_run=true/false if running in GitHub Actions.
18
+ */
5
19
  const checkShouldRunTypeChecks = async () => {
6
20
  // paths-ignore:
7
21
  // - '.cspell.json'
@@ -1 +1 @@
1
- {"version":3,"file":"should-run.mjs","sources":["../../src/functions/should-run.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAIO,MAAM,wBAAwB,GAAG,YAA0B;;;;;;IAOhE,MAAM,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC;AAElD,IAAA,MAAM,KAAK,GAAG,MAAM,WAAW,CAAC,aAAa,CAAC;AAE9C,IAAA,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE;QACvB,OAAO,CAAC,KAAK,CAAC,qBAAqB,EAAE,KAAK,CAAC,KAAK,CAAC;AACjD,QAAA,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;;AAGjB,IAAA,MAAM,iBAAiB,GAAY,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CACnD,CAAC,IAAI,KACH,IAAI,KAAK,cAAc;AACvB,QAAA,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC;AACxB,QAAA,IAAI,CAAC,IAAI,CAAC,Q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AAE,CAAA,WAAA,EAAc,iBAAiB,CAAA,EAAA,CAAI,CAAC;;AAE3E;;;;"}
@@ -0,0 +1,25 @@
1
+ import { Result } from 'ts-data-forge';
2
+ import '../../node-global.mjs';
3
+ import { type Package } from './types.mjs';
4
+ /**
5
+ * Executes a npm script across multiple packages in parallel with a concurrency limit.
6
+ * @param packages - Array of Package objects to execute the script in
7
+ * @param scriptName - The name of the npm script to execute
8
+ * @param concurrency - Maximum number of packages to process simultaneously (default: 3)
9
+ * @returns A promise that resolves to an array of execution results
10
+ */
11
+ export declare const executeParallel: (packages: readonly Package[], scriptName: string, concurrency?: number) => Promise<readonly Result<Readonly<{
12
+ code?: number;
13
+ skipped?: boolean;
14
+ }>, Error>[]>;
15
+ /**
16
+ * Executes a npm script across packages in dependency order stages.
17
+ * Packages are grouped into stages where each stage contains packages whose
18
+ * dependencies have been completed in previous stages.
19
+ * @param packages - Array of Package objects to execute the script in
20
+ * @param scriptName - The name of the npm script to execute
21
+ * @param concurrency - Maximum number of packages to process simultaneously within each stage (default: 3)
22
+ * @returns A promise that resolves when all stages are complete
23
+ */
24
+ export declare const executeStages: (packages: readonly Package[], scriptName: string, concurrency?: number) => Promise<void>;
25
+ //# sourceMappingURL=execute-parallel.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execute-parallel.d.mts","sourceRoot":"","sources":["../../../src/functions/workspace-utils/execute-parallel.mts"],"names":[],"mappings":"AACA,OAAO,EAML,MAAM,EACP,MAAM,eAAe,CAAC;AACvB,OAAO,uBAAuB,CAAC;AAC/B,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AAI3C;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,GAC1B,UAAU,SAAS,OAAO,EAAE,EAC5B,YAAY,MAAM,EAClB,cAAa,MAAU,KACtB,OAAO,CACR,SAAS,MAAM,CAAC,QAAQ,CAAC;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC,EAAE,KAAK,CAAC,EAAE,CAkCzE,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,aAAa,GACxB,UAAU,SAAS,OAAO,EAAE,EAC5B,YAAY,MAAM,EAClB,cAAa,MAAU,KACtB,OAAO,CAAC,IAAI,CAuCd,CAAC"}
@@ -0,0 +1,162 @@
1
+ import { spawn } from 'child_process';
2
+ import { pipe, createPromise, isRecord, hasKey, Result, isNotUndefined } from 'ts-data-forge';
3
+ import '../../node-global.mjs';
4
+
5
+ /**
6
+ * Executes a npm script across multiple packages in parallel with a concurrency limit.
7
+ * @param packages - Array of Package objects to execute the script in
8
+ * @param scriptName - The name of the npm script to execute
9
+ * @param concurrency - Maximum number of packages to process simultaneously (default: 3)
10
+ * @returns A promise that resolves to an array of execution results
11
+ */
12
+ const executeParallel = async (packages, scriptName, concurrency = 3) => {
13
+ const mut_resultPromises = [];
14
+ const executing = new Set();
15
+ for (const pkg of packages) {
16
+ const promise = executeScript(pkg, scriptName);
17
+ mut_resultPromises.push(promise);
18
+ const wrappedPromise = promise.finally(() => {
19
+ executing.delete(wrappedPromise);
20
+ });
21
+ executing.add(wrappedPromise);
22
+ // If we reach concurrency limit, wait for one to finish
23
+ if (executing.size >= concurrency) {
24
+ // eslint-disable-next-line no-await-in-loop
25
+ await Promise.race(executing);
26
+ }
27
+ }
28
+ return Promise.all(mut_resultPromises);
29
+ };
30
+ /**
31
+ * Executes a npm script across packages in dependency order stages.
32
+ * Packages are grouped into stages where each stage contains packages whose
33
+ * dependencies have been completed in previous stages.
34
+ * @param packages - Array of Package objects to execute the script in
35
+ * @param scriptName - The name of the npm script to execute
36
+ * @param concurrency - Maximum number of packages to process simultaneously within each stage (default: 3)
37
+ * @returns A promise that resolves when all stages are complete
38
+ */
39
+ const executeStages = async (packages, scriptName, concurrency = 3) => {
40
+ const dependencyGraph = buildDependencyGraph(packages);
41
+ const sorted = topologicalSortPackages(packages, dependencyGraph);
42
+ const stages = [];
43
+ const completed = new Set();
44
+ while (completed.size < sorted.length) {
45
+ const stage = [];
46
+ for (const pkg of sorted) {
47
+ if (completed.has(pkg.name))
48
+ continue;
49
+ const deps = dependencyGraph.get(pkg.name) ?? [];
50
+ const depsCompleted = deps.every((dep) => completed.has(dep));
51
+ if (depsCompleted) {
52
+ stage.push(pkg);
53
+ }
54
+ }
55
+ if (stage.length === 0) {
56
+ throw new Error('Circular dependency detected');
57
+ }
58
+ stages.push(stage);
59
+ for (const pkg of stage)
60
+ completed.add(pkg.name);
61
+ }
62
+ console.log(`\nExecuting ${scriptName} in ${stages.length} stages...\n`);
63
+ for (const [i, stage] of stages.entries()) {
64
+ if (stage.length > 0) {
65
+ console.log(`Stage ${i + 1}: ${stage.map((p) => p.name).join(', ')}`);
66
+ // eslint-disable-next-line no-await-in-loop
67
+ await executeParallel(stage, scriptName, concurrency);
68
+ }
69
+ }
70
+ };
71
+ /**
72
+ * Executes a npm script in a specific package directory.
73
+ * @param pkg - The package object containing path and metadata
74
+ * @param scriptName - The name of the npm script to execute
75
+ * @param options - Configuration options
76
+ * @param options.prefix - Whether to prefix output with package name (default: true)
77
+ * @returns A promise that resolves to execution result with exit code or skipped flag
78
+ */
79
+ const executeScript = (pkg, scriptName, { prefix = true } = {}) => pipe(createPromise((resolve, reject) => {
80
+ const packageJsonScripts = isRecord(pkg.packageJson) && isRecord(pkg.packageJson['scripts'])
81
+ ? pkg.packageJson['scripts']
82
+ : {};
83
+ const hasScript = hasKey(packageJsonScripts, scriptName);
84
+ if (!hasScript) {
85
+ resolve({ skipped: true });
86
+ return;
87
+ }
88
+ const prefixStr = prefix ? `[${pkg.name}] ` : '';
89
+ const proc = spawn('npm', ['run', scriptName], {
90
+ cwd: pkg.path,
91
+ shell: true,
92
+ stdio: 'pipe',
93
+ });
94
+ proc.stdout.on('data', (data) => {
95
+ process.stdout.write(prefixStr + data.toString());
96
+ });
97
+ proc.stderr.on('data', (data) => {
98
+ process.stderr.write(prefixStr + data.toString());
99
+ });
100
+ proc.on('close', (code) => {
101
+ if (code === 0) {
102
+ resolve({ code });
103
+ }
104
+ else {
105
+ reject(new Error(`${pkg.name} exited with code ${code}`));
106
+ }
107
+ });
108
+ proc.on('error', reject);
109
+ })).map((result) => result.then(Result.mapErr((err) => {
110
+ const errorMessage = err instanceof Error
111
+ ? err.message
112
+ : isRecord(err) && hasKey(err, 'message')
113
+ ? (err.message?.toString() ?? 'Unknown error message')
114
+ : 'Unknown error';
115
+ console.error(`\nError in ${pkg.name}:`, errorMessage);
116
+ return err instanceof Error ? err : new Error(errorMessage);
117
+ }))).value;
118
+ /**
119
+ * Performs a topological sort on packages based on their dependencies,
120
+ * ensuring dependencies are ordered before their dependents.
121
+ * @param packages - Array of Package objects to sort
122
+ * @returns An array of packages in dependency order (dependencies first)
123
+ */
124
+ const topologicalSortPackages = (packages, dependencyGraph) => {
125
+ const visited = new Set();
126
+ const result = [];
127
+ const packageMap = new Map(packages.map((p) => [p.name, p]));
128
+ const visit = (pkgName) => {
129
+ if (visited.has(pkgName))
130
+ return;
131
+ visited.add(pkgName);
132
+ const deps = dependencyGraph.get(pkgName) ?? [];
133
+ for (const dep of deps) {
134
+ visit(dep);
135
+ }
136
+ result.push(pkgName);
137
+ };
138
+ for (const pkg of packages) {
139
+ visit(pkg.name);
140
+ }
141
+ return result
142
+ .map((pkgName) => packageMap.get(pkgName))
143
+ .filter(isNotUndefined);
144
+ };
145
+ /**
146
+ * Builds a dependency graph from the given packages, mapping each package name
147
+ * to its internal workspace dependencies.
148
+ * @param packages - Array of Package objects to analyze
149
+ * @returns A readonly map where keys are package names and values are arrays of their dependency package names
150
+ */
151
+ const buildDependencyGraph = (packages) => {
152
+ const packageMap = new Map(packages.map((p) => [p.name, p]));
153
+ const graph = new Map();
154
+ for (const pkg of packages) {
155
+ const deps = Object.keys(pkg.dependencies).filter((depName) => packageMap.has(depName));
156
+ graph.set(pkg.name, deps);
157
+ }
158
+ return graph;
159
+ };
160
+
161
+ export { executeParallel, executeStages };
162
+ //# sourceMappingURL=execute-parallel.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execute-parallel.mjs","sources":["../../../src/functions/workspace-utils/execute-parallel.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAcA;;;;;;AAMG;AACI,MAAM,eAAe,GAAG,OAC7B,QAA4B,EAC5B,UAAkB,EAClB,WAAA,GAAsB,CAAC,KAGrB;IACF,MAAM,kBAAkB,GAElB,EAAE;AAER,IAAA,MAAM,SAAS,GAAG,IAAI,GAAG,EAAoB;AAE7C,IAAA,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE;QAC1B,MAAM,OAAO,GAAG,aAAa,CAAC,GAAG,EAAE,UAAU,CAAC;AAE9C,QAAA,kBAAkB,CAAC,IAAI,CAAC,OAAO,CAAC;AAEhC,QAAA,MAAM,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,MAAK;AAC1C,YAAA,SAAS,CAAC,MAAM,CAAC,cAAc,CAAC;AAIlC,SAAC,CAAC;AAEF,QAAA,SAAS,CAAC,GAAG,CAAC,cAAc,CAAC;;AAO7B,QAAA,IAAI,SAAS,CAAC,IAAI,IAAI,WAAW,EAAE;;AAEjC,YAAA,MAAM,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC;;;AAIjC,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC;AACxC;AAEA;;;;;;;;AAQG;AACI,MAAM,aAAa,GAAG,OAC3B,QAA4B,EAC5B,UAAkB,EAClB,WAAA,GAAsB,CAAC,KACN;AACjB,IAAA,MAAM,eAAe,GAAG,oBAAoB,CAAC,QAAQ,CAAC;IAEtD,MAAM,MAAM,GAAG,uBAAuB,CAAC,QAAQ,EAAE,eAAe,CAAC;IAEjE,MAAM,MAAM,GAA2B,EAAE;AACzC,IAAA,MAAM,SAAS,GAAG,IAAI,GAAG,EAAU;IAEnC,OAAO,SAAS,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE;QACrC,MAA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CAAC;YAAE;AAC1B,QAAA,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC;QAEpB,MAAM,IAAI,GAAG,eAAe,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE;AAC/C,QAAA,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE;YACtB,KAAK,CAAC,GAAG,CAAC;;AAGZ,QAAA,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC;AACtB,KAAC;AAED,IAAA,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE;AAC1B,QAAA,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC;;AAGjB,IAAA,OAAO;AACJ,SAAA,GAAG,CAAC,CAAC,OAAO,KAAK,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC;SACxC,MAAM,CAAC,cAAc,CAAC;AAC3B,CAAC;AAED;;;;;AAKG;AACH,MAAM,oBAAoB,GAAG,CAC3B,QAA4B,KACc;IAC1C,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;AAC5D,IAAA,MAAM,KAAK,GAAG,IAAI,GAAG,EAA6B;AAElD,IAAA,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE;QAC1B,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC,OAAO,KACxD,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC,CACxB;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC;;AAG3B,IAAA,OAAO,KAAK;AACd,CAAC;;;;"}
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env tsx
2
+ import '../../node-global.mjs';
3
+ import { type Package } from './types.mjs';
4
+ /**
5
+ * Retrieves all workspace packages from a monorepo based on the workspace patterns
6
+ * defined in the root package.json file.
7
+ * @param rootPackageJsonDir - The directory containing the root package.json file
8
+ * @returns A promise that resolves to an array of Package objects containing package metadata
9
+ */
10
+ export declare const getWorkspacePackages: (rootPackageJsonDir: string) => Promise<readonly Package[]>;
11
+ //# sourceMappingURL=get-workspace-packages.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-workspace-packages.d.mts","sourceRoot":"","sources":["../../../src/functions/workspace-utils/get-workspace-packages.mts"],"names":[],"mappings":";AAUA,OAAO,uBAAuB,CAAC;AAC/B,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AAE3C;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,GAC/B,oBAAoB,MAAM,KACzB,OAAO,CAAC,SAAS,OAAO,EAAE,CA+D5B,CAAC"}
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env tsx
2
+ import { Result, Json, isNotUndefined, isRecord, hasKey, isString } from 'ts-data-forge';
3
+ import '../../node-global.mjs';
4
+
5
+ /**
6
+ * Retrieves all workspace packages from a monorepo based on the workspace patterns
7
+ * defined in the root package.json file.
8
+ * @param rootPackageJsonDir - The directory containing the root package.json file
9
+ * @returns A promise that resolves to an array of Package objects containing package metadata
10
+ */
11
+ const getWorkspacePackages = async (rootPackageJsonDir) => {
12
+ // Read root package.json
13
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
14
+ const rootPackageJson = JSON.parse(await fs.readFile(path.join(rootPackageJsonDir, 'package.json'), 'utf8'));
15
+ const workspacePatterns = getStrArrayFromJsonValue(rootPackageJson, 'workspaces');
16
+ const packagePromises = workspacePatterns.map(async (pattern) => {
17
+ const matches = await glob(pattern, {
18
+ cwd: rootPackageJsonDir,
19
+ ignore: ['**/node_modules/**'],
20
+ onlyDirectories: true,
21
+ absolute: true,
22
+ });
23
+ const packageJsonList = await Promise.all(matches.map(async (match) => {
24
+ const maybePackagePath = path.join(match, 'package.json');
25
+ const result = await Result.fromPromise(fs.readFile(maybePackagePath, 'utf8'));
26
+ if (Result.isErr(result))
27
+ return undefined;
28
+ const parsed = Json.parse(result.value);
29
+ if (Result.isErr(parsed))
30
+ return undefined;
31
+ return [maybePackagePath, parsed.value];
32
+ }));
33
+ const packageInfos = await Promise.all(packageJsonList
34
+ .filter(isNotUndefined)
35
+ .map(([packagePath, packageJson]) => ({
36
+ name: getStrFromJsonValue(packageJson, 'name'),
37
+ path: path.dirname(packagePath),
38
+ packageJson,
39
+ dependencies: {
40
+ ...getKeyValueRecordFromJsonValue(packageJson, 'dependencies'),
41
+ ...getKeyValueRecordFromJsonValue(packageJson, 'devDependencies'),
42
+ ...getKeyValueRecordFromJsonValue(packageJson, 'peerDependencies'),
43
+ },
44
+ })));
45
+ return packageInfos;
46
+ });
47
+ const allPackageArrays = await Promise.all(packagePromises);
48
+ const finalPackages = allPackageArrays.flat();
49
+ return finalPackages;
50
+ };
51
+ const getStrFromJsonValue = (value, key) => isRecord(value) && hasKey(value, key) && isString(value[key])
52
+ ? value[key]
53
+ : '';
54
+ const getStrArrayFromJsonValue = (value, key) => isRecord(value) &&
55
+ hasKey(value, key) &&
56
+ Array.isArray(value[key]) &&
57
+ value[key].every(isString)
58
+ ? value[key]
59
+ : [];
60
+ const getKeyValueRecordFromJsonValue = (value, key) => {
61
+ if (!isRecord(value) || !hasKey(value, key)) {
62
+ return {};
63
+ }
64
+ const obj = value[key];
65
+ if (!isRecord(obj)) {
66
+ return {};
67
+ }
68
+ const entries = Object.entries(obj).filter((entry) => isString(entry[1]));
69
+ return Object.fromEntries(entries);
70
+ };
71
+
72
+ export { getWorkspacePackages };
73
+ //# sourceMappingURL=get-workspace-packages.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-workspace-packages.mjs","sources":["../../../src/functions/workspace-utils/get-workspace-packages.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAaA;;;;;AAKG;MACU,oBAAoB,GAAG,OAClC,kBAA0B,KACK;;;IAG/B,MAAM,eAAe,GAAc,IAAI,CAAC,KAAK,CAC3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AAE;;AAEX,IAAA,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC;AACtB,IAAA,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;AAClB,QAAA,OAAO,EAAE;;IAEX,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CACxC,CAAC,KAAK,KAAgC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CACzD;AACD,IAAA,OAAO,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC;AACpC,CAAC;;;;"}
@@ -0,0 +1,6 @@
1
+ export * from './execute-parallel.mjs';
2
+ export * from './get-workspace-packages.mjs';
3
+ export * from './run-cmd-in-parallel.mjs';
4
+ export * from './run-cmd-in-stages.mjs';
5
+ export * from './types.mjs';
6
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/functions/workspace-utils/index.mts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,aAAa,CAAC"}
@@ -0,0 +1,5 @@
1
+ export { executeParallel, executeStages } from './execute-parallel.mjs';
2
+ export { getWorkspacePackages } from './get-workspace-packages.mjs';
3
+ export { runCmdInParallelAcrossWorkspaces } from './run-cmd-in-parallel.mjs';
4
+ export { runCmdInStagesAcrossWorkspaces } from './run-cmd-in-stages.mjs';
5
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env tsx
2
+ /**
3
+ * Executes a npm script command across all workspace packages in parallel.
4
+ * @param options - Configuration options for the parallel execution
5
+ * @param options.rootPackageJsonDir - The directory containing the root package.json file
6
+ * @param options.cmd - The npm script command to execute in each package
7
+ * @param options.concurrency - Maximum number of packages to process simultaneously (default: 3)
8
+ * @param options.filterWorkspacePattern - Optional function to filter packages by name
9
+ * @returns A promise that resolves when all packages have completed execution
10
+ */
11
+ export declare const runCmdInParallelAcrossWorkspaces: ({ rootPackageJsonDir, cmd, concurrency, filterWorkspacePattern, }: Readonly<{
12
+ rootPackageJsonDir: string;
13
+ cmd: string;
14
+ concurrency?: number;
15
+ filterWorkspacePattern?: (packageName: string) => boolean;
16
+ }>) => Promise<void>;
17
+ //# sourceMappingURL=run-cmd-in-parallel.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run-cmd-in-parallel.d.mts","sourceRoot":"","sources":["../../../src/functions/workspace-utils/run-cmd-in-parallel.mts"],"names":[],"mappings":";AAKA;;;;;;;;GAQG;AACH,eAAO,MAAM,gCAAgC,GAAU,mEAKpD,QAAQ,CAAC;IACV,kBAAkB,EAAE,MAAM,CAAC;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,sBAAsB,CAAC,EAAE,CAAC,WAAW,EAAE,MAAM,KAAK,OAAO,CAAC;CAC3D,CAAC,KAAG,OAAO,CAAC,IAAI,CAkBhB,CAAC"}
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env tsx
2
+ import { executeParallel } from './execute-parallel.mjs';
3
+ import { getWorkspacePackages } from './get-workspace-packages.mjs';
4
+
5
+ /**
6
+ * Executes a npm script command across all workspace packages in parallel.
7
+ * @param options - Configuration options for the parallel execution
8
+ * @param options.rootPackageJsonDir - The directory containing the root package.json file
9
+ * @param options.cmd - The npm script command to execute in each package
10
+ * @param options.concurrency - Maximum number of packages to process simultaneously (default: 3)
11
+ * @param options.filterWorkspacePattern - Optional function to filter packages by name
12
+ * @returns A promise that resolves when all packages have completed execution
13
+ */
14
+ const runCmdInParallelAcrossWorkspaces = async ({ rootPackageJsonDir, cmd, concurrency = 3, filterWorkspacePattern, }) => {
15
+ try {
16
+ const packages = await getWorkspacePackages(rootPackageJsonDir);
17
+ const filteredPackages = filterWorkspacePattern === undefined
18
+ ? packages
19
+ : packages.filter((pkg) => filterWorkspacePattern(pkg.name));
20
+ await executeParallel(filteredPackages, cmd, concurrency);
21
+ console.log(`\n✅ ${cmd} completed successfully`);
22
+ }
23
+ catch (err) {
24
+ console.error(`\n❌ ${cmd} failed:`, err instanceof Error ? err.message : (err?.toString() ?? ''));
25
+ process.exit(1);
26
+ }
27
+ };
28
+
29
+ export { runCmdInParallelAcrossWorkspaces };
30
+ //# sourceMappingURL=run-cmd-in-parallel.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run-cmd-in-parallel.mjs","sources":["../../../src/functions/workspace-utils/run-cmd-in-parallel.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAKA;;;;;;;;AAQG;AACI,MAAM,gCAAgC,GAAG,OAAO,EACrD,kBAAkB,EAClB,GAAG,EACH,WAAW,GAAG,CAAC,EACf,sBAAsB,GAMtB,KAAmB;AACnB,IAAA,IAAI;AACF,QAAA,MAAM,QAAQ,GAAG,MAAM,oBAAoB,CAAC,kBAAkB,CAAC;AAE/D,QAAA,MAAM,gBAAgB,GACpB,sBAAsB,KAAK;AACzB,cAAE;AACF,cAAE,QAAQ,CAAC,MAAM,CAAC,CAAC,GAAG,KAAK,sBAAsB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAEhE,MAAM,eAAe,CAAC,gBAAgB,EAAE,GAAG,EAAE,WAAW,CAAC;AACzD,QAAA,OAAO,CAAC,GAAG,CAAC,OAAO,GAAG,CAAA,uBAAA,CAAyB,CAAC;;IAChD,OAAO,GAAG,EAAE;AACZ,QAAA,OAAO,CAAC,KAAK,CACX,CAAA,IAAA,EAAO,GAAG,CAAA,QAAA,CAAU,EACpB,GAAG,YAAY,KAAK,GAAG,GAAG,CAAC,OAAO,IAAI,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAC7D;AACD,QAAA,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;;AAEnB;;;;"}
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env tsx
2
+ /**
3
+ * Executes a npm script command across all workspace packages in dependency order stages.
4
+ * Packages are grouped into stages where each stage contains packages whose
5
+ * dependencies have been completed in previous stages.
6
+ * @param options - Configuration options for the staged execution
7
+ * @param options.rootPackageJsonDir - The directory containing the root package.json file
8
+ * @param options.cmd - The npm script command to execute in each package
9
+ * @param options.concurrency - Maximum number of packages to process simultaneously within each stage (default: 3)
10
+ * @param options.filterWorkspacePattern - Optional function to filter packages by name
11
+ * @returns A promise that resolves when all stages have completed execution
12
+ */
13
+ export declare const runCmdInStagesAcrossWorkspaces: ({ rootPackageJsonDir, cmd, concurrency, filterWorkspacePattern, }: Readonly<{
14
+ rootPackageJsonDir: string;
15
+ cmd: string;
16
+ concurrency?: number;
17
+ filterWorkspacePattern?: (packageName: string) => boolean;
18
+ }>) => Promise<void>;
19
+ //# sourceMappingURL=run-cmd-in-stages.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run-cmd-in-stages.d.mts","sourceRoot":"","sources":["../../../src/functions/workspace-utils/run-cmd-in-stages.mts"],"names":[],"mappings":";AAKA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,8BAA8B,GAAU,mEAKlD,QAAQ,CAAC;IACV,kBAAkB,EAAE,MAAM,CAAC;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,sBAAsB,CAAC,EAAE,CAAC,WAAW,EAAE,MAAM,KAAK,OAAO,CAAC;CAC3D,CAAC,KAAG,OAAO,CAAC,IAAI,CAkBhB,CAAC"}
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env tsx
2
+ import { executeStages } from './execute-parallel.mjs';
3
+ import { getWorkspacePackages } from './get-workspace-packages.mjs';
4
+
5
+ /**
6
+ * Executes a npm script command across all workspace packages in dependency order stages.
7
+ * Packages are grouped into stages where each stage contains packages whose
8
+ * dependencies have been completed in previous stages.
9
+ * @param options - Configuration options for the staged execution
10
+ * @param options.rootPackageJsonDir - The directory containing the root package.json file
11
+ * @param options.cmd - The npm script command to execute in each package
12
+ * @param options.concurrency - Maximum number of packages to process simultaneously within each stage (default: 3)
13
+ * @param options.filterWorkspacePattern - Optional function to filter packages by name
14
+ * @returns A promise that resolves when all stages have completed execution
15
+ */
16
+ const runCmdInStagesAcrossWorkspaces = async ({ rootPackageJsonDir, cmd, concurrency = 3, filterWorkspacePattern, }) => {
17
+ try {
18
+ const packages = await getWorkspacePackages(rootPackageJsonDir);
19
+ const filteredPackages = filterWorkspacePattern === undefined
20
+ ? packages
21
+ : packages.filter((pkg) => filterWorkspacePattern(pkg.name));
22
+ await executeStages(filteredPackages, cmd, concurrency);
23
+ console.log(`\n✅ ${cmd} completed successfully`);
24
+ }
25
+ catch (err) {
26
+ console.error(`\n❌ ${cmd} failed:`, err instanceof Error ? err.message : (err?.toString() ?? ''));
27
+ process.exit(1);
28
+ }
29
+ };
30
+
31
+ export { runCmdInStagesAcrossWorkspaces };
32
+ //# sourceMappingURL=run-cmd-in-stages.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run-cmd-in-stages.mjs","sources":["../../../src/functions/workspace-utils/run-cmd-in-stages.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAKA;;;;;;;;;;AAUG;AACI,MAAM,8BAA8B,GAAG,OAAO,EACnD,kBAAkB,EAClB,GAAG,EACH,WAAW,GAAG,CAAC,EACf,sBAAsB,GAMtB,KAAmB;AACnB,IAAA,IAAI;AACF,QAAA,MAAM,QAAQ,GAAG,MAAM,oBAAoB,CAAC,kBAAkB,CAAC;AAE/D,QAAA,MAAM,gBAAgB,GACpB,sBAAsB,KAAK;AACzB,cAAE;AACF,cAAE,QAAQ,CAAC,MAAM,CAAC,CAAC,GAAG,KAAK,sBAAsB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAEhE,MAAM,aAAa,CAAC,gBAAgB,EAAE,GAAG,EAAE,WAAW,CAAC;AACvD,QAAA,OAAO,CAAC,GAAG,CAAC,OAAO,GAAG,CAAA,uBAAA,CAAyB,CAAC;;IAChD,OAAO,GAAG,EAAE;AACZ,QAAA,OAAO,CAAC,KAAK,CACX,CAAA,IAAA,EAAO,GAAG,CAAA,QAAA,CAAU,EACpB,GAAG,YAAY,KAAK,GAAG,GAAG,CAAC,OAAO,IAAI,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAC7D;AACD,QAAA,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;;AAEnB;;;;"}
@@ -0,0 +1,7 @@
1
+ export type Package = Readonly<{
2
+ name: string;
3
+ path: string;
4
+ packageJson: JsonValue;
5
+ dependencies: ReadonlyRecord<string, string>;
6
+ }>;
7
+ //# sourceMappingURL=types.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../../../src/functions/workspace-utils/types.mts"],"names":[],"mappings":"AACA,MAAM,MAAM,OAAO,GAAG,QAAQ,CAAC;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,SAAS,CAAC;IACvB,YAAY,EAAE,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC9C,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+
2
+ //# sourceMappingURL=types.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/dist/index.mjs CHANGED
@@ -6,4 +6,8 @@ export { $ } from './functions/exec-async.mjs';
6
6
  export { formatDiffFrom, formatFiles, formatFilesList, formatUntracked } from './functions/format.mjs';
7
7
  export { genIndex } from './functions/gen-index.mjs';
8
8
  export { checkShouldRunTypeChecks } from './functions/should-run.mjs';
9
+ export { executeParallel, executeStages } from './functions/workspace-utils/execute-parallel.mjs';
10
+ export { getWorkspacePackages } from './functions/workspace-utils/get-workspace-packages.mjs';
11
+ export { runCmdInParallelAcrossWorkspaces } from './functions/workspace-utils/run-cmd-in-parallel.mjs';
12
+ export { runCmdInStagesAcrossWorkspaces } from './functions/workspace-utils/run-cmd-in-stages.mjs';
9
13
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-repo-utils",
3
- "version": "3.0.1",
3
+ "version": "3.1.1",
4
4
  "private": false,
5
5
  "keywords": [
6
6
  "typescript"
@@ -30,14 +30,14 @@
30
30
  "LICENSE"
31
31
  ],
32
32
  "scripts": {
33
- "build": "npm run z:node-esm -- ./scripts/cmd/build.mjs",
34
- "check-all": "npm run z:node-esm -- ./scripts/cmd/check-all.mjs",
35
- "check:ext": "npm run z:node-esm -- ./scripts/cmd/check-ext.mjs",
33
+ "build": "tsx ./scripts/cmd/build.mjs",
34
+ "check-all": "tsx ./scripts/cmd/check-all.mjs",
35
+ "check:ext": "tsx ./scripts/cmd/check-ext.mjs",
36
36
  "cspell": "cspell \"**\" --gitignore --gitignore-root ./ --no-progress",
37
- "doc": "npm run z:node-esm -- ./scripts/cmd/gen-docs.mjs",
38
- "fmt": "npm run z:node-esm -- ./scripts/cmd/fmt-diff.mjs",
37
+ "doc": "tsx ./scripts/cmd/gen-docs.mjs",
38
+ "fmt": "tsx ./scripts/cmd/fmt-diff.mjs",
39
39
  "fmt:full": "prettier --write .",
40
- "gi": "npm run z:node-esm -- ./scripts/cmd/gi.mjs",
40
+ "gi": "tsx ./scripts/cmd/gi.mjs",
41
41
  "lint": "eslint .",
42
42
  "lint:fix": "eslint . --fix",
43
43
  "md": "markdownlint-cli2",
@@ -50,7 +50,6 @@
50
50
  "tscw": "tsc --noEmit --watch",
51
51
  "type-check": "tsc --noEmit",
52
52
  "update-packages": "npx npm-check-updates -u --install always --reject @types/node",
53
- "z:node-esm": "node --import tsx/esm",
54
53
  "z:vitest": "vitest --config ./configs/vitest.config.ts"
55
54
  },
56
55
  "dependencies": {
@@ -58,7 +57,7 @@
58
57
  "fast-glob": "^3.3.3",
59
58
  "micromatch": "^4.0.8",
60
59
  "prettier": "^3.6.2",
61
- "ts-data-forge": "^1.2.0"
60
+ "ts-data-forge": "^2.0.0"
62
61
  },
63
62
  "devDependencies": {
64
63
  "@eslint/js": "^9.30.0",
@@ -72,21 +71,21 @@
72
71
  "@semantic-release/github": "^11.0.3",
73
72
  "@semantic-release/npm": "^12.0.1",
74
73
  "@semantic-release/release-notes-generator": "^14.0.3",
75
- "@types/node": "^20.19.1",
74
+ "@types/node": "^20.19.4",
76
75
  "@vitest/coverage-v8": "^3.2.4",
77
76
  "@vitest/ui": "^3.2.4",
78
77
  "conventional-changelog-conventionalcommits": "^9.0.0",
79
78
  "cspell": "^9.1.2",
80
79
  "dedent": "^1.6.0",
81
- "eslint": "^9.29.0",
80
+ "eslint": "^9.30.1",
82
81
  "markdownlint-cli2": "^0.18.1",
83
82
  "prettier-plugin-organize-imports": "^4.1.0",
84
- "prettier-plugin-packagejson": "^2.5.15",
83
+ "prettier-plugin-packagejson": "^2.5.18",
85
84
  "rollup": "^4.44.1",
86
- "semantic-release": "^24.2.5",
85
+ "semantic-release": "^24.2.6",
87
86
  "ts-type-forge": "^2.0.3",
88
87
  "tsx": "^4.20.3",
89
- "typedoc": "^0.28.5",
88
+ "typedoc": "^0.28.7",
90
89
  "typedoc-plugin-markdown": "^4.7.0",
91
90
  "typescript": "^5.8.3",
92
91
  "typescript-eslint": "^8.35.0",
@@ -6,3 +6,4 @@ export * from './exec-async.mjs';
6
6
  export * from './format.mjs';
7
7
  export * from './gen-index.mjs';
8
8
  export * from './should-run.mjs';
9
+ export * from './workspace-utils/index.mjs';
@@ -2,6 +2,20 @@ import { pipe, Result } from 'ts-data-forge';
2
2
  import '../node-global.mjs';
3
3
  import { getDiffFrom } from './diff.mjs';
4
4
 
5
+ /**
6
+ * Checks if TypeScript type checks should run based on the diff from origin/main.
7
+ * Skips type checks if all changed files are documentation files, spell check config,
8
+ * or other non-TypeScript files that don't affect type checking.
9
+ *
10
+ * Ignored file patterns:
11
+ * - '.cspell.json'
12
+ * - '**.md'
13
+ * - '**.txt'
14
+ * - 'docs/**'
15
+ *
16
+ * @returns A promise that resolves when the check is complete. Sets GITHUB_OUTPUT
17
+ * environment variable with should_run=true/false if running in GitHub Actions.
18
+ */
5
19
  export const checkShouldRunTypeChecks = async (): Promise<void> => {
6
20
  // paths-ignore:
7
21
  // - '.cspell.json'
@@ -0,0 +1,247 @@
1
+ import { spawn } from 'child_process';
2
+ import {
3
+ createPromise,
4
+ hasKey,
5
+ isNotUndefined,
6
+ isRecord,
7
+ pipe,
8
+ Result,
9
+ } from 'ts-data-forge';
10
+ import '../../node-global.mjs';
11
+ import { type Package } from './types.mjs';
12
+
13
+ const DEBUG = false as boolean;
14
+
15
+ /**
16
+ * Executes a npm script across multiple packages in parallel with a concurrency limit.
17
+ * @param packages - Array of Package objects to execute the script in
18
+ * @param scriptName - The name of the npm script to execute
19
+ * @param concurrency - Maximum number of packages to process simultaneously (default: 3)
20
+ * @returns A promise that resolves to an array of execution results
21
+ */
22
+ export const executeParallel = async (
23
+ packages: readonly Package[],
24
+ scriptName: string,
25
+ concurrency: number = 3,
26
+ ): Promise<
27
+ readonly Result<Readonly<{ code?: number; skipped?: boolean }>, Error>[]
28
+ > => {
29
+ const mut_resultPromises: Promise<
30
+ Result<Readonly<{ code?: number; skipped?: boolean }>, Error>
31
+ >[] = [];
32
+
33
+ const executing = new Set<Promise<unknown>>();
34
+
35
+ for (const pkg of packages) {
36
+ const promise = executeScript(pkg, scriptName);
37
+
38
+ mut_resultPromises.push(promise);
39
+
40
+ const wrappedPromise = promise.finally(() => {
41
+ executing.delete(wrappedPromise);
42
+ if (DEBUG) {
43
+ console.debug('executing size', executing.size);
44
+ }
45
+ });
46
+
47
+ executing.add(wrappedPromise);
48
+
49
+ if (DEBUG) {
50
+ console.debug('executing size', executing.size);
51
+ }
52
+
53
+ // If we reach concurrency limit, wait for one to finish
54
+ if (executing.size >= concurrency) {
55
+ // eslint-disable-next-line no-await-in-loop
56
+ await Promise.race(executing);
57
+ }
58
+ }
59
+
60
+ return Promise.all(mut_resultPromises);
61
+ };
62
+
63
+ /**
64
+ * Executes a npm script across packages in dependency order stages.
65
+ * Packages are grouped into stages where each stage contains packages whose
66
+ * dependencies have been completed in previous stages.
67
+ * @param packages - Array of Package objects to execute the script in
68
+ * @param scriptName - The name of the npm script to execute
69
+ * @param concurrency - Maximum number of packages to process simultaneously within each stage (default: 3)
70
+ * @returns A promise that resolves when all stages are complete
71
+ */
72
+ export const executeStages = async (
73
+ packages: readonly Package[],
74
+ scriptName: string,
75
+ concurrency: number = 3,
76
+ ): Promise<void> => {
77
+ const dependencyGraph = buildDependencyGraph(packages);
78
+
79
+ const sorted = topologicalSortPackages(packages, dependencyGraph);
80
+
81
+ const stages: (readonly Package[])[] = [];
82
+ const completed = new Set<string>();
83
+
84
+ while (completed.size < sorted.length) {
85
+ const stage: Package[] = [];
86
+
87
+ for (const pkg of sorted) {
88
+ if (completed.has(pkg.name)) continue;
89
+
90
+ const deps = dependencyGraph.get(pkg.name) ?? [];
91
+ const depsCompleted = deps.every((dep) => completed.has(dep));
92
+
93
+ if (depsCompleted) {
94
+ stage.push(pkg);
95
+ }
96
+ }
97
+
98
+ if (stage.length === 0) {
99
+ throw new Error('Circular dependency detected');
100
+ }
101
+
102
+ stages.push(stage);
103
+ for (const pkg of stage) completed.add(pkg.name);
104
+ }
105
+
106
+ console.log(`\nExecuting ${scriptName} in ${stages.length} stages...\n`);
107
+
108
+ for (const [i, stage] of stages.entries()) {
109
+ if (stage.length > 0) {
110
+ console.log(`Stage ${i + 1}: ${stage.map((p) => p.name).join(', ')}`);
111
+ // eslint-disable-next-line no-await-in-loop
112
+ await executeParallel(stage, scriptName, concurrency);
113
+ }
114
+ }
115
+ };
116
+
117
+ /**
118
+ * Executes a npm script in a specific package directory.
119
+ * @param pkg - The package object containing path and metadata
120
+ * @param scriptName - The name of the npm script to execute
121
+ * @param options - Configuration options
122
+ * @param options.prefix - Whether to prefix output with package name (default: true)
123
+ * @returns A promise that resolves to execution result with exit code or skipped flag
124
+ */
125
+ const executeScript = (
126
+ pkg: Package,
127
+ scriptName: string,
128
+ { prefix = true }: Readonly<{ prefix?: boolean }> = {},
129
+ ): Promise<Result<Readonly<{ code?: number; skipped?: boolean }>, Error>> =>
130
+ pipe(
131
+ createPromise(
132
+ (
133
+ resolve: (
134
+ value: Readonly<{ code?: number; skipped?: boolean }>,
135
+ ) => void,
136
+ reject: (reason: unknown) => void,
137
+ ) => {
138
+ const packageJsonScripts =
139
+ isRecord(pkg.packageJson) && isRecord(pkg.packageJson['scripts'])
140
+ ? pkg.packageJson['scripts']
141
+ : {};
142
+
143
+ const hasScript = hasKey(packageJsonScripts, scriptName);
144
+ if (!hasScript) {
145
+ resolve({ skipped: true });
146
+ return;
147
+ }
148
+
149
+ const prefixStr = prefix ? `[${pkg.name}] ` : '';
150
+ const proc = spawn('npm', ['run', scriptName], {
151
+ cwd: pkg.path,
152
+ shell: true,
153
+ stdio: 'pipe',
154
+ });
155
+
156
+ proc.stdout.on('data', (data: Readonly<Buffer>) => {
157
+ process.stdout.write(prefixStr + data.toString());
158
+ });
159
+
160
+ proc.stderr.on('data', (data: Readonly<Buffer>) => {
161
+ process.stderr.write(prefixStr + data.toString());
162
+ });
163
+
164
+ proc.on('close', (code: number | null) => {
165
+ if (code === 0) {
166
+ resolve({ code });
167
+ } else {
168
+ reject(new Error(`${pkg.name} exited with code ${code}`));
169
+ }
170
+ });
171
+
172
+ proc.on('error', reject);
173
+ },
174
+ ),
175
+ ).map((result) =>
176
+ result.then(
177
+ Result.mapErr((err) => {
178
+ const errorMessage: string =
179
+ err instanceof Error
180
+ ? err.message
181
+ : isRecord(err) && hasKey(err, 'message')
182
+ ? (err.message?.toString() ?? 'Unknown error message')
183
+ : 'Unknown error';
184
+
185
+ console.error(`\nError in ${pkg.name}:`, errorMessage);
186
+ return err instanceof Error ? err : new Error(errorMessage);
187
+ }),
188
+ ),
189
+ ).value;
190
+
191
+ /**
192
+ * Performs a topological sort on packages based on their dependencies,
193
+ * ensuring dependencies are ordered before their dependents.
194
+ * @param packages - Array of Package objects to sort
195
+ * @returns An array of packages in dependency order (dependencies first)
196
+ */
197
+ const topologicalSortPackages = (
198
+ packages: readonly Package[],
199
+ dependencyGraph: ReadonlyMap<string, readonly string[]>,
200
+ ): readonly Package[] => {
201
+ const visited = new Set<string>();
202
+ const result: string[] = [];
203
+
204
+ const packageMap = new Map(packages.map((p) => [p.name, p]));
205
+
206
+ const visit = (pkgName: string): void => {
207
+ if (visited.has(pkgName)) return;
208
+ visited.add(pkgName);
209
+
210
+ const deps = dependencyGraph.get(pkgName) ?? [];
211
+ for (const dep of deps) {
212
+ visit(dep);
213
+ }
214
+
215
+ result.push(pkgName);
216
+ };
217
+
218
+ for (const pkg of packages) {
219
+ visit(pkg.name);
220
+ }
221
+
222
+ return result
223
+ .map((pkgName) => packageMap.get(pkgName))
224
+ .filter(isNotUndefined);
225
+ };
226
+
227
+ /**
228
+ * Builds a dependency graph from the given packages, mapping each package name
229
+ * to its internal workspace dependencies.
230
+ * @param packages - Array of Package objects to analyze
231
+ * @returns A readonly map where keys are package names and values are arrays of their dependency package names
232
+ */
233
+ const buildDependencyGraph = (
234
+ packages: readonly Package[],
235
+ ): ReadonlyMap<string, readonly string[]> => {
236
+ const packageMap = new Map(packages.map((p) => [p.name, p]));
237
+ const graph = new Map<string, readonly string[]>();
238
+
239
+ for (const pkg of packages) {
240
+ const deps = Object.keys(pkg.dependencies).filter((depName) =>
241
+ packageMap.has(depName),
242
+ );
243
+ graph.set(pkg.name, deps);
244
+ }
245
+
246
+ return graph;
247
+ };
@@ -0,0 +1,118 @@
1
+ #!/usr/bin/env tsx
2
+
3
+ import {
4
+ hasKey,
5
+ isNotUndefined,
6
+ isRecord,
7
+ isString,
8
+ Json,
9
+ Result,
10
+ } from 'ts-data-forge';
11
+ import '../../node-global.mjs';
12
+ import { type Package } from './types.mjs';
13
+
14
+ /**
15
+ * Retrieves all workspace packages from a monorepo based on the workspace patterns
16
+ * defined in the root package.json file.
17
+ * @param rootPackageJsonDir - The directory containing the root package.json file
18
+ * @returns A promise that resolves to an array of Package objects containing package metadata
19
+ */
20
+ export const getWorkspacePackages = async (
21
+ rootPackageJsonDir: string,
22
+ ): Promise<readonly Package[]> => {
23
+ // Read root package.json
24
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
25
+ const rootPackageJson: JsonValue = JSON.parse(
26
+ await fs.readFile(path.join(rootPackageJsonDir, 'package.json'), 'utf8'),
27
+ );
28
+
29
+ const workspacePatterns: readonly string[] = getStrArrayFromJsonValue(
30
+ rootPackageJson,
31
+ 'workspaces',
32
+ );
33
+
34
+ const packagePromises = workspacePatterns.map(async (pattern) => {
35
+ const matches = await glob(pattern, {
36
+ cwd: rootPackageJsonDir,
37
+ ignore: ['**/node_modules/**'],
38
+ onlyDirectories: true,
39
+ absolute: true,
40
+ });
41
+
42
+ const packageJsonList: readonly (
43
+ | readonly [string, JsonValue]
44
+ | undefined
45
+ )[] = await Promise.all(
46
+ matches.map(async (match) => {
47
+ const maybePackagePath = path.join(match, 'package.json');
48
+
49
+ const result = await Result.fromPromise(
50
+ fs.readFile(maybePackagePath, 'utf8'),
51
+ );
52
+
53
+ if (Result.isErr(result)) return undefined;
54
+
55
+ const parsed = Json.parse(result.value);
56
+
57
+ if (Result.isErr(parsed)) return undefined;
58
+
59
+ return [maybePackagePath, parsed.value] as const;
60
+ }),
61
+ );
62
+
63
+ const packageInfos: readonly Package[] = await Promise.all(
64
+ packageJsonList
65
+ .filter(isNotUndefined)
66
+ .map(([packagePath, packageJson]) => ({
67
+ name: getStrFromJsonValue(packageJson, 'name'),
68
+ path: path.dirname(packagePath),
69
+ packageJson,
70
+ dependencies: {
71
+ ...getKeyValueRecordFromJsonValue(packageJson, 'dependencies'),
72
+ ...getKeyValueRecordFromJsonValue(packageJson, 'devDependencies'),
73
+ ...getKeyValueRecordFromJsonValue(packageJson, 'peerDependencies'),
74
+ },
75
+ })),
76
+ );
77
+
78
+ return packageInfos;
79
+ });
80
+
81
+ const allPackageArrays = await Promise.all(packagePromises);
82
+ const finalPackages = allPackageArrays.flat();
83
+
84
+ return finalPackages;
85
+ };
86
+
87
+ const getStrFromJsonValue = (value: JsonValue, key: string): string =>
88
+ isRecord(value) && hasKey(value, key) && isString(value[key])
89
+ ? value[key]
90
+ : '';
91
+
92
+ const getStrArrayFromJsonValue = (
93
+ value: JsonValue,
94
+ key: string,
95
+ ): readonly string[] =>
96
+ isRecord(value) &&
97
+ hasKey(value, key) &&
98
+ Array.isArray(value[key]) &&
99
+ value[key].every(isString)
100
+ ? value[key]
101
+ : [];
102
+
103
+ const getKeyValueRecordFromJsonValue = (
104
+ value: JsonValue,
105
+ key: string,
106
+ ): ReadonlyRecord<string, string> => {
107
+ if (!isRecord(value) || !hasKey(value, key)) {
108
+ return {};
109
+ }
110
+ const obj = value[key];
111
+ if (!isRecord(obj)) {
112
+ return {};
113
+ }
114
+ const entries = Object.entries(obj).filter(
115
+ (entry): entry is [string, string] => isString(entry[1]),
116
+ );
117
+ return Object.fromEntries(entries);
118
+ };
@@ -0,0 +1,5 @@
1
+ export * from './execute-parallel.mjs';
2
+ export * from './get-workspace-packages.mjs';
3
+ export * from './run-cmd-in-parallel.mjs';
4
+ export * from './run-cmd-in-stages.mjs';
5
+ export * from './types.mjs';
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env tsx
2
+
3
+ import { executeParallel } from './execute-parallel.mjs';
4
+ import { getWorkspacePackages } from './get-workspace-packages.mjs';
5
+
6
+ /**
7
+ * Executes a npm script command across all workspace packages in parallel.
8
+ * @param options - Configuration options for the parallel execution
9
+ * @param options.rootPackageJsonDir - The directory containing the root package.json file
10
+ * @param options.cmd - The npm script command to execute in each package
11
+ * @param options.concurrency - Maximum number of packages to process simultaneously (default: 3)
12
+ * @param options.filterWorkspacePattern - Optional function to filter packages by name
13
+ * @returns A promise that resolves when all packages have completed execution
14
+ */
15
+ export const runCmdInParallelAcrossWorkspaces = async ({
16
+ rootPackageJsonDir,
17
+ cmd,
18
+ concurrency = 3,
19
+ filterWorkspacePattern,
20
+ }: Readonly<{
21
+ rootPackageJsonDir: string;
22
+ cmd: string;
23
+ concurrency?: number;
24
+ filterWorkspacePattern?: (packageName: string) => boolean;
25
+ }>): Promise<void> => {
26
+ try {
27
+ const packages = await getWorkspacePackages(rootPackageJsonDir);
28
+
29
+ const filteredPackages =
30
+ filterWorkspacePattern === undefined
31
+ ? packages
32
+ : packages.filter((pkg) => filterWorkspacePattern(pkg.name));
33
+
34
+ await executeParallel(filteredPackages, cmd, concurrency);
35
+ console.log(`\n✅ ${cmd} completed successfully`);
36
+ } catch (err) {
37
+ console.error(
38
+ `\n❌ ${cmd} failed:`,
39
+ err instanceof Error ? err.message : (err?.toString() ?? ''),
40
+ );
41
+ process.exit(1);
42
+ }
43
+ };
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env tsx
2
+
3
+ import { executeStages } from './execute-parallel.mjs';
4
+ import { getWorkspacePackages } from './get-workspace-packages.mjs';
5
+
6
+ /**
7
+ * Executes a npm script command across all workspace packages in dependency order stages.
8
+ * Packages are grouped into stages where each stage contains packages whose
9
+ * dependencies have been completed in previous stages.
10
+ * @param options - Configuration options for the staged execution
11
+ * @param options.rootPackageJsonDir - The directory containing the root package.json file
12
+ * @param options.cmd - The npm script command to execute in each package
13
+ * @param options.concurrency - Maximum number of packages to process simultaneously within each stage (default: 3)
14
+ * @param options.filterWorkspacePattern - Optional function to filter packages by name
15
+ * @returns A promise that resolves when all stages have completed execution
16
+ */
17
+ export const runCmdInStagesAcrossWorkspaces = async ({
18
+ rootPackageJsonDir,
19
+ cmd,
20
+ concurrency = 3,
21
+ filterWorkspacePattern,
22
+ }: Readonly<{
23
+ rootPackageJsonDir: string;
24
+ cmd: string;
25
+ concurrency?: number;
26
+ filterWorkspacePattern?: (packageName: string) => boolean;
27
+ }>): Promise<void> => {
28
+ try {
29
+ const packages = await getWorkspacePackages(rootPackageJsonDir);
30
+
31
+ const filteredPackages =
32
+ filterWorkspacePattern === undefined
33
+ ? packages
34
+ : packages.filter((pkg) => filterWorkspacePattern(pkg.name));
35
+
36
+ await executeStages(filteredPackages, cmd, concurrency);
37
+ console.log(`\n✅ ${cmd} completed successfully`);
38
+ } catch (err) {
39
+ console.error(
40
+ `\n❌ ${cmd} failed:`,
41
+ err instanceof Error ? err.message : (err?.toString() ?? ''),
42
+ );
43
+ process.exit(1);
44
+ }
45
+ };
@@ -0,0 +1,7 @@
1
+ // Get all workspace packages
2
+ export type Package = Readonly<{
3
+ name: string;
4
+ path: string;
5
+ packageJson: JsonValue;
6
+ dependencies: ReadonlyRecord<string, string>;
7
+ }>;