@specdest/devkit 0.3.2 → 0.3.4
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/README.md +2 -2
- package/package.json +1 -1
- package/src/canonical-project-operations.js +26 -8
- package/src/canonical-source.js +143 -39
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
`@specdest/devkit` is the canonical Devkit CLI for Specdest repositories.
|
|
4
4
|
|
|
5
|
-
The npm package contains only the CLI/core runtime. Canonical rules stay in the private Devkit GitHub repository and are fetched into a local machine cache when needed. Rule changes therefore do not require an npm release.
|
|
5
|
+
The npm package contains only the CLI/core runtime. Canonical rules stay in the private Devkit GitHub repository and are fetched with normal Git/SSH transport into a local machine cache when needed. Rule changes therefore do not require an npm release.
|
|
6
6
|
|
|
7
7
|
## Commands
|
|
8
8
|
|
|
@@ -24,4 +24,4 @@ See [`docs/operations.md`](docs/operations.md) for cache, dashboard, CI, and rel
|
|
|
24
24
|
|
|
25
25
|
## Canonical cache runtime
|
|
26
26
|
|
|
27
|
-
Canonical authoring lives under `modules/**`. The CLI
|
|
27
|
+
Canonical authoring lives under `modules/**`. The CLI refreshes a single-branch bare Git source and hydrates required modules from the same catalog revision into the local Devkit cache and materializes their `.agents/skills/**` target layout **inside the cache** so checker-relative imports keep working without copying canonical skills/checkers into project repositories. `catalog/**` is compatibility/test data, not a rule-authoring source.
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
-
import { readdir, readFile, rm, rmdir } from 'node:fs/promises';
|
|
2
|
+
import { access, readdir, readFile, rm, rmdir } from 'node:fs/promises';
|
|
3
3
|
import { join, relative } from 'node:path';
|
|
4
4
|
|
|
5
5
|
import { createCanonicalSource, detectProjectStack } from './canonical-source.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}`);
|
|
@@ -26,8 +39,8 @@ function flags(args) {
|
|
|
26
39
|
}
|
|
27
40
|
|
|
28
41
|
async function cachedManifest(source, entry, canonicalCheckRoot, checkerRuntimeRoot, { offline = false } = {}) {
|
|
29
|
-
if (offline && !(await source.hasModule(entry.id, entry.version))) return null;
|
|
30
|
-
const root = await source.module(entry.id, entry.version);
|
|
42
|
+
if (offline && !(await source.hasModule(entry.id, entry.version, entry.contentHash))) return null;
|
|
43
|
+
const root = await source.module(entry.id, entry.version, { contentHash: entry.contentHash });
|
|
31
44
|
return await prepareCachedManifest(entry, root, canonicalCheckRoot, checkerRuntimeRoot);
|
|
32
45
|
}
|
|
33
46
|
|
|
@@ -40,13 +53,13 @@ async function canonicalContext(projectRoot, source, options = {}) {
|
|
|
40
53
|
let canonicalCheckRoot = null;
|
|
41
54
|
let checkerRuntimeRoot = null;
|
|
42
55
|
if (options.withCheckers && canonicalCheck) {
|
|
43
|
-
if (!options.offline || await source.hasModule(canonicalCheck.id, canonicalCheck.version)) {
|
|
44
|
-
canonicalCheckRoot = await source.module(canonicalCheck.id, canonicalCheck.version);
|
|
56
|
+
if (!options.offline || await source.hasModule(canonicalCheck.id, canonicalCheck.version, canonicalCheck.contentHash)) {
|
|
57
|
+
canonicalCheckRoot = await source.module(canonicalCheck.id, canonicalCheck.version, { contentHash: canonicalCheck.contentHash });
|
|
45
58
|
}
|
|
46
59
|
}
|
|
47
60
|
if (options.withCheckers && checkerRuntime) {
|
|
48
|
-
if (!options.offline || await source.hasModule(checkerRuntime.id, checkerRuntime.version)) {
|
|
49
|
-
checkerRuntimeRoot = await source.module(checkerRuntime.id, checkerRuntime.version);
|
|
61
|
+
if (!options.offline || await source.hasModule(checkerRuntime.id, checkerRuntime.version, checkerRuntime.contentHash)) {
|
|
62
|
+
checkerRuntimeRoot = await source.module(checkerRuntime.id, checkerRuntime.version, { contentHash: checkerRuntime.contentHash });
|
|
50
63
|
}
|
|
51
64
|
}
|
|
52
65
|
return { catalog, stack, applicable, canonicalCheckRoot, checkerRuntimeRoot };
|
|
@@ -111,7 +124,12 @@ export async function canonicalSync(projectRoot, source) {
|
|
|
111
124
|
if (!installed) {
|
|
112
125
|
await installRule({ projectRoot, payloadRoot: source.cachedModulePath(entry.id, entry.version), manifest: prepared.manifest });
|
|
113
126
|
changed.push({ id: entry.id, action: 'installed', version: entry.version });
|
|
114
|
-
} else if (
|
|
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
|
+
) {
|
|
115
133
|
await syncRule({ projectRoot, payloadRoot: source.cachedModulePath(entry.id, entry.version), manifest: prepared.manifest });
|
|
116
134
|
changed.push({ id: entry.id, action: 'updated', version: entry.version });
|
|
117
135
|
}
|
package/src/canonical-source.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import { homedir } from 'node:os';
|
|
3
3
|
import { dirname, join } from 'node:path';
|
|
4
|
-
import { mkdir, readFile, stat, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { mkdir, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises';
|
|
5
5
|
|
|
6
6
|
const DEFAULT_REPOSITORY = 'specdest-org/devkit';
|
|
7
7
|
const DEFAULT_REF = 'main';
|
|
@@ -14,6 +14,15 @@ function defaultCacheRoot(env = process.env) {
|
|
|
14
14
|
return join(env.XDG_CACHE_HOME?.trim() || join(homedir(), '.cache'), 'specdest', 'devkit');
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
function repositoryUrl(options = {}) {
|
|
18
|
+
if (options.repositoryUrl) return String(options.repositoryUrl);
|
|
19
|
+
const repository = String(options.repository ?? DEFAULT_REPOSITORY);
|
|
20
|
+
if (repository.startsWith('/') || repository.startsWith('.') || repository.includes('://') || repository.startsWith('git@')) {
|
|
21
|
+
return repository;
|
|
22
|
+
}
|
|
23
|
+
return `git@github.com:${repository}.git`;
|
|
24
|
+
}
|
|
25
|
+
|
|
17
26
|
function run(command, args, options = {}) {
|
|
18
27
|
return new Promise((resolve, reject) => {
|
|
19
28
|
const child = spawn(command, args, { cwd: options.cwd, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
@@ -28,36 +37,42 @@ function run(command, args, options = {}) {
|
|
|
28
37
|
});
|
|
29
38
|
}
|
|
30
39
|
|
|
31
|
-
async function
|
|
32
|
-
const
|
|
33
|
-
|
|
40
|
+
async function ensureGitRepository(cacheRoot, options = {}) {
|
|
41
|
+
const gitDir = join(cacheRoot, 'source.git');
|
|
42
|
+
const url = repositoryUrl(options);
|
|
43
|
+
const ref = String(options.ref ?? DEFAULT_REF);
|
|
44
|
+
if (!(await exists(join(gitDir, 'HEAD')))) {
|
|
45
|
+
await mkdir(cacheRoot, { recursive: true });
|
|
46
|
+
await run('git', ['clone', '--bare', '--single-branch', '--branch', ref, url, gitDir]);
|
|
47
|
+
} else {
|
|
48
|
+
const configured = (await run('git', ['--git-dir', gitDir, 'remote', 'get-url', 'origin'])).trim();
|
|
49
|
+
if (configured !== url) throw new Error(`Canonical Devkit cache remote mismatch: ${configured}`);
|
|
50
|
+
}
|
|
51
|
+
return gitDir;
|
|
34
52
|
}
|
|
35
53
|
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
54
|
+
async function fetchGitRevision(cacheRoot, options = {}) {
|
|
55
|
+
const gitDir = await ensureGitRepository(cacheRoot, options);
|
|
56
|
+
const ref = String(options.ref ?? DEFAULT_REF);
|
|
57
|
+
await run('git', ['--git-dir', gitDir, 'fetch', '--prune', 'origin', ref]);
|
|
58
|
+
const revision = (await run('git', ['--git-dir', gitDir, 'rev-parse', 'FETCH_HEAD'])).trim();
|
|
59
|
+
return { gitDir, revision };
|
|
40
60
|
}
|
|
41
61
|
|
|
42
|
-
|
|
62
|
+
async function readGitFile(gitDir, revision, path) {
|
|
63
|
+
return await run('git', ['--git-dir', gitDir, 'show', `${revision}:${path}`]);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function readGitModule(gitDir, revision, id) {
|
|
67
|
+
const prefix = `modules/${id}/`;
|
|
68
|
+
const listed = await run('git', ['--git-dir', gitDir, 'ls-tree', '-r', '--name-only', revision, '--', `modules/${id}`]);
|
|
69
|
+
const paths = listed.split('\n').map((value) => value.trim()).filter((value) => value.startsWith(prefix));
|
|
70
|
+
if (!paths.length) throw new Error(`Canonical module directory not found: ${id}`);
|
|
43
71
|
const files = [];
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
const path = pending.pop();
|
|
47
|
-
const entries = await ghJson(path, options.repository, options.ref);
|
|
48
|
-
if (!Array.isArray(entries)) throw new Error(`Canonical module directory not found: ${id}`);
|
|
49
|
-
for (const entry of entries) {
|
|
50
|
-
if (entry.type === 'dir') pending.push(entry.path);
|
|
51
|
-
else if (entry.type === 'file') {
|
|
52
|
-
const file = await ghJson(entry.path, options.repository, options.ref);
|
|
53
|
-
files.push({
|
|
54
|
-
path: entry.path.slice(`modules/${id}/`.length),
|
|
55
|
-
content: Buffer.from(String(file.content ?? '').replace(/\n/g, ''), 'base64').toString('utf8'),
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
}
|
|
72
|
+
for (const path of paths.sort()) {
|
|
73
|
+
files.push({ path: path.slice(prefix.length), content: await readGitFile(gitDir, revision, path) });
|
|
59
74
|
}
|
|
60
|
-
return { files
|
|
75
|
+
return { files };
|
|
61
76
|
}
|
|
62
77
|
|
|
63
78
|
async function writePayload(root, files) {
|
|
@@ -68,36 +83,125 @@ async function writePayload(root, files) {
|
|
|
68
83
|
}
|
|
69
84
|
}
|
|
70
85
|
|
|
86
|
+
async function readMarker(path) {
|
|
87
|
+
try { return JSON.parse(await readFile(path, 'utf8')); } catch (error) {
|
|
88
|
+
if (error?.code === 'ENOENT' || error instanceof SyntaxError) return null;
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function markerMatches(marker, id, version, contentHash) {
|
|
94
|
+
return Boolean(marker && marker.id === id && marker.version === version && (!contentHash || marker.contentHash === contentHash));
|
|
95
|
+
}
|
|
96
|
+
|
|
71
97
|
export function createCanonicalSource(options = {}) {
|
|
72
98
|
const cacheRoot = options.cacheRoot ?? defaultCacheRoot(options.env);
|
|
73
99
|
const catalogPath = join(cacheRoot, 'catalog.json');
|
|
74
|
-
const
|
|
75
|
-
const
|
|
100
|
+
const catalogMetaPath = join(cacheRoot, 'catalog-meta.json');
|
|
101
|
+
const customFetch = Boolean(options.fetchCatalog || options.fetchModule);
|
|
102
|
+
let activeRevision = null;
|
|
103
|
+
let activeGitDir = null;
|
|
104
|
+
|
|
105
|
+
async function cachedCatalog() {
|
|
106
|
+
if (!(await exists(catalogPath)) || !(await exists(catalogMetaPath))) return null;
|
|
107
|
+
const meta = JSON.parse(await readFile(catalogMetaPath, 'utf8'));
|
|
108
|
+
if (!customFetch && (meta.repositoryUrl !== repositoryUrl(options) || meta.ref !== String(options.ref ?? DEFAULT_REF))) return null;
|
|
109
|
+
activeRevision = meta.revision ?? null;
|
|
110
|
+
if (!customFetch) activeGitDir = await ensureGitRepository(cacheRoot, options);
|
|
111
|
+
return JSON.parse(await readFile(catalogPath, 'utf8'));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function refreshCatalog() {
|
|
115
|
+
let catalog;
|
|
116
|
+
let revision;
|
|
117
|
+
if (options.fetchCatalog) {
|
|
118
|
+
catalog = await options.fetchCatalog();
|
|
119
|
+
revision = String(catalog.repositoryRevision ?? catalog.package?.version ?? 'custom');
|
|
120
|
+
} else {
|
|
121
|
+
const fetched = await fetchGitRevision(cacheRoot, options);
|
|
122
|
+
activeGitDir = fetched.gitDir;
|
|
123
|
+
revision = fetched.revision;
|
|
124
|
+
catalog = JSON.parse(await readGitFile(activeGitDir, revision, 'catalog.json'));
|
|
125
|
+
}
|
|
126
|
+
activeRevision = revision;
|
|
127
|
+
await mkdir(cacheRoot, { recursive: true });
|
|
128
|
+
await writeFile(catalogPath, `${JSON.stringify(catalog, null, 2)}\n`, 'utf8');
|
|
129
|
+
await writeFile(catalogMetaPath, `${JSON.stringify({
|
|
130
|
+
revision,
|
|
131
|
+
ref: String(options.ref ?? DEFAULT_REF),
|
|
132
|
+
repositoryUrl: customFetch ? 'custom' : repositoryUrl(options),
|
|
133
|
+
}, null, 2)}\n`, 'utf8');
|
|
134
|
+
return catalog;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function revisionForModule() {
|
|
138
|
+
if (activeRevision) return activeRevision;
|
|
139
|
+
const catalog = await cachedCatalog();
|
|
140
|
+
if (catalog && activeRevision) return activeRevision;
|
|
141
|
+
await refreshCatalog();
|
|
142
|
+
return activeRevision;
|
|
143
|
+
}
|
|
144
|
+
|
|
76
145
|
return {
|
|
77
146
|
cacheRoot,
|
|
78
147
|
cachedModulePath(id, version) { return join(cacheRoot, 'rules', id, version); },
|
|
79
|
-
async hasModule(id, version
|
|
148
|
+
async hasModule(id, version, contentHash = null) {
|
|
149
|
+
const marker = await readMarker(join(cacheRoot, 'rules', id, version, '.complete'));
|
|
150
|
+
return markerMatches(marker, id, version, contentHash);
|
|
151
|
+
},
|
|
80
152
|
async catalog({ refresh = false } = {}) {
|
|
81
|
-
if (!refresh
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
return
|
|
153
|
+
if (!refresh) {
|
|
154
|
+
const cached = await cachedCatalog();
|
|
155
|
+
if (cached) return cached;
|
|
156
|
+
}
|
|
157
|
+
return await refreshCatalog();
|
|
86
158
|
},
|
|
87
|
-
async module(id, version, { refresh = false } = {}) {
|
|
159
|
+
async module(id, version, { refresh = false, contentHash = null } = {}) {
|
|
88
160
|
const root = join(cacheRoot, 'rules', id, version);
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
if (
|
|
92
|
-
|
|
161
|
+
const markerPath = join(root, '.complete');
|
|
162
|
+
const marker = await readMarker(markerPath);
|
|
163
|
+
if (!refresh && markerMatches(marker, id, version, contentHash)) return root;
|
|
164
|
+
|
|
165
|
+
const revision = await revisionForModule();
|
|
166
|
+
let payload;
|
|
167
|
+
if (options.fetchModule) payload = await options.fetchModule(id, { revision });
|
|
168
|
+
else {
|
|
169
|
+
activeGitDir ??= await ensureGitRepository(cacheRoot, options);
|
|
170
|
+
payload = await readGitModule(activeGitDir, revision, id);
|
|
171
|
+
}
|
|
172
|
+
await rm(root, { recursive: true, force: true });
|
|
93
173
|
await mkdir(root, { recursive: true });
|
|
94
174
|
await writePayload(root, payload.files ?? []);
|
|
95
|
-
await writeFile(
|
|
175
|
+
await writeFile(markerPath, `${JSON.stringify({ id, version, contentHash, revision })}\n`, 'utf8');
|
|
96
176
|
return root;
|
|
97
177
|
},
|
|
98
178
|
};
|
|
99
179
|
}
|
|
100
180
|
|
|
181
|
+
async function directoryFiles(root, prefix = '') {
|
|
182
|
+
const files = [];
|
|
183
|
+
for (const entry of (await readdir(root, { withFileTypes: true })).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
184
|
+
if (entry.name === '.complete') continue;
|
|
185
|
+
const relative = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
186
|
+
if (entry.isDirectory()) files.push(...await directoryFiles(join(root, entry.name), relative));
|
|
187
|
+
else if (entry.isFile()) files.push({ path: relative, content: await readFile(join(root, entry.name), 'utf8') });
|
|
188
|
+
}
|
|
189
|
+
return files;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export async function fetchGitHubCatalog(options = {}) {
|
|
193
|
+
return await createCanonicalSource(options).catalog({ refresh: true });
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export async function fetchGitHubModule(id, options = {}) {
|
|
197
|
+
const source = createCanonicalSource(options);
|
|
198
|
+
const catalog = await source.catalog({ refresh: true });
|
|
199
|
+
const entry = (catalog.entries ?? []).find((candidate) => candidate.id === id);
|
|
200
|
+
if (!entry) throw new Error(`Canonical module not found in catalog: ${id}`);
|
|
201
|
+
const root = await source.module(id, entry.version, { contentHash: entry.contentHash });
|
|
202
|
+
return { files: await directoryFiles(root) };
|
|
203
|
+
}
|
|
204
|
+
|
|
101
205
|
export async function detectProjectStack(projectRoot) {
|
|
102
206
|
let pkg = {};
|
|
103
207
|
try { pkg = JSON.parse(await readFile(join(projectRoot, 'package.json'), 'utf8')); } catch (error) { if (error?.code !== 'ENOENT') throw error; }
|