-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinstall.js
More file actions
79 lines (71 loc) · 2.69 KB
/
install.js
File metadata and controls
79 lines (71 loc) · 2.69 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
function isInsideNodeModules() {
return __dirname.includes(path.sep + 'node_modules' + path.sep);
}
function getProjectRoot() {
if (!isInsideNodeModules()) return null;
let current = __dirname;
while (current !== path.dirname(current)) {
current = path.dirname(current);
if (path.basename(current) === 'node_modules') return path.dirname(current);
}
return null;
}
function safeCopyDirectory(src, dst) {
try {
if (!fs.existsSync(src)) return false;
fs.mkdirSync(dst, { recursive: true });
fs.readdirSync(src, { withFileTypes: true }).forEach(entry => {
const s = path.join(src, entry.name), d = path.join(dst, entry.name);
if (entry.isDirectory()) safeCopyDirectory(s, d);
else { fs.mkdirSync(path.dirname(d), { recursive: true }); fs.copyFileSync(s, d); try { fs.chmodSync(d, fs.statSync(s).mode); } catch (e) {} }
});
return true;
} catch (e) { return false; }
}
function safeCopyFile(src, dst) {
try {
const dstDir = path.dirname(dst);
if (!fs.existsSync(dstDir)) fs.mkdirSync(dstDir, { recursive: true });
fs.writeFileSync(dst, fs.readFileSync(src, 'utf-8'), 'utf-8');
return true;
} catch (e) { return false; }
}
function safeCopyDirectoryFull(src, dst) {
try {
if (!fs.existsSync(src)) return false;
fs.mkdirSync(dst, { recursive: true });
fs.readdirSync(src, { withFileTypes: true }).forEach(entry => {
const s = path.join(src, entry.name), d = path.join(dst, entry.name);
if (entry.isDirectory()) safeCopyDirectoryFull(s, d);
else safeCopyFile(s, d);
});
return true;
} catch (e) { return false; }
}
function updateGitignore(projectRoot) {
try {
const gitignorePath = path.join(projectRoot, '.gitignore');
const entry = '.gm-stop-verified';
let content = fs.existsSync(gitignorePath) ? fs.readFileSync(gitignorePath, 'utf-8') : '';
if (content.includes(entry)) return true;
if (content && !content.endsWith('\n')) content += '\n';
content += entry + '\n';
fs.writeFileSync(gitignorePath, content, 'utf-8');
return true;
} catch (e) { return false; }
}
function install() {
if (!isInsideNodeModules()) return;
const projectRoot = getProjectRoot();
if (!projectRoot) return;
const claudeDir = path.join(projectRoot, '.claude');
const sourceDir = __dirname.replace(/[\/]scripts$/, '');
safeCopyDirectoryFull(path.join(sourceDir, 'agents'), path.join(claudeDir, 'agents'));
safeCopyDirectoryFull(path.join(sourceDir, 'hooks'), path.join(claudeDir, 'hooks'));
safeCopyFile(path.join(sourceDir, '.mcp.json'), path.join(claudeDir, '.mcp.json'));
updateGitignore(projectRoot);
}
install();