@specdest/devkit 0.3.1 → 0.3.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@specdest/devkit",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "specdest": "src/cli.js"
@@ -1,6 +1,6 @@
1
1
  import { spawn } from 'node:child_process';
2
- import { readFile } from 'node:fs/promises';
3
- import { join } from 'node:path';
2
+ import { access, readdir, readFile, rm, rmdir } from 'node:fs/promises';
3
+ import { join, relative } from 'node:path';
4
4
 
5
5
  import { createCanonicalSource, detectProjectStack } from './canonical-source.js';
6
6
  import { applicableCatalogEntries, prepareCachedManifest } from './canonical-project.js';
@@ -19,6 +19,19 @@ function execute(command, args, cwd) {
19
19
  });
20
20
  }
21
21
 
22
+ async function fileExists(path) {
23
+ try { await access(path); return true; } catch (error) { if (error?.code === 'ENOENT') return false; throw error; }
24
+ }
25
+
26
+ async function missingProjectScaffold(projectRoot, copies) {
27
+ for (const copy of copies ?? []) {
28
+ if (copy.mode !== 'scaffold' && copy.mode !== 'create-only') continue;
29
+ const target = String(copy.target ?? copy.path ?? '');
30
+ if (target && !(await fileExists(join(projectRoot, target)))) return true;
31
+ }
32
+ return false;
33
+ }
34
+
22
35
  function flags(args) {
23
36
  const known = new Set(['--refresh', '--json']);
24
37
  for (const arg of args) if (!known.has(arg)) throw new Error(`Unknown option: ${arg}`);
@@ -76,40 +89,71 @@ export async function canonicalStatus(projectRoot, source, options = {}) {
76
89
  };
77
90
  }
78
91
 
92
+ async function cleanupCanonicalSkillTree(projectRoot, allowedFiles) {
93
+ const root = join(projectRoot, '.agents', 'skills');
94
+ async function walk(directory) {
95
+ let entries;
96
+ try { entries = await readdir(directory, { withFileTypes: true }); }
97
+ catch (error) { if (error?.code === 'ENOENT') return; throw error; }
98
+ for (const entry of entries) {
99
+ const path = join(directory, entry.name);
100
+ if (entry.isDirectory()) {
101
+ await walk(path);
102
+ try { await rmdir(path); } catch (error) { if (error?.code !== 'ENOTEMPTY' && error?.code !== 'ENOENT') throw error; }
103
+ continue;
104
+ }
105
+ const projectPath = relative(projectRoot, path).split('\\').join('/');
106
+ if (!allowedFiles.has(projectPath)) await rm(path, { force: true });
107
+ }
108
+ }
109
+ await walk(root);
110
+ }
111
+
79
112
  export async function canonicalSync(projectRoot, source) {
80
113
  const current = await readInstalledState(projectRoot);
81
114
  const { stack, applicable, canonicalCheckRoot, checkerRuntimeRoot } = await canonicalContext(projectRoot, source, { refresh: true, withCheckers: true });
82
115
  const changed = [];
116
+ const allowedSkillFiles = new Set();
83
117
  for (const entry of applicable) {
84
118
  const prepared = await cachedManifest(source, entry, canonicalCheckRoot, checkerRuntimeRoot);
119
+ for (const copy of prepared.manifest.copies ?? []) {
120
+ const target = String(copy.target ?? copy.path ?? '');
121
+ if (target.startsWith('.agents/skills/')) allowedSkillFiles.add(target);
122
+ }
85
123
  const installed = current.installed[entry.id];
86
124
  if (!installed) {
87
125
  await installRule({ projectRoot, payloadRoot: source.cachedModulePath(entry.id, entry.version), manifest: prepared.manifest });
88
126
  changed.push({ id: entry.id, action: 'installed', version: entry.version });
89
- } else if (installed.version !== entry.version || installed.contentHash !== entry.contentHash || installed.check?.command?.[0] !== 'specdest-cache') {
127
+ } else if (
128
+ installed.version !== entry.version ||
129
+ installed.contentHash !== entry.contentHash ||
130
+ (prepared.checkerPath && installed.check?.command?.[0] !== 'specdest-cache') ||
131
+ await missingProjectScaffold(projectRoot, prepared.manifest.copies)
132
+ ) {
90
133
  await syncRule({ projectRoot, payloadRoot: source.cachedModulePath(entry.id, entry.version), manifest: prepared.manifest });
91
134
  changed.push({ id: entry.id, action: 'updated', version: entry.version });
92
135
  }
93
136
  }
137
+ await cleanupCanonicalSkillTree(projectRoot, allowedSkillFiles);
94
138
  return { stack, applicable: applicable.length, changed };
95
139
  }
96
140
 
97
141
  export async function canonicalCheck(projectRoot, source) {
98
142
  const current = await readInstalledState(projectRoot);
99
- const { applicable, canonicalCheckRoot, checkerRuntimeRoot } = await canonicalContext(projectRoot, source, { offline: true, withCheckers: true });
143
+ const { applicable, canonicalCheckRoot, checkerRuntimeRoot } = await canonicalContext(projectRoot, source, { offline: false, withCheckers: true });
100
144
  const byId = new Map(applicable.map((entry) => [entry.id, entry]));
101
145
  const results = {};
102
146
  let exitCode = 0;
103
147
  for (const [id, installed] of Object.entries(current.installed).sort(([a], [b]) => a.localeCompare(b))) {
104
148
  const entry = byId.get(id);
105
149
  if (!entry || installed.kind !== 'rule') continue;
106
- if (!(await source.hasModule(id, entry.version))) {
107
- results[id] = { status: 'fail', exitCode: 1, stderr: 'Rule cache missing; run specdest sync first.' };
150
+ const prepared = await cachedManifest(source, entry, canonicalCheckRoot, checkerRuntimeRoot);
151
+ if (!prepared?.checkerPath) {
152
+ if ((entry.enforcement ?? 'guidance') === 'guidance') continue;
153
+ results[id] = { status: 'fail', exitCode: 1, stderr: 'Canonical checker unavailable for enforced installed rule.' };
108
154
  exitCode = 1;
109
155
  continue;
110
156
  }
111
- const prepared = await cachedManifest(source, entry, canonicalCheckRoot, checkerRuntimeRoot, { offline: true });
112
- if (!prepared?.checkerPath) continue;
113
157
  const execution = await execute('node', [prepared.checkerPath], projectRoot);
114
158
  results[id] = { status: execution.exitCode === 0 ? 'pass' : 'fail', ...execution };
115
159
  if (execution.exitCode !== 0) exitCode = 1;
@@ -1,5 +1,5 @@
1
1
  import { cp, mkdir, readFile, rm } from 'node:fs/promises';
2
- import { dirname, join } from 'node:path';
2
+ import { basename, dirname, join } from 'node:path';
3
3
 
4
4
  function isSkillTarget(copy) {
5
5
  return String(copy?.target ?? copy?.path ?? '').startsWith('.agents/skills/');
@@ -27,6 +27,26 @@ async function readManifest(root) {
27
27
  return JSON.parse(await readFile(join(root, 'manifest.json'), 'utf8'));
28
28
  }
29
29
 
30
+ async function cacheProjectCopies(copies, sourceRoot, ruleRoot, label) {
31
+ const result = [];
32
+ for (const copy of copies.filter(keepProjectCopy)) {
33
+ if (sourceRoot === ruleRoot) {
34
+ result.push(copy);
35
+ continue;
36
+ }
37
+ const source = String(copy.source ?? '');
38
+ const cachedSource = join('.project-scaffolds', label, source);
39
+ const target = join(ruleRoot, cachedSource);
40
+ await mkdir(dirname(target), { recursive: true });
41
+ await cp(join(sourceRoot, source), target, {
42
+ recursive: copy.type === 'directory' || copy.directory === true,
43
+ force: true,
44
+ });
45
+ result.push({ ...copy, source: cachedSource });
46
+ }
47
+ return result;
48
+ }
49
+
30
50
  function runtimeCheckerPath(manifest, runtimeRoot) {
31
51
  if (manifest?.check?.command?.[0] !== 'node') return null;
32
52
  const target = String(manifest.check.command[1] ?? '');
@@ -51,16 +71,22 @@ export async function prepareCachedManifest(entry, ruleRoot, canonicalCheckRoot,
51
71
  }
52
72
  await materializeCopies(raw, ruleRoot, runtimeRoot);
53
73
 
74
+ const copies = await cacheProjectCopies(raw.copies ?? [], ruleRoot, ruleRoot, entry.id);
54
75
  let checkerPath = runtimeCheckerPath(raw, runtimeRoot);
55
76
  if (!checkerPath && raw.checker?.path && canonicalCheckRoot) {
56
77
  const relative = String(raw.checker.path).replace(/^canonical-check\//u, '');
57
78
  const implementationRoot = dirname(join(canonicalCheckRoot, relative));
58
79
  const implementationManifest = await readManifest(implementationRoot);
59
80
  await materializeCopies(implementationManifest, implementationRoot, runtimeRoot);
81
+ copies.push(...await cacheProjectCopies(
82
+ implementationManifest.copies ?? [],
83
+ implementationRoot,
84
+ ruleRoot,
85
+ basename(implementationRoot),
86
+ ));
60
87
  checkerPath = runtimeCheckerPath(implementationManifest, runtimeRoot);
61
88
  }
62
89
 
63
- const copies = (raw.copies ?? []).filter(keepProjectCopy);
64
90
  const markdownSections = (raw.markdownSections ?? []).filter(
65
91
  (section) => !String(section?.body ?? section?.content ?? '').includes('.agents/skills/'),
66
92
  );
@@ -70,7 +96,7 @@ export async function prepareCachedManifest(entry, ruleRoot, canonicalCheckRoot,
70
96
  version: entry.version ?? raw.version,
71
97
  ...(entry.contentHash ? { contentHash: entry.contentHash } : {}),
72
98
  ...(entry.revision ? { revision: entry.revision } : {}),
73
- copies,
99
+ copies: [...new Map(copies.map((copy) => [String(copy.target ?? copy.path ?? copy.source), copy])).values()],
74
100
  markdownSections,
75
101
  ...(checkerPath ? { check: { command: ['specdest-cache', entry.id] } } : {}),
76
102
  };