@taranek/orche 0.0.8

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.
@@ -0,0 +1,121 @@
1
+ import { execSync } from "node:child_process";
2
+ import {
3
+ existsSync,
4
+ readFileSync,
5
+ appendFileSync,
6
+ mkdirSync,
7
+ symlinkSync,
8
+ readdirSync,
9
+ lstatSync,
10
+ } from "node:fs";
11
+ import path from "node:path";
12
+
13
+ export interface WorktreeInfo {
14
+ worktreePath: string;
15
+ branchName: string;
16
+ }
17
+
18
+ function slugify(text: string): string {
19
+ return text
20
+ .toLowerCase()
21
+ .replace(/[^a-z0-9]+/g, "-")
22
+ .replace(/^-|-$/g, "")
23
+ .slice(0, 50);
24
+ }
25
+
26
+ function ensureGitignore(repoPath: string, pattern: string): void {
27
+ const gitignorePath = path.join(repoPath, ".gitignore");
28
+ let content = "";
29
+ if (existsSync(gitignorePath)) {
30
+ content = readFileSync(gitignorePath, "utf-8");
31
+ }
32
+ if (!content.includes(pattern)) {
33
+ appendFileSync(gitignorePath, `\n# Agent worktrees\n${pattern}\n`);
34
+ }
35
+ }
36
+
37
+ function ensureGitRepo(repoPath: string): void {
38
+ try {
39
+ execSync("git rev-parse --git-dir", { cwd: repoPath, stdio: "ignore" });
40
+ } catch {
41
+ execSync(
42
+ 'git init && git add -A && git commit -m "Initial commit" --allow-empty',
43
+ { cwd: repoPath, stdio: "ignore" }
44
+ );
45
+ }
46
+ }
47
+
48
+ export function createWorktree(repoPath: string, name: string): WorktreeInfo {
49
+ ensureGitRepo(repoPath);
50
+
51
+ const slug = slugify(name);
52
+ const timestamp = Date.now();
53
+ const branchName = `agent/${slug}-${timestamp}`;
54
+
55
+ const worktreesDir = path.join(repoPath, ".orche", "worktrees");
56
+ const worktreePath = path.join(worktreesDir, `${slug}-${timestamp}`);
57
+
58
+ mkdirSync(worktreesDir, { recursive: true });
59
+ ensureGitignore(repoPath, ".orche/");
60
+
61
+ execSync(`git worktree add -b "${branchName}" "${worktreePath}"`, {
62
+ cwd: repoPath,
63
+ stdio: "ignore",
64
+ });
65
+
66
+ symlinkNodeModules(repoPath, worktreePath);
67
+
68
+ console.log(` worktree: ${worktreePath} (${branchName})`);
69
+ return { worktreePath, branchName };
70
+ }
71
+
72
+ function symlinkNodeModules(repoPath: string, worktreePath: string): void {
73
+ // Symlink root node_modules
74
+ const rootNm = path.join(repoPath, "node_modules");
75
+ const targetNm = path.join(worktreePath, "node_modules");
76
+ if (existsSync(rootNm) && !existsSync(targetNm)) {
77
+ symlinkSync(rootNm, targetNm);
78
+ }
79
+
80
+ // Symlink node_modules in subdirectories (monorepo packages)
81
+ symlinkNestedNodeModules(repoPath, worktreePath, repoPath);
82
+ }
83
+
84
+ function symlinkNestedNodeModules(
85
+ dir: string,
86
+ worktreePath: string,
87
+ repoPath: string
88
+ ): void {
89
+ let entries;
90
+ try {
91
+ entries = readdirSync(dir, { withFileTypes: true });
92
+ } catch {
93
+ return;
94
+ }
95
+
96
+ for (const entry of entries) {
97
+ if (!entry.isDirectory()) continue;
98
+ if (entry.name === "node_modules" || entry.name === ".git" || entry.name === ".orche") continue;
99
+
100
+ const fullPath = path.join(dir, entry.name);
101
+
102
+ // Skip symlinks
103
+ try {
104
+ if (lstatSync(fullPath).isSymbolicLink()) continue;
105
+ } catch {
106
+ continue;
107
+ }
108
+
109
+ const nm = path.join(fullPath, "node_modules");
110
+ if (existsSync(nm)) {
111
+ const rel = path.relative(repoPath, fullPath);
112
+ const targetDir = path.join(worktreePath, rel);
113
+ const targetNm = path.join(targetDir, "node_modules");
114
+ if (!existsSync(targetNm) && existsSync(targetDir)) {
115
+ symlinkSync(nm, targetNm);
116
+ }
117
+ }
118
+
119
+ symlinkNestedNodeModules(fullPath, worktreePath, repoPath);
120
+ }
121
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "NodeNext",
5
+ "moduleResolution": "NodeNext",
6
+ "outDir": "dist",
7
+ "rootDir": "src",
8
+ "strict": true,
9
+ "skipLibCheck": true,
10
+ "resolveJsonModule": true,
11
+ "declaration": true
12
+ },
13
+ "include": ["src"]
14
+ }