codemodctl 0.1.27 → 0.1.29

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/cli.d.mts ADDED
@@ -0,0 +1,2 @@
1
+
2
+ export { };
package/dist/cli.mjs ADDED
@@ -0,0 +1,405 @@
1
+ #!/usr/bin/env node
2
+ import { analyzeCodeowners } from "./codeowners.mjs";
3
+ import { analyzeDirectories } from "./directory.mjs";
4
+ import { defineCommand, runMain } from "citty";
5
+ import crypto from "node:crypto";
6
+ import { $ } from "execa";
7
+ import { writeFile } from "node:fs/promises";
8
+
9
+ //#region src/commands/git/create-pr.ts
10
+ function logExecError(message, error) {
11
+ console.error(message);
12
+ if (error && typeof error === "object" && "stderr" in error) {
13
+ const execError = error;
14
+ if (execError.stderr) console.error(execError.stderr);
15
+ if (execError.stdout) console.error(execError.stdout);
16
+ } else console.error(error instanceof Error ? error.message : String(error));
17
+ }
18
+ const createPrCommand = defineCommand({
19
+ meta: {
20
+ name: "create-pr",
21
+ description: "Create a pull request"
22
+ },
23
+ args: {
24
+ title: {
25
+ type: "string",
26
+ description: "Title of the pull request",
27
+ required: true
28
+ },
29
+ body: {
30
+ type: "string",
31
+ description: "Body/description of the pull request",
32
+ required: false
33
+ },
34
+ head: {
35
+ type: "string",
36
+ description: "Head branch for the pull request",
37
+ required: false
38
+ },
39
+ base: {
40
+ type: "string",
41
+ description: "Base branch to merge into",
42
+ required: false
43
+ },
44
+ push: {
45
+ type: "boolean",
46
+ required: false
47
+ },
48
+ commitMessage: {
49
+ alias: "m",
50
+ type: "string",
51
+ description: "Message to commit",
52
+ required: false
53
+ },
54
+ branchName: {
55
+ alias: "b",
56
+ type: "string",
57
+ description: "Branch to create the pull request from",
58
+ required: false
59
+ }
60
+ },
61
+ async run({ args }) {
62
+ const { title, body, head, base, push, commitMessage, branchName } = args;
63
+ if (push && !commitMessage) {
64
+ console.error("Error: commitMessage is required if commit is true");
65
+ process.exit(1);
66
+ }
67
+ const apiEndpoint = process.env.BUTTERFLOW_API_ENDPOINT;
68
+ const authToken = process.env.BUTTERFLOW_API_AUTH_TOKEN;
69
+ const taskId = process.env.CODEMOD_TASK_ID;
70
+ if (!taskId) {
71
+ console.error("Error: CODEMOD_TASK_ID environment variable is required");
72
+ process.exit(1);
73
+ }
74
+ if (!apiEndpoint) {
75
+ console.error("Error: BUTTERFLOW_API_ENDPOINT environment variable is required");
76
+ process.exit(1);
77
+ }
78
+ if (!authToken) {
79
+ console.error("Error: BUTTERFLOW_API_AUTH_TOKEN environment variable is required");
80
+ process.exit(1);
81
+ }
82
+ const prData = { title };
83
+ if (push) {
84
+ try {
85
+ await $`git diff --quiet`;
86
+ await $`git diff --cached --quiet`;
87
+ console.error("No changes detected, skipping pull request creation.");
88
+ process.exit(0);
89
+ } catch {
90
+ console.log("Changes detected, proceeding with pull request creation...");
91
+ }
92
+ const taskIdSignature = crypto.createHash("sha256").update(taskId).digest("hex").slice(0, 8);
93
+ const codemodBranchName = branchName ? branchName : `codemod-${taskIdSignature}`;
94
+ let remoteBaseBranch;
95
+ try {
96
+ remoteBaseBranch = (await $`git remote show origin`).stdout.match(/HEAD branch: (.+)/)?.[1]?.trim() || "main";
97
+ } catch (error) {
98
+ logExecError("Error: Failed to get remote base branch", error);
99
+ process.exit(1);
100
+ }
101
+ if (codemodBranchName) prData.head = codemodBranchName;
102
+ if (remoteBaseBranch) prData.base = remoteBaseBranch;
103
+ console.debug(`Remote base branch: ${remoteBaseBranch}`);
104
+ try {
105
+ await $`git checkout -b ${codemodBranchName}`;
106
+ } catch (error) {
107
+ logExecError("Error: Failed to checkout branch", error);
108
+ process.exit(1);
109
+ }
110
+ try {
111
+ await $`git add .`;
112
+ } catch (error) {
113
+ logExecError("Error: Failed to add changes", error);
114
+ process.exit(1);
115
+ }
116
+ try {
117
+ await $`git commit --no-verify -m ${commitMessage}`;
118
+ } catch (error) {
119
+ logExecError("Error: Failed to commit changes", error);
120
+ process.exit(1);
121
+ }
122
+ try {
123
+ await $({ env: process.env.DEBUG ? {
124
+ GIT_TRACE: "1",
125
+ GIT_TRACE_PACKET: "1"
126
+ } : {} })`git push origin ${codemodBranchName} --force`;
127
+ console.log(`Pushed branch to origin: ${codemodBranchName}`);
128
+ } catch (error) {
129
+ if ((error && typeof error === "object" && "stderr" in error ? String(error.stderr) : "").includes("Everything up-to-date")) console.log(`Branch ${codemodBranchName} is already up-to-date on remote, continuing...`);
130
+ else {
131
+ logExecError("Error: Failed to push changes", error);
132
+ process.exit(1);
133
+ }
134
+ }
135
+ }
136
+ if (body) prData.body = body;
137
+ if (head && !prData.head) prData.head = head;
138
+ if (base && !prData.base) prData.base = base;
139
+ const prUrl = `${apiEndpoint}/api/butterflow/v1/tasks/${taskId}/pull-request`;
140
+ try {
141
+ console.log("Creating pull request...");
142
+ console.log(` URL: ${prUrl}`);
143
+ console.log(` Title: ${title}`);
144
+ console.log(` Head: ${prData.head ?? head ?? "(not set)"}`);
145
+ console.log(` Base: ${prData.base ?? base ?? "(not set)"}`);
146
+ if (body) console.log(` Body: ${body}`);
147
+ const response = await fetch(prUrl, {
148
+ method: "POST",
149
+ headers: {
150
+ Authorization: `Bearer ${authToken}`,
151
+ "Content-Type": "application/json"
152
+ },
153
+ body: JSON.stringify(prData)
154
+ });
155
+ if (!response.ok) {
156
+ const errorText = await response.text();
157
+ console.error(`❌ Failed to create pull request: HTTP ${response.status}`);
158
+ console.error(` Response: ${errorText}`);
159
+ process.exit(1);
160
+ }
161
+ await response.json();
162
+ console.log("✅ Pull request created successfully!");
163
+ } catch (error) {
164
+ console.error("❌ Failed to create pull request:");
165
+ console.error(` URL: ${prUrl}`);
166
+ console.error(` PR data: ${JSON.stringify(prData)}`);
167
+ if (error instanceof TypeError && error.message === "fetch failed") {
168
+ const cause = error.cause;
169
+ console.error(` Cause: ${cause instanceof Error ? cause.message : String(cause ?? "unknown")}`);
170
+ } else console.error(` Error: ${error instanceof Error ? error.message : String(error)}`);
171
+ process.exit(1);
172
+ }
173
+ }
174
+ });
175
+
176
+ //#endregion
177
+ //#region src/commands/git/index.ts
178
+ const gitCommand = defineCommand({
179
+ meta: {
180
+ name: "git",
181
+ description: "Git operations"
182
+ },
183
+ subCommands: { "create-pr": createPrCommand }
184
+ });
185
+
186
+ //#endregion
187
+ //#region src/commands/shard/codeowner.ts
188
+ /**
189
+ * Codeowner-based sharding command
190
+ *
191
+ * Creates shards by grouping files by their CODEOWNERS team assignments.
192
+ * Uses simple file distribution within each team group, maintaining
193
+ * consistency with existing state when available.
194
+ *
195
+ * Example usage:
196
+ * npx codemodctl shard codeowner -l tsx -c ./codemod.ts -s 30 --stateProp shards --codeowners .github/CODEOWNERS
197
+ *
198
+ * This will analyze all applicable files, group them by CODEOWNERS team assignments, and create
199
+ * shards with approximately 30 files each within each team.
200
+ */
201
+ const codeownerCommand = defineCommand({
202
+ meta: {
203
+ name: "codeowner",
204
+ description: "Analyze GitHub CODEOWNERS file and create sharding output"
205
+ },
206
+ args: {
207
+ shardSize: {
208
+ type: "string",
209
+ alias: "s",
210
+ description: "Number of files per shard",
211
+ required: true
212
+ },
213
+ stateProp: {
214
+ type: "string",
215
+ alias: "p",
216
+ description: "Property name for state output",
217
+ required: true
218
+ },
219
+ codeowners: {
220
+ type: "string",
221
+ description: "Path to CODEOWNERS file (optional)",
222
+ required: false
223
+ },
224
+ codemodFile: {
225
+ type: "string",
226
+ alias: "c",
227
+ description: "Path to codemod file",
228
+ required: true
229
+ },
230
+ language: {
231
+ type: "string",
232
+ alias: "l",
233
+ description: "Language of the codemod",
234
+ required: true
235
+ }
236
+ },
237
+ async run({ args }) {
238
+ const { shardSize: shardSizeStr, stateProp, codeowners: codeownersPath, codemodFile: codemodFilePath, language } = args;
239
+ const shardSize = parseInt(shardSizeStr, 10);
240
+ if (isNaN(shardSize) || shardSize <= 0) {
241
+ console.error("Error: shard-size must be a positive number");
242
+ process.exit(1);
243
+ }
244
+ const stateOutputsPath = process.env.STATE_OUTPUTS;
245
+ if (!stateOutputsPath) {
246
+ console.error("Error: STATE_OUTPUTS environment variable is required");
247
+ process.exit(1);
248
+ }
249
+ try {
250
+ console.log(`State property: ${stateProp}`);
251
+ const existingStateJson = process.env.CODEMOD_STATE;
252
+ let existingState;
253
+ if (existingStateJson) try {
254
+ existingState = JSON.parse(existingStateJson)[stateProp];
255
+ console.log(`Found existing state with ${existingState.length} shards`);
256
+ } catch (parseError) {
257
+ console.warn(`Warning: Failed to parse existing state: ${parseError}`);
258
+ existingState = void 0;
259
+ }
260
+ const result = await analyzeCodeowners({
261
+ shardSize,
262
+ codeownersPath,
263
+ rulePath: codemodFilePath,
264
+ projectRoot: process.cwd(),
265
+ language,
266
+ existingState
267
+ });
268
+ const stateOutput = `${stateProp}=${JSON.stringify(result.shards)}\n`;
269
+ console.log(`Writing state output to: ${stateOutputsPath}`);
270
+ await writeFile(stateOutputsPath, stateOutput, { flag: "a" });
271
+ console.log("✅ Sharding completed successfully!");
272
+ console.log("Generated shards:", JSON.stringify(result.shards, null, 2));
273
+ } catch (error) {
274
+ console.error("❌ Failed to process codeowner file:");
275
+ console.error(error instanceof Error ? error.message : String(error));
276
+ process.exit(1);
277
+ }
278
+ }
279
+ });
280
+
281
+ //#endregion
282
+ //#region src/commands/shard/directory.ts
283
+ /**
284
+ * Directory-based sharding command
285
+ *
286
+ * Creates shards by grouping files within subdirectories of a target directory.
287
+ * Uses consistent hashing to distribute files within each directory group, maintaining
288
+ * consistency with existing state when available.
289
+ *
290
+ * Example usage:
291
+ * npx codemodctl shard directory -l tsx -c ./codemod.ts -s 30 --stateProp shards --target packages/
292
+ *
293
+ * This will analyze all applicable files within subdirectories of 'packages/' and create
294
+ * shards with approximately 30 files each, grouped by directory.
295
+ */
296
+ const directoryCommand = defineCommand({
297
+ meta: {
298
+ name: "directory",
299
+ description: "Create directory-based sharding output"
300
+ },
301
+ args: {
302
+ shardSize: {
303
+ type: "string",
304
+ alias: "s",
305
+ description: "Number of files per shard",
306
+ required: true
307
+ },
308
+ stateProp: {
309
+ type: "string",
310
+ alias: "p",
311
+ description: "Property name for state output",
312
+ required: true
313
+ },
314
+ target: {
315
+ type: "string",
316
+ description: "Target directory to shard by subdirectories",
317
+ required: true
318
+ },
319
+ codemodFile: {
320
+ type: "string",
321
+ alias: "c",
322
+ description: "Path to codemod file",
323
+ required: true
324
+ },
325
+ language: {
326
+ type: "string",
327
+ alias: "l",
328
+ description: "Language of the codemod",
329
+ required: true
330
+ }
331
+ },
332
+ async run({ args }) {
333
+ const { shardSize: shardSizeStr, stateProp, target, codemodFile: codemodFilePath, language } = args;
334
+ const shardSize = parseInt(shardSizeStr, 10);
335
+ if (isNaN(shardSize) || shardSize <= 0) {
336
+ console.error("Error: shard-size must be a positive number");
337
+ process.exit(1);
338
+ }
339
+ const stateOutputsPath = process.env.STATE_OUTPUTS;
340
+ if (!stateOutputsPath) {
341
+ console.error("Error: STATE_OUTPUTS environment variable is required");
342
+ process.exit(1);
343
+ }
344
+ try {
345
+ console.log(`State property: ${stateProp}`);
346
+ console.log(`Target directory: ${target}`);
347
+ const existingStateJson = process.env.CODEMOD_STATE;
348
+ let existingState;
349
+ if (existingStateJson) try {
350
+ existingState = JSON.parse(existingStateJson)[stateProp];
351
+ console.log(`Found existing state with ${existingState.length} shards`);
352
+ } catch (parseError) {
353
+ console.warn(`Warning: Failed to parse existing state: ${parseError}`);
354
+ existingState = void 0;
355
+ }
356
+ const result = await analyzeDirectories({
357
+ shardSize,
358
+ target,
359
+ rulePath: codemodFilePath,
360
+ projectRoot: process.cwd(),
361
+ language,
362
+ existingState
363
+ });
364
+ const stateOutput = `${stateProp}=${JSON.stringify(result.shards)}\n`;
365
+ console.log(`Writing state output to: ${stateOutputsPath}`);
366
+ await writeFile(stateOutputsPath, stateOutput, { flag: "a" });
367
+ console.log("✅ Directory-based sharding completed successfully!");
368
+ console.log("Generated shards:", JSON.stringify(result.shards, null, 2));
369
+ } catch (error) {
370
+ console.error("❌ Failed to process directory sharding:");
371
+ console.error(error instanceof Error ? error.message : String(error));
372
+ process.exit(1);
373
+ }
374
+ }
375
+ });
376
+
377
+ //#endregion
378
+ //#region src/commands/shard/index.ts
379
+ const shardCommand = defineCommand({
380
+ meta: {
381
+ name: "shard",
382
+ description: "Sharding operations for distributing work"
383
+ },
384
+ subCommands: {
385
+ codeowner: codeownerCommand,
386
+ directory: directoryCommand
387
+ }
388
+ });
389
+
390
+ //#endregion
391
+ //#region src/cli.ts
392
+ runMain(defineCommand({
393
+ meta: {
394
+ name: "codemodctl",
395
+ version: "0.1.0",
396
+ description: "CLI tool for workflow engine operations"
397
+ },
398
+ subCommands: {
399
+ git: gitCommand,
400
+ shard: shardCommand
401
+ }
402
+ }));
403
+
404
+ //#endregion
405
+ export { };
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env node
2
+ import { execSync } from "node:child_process";
3
+
4
+ //#region src/utils/codemod-cli.ts
5
+ /**
6
+ * Executes the codemod CLI command and returns applicable file paths
7
+ */
8
+ async function getApplicableFiles(rulePath, language, projectRoot) {
9
+ try {
10
+ const command = `npx -y codemod@latest jssg list-applicable --allow-fs --allow-fetch --allow-child-process --language ${language} --target ${projectRoot} ${rulePath}`;
11
+ console.debug(`Executing: ${command}`);
12
+ const applicableFiles = execSync(command, {
13
+ encoding: "utf8",
14
+ cwd: projectRoot,
15
+ maxBuffer: 10 * 1024 * 1024
16
+ }).split("\n").filter((line) => line.startsWith("[Applicable] ")).map((line) => line.replace("[Applicable] ", "").trim()).filter((filePath) => filePath.length > 0);
17
+ console.debug(`Found ${applicableFiles.length} applicable files`);
18
+ return applicableFiles;
19
+ } catch (error) {
20
+ console.error("Error executing codemod CLI:", error);
21
+ throw new Error(`Failed to execute codemod CLI: ${error}`);
22
+ }
23
+ }
24
+
25
+ //#endregion
26
+ export { getApplicableFiles as t };
@@ -0,0 +1,96 @@
1
+
2
+ //#region src/utils/codeowner-analysis.d.ts
3
+ /**
4
+ * Result for a single team-based shard
5
+ */
6
+ interface ShardResult {
7
+ /** The team that owns these files */
8
+ team: string;
9
+ /** The shard identifier string (e.g., "1/3") */
10
+ shard: string;
11
+ /** The combined shard ID (e.g., "team-name 1/3") */
12
+ shardId: string;
13
+ /** Array of file paths in this shard */
14
+ _meta_files: string[];
15
+ }
16
+ /**
17
+ * Information about a team and their files
18
+ */
19
+ interface TeamFileInfo {
20
+ /** Team name */
21
+ team: string;
22
+ /** Number of files owned by this team */
23
+ fileCount: number;
24
+ /** Array of file paths owned by this team */
25
+ files: string[];
26
+ }
27
+ /**
28
+ * Options for codeowner-based analysis
29
+ */
30
+ interface CodeownerAnalysisOptions {
31
+ /** Target number of files per shard */
32
+ shardSize: number;
33
+ /** Optional path to CODEOWNERS file */
34
+ codeownersPath?: string;
35
+ /** Path to the codemod rule file */
36
+ rulePath: string;
37
+ /** Programming language for the codemod */
38
+ language: string;
39
+ /** Project root directory (defaults to process.cwd()) */
40
+ projectRoot?: string;
41
+ /** Existing state for consistency (optional) */
42
+ existingState?: ShardResult[];
43
+ }
44
+ /**
45
+ * Result of codeowner-based analysis
46
+ */
47
+ interface CodeownerAnalysisResult {
48
+ /** Array of team information */
49
+ teams: TeamFileInfo[];
50
+ /** Array of team-based shards with file assignments */
51
+ shards: ShardResult[];
52
+ /** Total number of files processed */
53
+ totalFiles: number;
54
+ }
55
+ /**
56
+ * Finds and resolves the CODEOWNERS file path
57
+ * Searches in common locations: root, .github/, docs/
58
+ * Returns null if no CODEOWNERS file is found
59
+ */
60
+ declare function findCodeownersFile(projectRoot?: string, explicitPath?: string): Promise<string | null>;
61
+ /**
62
+ * Normalizes owner name by removing `@` prefix and converting to lowercase
63
+ */
64
+ declare function normalizeOwnerName(owner: string): string;
65
+ /**
66
+ * Analyzes files and groups them by codeowner team
67
+ */
68
+ declare function analyzeFilesByOwner(codeownersPath: string, language: string, rulePath: string, projectRoot?: string): Promise<Map<string, string[]>>;
69
+ /**
70
+ * Analyzes files without codeowner parsing - assigns all files to "unassigned"
71
+ */
72
+ declare function analyzeFilesWithoutOwner(language: string, rulePath: string, projectRoot?: string): Promise<Map<string, string[]>>;
73
+ /**
74
+ * Generates shard configuration from team file analysis with actual file distribution.
75
+ * Maintains consistency with existing state when provided.
76
+ *
77
+ * @param filesByOwner - Map of team names to their file arrays
78
+ * @param shardSize - Target number of files per shard
79
+ * @param existingState - Optional existing state for consistency
80
+ * @returns Array of team-based shards with file assignments
81
+ */
82
+ declare function generateShards(filesByOwner: Map<string, string[]>, shardSize: number, existingState?: ShardResult[]): ShardResult[];
83
+ /**
84
+ * Converts file ownership map to team info array
85
+ */
86
+ declare function getTeamFileInfo(filesByOwner: Map<string, string[]>): TeamFileInfo[];
87
+ /**
88
+ * Main function to analyze codeowners and generate shard configuration.
89
+ * Maintains consistency with existing state when provided.
90
+ *
91
+ * @param options - Configuration options for codeowner analysis
92
+ * @returns Promise resolving to codeowner analysis result
93
+ */
94
+ declare function analyzeCodeowners(options: CodeownerAnalysisOptions): Promise<CodeownerAnalysisResult>;
95
+ //#endregion
96
+ export { CodeownerAnalysisOptions, CodeownerAnalysisResult, ShardResult, TeamFileInfo, analyzeCodeowners, analyzeFilesByOwner, analyzeFilesWithoutOwner, findCodeownersFile, generateShards, getTeamFileInfo, normalizeOwnerName };
@@ -0,0 +1,158 @@
1
+ #!/usr/bin/env node
2
+ import { t as getApplicableFiles } from "./codemod-cli-D-ormE6R.mjs";
3
+ import Codeowners from "codeowners";
4
+ import { existsSync } from "node:fs";
5
+ import path, { resolve } from "node:path";
6
+
7
+ //#region src/utils/codeowner-analysis.ts
8
+ /**
9
+ * Finds and resolves the CODEOWNERS file path
10
+ * Searches in common locations: root, .github/, docs/
11
+ * Returns null if no CODEOWNERS file is found
12
+ */
13
+ async function findCodeownersFile(projectRoot = process.cwd(), explicitPath) {
14
+ if (explicitPath) {
15
+ const resolvedPath = resolve(explicitPath);
16
+ if (!existsSync(resolvedPath)) throw new Error(`CODEOWNERS file not found at: ${resolvedPath}`);
17
+ return resolvedPath;
18
+ }
19
+ const searchPaths = [
20
+ resolve(projectRoot, "CODEOWNERS"),
21
+ resolve(projectRoot, ".github", "CODEOWNERS"),
22
+ resolve(projectRoot, "docs", "CODEOWNERS")
23
+ ];
24
+ for (const searchPath of searchPaths) if (existsSync(searchPath)) return searchPath;
25
+ return null;
26
+ }
27
+ /**
28
+ * Normalizes owner name by removing `@` prefix and converting to lowercase
29
+ */
30
+ function normalizeOwnerName(owner) {
31
+ return owner.replace("@", "").toLowerCase();
32
+ }
33
+ /**
34
+ * Analyzes files and groups them by codeowner team
35
+ */
36
+ async function analyzeFilesByOwner(codeownersPath, language, rulePath, projectRoot = process.cwd()) {
37
+ const codeowners = new Codeowners(codeownersPath);
38
+ const gitRootDir = codeowners.codeownersDirectory;
39
+ const filesByOwner = /* @__PURE__ */ new Map();
40
+ const applicableFiles = await getApplicableFiles(rulePath, language, projectRoot);
41
+ for (const filePath of applicableFiles) {
42
+ const absolutePath = path.resolve(projectRoot, filePath);
43
+ const relativePath = path.relative(gitRootDir, absolutePath);
44
+ const owners = codeowners.getOwner(relativePath);
45
+ let ownerKey;
46
+ if (owners && owners.length > 0) {
47
+ const owner = owners[0];
48
+ ownerKey = normalizeOwnerName(owner ?? "unknown");
49
+ } else ownerKey = "unassigned";
50
+ if (!filesByOwner.has(ownerKey)) filesByOwner.set(ownerKey, []);
51
+ filesByOwner.get(ownerKey)?.push(relativePath);
52
+ }
53
+ return filesByOwner;
54
+ }
55
+ /**
56
+ * Analyzes files without codeowner parsing - assigns all files to "unassigned"
57
+ */
58
+ async function analyzeFilesWithoutOwner(language, rulePath, projectRoot = process.cwd()) {
59
+ const filesByOwner = /* @__PURE__ */ new Map();
60
+ const unassignedFiles = (await getApplicableFiles(rulePath, language, projectRoot)).map((filePath) => {
61
+ return path.relative(projectRoot, path.resolve(projectRoot, filePath));
62
+ });
63
+ filesByOwner.set("unassigned", unassignedFiles);
64
+ return filesByOwner;
65
+ }
66
+ /**
67
+ * Calculate optimal number of shards based on target shard size
68
+ *
69
+ * @param totalFiles - Total number of files
70
+ * @param targetShardSize - Desired number of files per shard
71
+ * @returns Number of shards needed
72
+ */
73
+ function calculateOptimalShardCount(totalFiles, targetShardSize) {
74
+ return Math.ceil(totalFiles / targetShardSize);
75
+ }
76
+ /**
77
+ * Generates shard configuration from team file analysis with actual file distribution.
78
+ * Maintains consistency with existing state when provided.
79
+ *
80
+ * @param filesByOwner - Map of team names to their file arrays
81
+ * @param shardSize - Target number of files per shard
82
+ * @param existingState - Optional existing state for consistency
83
+ * @returns Array of team-based shards with file assignments
84
+ */
85
+ function generateShards(filesByOwner, shardSize, existingState) {
86
+ const allShards = [];
87
+ const existingByTeam = /* @__PURE__ */ new Map();
88
+ if (existingState) for (const shard of existingState) {
89
+ if (!existingByTeam.has(shard.team)) existingByTeam.set(shard.team, []);
90
+ existingByTeam.get(shard.team)?.push(shard);
91
+ }
92
+ for (const [team, files] of filesByOwner.entries()) {
93
+ const fileCount = files.length;
94
+ const optimalShardCount = calculateOptimalShardCount(fileCount, shardSize);
95
+ const existingShardCount = (existingByTeam.get(team) || []).length;
96
+ const numShards = existingShardCount > 0 ? existingShardCount : optimalShardCount;
97
+ console.log(`Team "${team}" owns ${fileCount} files, ${existingShardCount > 0 ? `maintaining ${numShards} existing shards` : `creating ${numShards} new shards`}`);
98
+ const sortedFiles = [...files].sort();
99
+ for (let i = 1; i <= numShards; i++) {
100
+ const shardFiles = [];
101
+ for (let fileIndex = i - 1; fileIndex < sortedFiles.length; fileIndex += numShards) shardFiles.push(sortedFiles[fileIndex] ?? "");
102
+ allShards.push({
103
+ team,
104
+ shard: `${i}/${numShards}`,
105
+ shardId: `${team} ${i}/${numShards}`,
106
+ _meta_files: shardFiles
107
+ });
108
+ }
109
+ }
110
+ return allShards;
111
+ }
112
+ /**
113
+ * Converts file ownership map to team info array
114
+ */
115
+ function getTeamFileInfo(filesByOwner) {
116
+ return Array.from(filesByOwner.entries()).map(([team, files]) => ({
117
+ team,
118
+ fileCount: files.length,
119
+ files
120
+ }));
121
+ }
122
+ /**
123
+ * Main function to analyze codeowners and generate shard configuration.
124
+ * Maintains consistency with existing state when provided.
125
+ *
126
+ * @param options - Configuration options for codeowner analysis
127
+ * @returns Promise resolving to codeowner analysis result
128
+ */
129
+ async function analyzeCodeowners(options) {
130
+ const { shardSize, codeownersPath, rulePath, language, projectRoot = process.cwd(), existingState } = options;
131
+ const resolvedCodeownersPath = await findCodeownersFile(projectRoot, codeownersPath);
132
+ let filesByOwner;
133
+ console.debug(`Using rule file: ${rulePath}`);
134
+ console.debug(`Shard size: ${shardSize}`);
135
+ if (resolvedCodeownersPath) {
136
+ console.log(`Analyzing CODEOWNERS file: ${resolvedCodeownersPath}`);
137
+ console.log("Analyzing files with CLI command...");
138
+ filesByOwner = await analyzeFilesByOwner(resolvedCodeownersPath, language, rulePath, projectRoot);
139
+ } else {
140
+ console.log("No CODEOWNERS file found, assigning all files to 'unassigned'");
141
+ console.log("Analyzing files with CLI command...");
142
+ filesByOwner = await analyzeFilesWithoutOwner(language, rulePath, projectRoot);
143
+ }
144
+ console.log("File analysis completed. Generating shards...");
145
+ if (existingState) console.debug(`Using existing state with ${existingState.length} shards`);
146
+ const teams = getTeamFileInfo(filesByOwner);
147
+ const shards = generateShards(filesByOwner, shardSize, existingState);
148
+ const totalFiles = Array.from(filesByOwner.values()).reduce((sum, files) => sum + files.length, 0);
149
+ console.log(`Generated ${shards.length} total shards for ${totalFiles} files`);
150
+ return {
151
+ teams,
152
+ shards,
153
+ totalFiles
154
+ };
155
+ }
156
+
157
+ //#endregion
158
+ export { analyzeCodeowners, analyzeFilesByOwner, analyzeFilesWithoutOwner, findCodeownersFile, generateShards, getTeamFileInfo, normalizeOwnerName };
@@ -0,0 +1,73 @@
1
+
2
+ //#region src/utils/directory-analysis.d.ts
3
+ /**
4
+ * Result for a single directory-based shard
5
+ */
6
+ interface DirectoryShardResult {
7
+ /** The directory path this shard belongs to */
8
+ directory: string;
9
+ /** The shard number (1-based) within this directory */
10
+ shard: number;
11
+ /** Total number of shards for this directory */
12
+ shardCount: number;
13
+ /** Array of file paths in this shard */
14
+ _meta_files: string[];
15
+ /** The name of the shard */
16
+ name: string;
17
+ }
18
+ /**
19
+ * Options for directory-based analysis
20
+ */
21
+ interface DirectoryAnalysisOptions {
22
+ /** Target number of files per shard */
23
+ shardSize: number;
24
+ /** Target directory to analyze subdirectories within */
25
+ target: string;
26
+ /** Path to the codemod rule file */
27
+ rulePath: string;
28
+ /** Programming language for the codemod */
29
+ language: string;
30
+ /** Project root directory (defaults to process.cwd()) */
31
+ projectRoot?: string;
32
+ /** Existing state for consistency (optional) */
33
+ existingState?: DirectoryShardResult[];
34
+ }
35
+ /**
36
+ * Result of directory-based analysis
37
+ */
38
+ interface DirectoryAnalysisResult {
39
+ /** Array of directory-based shards */
40
+ shards: DirectoryShardResult[];
41
+ /** Total number of files processed */
42
+ totalFiles: number;
43
+ }
44
+ /**
45
+ * Groups files by their immediate subdirectory within the target directory
46
+ *
47
+ * @param files - Array of file paths to group
48
+ * @param target - Target directory to analyze subdirectories within
49
+ * @param projectRoot - Root directory of the project for resolving relative paths
50
+ * @returns Map of subdirectory paths to their file lists
51
+ */
52
+ declare function groupFilesByDirectory(files: string[], target: string, projectRoot: string): Map<string, string[]>;
53
+ /**
54
+ * Creates directory-based shards using consistent hashing within each directory group.
55
+ * Maintains consistency with existing state when provided.
56
+ *
57
+ * @param filesByDirectory - Map of directory paths to their file lists
58
+ * @param shardSize - Target number of files per shard
59
+ * @param existingState - Optional existing state for consistency
60
+ * @returns Array of directory-based shards
61
+ */
62
+ declare function createDirectoryShards(filesByDirectory: Map<string, string[]>, shardSize: number, existingState?: DirectoryShardResult[]): DirectoryShardResult[];
63
+ /**
64
+ * Main function to analyze directories and generate shard configuration.
65
+ * Maintains consistency with existing state when provided.
66
+ *
67
+ * @param options - Configuration options for directory analysis
68
+ * @returns Promise resolving to directory analysis result
69
+ * @throws Error if no files found in target subdirectories
70
+ */
71
+ declare function analyzeDirectories(options: DirectoryAnalysisOptions): Promise<DirectoryAnalysisResult>;
72
+ //#endregion
73
+ export { DirectoryAnalysisOptions, DirectoryAnalysisResult, DirectoryShardResult, analyzeDirectories, createDirectoryShards, groupFilesByDirectory };
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env node
2
+ import { t as getApplicableFiles } from "./codemod-cli-D-ormE6R.mjs";
3
+ import { calculateOptimalShardCount, distributeFilesAcrossShards } from "./sharding.mjs";
4
+ import path from "node:path";
5
+
6
+ //#region src/utils/directory-analysis.ts
7
+ /**
8
+ * Groups files by their immediate subdirectory within the target directory
9
+ *
10
+ * @param files - Array of file paths to group
11
+ * @param target - Target directory to analyze subdirectories within
12
+ * @param projectRoot - Root directory of the project for resolving relative paths
13
+ * @returns Map of subdirectory paths to their file lists
14
+ */
15
+ function groupFilesByDirectory(files, target, projectRoot) {
16
+ const resolvedTarget = path.resolve(projectRoot, target);
17
+ const filesByDirectory = /* @__PURE__ */ new Map();
18
+ for (const filePath of files) {
19
+ const normalizedFile = path.normalize(filePath);
20
+ const resolvedFile = path.resolve(projectRoot, normalizedFile);
21
+ if (!resolvedFile.startsWith(resolvedTarget)) continue;
22
+ const relativePath = path.relative(projectRoot, resolvedFile);
23
+ const relativeFromTarget = path.relative(resolvedTarget, resolvedFile);
24
+ if (!relativeFromTarget.includes(path.sep)) continue;
25
+ const firstDir = relativeFromTarget.split(path.sep)[0];
26
+ if (!firstDir) continue;
27
+ const subdirectory = path.relative(projectRoot, path.join(resolvedTarget, firstDir));
28
+ if (!filesByDirectory.has(subdirectory)) filesByDirectory.set(subdirectory, []);
29
+ filesByDirectory.get(subdirectory)?.push(relativePath);
30
+ }
31
+ return filesByDirectory;
32
+ }
33
+ /**
34
+ * Creates directory-based shards using consistent hashing within each directory group.
35
+ * Maintains consistency with existing state when provided.
36
+ *
37
+ * @param filesByDirectory - Map of directory paths to their file lists
38
+ * @param shardSize - Target number of files per shard
39
+ * @param existingState - Optional existing state for consistency
40
+ * @returns Array of directory-based shards
41
+ */
42
+ function createDirectoryShards(filesByDirectory, shardSize, existingState) {
43
+ const allShards = [];
44
+ const existingByDirectory = /* @__PURE__ */ new Map();
45
+ if (existingState) for (const shard of existingState) {
46
+ if (!existingByDirectory.has(shard.directory)) existingByDirectory.set(shard.directory, []);
47
+ existingByDirectory.get(shard.directory)?.push(shard);
48
+ }
49
+ for (const [directory, files] of filesByDirectory.entries()) {
50
+ const fileCount = files.length;
51
+ const optimalShardCount = calculateOptimalShardCount(fileCount, shardSize);
52
+ const existingShards = existingByDirectory.get(directory) || [];
53
+ const existingShardCount = existingShards.length > 0 ? existingShards[0]?.shardCount ?? 0 : 0;
54
+ const shardCount = existingShardCount > 0 ? existingShardCount : optimalShardCount;
55
+ console.log(`Directory "${directory}" contains ${fileCount} files, ${existingShardCount > 0 ? `maintaining ${shardCount} existing shards` : `creating ${shardCount} new shards`}`);
56
+ const shardMap = distributeFilesAcrossShards(files, shardCount);
57
+ for (let shardIndex = 0; shardIndex < shardCount; shardIndex++) {
58
+ const shardFiles = shardMap.get(shardIndex) || [];
59
+ allShards.push({
60
+ directory,
61
+ shard: shardIndex + 1,
62
+ shardCount,
63
+ _meta_files: shardFiles.sort(),
64
+ name: `${directory} (${shardIndex + 1}/${shardCount})`
65
+ });
66
+ }
67
+ }
68
+ return allShards;
69
+ }
70
+ /**
71
+ * Main function to analyze directories and generate shard configuration.
72
+ * Maintains consistency with existing state when provided.
73
+ *
74
+ * @param options - Configuration options for directory analysis
75
+ * @returns Promise resolving to directory analysis result
76
+ * @throws Error if no files found in target subdirectories
77
+ */
78
+ async function analyzeDirectories(options) {
79
+ const { shardSize, target, rulePath, language, projectRoot = process.cwd(), existingState } = options;
80
+ if (existingState) console.debug(`Using existing state with ${existingState.length} shards`);
81
+ console.log("Analyzing files with CLI command...");
82
+ const applicableFiles = await getApplicableFiles(rulePath, language, projectRoot);
83
+ console.log("Grouping files by directory...");
84
+ const filesByDirectory = groupFilesByDirectory(applicableFiles, path.resolve(projectRoot, target), projectRoot);
85
+ if (filesByDirectory.size === 0) throw new Error(`No files found in subdirectories of target: ${target}`);
86
+ console.log(`Found ${filesByDirectory.size} subdirectories in target`);
87
+ console.log("Generating directory-based shards...");
88
+ const shards = createDirectoryShards(filesByDirectory, shardSize, existingState);
89
+ const totalFiles = Array.from(filesByDirectory.values()).reduce((sum, files) => sum + files.length, 0);
90
+ console.log(`Generated ${shards.length} total shards for ${totalFiles} files across ${filesByDirectory.size} directories`);
91
+ return {
92
+ shards,
93
+ totalFiles
94
+ };
95
+ }
96
+
97
+ //#endregion
98
+ export { analyzeDirectories, createDirectoryShards, groupFilesByDirectory };
@@ -0,0 +1,4 @@
1
+
2
+ import { analyzeShardScaling, calculateOptimalShardCount, distributeFilesAcrossShards, fitsInShard, getFileHashPosition, getNumericFileNameSha1, getShardForFilename } from "./sharding.mjs";
3
+ import { CodeownerAnalysisOptions, CodeownerAnalysisResult, ShardResult, TeamFileInfo, analyzeCodeowners, analyzeFilesByOwner, analyzeFilesWithoutOwner, findCodeownersFile, generateShards, getTeamFileInfo, normalizeOwnerName } from "./codeowners.mjs";
4
+ export { CodeownerAnalysisOptions, CodeownerAnalysisResult, ShardResult, TeamFileInfo, analyzeCodeowners, analyzeFilesByOwner, analyzeFilesWithoutOwner, analyzeShardScaling, calculateOptimalShardCount, distributeFilesAcrossShards, findCodeownersFile, fitsInShard, generateShards, getFileHashPosition, getNumericFileNameSha1, getShardForFilename, getTeamFileInfo, normalizeOwnerName };
package/dist/index.mjs ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+ import { analyzeCodeowners, analyzeFilesByOwner, analyzeFilesWithoutOwner, findCodeownersFile, generateShards, getTeamFileInfo, normalizeOwnerName } from "./codeowners.mjs";
3
+ import { analyzeShardScaling, calculateOptimalShardCount, distributeFilesAcrossShards, fitsInShard, getFileHashPosition, getNumericFileNameSha1, getShardForFilename } from "./sharding.mjs";
4
+
5
+ export { analyzeCodeowners, analyzeFilesByOwner, analyzeFilesWithoutOwner, analyzeShardScaling, calculateOptimalShardCount, distributeFilesAcrossShards, findCodeownersFile, fitsInShard, generateShards, getFileHashPosition, getNumericFileNameSha1, getShardForFilename, getTeamFileInfo, normalizeOwnerName };
@@ -0,0 +1,68 @@
1
+
2
+ //#region src/utils/consistent-sharding.d.ts
3
+ /**
4
+ * Generates a numeric hash from a filename using SHA1
5
+ * Uses only the first 8 characters of the hex digest to avoid JavaScript number precision issues
6
+ */
7
+ declare function getNumericFileNameSha1(filename: string): number;
8
+ /**
9
+ * Maps a filename to a consistent position on the hash ring (0 to HASH_RING_SIZE-1)
10
+ * This position remains constant regardless of shard count changes
11
+ */
12
+ declare function getFileHashPosition(filename: string): number;
13
+ /**
14
+ * Gets the shard index for a filename using consistent hashing
15
+ * Files are assigned to the next shard clockwise on the hash ring
16
+ *
17
+ * @param filename - The file path to hash
18
+ * @param shardCount - Total number of shards
19
+ * @returns Shard index (0-based)
20
+ */
21
+ declare function getShardForFilename(filename: string, {
22
+ shardCount
23
+ }: {
24
+ shardCount: number;
25
+ }): number;
26
+ /**
27
+ * Checks if a file belongs to a specific shard by simply checking if it's in the shard's files list
28
+ *
29
+ * @param filename - The file path to check
30
+ * @param shard - Shard object containing files array
31
+ * @returns True if file is in the shard's files list
32
+ */
33
+ declare function fitsInShard(filename: string, shard: {
34
+ _meta_files: string[];
35
+ }): boolean;
36
+ /**
37
+ * Distributes files across shards using deterministic hashing
38
+ *
39
+ * @param filenames - Array of file paths
40
+ * @param shardCount - Total number of shards
41
+ * @returns Map of shard index to array of filenames
42
+ */
43
+ declare function distributeFilesAcrossShards(filenames: string[], shardCount: number): Map<number, string[]>;
44
+ /**
45
+ * Calculate optimal number of shards based on target shard size
46
+ *
47
+ * @param totalFiles - Total number of files
48
+ * @param targetShardSize - Desired number of files per shard
49
+ * @returns Number of shards needed
50
+ */
51
+ declare function calculateOptimalShardCount(totalFiles: number, targetShardSize: number): number;
52
+ /**
53
+ * Analyzes file reassignment when scaling from oldShardCount to newShardCount
54
+ * Returns statistics about how many files would need to be reassigned
55
+ *
56
+ * @param filenames - Array of file paths to analyze
57
+ * @param oldShardCount - Current number of shards
58
+ * @param newShardCount - Target number of shards
59
+ * @returns Object with reassignment statistics
60
+ */
61
+ declare function analyzeShardScaling(filenames: string[], oldShardCount: number, newShardCount: number): {
62
+ totalFiles: number;
63
+ reassignedFiles: number;
64
+ reassignmentPercentage: number;
65
+ stableFiles: number;
66
+ };
67
+ //#endregion
68
+ export { analyzeShardScaling, calculateOptimalShardCount, distributeFilesAcrossShards, fitsInShard, getFileHashPosition, getNumericFileNameSha1, getShardForFilename };
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env node
2
+ import crypto from "node:crypto";
3
+
4
+ //#region src/utils/consistent-sharding.ts
5
+ const HASH_RING_SIZE = 1e6;
6
+ /**
7
+ * Generates a numeric hash from a filename using SHA1
8
+ * Uses only the first 8 characters of the hex digest to avoid JavaScript number precision issues
9
+ */
10
+ function getNumericFileNameSha1(filename) {
11
+ const hex = crypto.createHash("sha1").update(filename).digest("hex").substring(0, 8);
12
+ return parseInt(hex, 16);
13
+ }
14
+ /**
15
+ * Maps a filename to a consistent position on the hash ring (0 to HASH_RING_SIZE-1)
16
+ * This position remains constant regardless of shard count changes
17
+ */
18
+ function getFileHashPosition(filename) {
19
+ return getNumericFileNameSha1(filename) % HASH_RING_SIZE;
20
+ }
21
+ /**
22
+ * Get the position for a specific shard index on the hash ring
23
+ * Shards get fixed positions that don't change when other shards are added
24
+ */
25
+ function getShardPosition(shardIndex) {
26
+ return parseInt(crypto.createHash("sha1").update(`shard-${shardIndex}`).digest("hex").substring(0, 8), 16) % HASH_RING_SIZE;
27
+ }
28
+ /**
29
+ * Gets the shard index for a filename using consistent hashing
30
+ * Files are assigned to the next shard clockwise on the hash ring
31
+ *
32
+ * @param filename - The file path to hash
33
+ * @param shardCount - Total number of shards
34
+ * @returns Shard index (0-based)
35
+ */
36
+ function getShardForFilename(filename, { shardCount }) {
37
+ if (shardCount <= 0) throw new Error("Shard count must be greater than 0");
38
+ const filePosition = getFileHashPosition(filename);
39
+ const shardInfo = [];
40
+ for (let i = 0; i < shardCount; i++) shardInfo.push({
41
+ index: i,
42
+ position: getShardPosition(i)
43
+ });
44
+ shardInfo.sort((a, b) => a.position - b.position);
45
+ for (const shard of shardInfo) if (filePosition <= shard.position) return shard.index;
46
+ return shardInfo[0]?.index ?? 0;
47
+ }
48
+ /**
49
+ * Checks if a file belongs to a specific shard by simply checking if it's in the shard's files list
50
+ *
51
+ * @param filename - The file path to check
52
+ * @param shard - Shard object containing files array
53
+ * @returns True if file is in the shard's files list
54
+ */
55
+ function fitsInShard(filename, shard) {
56
+ return shard._meta_files.includes(filename);
57
+ }
58
+ /**
59
+ * Distributes files across shards using deterministic hashing
60
+ *
61
+ * @param filenames - Array of file paths
62
+ * @param shardCount - Total number of shards
63
+ * @returns Map of shard index to array of filenames
64
+ */
65
+ function distributeFilesAcrossShards(filenames, shardCount) {
66
+ if (shardCount <= 0) throw new Error("Shard count must be greater than 0");
67
+ const shardMap = /* @__PURE__ */ new Map();
68
+ for (let i = 0; i < shardCount; i++) shardMap.set(i, []);
69
+ for (const filename of filenames) {
70
+ const shardIndex = getShardForFilename(filename, { shardCount });
71
+ shardMap.get(shardIndex)?.push(filename);
72
+ }
73
+ return shardMap;
74
+ }
75
+ /**
76
+ * Calculate optimal number of shards based on target shard size
77
+ *
78
+ * @param totalFiles - Total number of files
79
+ * @param targetShardSize - Desired number of files per shard
80
+ * @returns Number of shards needed
81
+ */
82
+ function calculateOptimalShardCount(totalFiles, targetShardSize) {
83
+ return Math.ceil(totalFiles / targetShardSize);
84
+ }
85
+ /**
86
+ * Analyzes file reassignment when scaling from oldShardCount to newShardCount
87
+ * Returns statistics about how many files would need to be reassigned
88
+ *
89
+ * @param filenames - Array of file paths to analyze
90
+ * @param oldShardCount - Current number of shards
91
+ * @param newShardCount - Target number of shards
92
+ * @returns Object with reassignment statistics
93
+ */
94
+ function analyzeShardScaling(filenames, oldShardCount, newShardCount) {
95
+ let reassignedFiles = 0;
96
+ for (const filename of filenames) if (getShardForFilename(filename, { shardCount: oldShardCount }) !== getShardForFilename(filename, { shardCount: newShardCount })) reassignedFiles++;
97
+ const stableFiles = filenames.length - reassignedFiles;
98
+ const reassignmentPercentage = filenames.length > 0 ? reassignedFiles / filenames.length * 100 : 0;
99
+ return {
100
+ totalFiles: filenames.length,
101
+ reassignedFiles,
102
+ reassignmentPercentage,
103
+ stableFiles
104
+ };
105
+ }
106
+
107
+ //#endregion
108
+ export { analyzeShardScaling, calculateOptimalShardCount, distributeFilesAcrossShards, fitsInShard, getFileHashPosition, getNumericFileNameSha1, getShardForFilename };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codemodctl",
3
- "version": "0.1.27",
3
+ "version": "0.1.29",
4
4
  "description": "CLI tool and utilities for workflow engine operations, file sharding, and codeowner analysis",
5
5
  "type": "module",
6
6
  "exports": {
@@ -33,9 +33,9 @@
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/node": "^22.16.5",
36
- "@typescript/native-preview": "7.0.0-dev.20251120.1",
36
+ "@typescript/native-preview": "7.0.0-dev.20260122.3",
37
37
  "oxlint": "^1.29.0",
38
- "tsdown": "^0.15.4",
38
+ "tsdown": "0.20.3",
39
39
  "typescript": "^5.9.3",
40
40
  "vitest": "^4.0.9",
41
41
  "@acme/tsconfig": "0.1.0"