kitskill 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +397 -2
- package/THIRD-PARTY-NOTICES.md +24 -0
- package/banner.jpeg +0 -0
- package/bin/cli.mjs +464 -0
- package/lib/fs-safe.mjs +136 -0
- package/lib/hook.mjs +79 -0
- package/lib/installer.mjs +449 -0
- package/lib/manifest.mjs +47 -0
- package/lib/permissions.mjs +175 -0
- package/lib/profile.mjs +124 -0
- package/lib/project-scan.mjs +186 -0
- package/lib/validator.mjs +72 -0
- package/logo.png +0 -0
- package/logohead.png +0 -0
- package/package.json +54 -4
- package/templates/core/memory/PROJECT.md +33 -0
- package/templates/core/memory/decisions/ADR.md +22 -0
- package/templates/core/profile.json +17 -0
- package/templates/core/router.md +89 -0
- package/templates/core/state/STATE.md +32 -0
- package/templates/opencode/policy-plugin.js +66 -0
- package/templates/scripts/graph-client.mjs +231 -0
- package/templates/scripts/review-packet.mjs +250 -0
- package/templates/scripts/shrink-output.mjs +159 -0
- package/templates/scripts/ui-audit.mjs +639 -0
- package/templates/scripts/verify.mjs +293 -0
- package/templates/skills/ks-api/SKILL.md +87 -0
- package/templates/skills/ks-api/references/api-contract.md +131 -0
- package/templates/skills/ks-build/SKILL.md +92 -0
- package/templates/skills/ks-data/SKILL.md +81 -0
- package/templates/skills/ks-data/references/data-migrations.md +120 -0
- package/templates/skills/ks-deliver/SKILL.md +99 -0
- package/templates/skills/ks-deliver/references/doc-patterns.md +180 -0
- package/templates/skills/ks-design/SKILL.md +147 -0
- package/templates/skills/ks-design/references/app-models.md +216 -0
- package/templates/skills/ks-design/references/design-contract.md +98 -0
- package/templates/skills/ks-design/references/visual-directions.md +159 -0
- package/templates/skills/ks-plan/SKILL.md +84 -0
- package/templates/skills/ks-review/SKILL.md +71 -0
- package/templates/skills/ks-secure/SKILL.md +55 -0
package/bin/cli.mjs
ADDED
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import { existsSync, readFileSync, realpathSync } from 'node:fs';
|
|
4
|
+
import { copyFile, mkdir, unlink } from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
SUPPORTED_TARGETS,
|
|
10
|
+
doctorProject,
|
|
11
|
+
initProject,
|
|
12
|
+
uninstallProject,
|
|
13
|
+
updateProject,
|
|
14
|
+
} from '../lib/installer.mjs';
|
|
15
|
+
import {
|
|
16
|
+
applyPermissions,
|
|
17
|
+
auditPermissions,
|
|
18
|
+
auditProjectPermissions,
|
|
19
|
+
readProjectConfig,
|
|
20
|
+
recommendedRules,
|
|
21
|
+
} from '../lib/permissions.mjs';
|
|
22
|
+
import { hookStatus, installHook, removeHook } from '../lib/hook.mjs';
|
|
23
|
+
import { runVerify } from '../templates/scripts/verify.mjs';
|
|
24
|
+
import { scanProject, writeProjectProfile } from '../lib/project-scan.mjs';
|
|
25
|
+
import {
|
|
26
|
+
DOMAINS,
|
|
27
|
+
EXPLAIN_LEVELS,
|
|
28
|
+
LEVELS,
|
|
29
|
+
loadProfile,
|
|
30
|
+
renderProfile,
|
|
31
|
+
setExplain,
|
|
32
|
+
setExperience,
|
|
33
|
+
setLanguage,
|
|
34
|
+
} from '../lib/profile.mjs';
|
|
35
|
+
|
|
36
|
+
const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
37
|
+
const templatesDir = path.join(packageRoot, 'templates');
|
|
38
|
+
const POLICY_PLUGIN_TARGET = '.opencode/plugins/kitskill-policy.js';
|
|
39
|
+
|
|
40
|
+
function readVersion() {
|
|
41
|
+
try {
|
|
42
|
+
return JSON.parse(readFileSync(path.join(packageRoot, 'package.json'), 'utf8')).version ?? '0.0.0';
|
|
43
|
+
} catch {
|
|
44
|
+
return '0.0.0';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function usage() {
|
|
49
|
+
return [
|
|
50
|
+
'Usage: kitskill <command> [options]',
|
|
51
|
+
'',
|
|
52
|
+
'Commands:',
|
|
53
|
+
' init Install kitskill into the current project',
|
|
54
|
+
' update Update unmodified installed files',
|
|
55
|
+
' doctor Audit the installation and skill discovery layout',
|
|
56
|
+
' uninstall Remove installed files and instruction blocks',
|
|
57
|
+
' verify Run .kitskill/verify.json and write evidence',
|
|
58
|
+
' hook Install or remove the pre-commit gate (install|remove|status)',
|
|
59
|
+
' profile Show or edit the learner profile (show|set|explain|language)',
|
|
60
|
+
' scan Detect the project stack, commands, and paths',
|
|
61
|
+
' policy Show, merge, audit, or remove OpenCode rules (show|apply|audit|remove)',
|
|
62
|
+
' ui-audit Render a local HTML file and check overflow, names, labels, alt',
|
|
63
|
+
' browser-qa Audit a served URL with console errors and optional screenshots',
|
|
64
|
+
' graph Query the optional local Graphify index (status|query|explain)',
|
|
65
|
+
'',
|
|
66
|
+
'Options:',
|
|
67
|
+
` --target <${SUPPORTED_TARGETS.join('|')}|all> Target runtime (repeatable; default: opencode)`,
|
|
68
|
+
' --dry-run Print the plan without writing',
|
|
69
|
+
' --force Remove or overwrite modified files',
|
|
70
|
+
' --purge-state Remove state and memory on uninstall',
|
|
71
|
+
' --apply-permissions Write the OpenCode least-privilege profile (opt-in)',
|
|
72
|
+
' --json Machine-readable output',
|
|
73
|
+
' --level <0|1|2|3> Verification level for verify',
|
|
74
|
+
' --version Print the version',
|
|
75
|
+
' --help Print this help',
|
|
76
|
+
].join('\n');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function parseArgs(argv) {
|
|
80
|
+
const options = { targets: [], dryRun: false, force: false, purgeState: false, json: false, applyPermissions: false, hook: null, args: [], level: undefined };
|
|
81
|
+
let command = null;
|
|
82
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
83
|
+
const arg = argv[i];
|
|
84
|
+
if (command === 'ui-audit' || command === 'browser-qa' || command === 'graph') {
|
|
85
|
+
options.args.push(arg);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (arg === '--help' || arg === '-h') options.help = true;
|
|
89
|
+
else if (arg === '--version') options.version = true;
|
|
90
|
+
else if (arg === '--dry-run') options.dryRun = true;
|
|
91
|
+
else if (arg === '--force') options.force = true;
|
|
92
|
+
else if (arg === '--purge-state') options.purgeState = true;
|
|
93
|
+
else if (arg === '--apply-permissions') options.applyPermissions = true;
|
|
94
|
+
else if (arg === '--json') options.json = true;
|
|
95
|
+
else if (arg === '--level') {
|
|
96
|
+
const raw = argv[++i];
|
|
97
|
+
if (raw === 'auto') {
|
|
98
|
+
options.level = 'auto';
|
|
99
|
+
} else {
|
|
100
|
+
const value = Number.parseInt(raw, 10);
|
|
101
|
+
if (!Number.isInteger(value) || value < 0 || value > 3) {
|
|
102
|
+
throw new Error('--level must be auto, 0, 1, 2, or 3');
|
|
103
|
+
}
|
|
104
|
+
options.level = value;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else if (arg === '--target') {
|
|
108
|
+
const value = argv[++i];
|
|
109
|
+
if (!value) throw new Error('--target requires a value');
|
|
110
|
+
options.targets.push(...value.split(',').map((item) => item.trim()).filter(Boolean));
|
|
111
|
+
} else if (arg.startsWith('-')) {
|
|
112
|
+
throw new Error(`unknown option: ${arg}`);
|
|
113
|
+
} else if (!command) {
|
|
114
|
+
command = arg;
|
|
115
|
+
} else if (command === 'hook' && !options.hook) {
|
|
116
|
+
options.hook = arg;
|
|
117
|
+
} else if (command === 'profile' || command === 'policy' || command === 'ui-audit' || command === 'browser-qa') {
|
|
118
|
+
options.args.push(arg);
|
|
119
|
+
} else {
|
|
120
|
+
throw new Error(`unexpected argument: ${arg}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return { command, options };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function printPlan(result) {
|
|
127
|
+
for (const item of result.installed ?? []) {
|
|
128
|
+
process.stdout.write(` + ${item.path}${item.kind === 'symlink' ? ` -> ${item.target}` : ''}\n`);
|
|
129
|
+
}
|
|
130
|
+
for (const item of result.skipped ?? []) {
|
|
131
|
+
process.stdout.write(` = ${item.path} (${item.reason})\n`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async function main(argv) {
|
|
136
|
+
let parsed;
|
|
137
|
+
try {
|
|
138
|
+
parsed = parseArgs(argv);
|
|
139
|
+
} catch (error) {
|
|
140
|
+
process.stderr.write(`${error.message}\n\n${usage()}\n`);
|
|
141
|
+
return 2;
|
|
142
|
+
}
|
|
143
|
+
const { command, options } = parsed;
|
|
144
|
+
const version = readVersion();
|
|
145
|
+
|
|
146
|
+
if (options.help) {
|
|
147
|
+
process.stdout.write(`${usage()}\n`);
|
|
148
|
+
return 0;
|
|
149
|
+
}
|
|
150
|
+
if (options.version) {
|
|
151
|
+
process.stdout.write(`${version}\n`);
|
|
152
|
+
return 0;
|
|
153
|
+
}
|
|
154
|
+
if (!command) {
|
|
155
|
+
process.stderr.write(`${usage()}\n`);
|
|
156
|
+
return 2;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const root = process.cwd();
|
|
160
|
+
const targets = options.targets.length > 0 ? options.targets : ['opencode'];
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
if (command === 'init') {
|
|
164
|
+
if (options.applyPermissions && !targets.includes('all') && !targets.includes('opencode')) {
|
|
165
|
+
throw new Error('--apply-permissions requires the opencode target');
|
|
166
|
+
}
|
|
167
|
+
const result = await initProject(root, {
|
|
168
|
+
templatesDir,
|
|
169
|
+
targets,
|
|
170
|
+
version,
|
|
171
|
+
dryRun: options.dryRun,
|
|
172
|
+
});
|
|
173
|
+
if (result.status === 'already-installed') {
|
|
174
|
+
process.stdout.write('kitskill is already installed. Use `kitskill update` or `kitskill doctor`.\n');
|
|
175
|
+
} else {
|
|
176
|
+
process.stdout.write(
|
|
177
|
+
`${options.dryRun ? 'Plan' : 'Installed'} (${result.plan?.targets.join(', ') ?? result.manifest.targets.join(', ')})\n`,
|
|
178
|
+
);
|
|
179
|
+
printPlan(result);
|
|
180
|
+
if (!options.dryRun) {
|
|
181
|
+
process.stdout.write(`Manifest: .kitskill/manifest.json\n`);
|
|
182
|
+
process.stdout.write('Next: kitskill scan; kitskill profile\n');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (options.applyPermissions) {
|
|
186
|
+
const permissions = await applyPermissions(root, { dryRun: options.dryRun });
|
|
187
|
+
if (permissions.status === 'conflict') {
|
|
188
|
+
process.stdout.write(
|
|
189
|
+
`Permissions: skipped (${permissions.file} exists; merge the profile manually)\n`,
|
|
190
|
+
);
|
|
191
|
+
} else {
|
|
192
|
+
process.stdout.write(
|
|
193
|
+
`Permissions: ${permissions.status === 'dry-run' ? 'would write' : 'wrote'} ${permissions.file}\n`,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return 0;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (command === 'update') {
|
|
201
|
+
const result = await updateProject(root, { templatesDir, version, dryRun: options.dryRun });
|
|
202
|
+
process.stdout.write(`${options.dryRun ? 'Would update' : 'Updated'}: ${result.updated.length} file(s)\n`);
|
|
203
|
+
for (const item of result.updated) process.stdout.write(` ~ ${item}\n`);
|
|
204
|
+
for (const item of result.preserved) process.stdout.write(` = ${item.path} (${item.reason})\n`);
|
|
205
|
+
return 0;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (command === 'uninstall') {
|
|
209
|
+
const result = await uninstallProject(root, {
|
|
210
|
+
dryRun: options.dryRun,
|
|
211
|
+
force: options.force,
|
|
212
|
+
purgeState: options.purgeState,
|
|
213
|
+
});
|
|
214
|
+
process.stdout.write(`${options.dryRun ? 'Would remove' : 'Removed'}: ${result.removed.length} item(s)\n`);
|
|
215
|
+
for (const item of result.removed) process.stdout.write(` - ${item}\n`);
|
|
216
|
+
for (const item of result.preserved) process.stdout.write(` = ${item.path} (${item.reason})\n`);
|
|
217
|
+
for (const item of result.blocks) process.stdout.write(` - block in ${item}\n`);
|
|
218
|
+
return 0;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (command === 'verify') {
|
|
222
|
+
let evidence;
|
|
223
|
+
try {
|
|
224
|
+
evidence = await runVerify(root, { level: options.level });
|
|
225
|
+
} catch (error) {
|
|
226
|
+
process.stderr.write(`kitskill verify: ${error.message}\n`);
|
|
227
|
+
return 2;
|
|
228
|
+
}
|
|
229
|
+
if (options.json) {
|
|
230
|
+
process.stdout.write(`${JSON.stringify(evidence, null, 2)}\n`);
|
|
231
|
+
} else {
|
|
232
|
+
for (const check of evidence.checks) {
|
|
233
|
+
process.stdout.write(
|
|
234
|
+
`${check.ok ? 'PASS' : 'FAIL'} ${check.name} (${check.durationMs} ms)\n`,
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
process.stdout.write(
|
|
238
|
+
`${evidence.ok ? 'PASS' : 'FAIL'} verification: ` +
|
|
239
|
+
`${evidence.summary.passed}/${evidence.summary.total}` +
|
|
240
|
+
(evidence.summary.skipped > 0 ? ` (${evidence.summary.skipped} skipped)` : '') +
|
|
241
|
+
` at level ${evidence.level}\n`,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
return evidence.ok ? 0 : 1;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (command === 'hook') {
|
|
248
|
+
const sub = options.hook ?? 'status';
|
|
249
|
+
if (sub === 'install') {
|
|
250
|
+
const result = await installHook(root, { force: options.force });
|
|
251
|
+
process.stdout.write(`Hook installed: ${result.path}\n`);
|
|
252
|
+
if (result.backup) process.stdout.write(`Backup: ${result.backup}\n`);
|
|
253
|
+
return 0;
|
|
254
|
+
}
|
|
255
|
+
if (sub === 'remove') {
|
|
256
|
+
const result = await removeHook(root);
|
|
257
|
+
process.stdout.write(
|
|
258
|
+
result.removed ? `Hook removed: ${result.path}\n` : `No hook at ${result.path}\n`,
|
|
259
|
+
);
|
|
260
|
+
return 0;
|
|
261
|
+
}
|
|
262
|
+
if (sub === 'status') {
|
|
263
|
+
const result = await hookStatus(root);
|
|
264
|
+
process.stdout.write(
|
|
265
|
+
`${result.installed ? 'installed' : 'not installed'}: ${result.path}\n`,
|
|
266
|
+
);
|
|
267
|
+
return result.installed ? 0 : 1;
|
|
268
|
+
}
|
|
269
|
+
throw new Error(`unknown hook command: ${sub}`);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (command === 'graph') {
|
|
273
|
+
const script = path.join(root, '.kitskill/scripts/graph-client.mjs');
|
|
274
|
+
if (!existsSync(script)) throw new Error('graph client is not installed; run kitskill init');
|
|
275
|
+
const run = spawnSync(process.execPath, [script, ...options.args], {
|
|
276
|
+
cwd: root,
|
|
277
|
+
stdio: 'inherit',
|
|
278
|
+
});
|
|
279
|
+
return run.status ?? 1;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (command === 'ui-audit' || command === 'browser-qa') {
|
|
283
|
+
const script = path.join(root, '.kitskill/scripts/ui-audit.mjs');
|
|
284
|
+
if (!existsSync(script)) throw new Error('ui-audit script is not installed; run kitskill init');
|
|
285
|
+
const args = [...options.args];
|
|
286
|
+
if (command === 'browser-qa' && !args.includes('--console')) args.push('--console');
|
|
287
|
+
if (command === 'browser-qa' && !args.includes('--contrast')) args.push('--contrast');
|
|
288
|
+
if (command === 'browser-qa' && !args.includes('--keyboard')) args.push('--keyboard');
|
|
289
|
+
const run = spawnSync(process.execPath, [script, ...args], {
|
|
290
|
+
cwd: root,
|
|
291
|
+
stdio: 'inherit',
|
|
292
|
+
});
|
|
293
|
+
return run.status ?? 1;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (command === 'policy') {
|
|
297
|
+
const action = options.args[0] ?? 'show';
|
|
298
|
+
if (action === 'show') {
|
|
299
|
+
process.stdout.write(`${JSON.stringify(recommendedRules(), null, 2)}\n`);
|
|
300
|
+
return 0;
|
|
301
|
+
}
|
|
302
|
+
if (action === 'install') {
|
|
303
|
+
const target = path.join(root, POLICY_PLUGIN_TARGET);
|
|
304
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
305
|
+
await copyFile(path.join(templatesDir, 'opencode/policy-plugin.js'), target);
|
|
306
|
+
const result = await applyPermissions(root, { merge: true });
|
|
307
|
+
process.stdout.write(`Plugin: ${POLICY_PLUGIN_TARGET}\n`);
|
|
308
|
+
if (result.status === 'conflict') {
|
|
309
|
+
process.stdout.write(`Permissions: skipped (${result.file} is JSONC or invalid)\n`);
|
|
310
|
+
} else {
|
|
311
|
+
process.stdout.write(`Permissions: merged ${result.file}\n`);
|
|
312
|
+
}
|
|
313
|
+
return 0;
|
|
314
|
+
}
|
|
315
|
+
if (action === 'remove') {
|
|
316
|
+
const target = path.join(root, POLICY_PLUGIN_TARGET);
|
|
317
|
+
if (existsSync(target)) {
|
|
318
|
+
await unlink(target);
|
|
319
|
+
process.stdout.write(`Plugin removed: ${POLICY_PLUGIN_TARGET}\n`);
|
|
320
|
+
} else {
|
|
321
|
+
process.stdout.write(`No plugin at ${POLICY_PLUGIN_TARGET}\n`);
|
|
322
|
+
}
|
|
323
|
+
return 0;
|
|
324
|
+
}
|
|
325
|
+
if (action === 'apply') {
|
|
326
|
+
const result = await applyPermissions(root, { dryRun: options.dryRun, merge: true });
|
|
327
|
+
if (result.status === 'conflict') {
|
|
328
|
+
process.stderr.write(`policy: ${result.file} is JSONC or invalid; merge manually\n`);
|
|
329
|
+
return 2;
|
|
330
|
+
}
|
|
331
|
+
process.stdout.write(
|
|
332
|
+
`${options.dryRun ? 'Would merge' : 'Merged'} ${result.file}: ` +
|
|
333
|
+
`${result.config.permissions.length} rules\n`,
|
|
334
|
+
);
|
|
335
|
+
if (result.backup) process.stdout.write(`Backup: ${result.backup}\n`);
|
|
336
|
+
return 0;
|
|
337
|
+
}
|
|
338
|
+
if (action === 'audit') {
|
|
339
|
+
const existing = await readProjectConfig(root);
|
|
340
|
+
if (!existing) {
|
|
341
|
+
process.stdout.write('policy: no project permission profile\n');
|
|
342
|
+
return 1;
|
|
343
|
+
}
|
|
344
|
+
let config;
|
|
345
|
+
try {
|
|
346
|
+
config = JSON.parse(existing.text);
|
|
347
|
+
} catch (error) {
|
|
348
|
+
process.stderr.write(`policy: ${existing.file}: ${error.message}\n`);
|
|
349
|
+
return 2;
|
|
350
|
+
}
|
|
351
|
+
const audit = auditPermissions(config);
|
|
352
|
+
if (!existsSync(path.join(root, POLICY_PLUGIN_TARGET))) {
|
|
353
|
+
audit.warnings.push('no policy plugin; global allow rules can override project denies');
|
|
354
|
+
}
|
|
355
|
+
for (const warning of audit.warnings) process.stdout.write(`! ${warning}\n`);
|
|
356
|
+
for (const error of audit.errors) process.stdout.write(`✖ ${error}\n`);
|
|
357
|
+
process.stdout.write(
|
|
358
|
+
`policy: ${audit.errors.length} errors, ${audit.warnings.length} warnings\n`,
|
|
359
|
+
);
|
|
360
|
+
return audit.errors.length > 0 ? 3 : 0;
|
|
361
|
+
}
|
|
362
|
+
throw new Error(`unknown policy command: ${action}`);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (command === 'scan') {
|
|
366
|
+
const project = await scanProject(root);
|
|
367
|
+
const result = await writeProjectProfile(root, project);
|
|
368
|
+
if (options.json) {
|
|
369
|
+
process.stdout.write(`${JSON.stringify(project, null, 2)}\n`);
|
|
370
|
+
} else {
|
|
371
|
+
process.stdout.write(`Project profile: ${project.packageManager}, ${project.language}`);
|
|
372
|
+
process.stdout.write(project.framework ? `, ${project.framework}\n` : '\n');
|
|
373
|
+
for (const [name, command] of Object.entries(project.commands)) {
|
|
374
|
+
process.stdout.write(` ${name}: ${command}\n`);
|
|
375
|
+
}
|
|
376
|
+
if (result.verifyCreated) process.stdout.write(' verify: wrote .kitskill/verify.json\n');
|
|
377
|
+
if (result.docSkipped) process.stdout.write(` ${result.reason}\n`);
|
|
378
|
+
}
|
|
379
|
+
return 0;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (command === 'profile') {
|
|
383
|
+
const action = options.args[0] ?? 'show';
|
|
384
|
+
const profile = await loadProfile(root);
|
|
385
|
+
if (action === 'show') {
|
|
386
|
+
process.stdout.write(
|
|
387
|
+
options.json ? `${JSON.stringify(profile, null, 2)}\n` : renderProfile(profile),
|
|
388
|
+
);
|
|
389
|
+
return 0;
|
|
390
|
+
}
|
|
391
|
+
if (action === 'set') {
|
|
392
|
+
const domain = options.args[1];
|
|
393
|
+
const level = options.args[2];
|
|
394
|
+
if (!domain || !level) {
|
|
395
|
+
throw new Error(`usage: kitskill profile set <${DOMAINS.join('|')}> <${LEVELS.join('|')}>`);
|
|
396
|
+
}
|
|
397
|
+
const next = await setExperience(root, domain, level);
|
|
398
|
+
process.stdout.write(`profile: ${domain} = ${level}\n`);
|
|
399
|
+
return 0;
|
|
400
|
+
}
|
|
401
|
+
if (action === 'explain') {
|
|
402
|
+
const level = options.args[1];
|
|
403
|
+
if (!level) {
|
|
404
|
+
throw new Error(`usage: kitskill profile explain <${EXPLAIN_LEVELS.join('|')}>`);
|
|
405
|
+
}
|
|
406
|
+
await setExplain(root, level);
|
|
407
|
+
process.stdout.write(`profile: explain = ${level}\n`);
|
|
408
|
+
return 0;
|
|
409
|
+
}
|
|
410
|
+
if (action === 'language') {
|
|
411
|
+
const language = options.args[1];
|
|
412
|
+
if (!language) throw new Error('usage: kitskill profile language <code>');
|
|
413
|
+
await setLanguage(root, language);
|
|
414
|
+
process.stdout.write(`profile: language = ${language}\n`);
|
|
415
|
+
return 0;
|
|
416
|
+
}
|
|
417
|
+
throw new Error(`unknown profile command: ${action}`);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (command === 'doctor') {
|
|
421
|
+
const result = await doctorProject(root);
|
|
422
|
+
const permissions = await auditProjectPermissions(root);
|
|
423
|
+
result.errors.push(...permissions.errors);
|
|
424
|
+
result.warnings.push(...permissions.warnings);
|
|
425
|
+
if (options.json) {
|
|
426
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
427
|
+
} else {
|
|
428
|
+
process.stdout.write(`kitskill doctor\n`);
|
|
429
|
+
process.stdout.write(` errors: ${result.errors.length}\n`);
|
|
430
|
+
for (const item of result.errors) process.stdout.write(` ✖ ${item}\n`);
|
|
431
|
+
process.stdout.write(` warnings: ${result.warnings.length}\n`);
|
|
432
|
+
for (const item of result.warnings) process.stdout.write(` ! ${item}\n`);
|
|
433
|
+
}
|
|
434
|
+
return result.errors.length > 0 ? 3 : 0;
|
|
435
|
+
}
|
|
436
|
+
} catch (error) {
|
|
437
|
+
process.stderr.write(`kitskill: ${error.message}\n`);
|
|
438
|
+
return error.message.startsWith('unsupported target') || error.message.startsWith('--')
|
|
439
|
+
? 2
|
|
440
|
+
: 1;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
process.stderr.write(`unknown command: ${command}\n\n${usage()}\n`);
|
|
444
|
+
return 2;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
function isEntryPoint() {
|
|
448
|
+
if (!process.argv[1]) return false;
|
|
449
|
+
try {
|
|
450
|
+
return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));
|
|
451
|
+
} catch {
|
|
452
|
+
return false;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (isEntryPoint()) {
|
|
457
|
+
main(process.argv.slice(2)).then(
|
|
458
|
+
(code) => process.exit(code),
|
|
459
|
+
(error) => {
|
|
460
|
+
process.stderr.write(`${error.stack ?? error.message}\n`);
|
|
461
|
+
process.exit(1);
|
|
462
|
+
},
|
|
463
|
+
);
|
|
464
|
+
}
|
package/lib/fs-safe.mjs
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { lstat, mkdir, readFile, realpath, rmdir, unlink, writeFile } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
export function resolveInside(root, relative) {
|
|
5
|
+
if (typeof relative !== 'string' || relative.length === 0) {
|
|
6
|
+
throw new Error('empty path');
|
|
7
|
+
}
|
|
8
|
+
if (path.isAbsolute(relative)) {
|
|
9
|
+
throw new Error(`absolute path refused: ${relative}`);
|
|
10
|
+
}
|
|
11
|
+
const abs = path.resolve(root, relative);
|
|
12
|
+
const prefix = `${path.resolve(root)}${path.sep}`;
|
|
13
|
+
if (abs !== path.resolve(root) && !abs.startsWith(prefix)) {
|
|
14
|
+
throw new Error(`path escapes project root: ${relative}`);
|
|
15
|
+
}
|
|
16
|
+
return abs;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Refuses a path whose existing components include a symlink that leaves the
|
|
21
|
+
* project root. `resolveInside` is lexical; this is the filesystem check.
|
|
22
|
+
*/
|
|
23
|
+
export async function assertRealInside(root, abs, rootReal, { followLast = true } = {}) {
|
|
24
|
+
const rootAbs = path.resolve(root);
|
|
25
|
+
const realRoot = rootReal ?? (await realpath(rootAbs));
|
|
26
|
+
const target = path.resolve(abs);
|
|
27
|
+
const rel = path.relative(rootAbs, target);
|
|
28
|
+
if (rel === '') return;
|
|
29
|
+
if (rel.startsWith('..') || path.isAbsolute(rel)) {
|
|
30
|
+
throw new Error(`path escapes project root: ${rel}`);
|
|
31
|
+
}
|
|
32
|
+
const parts = rel.split(path.sep);
|
|
33
|
+
let current = rootAbs;
|
|
34
|
+
for (const [index, part] of parts.entries()) {
|
|
35
|
+
current = path.join(current, part);
|
|
36
|
+
let stat;
|
|
37
|
+
try {
|
|
38
|
+
stat = await lstat(current);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
if (error.code === 'ENOENT') return;
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
if (!stat.isSymbolicLink()) continue;
|
|
44
|
+
if (!followLast && index === parts.length - 1) return;
|
|
45
|
+
let real;
|
|
46
|
+
try {
|
|
47
|
+
real = await realpath(current);
|
|
48
|
+
} catch {
|
|
49
|
+
throw new Error(`refusing dangling symlink: ${path.relative(rootAbs, current)}`);
|
|
50
|
+
}
|
|
51
|
+
if (real !== realRoot && !real.startsWith(`${realRoot}${path.sep}`)) {
|
|
52
|
+
throw new Error(`refusing symlink outside project root: ${path.relative(rootAbs, current)}`);
|
|
53
|
+
}
|
|
54
|
+
current = real;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function pathExists(abs) {
|
|
59
|
+
try {
|
|
60
|
+
await lstat(abs);
|
|
61
|
+
return true;
|
|
62
|
+
} catch {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function readText(abs) {
|
|
68
|
+
try {
|
|
69
|
+
return await readFile(abs, 'utf8');
|
|
70
|
+
} catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function backupFile(root, abs, backupRoot) {
|
|
76
|
+
const rel = path.relative(root, abs);
|
|
77
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
78
|
+
const dest = path.join(backupRoot, stamp, rel);
|
|
79
|
+
await assertRealInside(root, dest);
|
|
80
|
+
await mkdir(path.dirname(dest), { recursive: true });
|
|
81
|
+
await writeFile(dest, await readFile(abs));
|
|
82
|
+
return dest;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function writeFileSafe(root, relative, content, { backupRoot } = {}) {
|
|
86
|
+
const abs = resolveInside(root, relative);
|
|
87
|
+
await assertRealInside(root, abs);
|
|
88
|
+
await mkdir(path.dirname(abs), { recursive: true });
|
|
89
|
+
if (backupRoot && (await pathExists(abs))) {
|
|
90
|
+
await backupFile(root, abs, backupRoot);
|
|
91
|
+
}
|
|
92
|
+
await writeFile(abs, content, 'utf8');
|
|
93
|
+
return abs;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Removes a file or symlink. Never follows a symlink and never removes a
|
|
98
|
+
* non-empty directory.
|
|
99
|
+
*/
|
|
100
|
+
export async function removeEntry(abs) {
|
|
101
|
+
let stat;
|
|
102
|
+
try {
|
|
103
|
+
stat = await lstat(abs);
|
|
104
|
+
} catch {
|
|
105
|
+
return { removed: false, reason: 'missing' };
|
|
106
|
+
}
|
|
107
|
+
if (stat.isSymbolicLink() || stat.isFile()) {
|
|
108
|
+
await unlink(abs);
|
|
109
|
+
return { removed: true, kind: stat.isSymbolicLink() ? 'symlink' : 'file' };
|
|
110
|
+
}
|
|
111
|
+
if (!stat.isDirectory()) return { removed: false, reason: 'unsupported' };
|
|
112
|
+
await rmdir(abs);
|
|
113
|
+
return { removed: true, kind: 'directory' };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export async function pruneEmptyParents(root, abs, stopAt = root) {
|
|
117
|
+
let current = path.dirname(abs);
|
|
118
|
+
const stop = path.resolve(stopAt);
|
|
119
|
+
while (current.startsWith(stop) && current !== stop) {
|
|
120
|
+
try {
|
|
121
|
+
await rmdir(current);
|
|
122
|
+
} catch {
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
current = path.dirname(current);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export async function readLinkTarget(abs) {
|
|
130
|
+
const { readlink } = await import('node:fs/promises');
|
|
131
|
+
try {
|
|
132
|
+
return await readlink(abs);
|
|
133
|
+
} catch {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
package/lib/hook.mjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { chmod, copyFile, mkdir, readFile, unlink, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
|
|
6
|
+
export const HOOK_MARKER = '# managed by kitskill verify';
|
|
7
|
+
const CONFIG_PATH = '.kitskill/verify.json';
|
|
8
|
+
|
|
9
|
+
export function hookScript() {
|
|
10
|
+
return [
|
|
11
|
+
'#!/bin/sh',
|
|
12
|
+
HOOK_MARKER,
|
|
13
|
+
'if [ ! -f .kitskill/scripts/verify.mjs ]; then',
|
|
14
|
+
' echo "kitskill: .kitskill/scripts/verify.mjs is missing" >&2',
|
|
15
|
+
' exit 1',
|
|
16
|
+
'fi',
|
|
17
|
+
'node .kitskill/scripts/verify.mjs',
|
|
18
|
+
'',
|
|
19
|
+
].join('\n');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function gitDir(root) {
|
|
23
|
+
const run = spawnSync('git', ['rev-parse', '--git-dir'], {
|
|
24
|
+
cwd: root,
|
|
25
|
+
encoding: 'utf8',
|
|
26
|
+
});
|
|
27
|
+
if (run.status !== 0) throw new Error('not a git repository');
|
|
28
|
+
return path.resolve(root, run.stdout.trim());
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function hookPath(root) {
|
|
32
|
+
return path.join(gitDir(root), 'hooks', 'pre-commit');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function isManagedHook(text) {
|
|
36
|
+
return String(text ?? '').includes(HOOK_MARKER);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function installHook(root, { force = false } = {}) {
|
|
40
|
+
if (!existsSync(path.join(root, CONFIG_PATH))) {
|
|
41
|
+
throw new Error(`create ${CONFIG_PATH} before installing the hook`);
|
|
42
|
+
}
|
|
43
|
+
const target = hookPath(root);
|
|
44
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
45
|
+
let backup = null;
|
|
46
|
+
if (existsSync(target)) {
|
|
47
|
+
const current = await readFile(target, 'utf8');
|
|
48
|
+
if (!isManagedHook(current)) {
|
|
49
|
+
if (!force) {
|
|
50
|
+
throw new Error('pre-commit exists and is not managed by kitskill; use --force');
|
|
51
|
+
}
|
|
52
|
+
backup = `${target}.kitskill.bak-${Date.now()}`;
|
|
53
|
+
await copyFile(target, backup);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
await writeFile(target, hookScript(), 'utf8');
|
|
57
|
+
await chmod(target, 0o755);
|
|
58
|
+
return { path: target, backup };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function removeHook(root) {
|
|
62
|
+
const target = hookPath(root);
|
|
63
|
+
if (!existsSync(target)) {
|
|
64
|
+
return { path: target, removed: false, reason: 'missing' };
|
|
65
|
+
}
|
|
66
|
+
const current = await readFile(target, 'utf8');
|
|
67
|
+
if (!isManagedHook(current)) {
|
|
68
|
+
throw new Error('pre-commit is not managed by kitskill; leaving it in place');
|
|
69
|
+
}
|
|
70
|
+
await unlink(target);
|
|
71
|
+
return { path: target, removed: true };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function hookStatus(root) {
|
|
75
|
+
const target = hookPath(root);
|
|
76
|
+
if (!existsSync(target)) return { path: target, installed: false };
|
|
77
|
+
const current = await readFile(target, 'utf8');
|
|
78
|
+
return { path: target, installed: isManagedHook(current), managed: isManagedHook(current) };
|
|
79
|
+
}
|