-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
794 lines (688 loc) · 26.3 KB
/
Copy pathindex.js
File metadata and controls
794 lines (688 loc) · 26.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
const {
fs,
path,
PACKAGE_ROOT,
TOOL_NAME,
SHORT_TOOL_NAME,
GUARDEX_HOME_DIR,
AGENT_WORKTREE_RELATIVE_DIRS,
TEMPLATE_ROOT,
HOOK_NAMES,
LOCK_FILE_RELATIVE,
LEGACY_MANAGED_PACKAGE_SCRIPTS,
PACKAGE_ROOT_SOURCE_OVERRIDES,
USER_LEVEL_SKILL_ASSETS,
AGENTS_MARKER_START,
AGENTS_MARKER_END,
GITIGNORE_MARKER_START,
GITIGNORE_MARKER_END,
SHARED_VSCODE_SETTINGS_RELATIVE,
REPO_SCAN_IGNORED_FOLDERS_SETTING,
MANAGED_REPO_SCAN_IGNORED_FOLDERS,
REPO_SCAFFOLD_DIRECTORIES,
OMX_SCAFFOLD_DIRECTORIES,
OMX_SCAFFOLD_FILES,
toDestinationPath,
EXECUTABLE_RELATIVE_PATHS,
CRITICAL_GUARDRAIL_PATHS,
} = require('../context');
const { parse: parseJsonc, printParseErrorCode } = require('jsonc-parser');
const { run } = require('../core/runtime');
function ensureParentDir(repoRoot, filePath, dryRun) {
if (dryRun) return;
const parentDir = path.dirname(filePath);
const relativeParentDir = path.relative(repoRoot, parentDir);
const segments = relativeParentDir.split(path.sep).filter(Boolean);
let currentPath = repoRoot;
for (const segment of segments) {
currentPath = path.join(currentPath, segment);
if (fs.existsSync(currentPath) && !fs.statSync(currentPath).isDirectory()) {
const blockingPath = path.relative(repoRoot, currentPath) || path.basename(currentPath);
const targetPath = path.relative(repoRoot, filePath) || path.basename(filePath);
throw new Error(
`Path conflict: ${blockingPath} exists as a file, but ${targetPath} needs it to be a directory. ` +
`Remove or rename ${blockingPath} and rerun '${SHORT_TOOL_NAME} setup'.`,
);
}
}
fs.mkdirSync(parentDir, { recursive: true });
}
function ensureExecutable(destinationPath, relativePath, dryRun) {
if (dryRun) return;
if (EXECUTABLE_RELATIVE_PATHS.has(relativePath)) {
fs.chmodSync(destinationPath, 0o755);
}
}
function isCriticalGuardrailPath(relativePath) {
return CRITICAL_GUARDRAIL_PATHS.has(relativePath);
}
function shellSingleQuote(value) {
return `'${String(value).replace(/'/g, `'\"'\"'`)}'`;
}
function renderShellDispatchShim(commandParts) {
const rendered = commandParts.map((part) => shellSingleQuote(part)).join(' ');
return (
'#!/usr/bin/env bash\n' +
'set -euo pipefail\n' +
'\n' +
'if [[ -n "${GUARDEX_CLI_ENTRY:-}" ]]; then\n' +
' node_bin="${GUARDEX_NODE_BIN:-node}"\n' +
` exec "$node_bin" "$GUARDEX_CLI_ENTRY" ${rendered} "$@"\n` +
'fi\n' +
'\n' +
'resolve_guardex_cli() {\n' +
' if [[ -n "${GUARDEX_CLI_BIN:-}" ]]; then\n' +
' printf \'%s\' "$GUARDEX_CLI_BIN"\n' +
' return 0\n' +
' fi\n' +
' if command -v gx >/dev/null 2>&1; then\n' +
' printf \'%s\' "gx"\n' +
' return 0\n' +
' fi\n' +
' if command -v gitguardex >/dev/null 2>&1; then\n' +
' printf \'%s\' "gitguardex"\n' +
' return 0\n' +
' fi\n' +
' echo "[gitguardex-shim] Missing gx CLI in PATH." >&2\n' +
' exit 1\n' +
'}\n' +
'\n' +
'cli_bin="$(resolve_guardex_cli)"\n' +
`exec "$cli_bin" ${rendered} "$@"\n`
);
}
function renderPythonDispatchShim(commandParts) {
return (
'#!/usr/bin/env python3\n' +
'import os\n' +
'import shutil\n' +
'import subprocess\n' +
'import sys\n' +
'\n' +
`COMMAND = ${JSON.stringify(commandParts)}\n` +
'\n' +
'entry = os.environ.get("GUARDEX_CLI_ENTRY")\n' +
'if entry:\n' +
' node_bin = os.environ.get("GUARDEX_NODE_BIN") or shutil.which("node") or "node"\n' +
' raise SystemExit(subprocess.call([node_bin, entry, *COMMAND, *sys.argv[1:]]))\n' +
'cli = os.environ.get("GUARDEX_CLI_BIN") or shutil.which("gx") or shutil.which("gitguardex")\n' +
'if not cli:\n' +
' sys.stderr.write("[gitguardex-shim] Missing gx CLI in PATH.\\n")\n' +
' raise SystemExit(1)\n' +
'raise SystemExit(subprocess.call([cli, *COMMAND, *sys.argv[1:]]))\n'
);
}
function managedForceConflictMessage(relativePath) {
return (
`Refusing to overwrite existing file without --force: ${relativePath}\n` +
`Use '--force ${relativePath}' to rewrite only this managed file, or '--force' to rewrite all managed files.`
);
}
function renderManagedFile(repoRoot, relativePath, content, options = {}) {
const destinationPath = path.join(repoRoot, relativePath);
const destinationExists = fs.existsSync(destinationPath);
const force = Boolean(options.force);
const dryRun = Boolean(options.dryRun);
if (destinationExists) {
const existingContent = fs.readFileSync(destinationPath, 'utf8');
if (existingContent === content) {
ensureExecutable(destinationPath, relativePath, dryRun);
return { status: 'unchanged', file: relativePath };
}
if (!force && !isCriticalGuardrailPath(relativePath)) {
throw new Error(managedForceConflictMessage(relativePath));
}
}
ensureParentDir(repoRoot, destinationPath, dryRun);
if (!dryRun) {
fs.writeFileSync(destinationPath, content, 'utf8');
ensureExecutable(destinationPath, relativePath, dryRun);
}
if (destinationExists && !force && isCriticalGuardrailPath(relativePath)) {
return { status: dryRun ? 'would-repair-critical' : 'repaired-critical', file: relativePath };
}
return { status: destinationExists ? 'overwritten' : 'created', file: relativePath };
}
function ensureGeneratedScriptShim(repoRoot, spec, options = {}) {
const content = spec.kind === 'python'
? renderPythonDispatchShim(spec.command)
: renderShellDispatchShim(spec.command);
return renderManagedFile(repoRoot, spec.relativePath, content, options);
}
function ensureHookShim(repoRoot, hookName, options = {}) {
return renderManagedFile(
repoRoot,
path.posix.join('.githooks', hookName),
renderShellDispatchShim(['hook', 'run', hookName]),
options,
);
}
function copyManagedSourceFile(repoRoot, sourcePath, destinationPath, destinationRelativePath, force, dryRun) {
const sourceContent = fs.readFileSync(sourcePath);
const destinationExists = fs.existsSync(destinationPath);
if (destinationExists) {
const existingContent = fs.readFileSync(destinationPath);
if (existingContent.equals(sourceContent)) {
ensureExecutable(destinationPath, destinationRelativePath, dryRun);
return { status: 'unchanged', file: destinationRelativePath };
}
if (!force && !isCriticalGuardrailPath(destinationRelativePath)) {
throw new Error(managedForceConflictMessage(destinationRelativePath));
}
}
ensureParentDir(repoRoot, destinationPath, dryRun);
if (!dryRun) {
fs.writeFileSync(destinationPath, sourceContent);
ensureExecutable(destinationPath, destinationRelativePath, dryRun);
}
if (destinationExists && !force && isCriticalGuardrailPath(destinationRelativePath)) {
return { status: dryRun ? 'would-repair-critical' : 'repaired-critical', file: destinationRelativePath };
}
return { status: destinationExists ? 'overwritten' : 'created', file: destinationRelativePath };
}
function normalizeTemplatePath(relativeTemplatePath) {
return String(relativeTemplatePath).replace(/\\/g, '/');
}
function usesPackageRootSource(repoRoot, relativeTemplatePath) {
return (
path.resolve(repoRoot) === PACKAGE_ROOT &&
PACKAGE_ROOT_SOURCE_OVERRIDES.has(normalizeTemplatePath(relativeTemplatePath))
);
}
function resolveTemplateSourcePath(repoRoot, relativeTemplatePath) {
if (usesPackageRootSource(repoRoot, relativeTemplatePath)) {
return path.join(PACKAGE_ROOT, relativeTemplatePath);
}
return path.join(TEMPLATE_ROOT, relativeTemplatePath);
}
function copyTemplateFile(repoRoot, relativeTemplatePath, force, dryRun) {
const sourcePath = resolveTemplateSourcePath(repoRoot, relativeTemplatePath);
const destinationRelativePath = toDestinationPath(relativeTemplatePath);
const destinationPath = path.join(repoRoot, destinationRelativePath);
return copyManagedSourceFile(
repoRoot,
sourcePath,
destinationPath,
destinationRelativePath,
force,
dryRun,
);
}
function ensureTemplateFilePresent(repoRoot, relativeTemplatePath, dryRun) {
const sourcePath = resolveTemplateSourcePath(repoRoot, relativeTemplatePath);
const destinationRelativePath = toDestinationPath(relativeTemplatePath);
const destinationPath = path.join(repoRoot, destinationRelativePath);
const sourceContent = fs.readFileSync(sourcePath);
if (fs.existsSync(destinationPath)) {
const existingContent = fs.readFileSync(destinationPath);
if (existingContent.equals(sourceContent)) {
ensureExecutable(destinationPath, destinationRelativePath, dryRun);
return { status: 'unchanged', file: destinationRelativePath };
}
if (isCriticalGuardrailPath(destinationRelativePath)) {
if (!dryRun) {
fs.writeFileSync(destinationPath, sourceContent);
ensureExecutable(destinationPath, destinationRelativePath, dryRun);
}
return { status: dryRun ? 'would-repair-critical' : 'repaired-critical', file: destinationRelativePath };
}
return { status: 'skipped-conflict', file: destinationRelativePath };
}
ensureParentDir(repoRoot, destinationPath, dryRun);
if (!dryRun) {
fs.writeFileSync(destinationPath, sourceContent);
ensureExecutable(destinationPath, destinationRelativePath, dryRun);
}
return { status: 'created', file: destinationRelativePath };
}
function materializePackageRepoTemplateFiles(repoRoot, relativeTemplatePaths, dryRun) {
if (path.resolve(repoRoot) !== PACKAGE_ROOT) {
return [];
}
const operations = [];
for (const relativeTemplatePath of relativeTemplatePaths) {
if (!PACKAGE_ROOT_SOURCE_OVERRIDES.has(normalizeTemplatePath(relativeTemplatePath))) {
continue;
}
const templateRelativePath = path.posix.join('templates', normalizeTemplatePath(relativeTemplatePath));
operations.push(
copyManagedSourceFile(
PACKAGE_ROOT,
path.join(PACKAGE_ROOT, relativeTemplatePath),
path.join(PACKAGE_ROOT, templateRelativePath),
templateRelativePath,
true,
dryRun,
),
);
}
return operations;
}
function lockFilePath(repoRoot) {
return path.join(repoRoot, LOCK_FILE_RELATIVE);
}
function ensureOmxScaffold(repoRoot, dryRun) {
const operations = [];
for (const relativeDir of REPO_SCAFFOLD_DIRECTORIES) {
const absoluteDir = path.join(repoRoot, relativeDir);
if (fs.existsSync(absoluteDir)) {
if (!fs.statSync(absoluteDir).isDirectory()) {
throw new Error(`Expected directory at ${relativeDir} but found a file.`);
}
operations.push({ status: 'unchanged', file: relativeDir });
continue;
}
if (!dryRun) {
fs.mkdirSync(absoluteDir, { recursive: true });
}
operations.push({ status: 'created', file: relativeDir });
}
for (const relativeDir of OMX_SCAFFOLD_DIRECTORIES) {
const absoluteDir = path.join(repoRoot, relativeDir);
if (fs.existsSync(absoluteDir)) {
if (!fs.statSync(absoluteDir).isDirectory()) {
throw new Error(`Expected directory at ${relativeDir} but found a file.`);
}
operations.push({ status: 'unchanged', file: relativeDir });
continue;
}
if (!dryRun) {
fs.mkdirSync(absoluteDir, { recursive: true });
}
operations.push({ status: 'created', file: relativeDir });
}
for (const [relativeFile, defaultContent] of OMX_SCAFFOLD_FILES.entries()) {
const absoluteFile = path.join(repoRoot, relativeFile);
if (fs.existsSync(absoluteFile)) {
if (!fs.statSync(absoluteFile).isFile()) {
throw new Error(`Expected file at ${relativeFile} but found a directory.`);
}
operations.push({ status: 'unchanged', file: relativeFile });
continue;
}
if (!dryRun) {
fs.mkdirSync(path.dirname(absoluteFile), { recursive: true });
fs.writeFileSync(absoluteFile, defaultContent, 'utf8');
}
operations.push({ status: 'created', file: relativeFile });
}
return operations;
}
function ensureLockRegistry(repoRoot, dryRun) {
const absolutePath = lockFilePath(repoRoot);
if (fs.existsSync(absolutePath)) {
return { status: 'unchanged', file: LOCK_FILE_RELATIVE };
}
if (!dryRun) {
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
fs.writeFileSync(absolutePath, JSON.stringify({ locks: {} }, null, 2) + '\n', 'utf8');
}
return { status: 'created', file: LOCK_FILE_RELATIVE };
}
function lockStateOrError(repoRoot) {
const lockPath = lockFilePath(repoRoot);
if (!fs.existsSync(lockPath)) {
return { ok: false, error: `${LOCK_FILE_RELATIVE} is missing` };
}
try {
const parsed = JSON.parse(fs.readFileSync(lockPath, 'utf8'));
if (!parsed || typeof parsed !== 'object' || typeof parsed.locks !== 'object' || parsed.locks === null) {
return { ok: false, error: `${LOCK_FILE_RELATIVE} has invalid schema (expected { locks: {} })` };
}
for (const [filePath, entry] of Object.entries(parsed.locks)) {
if (!entry || typeof entry !== 'object') {
parsed.locks[filePath] = { branch: '', claimed_at: '', allow_delete: false };
continue;
}
if (!Object.prototype.hasOwnProperty.call(entry, 'allow_delete')) {
entry.allow_delete = false;
}
}
return { ok: true, raw: parsed, locks: parsed.locks };
} catch (error) {
return { ok: false, error: `${LOCK_FILE_RELATIVE} is invalid JSON: ${error.message}` };
}
}
function writeLockState(repoRoot, payload, dryRun) {
if (dryRun) return;
const lockPath = lockFilePath(repoRoot);
fs.mkdirSync(path.dirname(lockPath), { recursive: true });
fs.writeFileSync(lockPath, JSON.stringify(payload, null, 2) + '\n', 'utf8');
}
function removeLegacyPackageScripts(repoRoot, dryRun) {
const packagePath = path.join(repoRoot, 'package.json');
if (!fs.existsSync(packagePath)) {
return { status: 'skipped', file: 'package.json', note: 'package.json not found' };
}
let pkg;
try {
pkg = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
} catch (error) {
throw new Error(`Unable to parse package.json in target repo: ${error.message}`);
}
const existingScripts = pkg.scripts && typeof pkg.scripts === 'object'
? pkg.scripts
: {};
pkg.scripts = existingScripts;
let changed = false;
for (const [key, value] of Object.entries(LEGACY_MANAGED_PACKAGE_SCRIPTS)) {
if (existingScripts[key] === value) {
delete existingScripts[key];
changed = true;
}
}
if (!changed) {
return { status: 'unchanged', file: 'package.json', note: 'no Guardex-managed agent:* scripts found' };
}
if (!dryRun) {
fs.writeFileSync(packagePath, JSON.stringify(pkg, null, 2) + '\n', 'utf8');
}
return { status: dryRun ? 'would-update' : 'updated', file: 'package.json', note: 'removed Guardex-managed agent:* scripts' };
}
function installUserLevelAsset(asset, options = {}) {
const dryRun = Boolean(options.dryRun);
const force = Boolean(options.force);
const destinationPath = path.join(GUARDEX_HOME_DIR, asset.destination);
const sourceContent = fs.readFileSync(asset.source, 'utf8');
const destinationExists = fs.existsSync(destinationPath);
if (destinationExists) {
const existingContent = fs.readFileSync(destinationPath, 'utf8');
if (existingContent === sourceContent) {
return { status: 'unchanged', file: asset.destination };
}
if (!force) {
return { status: 'skipped-conflict', file: asset.destination };
}
}
if (!dryRun) {
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
fs.writeFileSync(destinationPath, sourceContent, 'utf8');
}
return { status: destinationExists ? (dryRun ? 'would-update' : 'updated') : 'created', file: asset.destination };
}
function removeLegacyManagedRepoFile(repoRoot, relativePath, options = {}) {
const dryRun = Boolean(options.dryRun);
const force = Boolean(options.force);
const absolutePath = path.join(repoRoot, relativePath);
if (!fs.existsSync(absolutePath)) {
return { status: 'unchanged', file: relativePath, note: 'not present' };
}
if (!fs.statSync(absolutePath).isFile()) {
return { status: 'skipped-conflict', file: relativePath, note: 'not a regular file' };
}
const skillAsset = USER_LEVEL_SKILL_ASSETS.find((asset) => asset.destination === relativePath);
if (skillAsset) {
const userLevelPath = path.join(GUARDEX_HOME_DIR, skillAsset.destination);
if (!fs.existsSync(userLevelPath)) {
return { status: 'skipped', file: relativePath, note: 'user-level replacement not installed' };
}
}
const templateRelative = skillAsset
? skillAsset.source.slice(TEMPLATE_ROOT.length + 1)
: relativePath.replace(/^\./, '');
const sourcePath = path.join(TEMPLATE_ROOT, templateRelative);
if (!fs.existsSync(sourcePath)) {
return { status: 'skipped', file: relativePath, note: 'template source missing' };
}
const sourceContent = fs.readFileSync(sourcePath, 'utf8');
const existingContent = fs.readFileSync(absolutePath, 'utf8');
if (existingContent !== sourceContent && !force) {
return { status: 'skipped-conflict', file: relativePath, note: 'local edits differ from managed template' };
}
if (!dryRun) {
fs.rmSync(absolutePath, { force: true });
}
return { status: dryRun ? 'would-remove' : 'removed', file: relativePath };
}
function ensureAgentsSnippet(repoRoot, dryRun) {
const agentsPath = path.join(repoRoot, 'AGENTS.md');
const snippet = fs.readFileSync(path.join(TEMPLATE_ROOT, 'AGENTS.multiagent-safety.md'), 'utf8').trimEnd();
const managedRegex = new RegExp(
`${AGENTS_MARKER_START.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}[\\s\\S]*?${AGENTS_MARKER_END.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`,
'm',
);
if (!fs.existsSync(agentsPath)) {
if (!dryRun) {
fs.writeFileSync(agentsPath, `# AGENTS\n\n${snippet}\n`, 'utf8');
}
return { status: 'created', file: 'AGENTS.md' };
}
const existing = fs.readFileSync(agentsPath, 'utf8');
if (managedRegex.test(existing)) {
const next = existing.replace(managedRegex, snippet);
if (next === existing) {
return { status: 'unchanged', file: 'AGENTS.md' };
}
if (!dryRun) {
fs.writeFileSync(agentsPath, next, 'utf8');
}
return { status: 'updated', file: 'AGENTS.md', note: 'refreshed gitguardex-managed block' };
}
if (existing.includes(AGENTS_MARKER_START)) {
return { status: 'unchanged', file: 'AGENTS.md', note: 'existing marker found without managed end marker' };
}
const separator = existing.endsWith('\n') ? '\n' : '\n\n';
if (!dryRun) {
fs.writeFileSync(agentsPath, `${existing}${separator}${snippet}\n`, 'utf8');
}
return { status: 'updated', file: 'AGENTS.md' };
}
function ensureClaudeAgentsLink(repoRoot, dryRun) {
const claudePath = path.join(repoRoot, 'CLAUDE.md');
try {
fs.lstatSync(claudePath);
return { status: 'unchanged', file: 'CLAUDE.md', note: 'existing path preserved' };
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
if (!dryRun) {
fs.symlinkSync('AGENTS.md', claudePath);
}
return { status: dryRun ? 'would-create' : 'created', file: 'CLAUDE.md', note: 'symlink to AGENTS.md' };
}
function ensureManagedGitignore(repoRoot, dryRun) {
const gitignorePath = path.join(repoRoot, '.gitignore');
const managedBlock = [
GITIGNORE_MARKER_START,
...require('../context').MANAGED_GITIGNORE_PATHS,
GITIGNORE_MARKER_END,
].join('\n');
const managedRegex = new RegExp(
`${GITIGNORE_MARKER_START.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}[\\s\\S]*?${GITIGNORE_MARKER_END.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`,
'm',
);
if (!fs.existsSync(gitignorePath)) {
if (!dryRun) {
fs.writeFileSync(gitignorePath, `${managedBlock}\n`, 'utf8');
}
return { status: 'created', file: '.gitignore', note: 'added gitguardex-managed entries' };
}
const existing = fs.readFileSync(gitignorePath, 'utf8');
if (managedRegex.test(existing)) {
const next = existing.replace(managedRegex, managedBlock);
if (next === existing) {
return { status: 'unchanged', file: '.gitignore' };
}
if (!dryRun) {
fs.writeFileSync(gitignorePath, next, 'utf8');
}
return { status: 'updated', file: '.gitignore', note: 'refreshed gitguardex-managed entries' };
}
const separator = existing.endsWith('\n') ? '\n' : '\n\n';
if (!dryRun) {
fs.writeFileSync(gitignorePath, `${existing}${separator}${managedBlock}\n`, 'utf8');
}
return { status: 'updated', file: '.gitignore', note: 'appended gitguardex-managed entries' };
}
function parseJsonObjectLikeFile(source, relativePath) {
const errors = [];
const parsed = parseJsonc(source, errors, { allowTrailingComma: true });
if (errors.length > 0) {
const formattedErrors = errors.map((entry) => printParseErrorCode(entry.error)).join(', ');
throw new Error(`Unable to parse ${relativePath} as JSON or JSONC: ${formattedErrors}`);
}
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
throw new Error(`${relativePath} must contain a top-level object.`);
}
return parsed;
}
function uniqueStringList(values) {
const seen = new Set();
const result = [];
for (const value of values) {
if (typeof value !== 'string' || seen.has(value)) {
continue;
}
seen.add(value);
result.push(value);
}
return result;
}
function buildRepoVscodeSettings(existingSettings = {}) {
const nextSettings = { ...existingSettings };
const existingIgnoredFolders = Array.isArray(existingSettings[REPO_SCAN_IGNORED_FOLDERS_SETTING])
? existingSettings[REPO_SCAN_IGNORED_FOLDERS_SETTING]
: [];
nextSettings[REPO_SCAN_IGNORED_FOLDERS_SETTING] = uniqueStringList([
...existingIgnoredFolders,
...MANAGED_REPO_SCAN_IGNORED_FOLDERS,
]);
return nextSettings;
}
function ensureRepoVscodeSettings(repoRoot, dryRun) {
const settingsPath = path.join(repoRoot, SHARED_VSCODE_SETTINGS_RELATIVE);
const destinationExists = fs.existsSync(settingsPath);
const existingContent = destinationExists ? fs.readFileSync(settingsPath, 'utf8') : '';
const existingSettings = destinationExists
? parseJsonObjectLikeFile(existingContent, SHARED_VSCODE_SETTINGS_RELATIVE)
: {};
const nextContent = `${JSON.stringify(buildRepoVscodeSettings(existingSettings), null, 2)}\n`;
if (destinationExists && existingContent === nextContent) {
return { status: 'unchanged', file: SHARED_VSCODE_SETTINGS_RELATIVE };
}
ensureParentDir(repoRoot, settingsPath, dryRun);
if (!dryRun) {
fs.writeFileSync(settingsPath, nextContent, 'utf8');
}
return {
status: destinationExists ? 'updated' : 'created',
file: SHARED_VSCODE_SETTINGS_RELATIVE,
note: 'shared VS Code repo scan ignores for Guardex worktrees',
};
}
function normalizeWorkspacePath(relativePath) {
return String(relativePath || '.').replace(/\\/g, '/');
}
function buildParentWorkspaceView(repoRoot) {
const parentDir = path.dirname(repoRoot);
const workspaceFileName = `${path.basename(repoRoot)}-branches.code-workspace`;
const workspacePath = path.join(parentDir, workspaceFileName);
const repoRelativePath = normalizeWorkspacePath(path.relative(parentDir, repoRoot) || '.');
return {
workspacePath,
payload: {
folders: [
{ path: repoRelativePath },
...AGENT_WORKTREE_RELATIVE_DIRS.map((relativeDir) => ({
path: normalizeWorkspacePath(
path.join(repoRelativePath === '.' ? '' : repoRelativePath, relativeDir),
),
})),
],
settings: {
'scm.alwaysShowRepositories': true,
},
},
};
}
function ensureParentWorkspaceView(repoRoot, dryRun) {
const { workspacePath, payload } = buildParentWorkspaceView(repoRoot);
const operationFile = path.relative(repoRoot, workspacePath) || path.basename(workspacePath);
const nextContent = `${JSON.stringify(payload, null, 2)}\n`;
const note = 'parent VS Code workspace view';
if (!fs.existsSync(workspacePath)) {
if (!dryRun) {
fs.writeFileSync(workspacePath, nextContent, 'utf8');
}
return { status: dryRun ? 'would-create' : 'created', file: operationFile, note };
}
const currentContent = fs.readFileSync(workspacePath, 'utf8');
if (currentContent === nextContent) {
return { status: 'unchanged', file: operationFile, note };
}
if (!dryRun) {
fs.writeFileSync(workspacePath, nextContent, 'utf8');
}
return { status: dryRun ? 'would-update' : 'updated', file: operationFile, note };
}
function configureHooks(repoRoot, dryRun) {
if (dryRun) {
return { status: 'would-set', key: 'core.hooksPath', value: '.githooks' };
}
const result = run('git', ['-C', repoRoot, 'config', 'core.hooksPath', '.githooks']);
if (result.status !== 0) {
throw new Error(`Failed to set git hooksPath: ${(result.stderr || '').trim()}`);
}
return { status: 'set', key: 'core.hooksPath', value: '.githooks' };
}
function printOperations(title, payload, dryRun = false) {
console.log(`[${TOOL_NAME}] ${title}: ${payload.repoRoot}`);
for (const operation of payload.operations) {
const note = operation.note ? ` (${operation.note})` : '';
console.log(` - ${operation.status.padEnd(12)} ${operation.file}${note}`);
}
console.log(
` - hooksPath ${payload.hookResult.status} ${payload.hookResult.key}=${payload.hookResult.value}`,
);
if (dryRun) {
console.log(`[${TOOL_NAME}] Dry run complete. No files were modified.`);
}
}
function printStandaloneOperations(title, rootLabel, operations, dryRun = false) {
console.log(`[${TOOL_NAME}] ${title}: ${rootLabel}`);
for (const operation of operations) {
const note = operation.note ? ` (${operation.note})` : '';
console.log(` - ${operation.status.padEnd(12)} ${operation.file}${note}`);
}
if (dryRun) {
console.log(`[${TOOL_NAME}] Dry run complete. No files were modified.`);
}
}
module.exports = {
HOOK_NAMES,
LOCK_FILE_RELATIVE,
toDestinationPath,
ensureParentDir,
ensureExecutable,
isCriticalGuardrailPath,
shellSingleQuote,
renderShellDispatchShim,
renderPythonDispatchShim,
managedForceConflictMessage,
renderManagedFile,
ensureGeneratedScriptShim,
ensureHookShim,
copyTemplateFile,
ensureTemplateFilePresent,
materializePackageRepoTemplateFiles,
ensureOmxScaffold,
ensureLockRegistry,
lockStateOrError,
writeLockState,
removeLegacyPackageScripts,
installUserLevelAsset,
removeLegacyManagedRepoFile,
ensureAgentsSnippet,
ensureClaudeAgentsLink,
ensureManagedGitignore,
parseJsonObjectLikeFile,
buildRepoVscodeSettings,
ensureRepoVscodeSettings,
buildParentWorkspaceView,
ensureParentWorkspaceView,
configureHooks,
printOperations,
printStandaloneOperations,
};