-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.js
More file actions
256 lines (221 loc) · 8.2 KB
/
Copy pathinstall.js
File metadata and controls
256 lines (221 loc) · 8.2 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
#!/usr/bin/env node
const fs = require("node:fs");
const path = require("node:path");
const { execSync } = require("node:child_process");
const SKILL_NAME = "code-review-gate";
const MARKER_START = "<!-- code-review-gate:start -->";
const MARKER_END = "<!-- code-review-gate:end -->";
const PACKAGE_ROOT = path.resolve(__dirname);
const SHARED_SKILL_DIR = path.join(osHome(), ".agents", "skills", SKILL_NAME);
const AGENT_TARGETS = {
claude: {
checkDir: path.join(osHome(), ".claude"),
instructionFile: path.join(osHome(), ".claude", "CLAUDE.md"),
},
opencode: {
checkDir: path.join(osHome(), ".config", "opencode"),
instructionFile: path.join(osHome(), ".config", "opencode", "AGENTS.md"),
},
gemini: {
checkDir: path.join(osHome(), ".gemini"),
instructionFile: path.join(osHome(), ".gemini", "GEMINI.md"),
},
codex: {
checkDir: process.env.CODEX_HOME || path.join(osHome(), ".codex"),
instructionFile: path.join(process.env.CODEX_HOME || path.join(osHome(), ".codex"), "AGENTS.md"),
},
copilot: {
checkDir: path.join(osHome(), ".copilot"),
instructionFile: path.join(osHome(), ".copilot", "copilot-instructions.md"),
},
};
function osHome() {
return process.env.HOME || process.env.USERPROFILE || ".";
}
function isNpmGlobal() {
if (process.env.npm_config_global === "true") {
return true;
}
try {
const globalRoot = execSync("npm root -g", { encoding: "utf-8", timeout: 5000 }).trim();
return PACKAGE_ROOT.startsWith(path.resolve(globalRoot));
} catch {
return false;
}
}
function parseArgs(argv) {
const options = {
agent: "auto",
force: false,
};
for (let index = 0; index < argv.length; index += 1) {
const item = argv[index];
if (item === "--agent" && argv[index + 1]) {
options.agent = argv[index + 1];
index += 1;
continue;
}
if (item === "--force") {
options.force = true;
continue;
}
}
return options;
}
function parseAgents(raw) {
if (!raw || raw === "auto") {
const detected = Object.entries(AGENT_TARGETS)
.filter(([, value]) => fs.existsSync(value.checkDir))
.map(([name]) => name);
return detected.length > 0 ? detected : ["claude", "opencode", "gemini"];
}
if (raw === "all") {
return Object.keys(AGENT_TARGETS);
}
return raw.split(",").map((item) => item.trim().toLowerCase()).filter(Boolean);
}
function copyRecursive(src, dest) {
const entries = fs.readdirSync(src, { withFileTypes: true });
fs.mkdirSync(dest, { recursive: true });
for (const entry of entries) {
if (entry.name === ".git" || entry.name === "node_modules" || entry.name === "__pycache__" || entry.name.endsWith(".pyc")) {
continue;
}
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyRecursive(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
function installSharedSkill(force) {
if (fs.existsSync(SHARED_SKILL_DIR)) {
if (!force) {
return SHARED_SKILL_DIR;
}
fs.rmSync(SHARED_SKILL_DIR, { recursive: true, force: true });
}
fs.mkdirSync(path.dirname(SHARED_SKILL_DIR), { recursive: true });
copyRecursive(PACKAGE_ROOT, SHARED_SKILL_DIR);
return SHARED_SKILL_DIR;
}
function adapterBody(agent) {
switch (agent) {
case "claude":
return `# Code Review Gate Adapter
When asked to review code, use the installed \`code-review-gate\` skill when available. If the repository also contains \`./ai/code-review-gate/\`, prefer the workspace pack so reports and policy stay local to that repo.
Collect:
- \`author_name\`
- \`review_date\`
- \`story_id\`
- \`requirement_source\`
- \`base_ref\`
Completion rule:
- output review artifacts under \`./CODE_REVIEW/{YYYYMMDD}_ROUND_{seq}/\`
- do not mark the task done until every hard gate in the canonical policy is \`PASS\`
`;
case "opencode":
return `# Code Review Gate Adapter
For review tasks, use the installed \`code-review-gate\` skill when available. If the repository also contains \`./ai/code-review-gate/\`, prefer the workspace pack so reports and policy stay local to that repo.
Review rules:
- scale effort by impact
- reuse \`legacy-impact-audit\` evidence when present
- in legacy or shared codebases, prefer existing similar structure and shared utilities
- require requirement mapping and logical-block change comments
- emit review artifacts under \`./CODE_REVIEW/{YYYYMMDD}_ROUND_{seq}/\`
- never mark the review complete while any hard gate is \`BLOCK\`
`;
case "gemini":
return `# Code Review Gate Adapter
When performing code review, use the installed \`code-review-gate\` skill when available. If the repository also contains \`./ai/code-review-gate/\`, prefer the workspace pack so reports and policy stay local to that repo.
Required inputs:
- \`author_name\`
- \`review_date\`
- \`story_id\`
- \`requirement_source\`
- \`base_ref\`
Do not mark the task done until all hard gates pass.
`;
case "codex":
return `# Code Review Gate Adapter
Use the installed \`code-review-gate\` skill for code review tasks. If the repository also contains \`./ai/code-review-gate/\`, prefer the workspace pack so reports and policy stay local to that repo.
Review expectations:
- determine review effort from impact
- reuse \`legacy-impact-audit\` artifacts when available
- follow legacy project structure and shared utility reuse rules
- require requirement mapping for each code change area
- require logical-block change comments using the canonical format
- write outputs under \`./CODE_REVIEW/{YYYYMMDD}_ROUND_{seq}/\`
- do not mark completion while any hard gate remains \`BLOCK\`
`;
case "copilot":
return `# Code Review Gate Adapter
When reviewing a code change, use the installed \`code-review-gate\` skill when available. If the repository also contains \`./ai/code-review-gate/\`, prefer the workspace pack so reports and policy stay local to that repo.
Required behavior:
- determine review effort from impact
- reuse \`legacy-impact-audit\` results when available
- prefer existing similar code structure and shared utilities in legacy projects
- generate outputs under \`./CODE_REVIEW/{YYYYMMDD}_ROUND_{seq}/\`
- do not mark the task done until all 8 hard gates pass
`;
default:
throw new Error(`Unsupported agent: ${agent}`);
}
}
function markedBlock(agent) {
return `${MARKER_START}\n${adapterBody(agent).trim()}\n${MARKER_END}\n`;
}
function writeMarkedBlock(filePath, agent) {
const block = markedBlock(agent);
fs.mkdirSync(path.dirname(filePath), { recursive: true });
const existing = fs.existsSync(filePath) ? fs.readFileSync(filePath, "utf-8") : "";
if (existing.includes(MARKER_START) && existing.includes(MARKER_END)) {
const before = existing.split(MARKER_START, 1)[0].trimEnd();
const after = existing.split(MARKER_END, 1)[1].trimStart();
const content = `${before}\n\n${block.trim()}\n\n${after}`.trim() + "\n";
fs.writeFileSync(filePath, content, "utf-8");
return;
}
const content = (existing.trimEnd() + "\n\n" + block.trim() + "\n").trimStart();
fs.writeFileSync(filePath, content, "utf-8");
}
function installAgents(agentList) {
const installed = [];
for (const agent of agentList) {
const target = AGENT_TARGETS[agent];
if (!target) {
throw new Error(`Unsupported agent: ${agent}`);
}
writeMarkedBlock(target.instructionFile, agent);
installed.push(target.instructionFile);
}
return installed;
}
function runInstall(options) {
const skillDir = installSharedSkill(options.force);
const agents = parseAgents(options.agent);
const installedFiles = installAgents(agents);
console.log(`[code-review-gate] Installed shared skill to ${skillDir}`);
installedFiles.forEach((filePath) => {
console.log(`[code-review-gate] Updated ${filePath}`);
});
}
function main(argv = process.argv.slice(2)) {
const options = parseArgs(argv);
runInstall(options);
return 0;
}
if (require.main === module) {
if (process.argv.length > 2) {
process.exitCode = main(process.argv.slice(2));
} else if (isNpmGlobal()) {
process.exitCode = main(["--force"]);
} else {
console.log("[code-review-gate] Skipping auto-install: not a global npm install.");
}
}
module.exports = {
main,
};