@specdest/devkit 0.4.5 → 0.4.8
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 +34 -4
- package/dist/npm/canonical-project-operations.d.ts +5 -0
- package/dist/npm/canonical-project-operations.js +5 -0
- package/dist/npm/canonical-project.d.ts +10 -0
- package/dist/npm/canonical-project.js +109 -0
- package/dist/npm/canonical-source.d.ts +14 -0
- package/dist/npm/canonical-source.js +216 -0
- package/dist/npm/catalog-snapshot.d.ts +47 -0
- package/dist/npm/catalog-snapshot.js +28 -0
- package/dist/npm/cli.d.ts +2 -0
- package/dist/npm/cli.js +15 -0
- package/dist/npm/commands.d.ts +9 -0
- package/dist/npm/commands.js +157 -0
- package/dist/npm/flow.d.ts +48 -0
- package/dist/npm/flow.js +396 -0
- package/dist/npm/fuzz.d.ts +138 -0
- package/dist/npm/fuzz.js +327 -0
- package/dist/npm/git-catalog.d.ts +39 -0
- package/dist/npm/git-catalog.js +190 -0
- package/dist/npm/index.d.ts +5 -0
- package/dist/npm/index.js +22 -0
- package/dist/npm/installer-primitives.d.ts +3 -0
- package/dist/npm/installer-primitives.js +50 -0
- package/dist/npm/predefined-packages/abac.d.ts +2 -0
- package/dist/npm/predefined-packages/abac.js +22 -0
- package/dist/npm/predefined-packages/auth.d.ts +2 -0
- package/dist/npm/predefined-packages/auth.js +21 -0
- package/dist/npm/predefined-packages/convex.d.ts +2 -0
- package/dist/npm/predefined-packages/convex.js +53 -0
- package/dist/npm/predefined-packages/expo.d.ts +2 -0
- package/dist/npm/predefined-packages/expo.js +84 -0
- package/dist/npm/predefined-packages/firebase-hosting.d.ts +2 -0
- package/dist/npm/predefined-packages/firebase-hosting.js +24 -0
- package/dist/npm/predefined-packages/health.d.ts +10 -0
- package/dist/npm/predefined-packages/health.js +52 -0
- package/dist/npm/predefined-packages/husky.d.ts +2 -0
- package/dist/npm/predefined-packages/husky.js +57 -0
- package/dist/npm/predefined-packages/index.d.ts +11 -0
- package/dist/npm/predefined-packages/index.js +56 -0
- package/dist/npm/predefined-packages/install.d.ts +19 -0
- package/dist/npm/predefined-packages/install.js +85 -0
- package/dist/npm/predefined-packages/local-logging.d.ts +2 -0
- package/dist/npm/predefined-packages/local-logging.js +17 -0
- package/dist/npm/predefined-packages/logging.d.ts +2 -0
- package/dist/npm/predefined-packages/logging.js +21 -0
- package/dist/npm/predefined-packages/multi-tenancy.d.ts +2 -0
- package/dist/npm/predefined-packages/multi-tenancy.js +21 -0
- package/dist/npm/predefined-packages/nps.d.ts +2 -0
- package/dist/npm/predefined-packages/nps.js +32 -0
- package/dist/npm/predefined-packages/oxfmt.d.ts +2 -0
- package/dist/npm/predefined-packages/oxfmt.js +22 -0
- package/dist/npm/predefined-packages/oxlint.d.ts +2 -0
- package/dist/npm/predefined-packages/oxlint.js +35 -0
- package/dist/npm/predefined-packages/package-json.d.ts +3 -0
- package/dist/npm/predefined-packages/package-json.js +61 -0
- package/dist/npm/predefined-packages/payments.d.ts +2 -0
- package/dist/npm/predefined-packages/payments.js +31 -0
- package/dist/npm/predefined-packages/playwright.d.ts +2 -0
- package/dist/npm/predefined-packages/playwright.js +35 -0
- package/dist/npm/predefined-packages/project-signals.d.ts +4 -0
- package/dist/npm/predefined-packages/project-signals.js +39 -0
- package/dist/npm/predefined-packages/registry.d.ts +3 -0
- package/dist/npm/predefined-packages/registry.js +63 -0
- package/dist/npm/predefined-packages/resolver.d.ts +8 -0
- package/dist/npm/predefined-packages/resolver.js +35 -0
- package/dist/npm/predefined-packages/sentry.d.ts +2 -0
- package/dist/npm/predefined-packages/sentry.js +29 -0
- package/dist/npm/predefined-packages/shadcn.d.ts +2 -0
- package/dist/npm/predefined-packages/shadcn.js +23 -0
- package/dist/npm/predefined-packages/storybook.d.ts +2 -0
- package/dist/npm/predefined-packages/storybook.js +25 -0
- package/dist/npm/predefined-packages/stryker.d.ts +2 -0
- package/dist/npm/predefined-packages/stryker.js +48 -0
- package/dist/npm/predefined-packages/supabase.d.ts +2 -0
- package/dist/npm/predefined-packages/supabase.js +84 -0
- package/dist/npm/predefined-packages/tailwind.d.ts +2 -0
- package/dist/npm/predefined-packages/tailwind.js +18 -0
- package/dist/npm/predefined-packages/tanstack-form.d.ts +2 -0
- package/dist/npm/predefined-packages/tanstack-form.js +19 -0
- package/dist/npm/predefined-packages/tanstack-query.d.ts +2 -0
- package/dist/npm/predefined-packages/tanstack-query.js +15 -0
- package/dist/npm/predefined-packages/tanstack-router.d.ts +2 -0
- package/dist/npm/predefined-packages/tanstack-router.js +18 -0
- package/dist/npm/predefined-packages/types.d.ts +31 -0
- package/dist/npm/predefined-packages/types.js +1 -0
- package/dist/npm/predefined-packages/vite.d.ts +2 -0
- package/dist/npm/predefined-packages/vite.js +123 -0
- package/dist/npm/predefined-packages/vitest.d.ts +2 -0
- package/dist/npm/predefined-packages/vitest.js +20 -0
- package/dist/npm/predefined-packages/zod.d.ts +2 -0
- package/dist/npm/predefined-packages/zod.js +18 -0
- package/dist/npm/project-check.d.ts +6 -0
- package/dist/npm/project-check.js +87 -0
- package/dist/npm/project-context.d.ts +42 -0
- package/dist/npm/project-context.js +192 -0
- package/dist/npm/project-rules.d.ts +22 -0
- package/dist/npm/project-rules.js +66 -0
- package/dist/npm/project-runtime.d.ts +18 -0
- package/dist/npm/project-runtime.js +27 -0
- package/dist/npm/project-status.d.ts +32 -0
- package/dist/npm/project-status.js +56 -0
- package/dist/npm/project-sync.d.ts +7 -0
- package/dist/npm/project-sync.js +135 -0
- package/dist/npm/remote-installer.d.ts +4 -0
- package/dist/npm/remote-installer.js +312 -0
- package/dist/npm/state.d.ts +14 -0
- package/dist/npm/state.js +182 -0
- package/package.json +10 -7
- package/src/canonical-project-operations.js +0 -433
- package/src/canonical-project.js +0 -136
- package/src/canonical-source.js +0 -250
- package/src/catalog-snapshot.js +0 -39
- package/src/catalog.js +0 -117
- package/src/cli.js +0 -14
- package/src/fuzz.js +0 -291
- package/src/git-catalog.js +0 -177
- package/src/index.js +0 -36
- package/src/installer-primitives.js +0 -198
- package/src/project-operations.js +0 -189
- package/src/project-targeting.js +0 -48
- package/src/remote-installer.js +0 -479
- package/src/rule-profile.js +0 -61
- package/src/setup-requirements.js +0 -82
- package/src/shared-file.js +0 -131
- package/src/sync-core.js +0 -390
package/src/shared-file.js
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
removeExactLines,
|
|
3
|
-
removeKeyValueLines,
|
|
4
|
-
removeMarkdownSection,
|
|
5
|
-
upsertExactLines,
|
|
6
|
-
upsertKeyValueLines,
|
|
7
|
-
upsertMarkdownSection,
|
|
8
|
-
} from './installer-primitives.js';
|
|
9
|
-
|
|
10
|
-
export const SHARED_ARTIFACT_KINDS = new Set([
|
|
11
|
-
'shared-json-field',
|
|
12
|
-
'shared-markdown-section',
|
|
13
|
-
'shared-key-value',
|
|
14
|
-
'shared-line',
|
|
15
|
-
]);
|
|
16
|
-
|
|
17
|
-
export function isSharedArtifact(artifact) {
|
|
18
|
-
return SHARED_ARTIFACT_KINDS.has(artifact?.kind);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function hasOwn(object, key) {
|
|
22
|
-
return Object.prototype.hasOwnProperty.call(object, key);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function validateSharedArtifact(artifact, { obsolete = false } = {}) {
|
|
26
|
-
if (!isSharedArtifact(artifact)) throw new Error(`Unsupported shared artifact kind: ${artifact?.kind}`);
|
|
27
|
-
if (!String(artifact.path ?? '').trim()) throw new Error('Shared artifact path is required');
|
|
28
|
-
if (artifact.kind === 'shared-json-field') {
|
|
29
|
-
if (!Array.isArray(artifact.fieldPath) || artifact.fieldPath.length === 0 || artifact.fieldPath.some((part) => !String(part).trim())) {
|
|
30
|
-
throw new Error(`Invalid shared JSON field path: ${artifact.path}`);
|
|
31
|
-
}
|
|
32
|
-
if (!obsolete && !hasOwn(artifact, 'value')) throw new Error(`Shared JSON field value is required: ${artifact.path}`);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (artifact.kind === 'shared-markdown-section') {
|
|
36
|
-
if (!String(artifact.heading ?? '').trim()) throw new Error(`Shared markdown heading is required: ${artifact.path}`);
|
|
37
|
-
if (!obsolete && typeof artifact.body !== 'string') throw new Error(`Shared markdown body is required: ${artifact.path}`);
|
|
38
|
-
}
|
|
39
|
-
if (artifact.kind === 'shared-key-value') {
|
|
40
|
-
if (!String(artifact.key ?? '').trim()) throw new Error(`Shared key is required: ${artifact.path}`);
|
|
41
|
-
if (!obsolete && !hasOwn(artifact, 'value')) throw new Error(`Shared key value is required: ${artifact.path}`);
|
|
42
|
-
}
|
|
43
|
-
if (artifact.kind === 'shared-line' && !String(artifact.line ?? '').trim()) {
|
|
44
|
-
throw new Error(`Shared line is required: ${artifact.path}`);
|
|
45
|
-
}
|
|
46
|
-
return artifact;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function sharedOwnershipKey(artifact) {
|
|
50
|
-
if (artifact.kind === 'shared-json-field') return `json:${artifact.fieldPath.map(String).join('.')}`;
|
|
51
|
-
if (artifact.kind === 'shared-markdown-section') return `markdown:${String(artifact.heading).trim()}`;
|
|
52
|
-
if (artifact.kind === 'shared-line') return `line:${String(artifact.line).trim()}`;
|
|
53
|
-
const separator = String(artifact.separator ?? '=');
|
|
54
|
-
return `key-value:${separator}:${String(artifact.key).trim()}`;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function parseJsonObject(existing, filePath) {
|
|
58
|
-
if (!existing.trim()) return {};
|
|
59
|
-
const parsed = JSON.parse(existing);
|
|
60
|
-
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
61
|
-
throw new Error(`Shared JSON file must contain an object: ${filePath}`);
|
|
62
|
-
}
|
|
63
|
-
return parsed;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function fieldValue(root, fieldPath) {
|
|
67
|
-
let current = root;
|
|
68
|
-
for (const key of fieldPath) {
|
|
69
|
-
if (!current || typeof current !== 'object' || Array.isArray(current) || !hasOwn(current, key)) return { found: false };
|
|
70
|
-
current = current[key];
|
|
71
|
-
}
|
|
72
|
-
return { found: true, value: current };
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function setJsonField(root, fieldPath, value, filePath) {
|
|
76
|
-
const existing = fieldValue(root, fieldPath);
|
|
77
|
-
if (existing.found && JSON.stringify(existing.value) === JSON.stringify(value)) return false;
|
|
78
|
-
let current = root;
|
|
79
|
-
for (let index = 0; index < fieldPath.length - 1; index += 1) {
|
|
80
|
-
const key = fieldPath[index];
|
|
81
|
-
if (!hasOwn(current, key)) current[key] = {};
|
|
82
|
-
else if (!current[key] || typeof current[key] !== 'object' || Array.isArray(current[key])) {
|
|
83
|
-
throw new Error(`Cannot own nested JSON field through non-object value: ${filePath}:${fieldPath.slice(0, index + 1).join('.')}`);
|
|
84
|
-
}
|
|
85
|
-
current = current[key];
|
|
86
|
-
}
|
|
87
|
-
current[fieldPath.at(-1)] = value;
|
|
88
|
-
return true;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function removeJsonField(root, fieldPath) {
|
|
92
|
-
let current = root;
|
|
93
|
-
for (let index = 0; index < fieldPath.length - 1; index += 1) {
|
|
94
|
-
const key = fieldPath[index];
|
|
95
|
-
if (!current || typeof current !== 'object' || Array.isArray(current) || !hasOwn(current, key)) return false;
|
|
96
|
-
current = current[key];
|
|
97
|
-
}
|
|
98
|
-
const key = fieldPath.at(-1);
|
|
99
|
-
if (!current || typeof current !== 'object' || Array.isArray(current) || !hasOwn(current, key)) return false;
|
|
100
|
-
delete current[key];
|
|
101
|
-
return true;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export function applySharedArtifact(existing, artifact) {
|
|
105
|
-
validateSharedArtifact(artifact);
|
|
106
|
-
if (artifact.kind === 'shared-json-field') {
|
|
107
|
-
const object = parseJsonObject(existing, artifact.path);
|
|
108
|
-
if (!setJsonField(object, artifact.fieldPath, artifact.value, artifact.path)) return existing;
|
|
109
|
-
return `${JSON.stringify(object, null, 2)}\n`;
|
|
110
|
-
}
|
|
111
|
-
if (artifact.kind === 'shared-markdown-section') {
|
|
112
|
-
return upsertMarkdownSection(existing, artifact.heading, artifact.body);
|
|
113
|
-
}
|
|
114
|
-
if (artifact.kind === 'shared-line') return upsertExactLines(existing, [artifact.line]);
|
|
115
|
-
return upsertKeyValueLines(existing, { [artifact.key]: String(artifact.value ?? '') }, artifact.separator ?? '=');
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export function removeSharedArtifact(existing, artifact) {
|
|
119
|
-
validateSharedArtifact(artifact, { obsolete: true });
|
|
120
|
-
if (artifact.kind === 'shared-json-field') {
|
|
121
|
-
if (!existing.trim()) return existing;
|
|
122
|
-
const object = parseJsonObject(existing, artifact.path);
|
|
123
|
-
if (!removeJsonField(object, artifact.fieldPath)) return existing;
|
|
124
|
-
return `${JSON.stringify(object, null, 2)}\n`;
|
|
125
|
-
}
|
|
126
|
-
if (artifact.kind === 'shared-markdown-section') {
|
|
127
|
-
return removeMarkdownSection(existing, artifact.heading);
|
|
128
|
-
}
|
|
129
|
-
if (artifact.kind === 'shared-line') return removeExactLines(existing, [artifact.line]);
|
|
130
|
-
return removeKeyValueLines(existing, [artifact.key], artifact.separator ?? '=');
|
|
131
|
-
}
|
package/src/sync-core.js
DELETED
|
@@ -1,390 +0,0 @@
|
|
|
1
|
-
import { mkdir, readFile, rename, stat, unlink, writeFile } from 'node:fs/promises';
|
|
2
|
-
import { dirname, join } from 'node:path';
|
|
3
|
-
|
|
4
|
-
import { applyFileOperation, planFileContent, readText, resolveProjectPath } from './installer-primitives.js';
|
|
5
|
-
import {
|
|
6
|
-
applySharedArtifact,
|
|
7
|
-
isSharedArtifact,
|
|
8
|
-
removeSharedArtifact,
|
|
9
|
-
sharedOwnershipKey,
|
|
10
|
-
validateSharedArtifact,
|
|
11
|
-
} from './shared-file.js';
|
|
12
|
-
|
|
13
|
-
export const STATE_SCHEMA_VERSION = 2;
|
|
14
|
-
export const CHECKS_SCHEMA_VERSION = 2;
|
|
15
|
-
export const INSTALLED_RELATIVE_PATH = '.specdest/installed.json';
|
|
16
|
-
export const STATE_RELATIVE_PATH = '.specdest/state.json';
|
|
17
|
-
export const CHECKS_RELATIVE_PATH = STATE_RELATIVE_PATH;
|
|
18
|
-
export const LEGACY_CHECKS_RELATIVE_PATH = '.specdest/checks.json';
|
|
19
|
-
|
|
20
|
-
const EMPTY_INSTALLED = Object.freeze({ schemaVersion: 2, requested: [], installed: {} });
|
|
21
|
-
const EMPTY_CHECKS = Object.freeze({ schemaVersion: CHECKS_SCHEMA_VERSION, results: {} });
|
|
22
|
-
|
|
23
|
-
function clone(value) {
|
|
24
|
-
return JSON.parse(JSON.stringify(value));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async function readJsonOrDefault(filePath, fallback) {
|
|
28
|
-
try {
|
|
29
|
-
return JSON.parse(await readFile(filePath, 'utf8'));
|
|
30
|
-
} catch (error) {
|
|
31
|
-
if (error?.code === 'ENOENT') return clone(fallback);
|
|
32
|
-
throw error;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const JSON_INDENT = 2;
|
|
37
|
-
const JSON_LINE_WIDTH = 100;
|
|
38
|
-
|
|
39
|
-
function isOmittedJsonValue(value) {
|
|
40
|
-
return value === undefined || ['function', 'symbol'].includes(typeof value);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function formatJsonValue(value, depth = 0, { arrayItem = false } = {}) {
|
|
44
|
-
if (isOmittedJsonValue(value)) return arrayItem ? 'null' : undefined;
|
|
45
|
-
if (value === null || typeof value !== 'object') return JSON.stringify(value);
|
|
46
|
-
const indent = ' '.repeat(depth * JSON_INDENT);
|
|
47
|
-
const childIndent = ' '.repeat((depth + 1) * JSON_INDENT);
|
|
48
|
-
if (Array.isArray(value)) {
|
|
49
|
-
if (!value.length) return '[]';
|
|
50
|
-
const primitive = value.every(
|
|
51
|
-
(item) => isOmittedJsonValue(item) || item === null || ['string', 'number', 'boolean'].includes(typeof item),
|
|
52
|
-
);
|
|
53
|
-
if (primitive) {
|
|
54
|
-
const inline = `[${value.map((item) => formatJsonValue(item, depth + 1, { arrayItem: true })).join(', ')}]`;
|
|
55
|
-
if (indent.length + inline.length <= JSON_LINE_WIDTH) return inline;
|
|
56
|
-
}
|
|
57
|
-
return `[\n${value.map((item) => `${childIndent}${formatJsonValue(item, depth + 1, { arrayItem: true })}`).join(',\n')}\n${indent}]`;
|
|
58
|
-
}
|
|
59
|
-
const entries = Object.entries(value).filter(([, item]) => !isOmittedJsonValue(item));
|
|
60
|
-
if (!entries.length) return '{}';
|
|
61
|
-
return `{\n${entries.map(([key, item]) => `${childIndent}${JSON.stringify(key)}: ${formatJsonValue(item, depth + 1)}`).join(',\n')}\n${indent}}`;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function formatJsonState(value) {
|
|
65
|
-
return `${formatJsonValue(value)}\n`;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async function writeJsonAtomic(filePath, value) {
|
|
69
|
-
await mkdir(dirname(filePath), { recursive: true });
|
|
70
|
-
const temp = `${filePath}.${process.pid}.tmp`;
|
|
71
|
-
await writeFile(temp, formatJsonState(value), 'utf8');
|
|
72
|
-
await rename(temp, filePath);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function validateInstalledState(state) {
|
|
76
|
-
const legacy = state?.schemaVersion === 1;
|
|
77
|
-
const current = state?.schemaVersion === STATE_SCHEMA_VERSION;
|
|
78
|
-
if ((!legacy && !current) || !state.installed || Array.isArray(state.installed) || typeof state.installed !== 'object') {
|
|
79
|
-
throw new Error('Invalid .specdest/installed.json state');
|
|
80
|
-
}
|
|
81
|
-
if (current && (!Array.isArray(state.requested) || state.requested.some((id) => !validId(id) || !state.installed[id]))) {
|
|
82
|
-
throw new Error('Invalid .specdest/installed.json requested roots');
|
|
83
|
-
}
|
|
84
|
-
return state;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function validateChecksState(state) {
|
|
88
|
-
const legacy = state?.schemaVersion === 1;
|
|
89
|
-
const current = state?.schemaVersion === CHECKS_SCHEMA_VERSION;
|
|
90
|
-
if ((!legacy && !current) || !state.results || Array.isArray(state.results) || typeof state.results !== 'object') {
|
|
91
|
-
throw new Error('Invalid .specdest/state.json state');
|
|
92
|
-
}
|
|
93
|
-
if (legacy && state.checkedAt !== null && typeof state.checkedAt !== 'string') {
|
|
94
|
-
throw new Error('Invalid .specdest/state.json checkedAt');
|
|
95
|
-
}
|
|
96
|
-
return state;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export async function readInstalledState(projectRoot) {
|
|
100
|
-
const state = await readJsonOrDefault(join(projectRoot, INSTALLED_RELATIVE_PATH), EMPTY_INSTALLED);
|
|
101
|
-
return validateInstalledState(state);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function formatInstalledState(state) {
|
|
105
|
-
return formatJsonState(state);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export async function writeInstalledState(projectRoot, state) {
|
|
109
|
-
validateInstalledState(state);
|
|
110
|
-
if (state.schemaVersion !== STATE_SCHEMA_VERSION) throw new Error('Cannot write legacy .specdest/installed.json state');
|
|
111
|
-
const filePath = join(projectRoot, INSTALLED_RELATIVE_PATH);
|
|
112
|
-
await mkdir(dirname(filePath), { recursive: true });
|
|
113
|
-
const temp = `${filePath}.${process.pid}.tmp`;
|
|
114
|
-
await writeFile(temp, formatInstalledState(state), 'utf8');
|
|
115
|
-
await rename(temp, filePath);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export async function readChecksState(projectRoot) {
|
|
119
|
-
const statePath = join(projectRoot, STATE_RELATIVE_PATH);
|
|
120
|
-
let state;
|
|
121
|
-
try {
|
|
122
|
-
state = JSON.parse(await readFile(statePath, 'utf8'));
|
|
123
|
-
} catch (error) {
|
|
124
|
-
if (error?.code !== 'ENOENT') throw error;
|
|
125
|
-
state = await readJsonOrDefault(join(projectRoot, LEGACY_CHECKS_RELATIVE_PATH), EMPTY_CHECKS);
|
|
126
|
-
}
|
|
127
|
-
return validateChecksState(state);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export async function writeChecksState(projectRoot, state) {
|
|
131
|
-
validateChecksState(state);
|
|
132
|
-
if (state.schemaVersion !== CHECKS_SCHEMA_VERSION) throw new Error('Cannot write legacy .specdest/state.json state');
|
|
133
|
-
const current = await readJsonOrDefault(join(projectRoot, STATE_RELATIVE_PATH), { schemaVersion: CHECKS_SCHEMA_VERSION });
|
|
134
|
-
await writeJsonAtomic(join(projectRoot, STATE_RELATIVE_PATH), { ...current, ...state, schemaVersion: CHECKS_SCHEMA_VERSION });
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function validId(value) {
|
|
138
|
-
return Boolean(
|
|
139
|
-
value &&
|
|
140
|
-
value.split('-').every((part) => part && /^[A-Za-z0-9_]+$/u.test(part)),
|
|
141
|
-
);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
export function validateManifest(manifest) {
|
|
145
|
-
if (!manifest || typeof manifest !== 'object' || Array.isArray(manifest)) throw new Error('Manifest must be an object');
|
|
146
|
-
if (!validId(manifest.id)) throw new Error(`Invalid manifest id: ${manifest.id}`);
|
|
147
|
-
if (!['rule', 'module'].includes(manifest.kind)) throw new Error(`Invalid manifest kind: ${manifest.kind}`);
|
|
148
|
-
if (!String(manifest.version ?? '').trim()) throw new Error(`Missing manifest version: ${manifest.id}`);
|
|
149
|
-
if (!Array.isArray(manifest.artifacts ?? [])) throw new Error(`Invalid artifacts: ${manifest.id}`);
|
|
150
|
-
if (!Array.isArray(manifest.obsolete ?? [])) throw new Error(`Invalid obsolete artifacts: ${manifest.id}`);
|
|
151
|
-
for (const artifact of manifest.artifacts ?? []) {
|
|
152
|
-
resolveProjectPath('/__specdest_project__', artifact.path);
|
|
153
|
-
if (['managed-file', 'scaffold'].includes(artifact?.kind)) {
|
|
154
|
-
if (typeof artifact.content !== 'string') throw new Error(`Artifact content must be text: ${manifest.id}:${artifact.path}`);
|
|
155
|
-
} else if (isSharedArtifact(artifact)) {
|
|
156
|
-
validateSharedArtifact(artifact);
|
|
157
|
-
} else throw new Error(`Unsupported artifact kind: ${artifact?.kind}`);
|
|
158
|
-
}
|
|
159
|
-
for (const artifact of manifest.obsolete ?? []) {
|
|
160
|
-
resolveProjectPath('/__specdest_project__', artifact.path);
|
|
161
|
-
if (artifact?.kind === 'file') continue;
|
|
162
|
-
if (isSharedArtifact(artifact)) validateSharedArtifact(artifact, { obsolete: true });
|
|
163
|
-
else throw new Error(`Unsupported obsolete artifact kind: ${artifact?.kind}`);
|
|
164
|
-
}
|
|
165
|
-
return manifest;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function claimFormat(artifact) {
|
|
169
|
-
if (!isSharedArtifact(artifact)) return 'whole';
|
|
170
|
-
if (artifact.kind === 'shared-json-field') return 'json';
|
|
171
|
-
if (artifact.kind === 'shared-markdown-section') return 'markdown';
|
|
172
|
-
if (artifact.kind === 'shared-line') return 'line';
|
|
173
|
-
return 'key-value';
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
export function buildOwnershipGraph(manifests) {
|
|
177
|
-
const ids = new Set();
|
|
178
|
-
const claims = new Map();
|
|
179
|
-
const claim = (owner, artifact) => {
|
|
180
|
-
const path = artifact.path;
|
|
181
|
-
const state = claims.get(path) ?? { whole: null, format: null, scopes: new Map() };
|
|
182
|
-
const format = claimFormat(artifact);
|
|
183
|
-
if (format === 'whole') {
|
|
184
|
-
if (state.whole || state.scopes.size) throw new Error(`Ownership conflict for ${path}`);
|
|
185
|
-
state.whole = owner;
|
|
186
|
-
} else {
|
|
187
|
-
if (state.whole) throw new Error(`Ownership conflict for ${path}`);
|
|
188
|
-
if (state.format && state.format !== format) throw new Error(`Ownership conflict for ${path}: incompatible shared formats`);
|
|
189
|
-
state.format = format;
|
|
190
|
-
const scope = sharedOwnershipKey(artifact);
|
|
191
|
-
if (state.scopes.has(scope)) throw new Error(`Ownership conflict for ${path}:${scope}`);
|
|
192
|
-
state.scopes.set(scope, owner);
|
|
193
|
-
}
|
|
194
|
-
claims.set(path, state);
|
|
195
|
-
};
|
|
196
|
-
for (const manifest of manifests) {
|
|
197
|
-
validateManifest(manifest);
|
|
198
|
-
if (ids.has(manifest.id)) throw new Error(`Duplicate manifest id: ${manifest.id}`);
|
|
199
|
-
ids.add(manifest.id);
|
|
200
|
-
for (const artifact of manifest.artifacts ?? []) claim(manifest.id, artifact);
|
|
201
|
-
for (const artifact of manifest.obsolete ?? []) claim(manifest.id, artifact);
|
|
202
|
-
}
|
|
203
|
-
return { claims };
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
async function fileState(filePath) {
|
|
207
|
-
try {
|
|
208
|
-
const info = await stat(filePath);
|
|
209
|
-
return info.isFile() ? 'file' : 'other';
|
|
210
|
-
} catch (error) {
|
|
211
|
-
if (error?.code === 'ENOENT') return 'missing';
|
|
212
|
-
throw error;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
function sortedInstalled(installed) {
|
|
217
|
-
return Object.fromEntries(Object.entries(installed).sort(([a], [b]) => a.localeCompare(b)));
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
export function inferRequestedRoots(entries) {
|
|
221
|
-
const ids = new Set(entries.map((entry) => entry.id));
|
|
222
|
-
const required = new Set();
|
|
223
|
-
for (const entry of entries) {
|
|
224
|
-
for (const id of entry.requires ?? []) if (ids.has(id)) required.add(id);
|
|
225
|
-
}
|
|
226
|
-
return [...ids].filter((id) => !required.has(id)).sort();
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
function removalArtifact(artifact) {
|
|
230
|
-
if (artifact.kind === 'managed-file') return { kind: 'file', path: artifact.path };
|
|
231
|
-
if (artifact.kind === 'scaffold') return null;
|
|
232
|
-
if (isSharedArtifact(artifact)) return artifact;
|
|
233
|
-
return null;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
export async function previewSync(projectRoot, manifests, options = {}) {
|
|
237
|
-
const currentState = await readInstalledState(projectRoot);
|
|
238
|
-
const catalog = new Map();
|
|
239
|
-
for (const manifest of manifests) {
|
|
240
|
-
validateManifest(manifest);
|
|
241
|
-
if (catalog.has(manifest.id)) throw new Error(`Duplicate manifest id: ${manifest.id}`);
|
|
242
|
-
catalog.set(manifest.id, manifest);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
const desiredIds = options.desiredIds ?? Object.keys(currentState.installed);
|
|
246
|
-
const requestedIds = options.requestedIds ?? (currentState.schemaVersion === 2 ? currentState.requested : desiredIds);
|
|
247
|
-
const selected = [...new Set(desiredIds)].sort().map((id) => {
|
|
248
|
-
const manifest = catalog.get(id);
|
|
249
|
-
if (!manifest) throw new Error(`Desired item has no current manifest: ${id}`);
|
|
250
|
-
return manifest;
|
|
251
|
-
});
|
|
252
|
-
buildOwnershipGraph(selected);
|
|
253
|
-
const activeWholePaths = new Set(
|
|
254
|
-
selected.flatMap((manifest) => (manifest.artifacts ?? [])
|
|
255
|
-
.filter((artifact) => artifact.kind === 'managed-file' || artifact.kind === 'scaffold')
|
|
256
|
-
.map((artifact) => artifact.path)),
|
|
257
|
-
);
|
|
258
|
-
|
|
259
|
-
const operations = [];
|
|
260
|
-
const sharedFiles = new Map();
|
|
261
|
-
const collectShared = (ownerId, artifact, obsolete) => {
|
|
262
|
-
const entry = sharedFiles.get(artifact.path) ?? { active: [], obsolete: [] };
|
|
263
|
-
entry[obsolete ? 'obsolete' : 'active'].push({ ownerId, artifact });
|
|
264
|
-
sharedFiles.set(artifact.path, entry);
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
for (const manifest of options.removedManifests ?? []) {
|
|
268
|
-
for (const artifact of manifest.artifacts ?? []) {
|
|
269
|
-
const obsolete = removalArtifact(artifact);
|
|
270
|
-
if (!obsolete) continue;
|
|
271
|
-
if (obsolete.kind === 'file' && activeWholePaths.has(obsolete.path)) continue;
|
|
272
|
-
if (isSharedArtifact(obsolete)) {
|
|
273
|
-
collectShared(manifest.id, obsolete, true);
|
|
274
|
-
continue;
|
|
275
|
-
}
|
|
276
|
-
const target = resolveProjectPath(projectRoot, obsolete.path);
|
|
277
|
-
const state = await fileState(target);
|
|
278
|
-
if (state === 'other') throw new Error(`Removed file target is not a file: ${obsolete.path}`);
|
|
279
|
-
operations.push({
|
|
280
|
-
relativePath: obsolete.path,
|
|
281
|
-
target,
|
|
282
|
-
action: state === 'file' ? 'delete' : 'unchanged',
|
|
283
|
-
ownerId: manifest.id,
|
|
284
|
-
artifactKind: 'removed-managed-file',
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
for (const manifest of selected) {
|
|
290
|
-
for (const artifact of manifest.artifacts ?? []) {
|
|
291
|
-
if (isSharedArtifact(artifact)) {
|
|
292
|
-
collectShared(manifest.id, artifact, false);
|
|
293
|
-
continue;
|
|
294
|
-
}
|
|
295
|
-
const operation = await planFileContent(projectRoot, artifact.path, artifact.content, {
|
|
296
|
-
createOnly: artifact.kind === 'scaffold',
|
|
297
|
-
});
|
|
298
|
-
operations.push({ ...operation, ownerId: manifest.id, artifactKind: artifact.kind });
|
|
299
|
-
}
|
|
300
|
-
for (const artifact of manifest.obsolete ?? []) {
|
|
301
|
-
if (isSharedArtifact(artifact)) {
|
|
302
|
-
collectShared(manifest.id, artifact, true);
|
|
303
|
-
continue;
|
|
304
|
-
}
|
|
305
|
-
const target = resolveProjectPath(projectRoot, artifact.path);
|
|
306
|
-
const state = await fileState(target);
|
|
307
|
-
if (state === 'other') throw new Error(`Obsolete file target is not a file: ${artifact.path}`);
|
|
308
|
-
operations.push({
|
|
309
|
-
relativePath: artifact.path,
|
|
310
|
-
target,
|
|
311
|
-
action: state === 'file' ? 'delete' : 'unchanged',
|
|
312
|
-
ownerId: manifest.id,
|
|
313
|
-
artifactKind: 'obsolete-file',
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
for (const [relativePath, entry] of [...sharedFiles.entries()].sort(([a], [b]) => a.localeCompare(b))) {
|
|
319
|
-
const target = resolveProjectPath(projectRoot, relativePath);
|
|
320
|
-
const state = await fileState(target);
|
|
321
|
-
if (state === 'other') throw new Error(`Shared file target is not a file: ${relativePath}`);
|
|
322
|
-
const existing = await readText(target);
|
|
323
|
-
let updated = existing;
|
|
324
|
-
for (const item of [...entry.obsolete].sort((a, b) => sharedOwnershipKey(a.artifact).localeCompare(sharedOwnershipKey(b.artifact)))) {
|
|
325
|
-
updated = removeSharedArtifact(updated, item.artifact);
|
|
326
|
-
}
|
|
327
|
-
for (const item of [...entry.active].sort((a, b) => sharedOwnershipKey(a.artifact).localeCompare(sharedOwnershipKey(b.artifact)))) {
|
|
328
|
-
updated = applySharedArtifact(updated, item.artifact);
|
|
329
|
-
}
|
|
330
|
-
let operation = await planFileContent(projectRoot, relativePath, updated);
|
|
331
|
-
if (entry.active.length === 0 && state === 'missing' && updated === '') {
|
|
332
|
-
operation = { ...operation, action: 'unchanged' };
|
|
333
|
-
}
|
|
334
|
-
operations.push({
|
|
335
|
-
...operation,
|
|
336
|
-
ownerIds: [...new Set([...entry.active, ...entry.obsolete].map((item) => item.ownerId))].sort(),
|
|
337
|
-
artifactKind: 'shared-file',
|
|
338
|
-
});
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
const nextInstalled = { ...currentState.installed };
|
|
342
|
-
for (const manifest of selected) {
|
|
343
|
-
nextInstalled[manifest.id] = { kind: manifest.kind, version: manifest.version };
|
|
344
|
-
}
|
|
345
|
-
for (const id of Object.keys(nextInstalled)) if (!desiredIds.includes(id)) delete nextInstalled[id];
|
|
346
|
-
const nextState = {
|
|
347
|
-
schemaVersion: STATE_SCHEMA_VERSION,
|
|
348
|
-
requested: [...new Set(requestedIds)].sort(),
|
|
349
|
-
installed: sortedInstalled(nextInstalled),
|
|
350
|
-
};
|
|
351
|
-
const normalizedCurrent = currentState.schemaVersion === STATE_SCHEMA_VERSION
|
|
352
|
-
? { schemaVersion: STATE_SCHEMA_VERSION, requested: [...currentState.requested].sort(), installed: sortedInstalled(currentState.installed) }
|
|
353
|
-
: { schemaVersion: 1, installed: sortedInstalled(currentState.installed) };
|
|
354
|
-
return {
|
|
355
|
-
projectRoot,
|
|
356
|
-
operations,
|
|
357
|
-
currentState: normalizedCurrent,
|
|
358
|
-
nextState,
|
|
359
|
-
stateChanged: JSON.stringify(normalizedCurrent) !== JSON.stringify(nextState),
|
|
360
|
-
};
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
export async function applySyncPlan(plan) {
|
|
364
|
-
const supported = new Set(['create', 'update', 'unchanged', 'delete']);
|
|
365
|
-
for (const operation of plan.operations) {
|
|
366
|
-
if (!supported.has(operation.action)) {
|
|
367
|
-
throw new Error(`Unsupported sync operation: ${operation.action}`);
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
for (const operation of plan.operations) {
|
|
371
|
-
if (operation.action === 'unchanged') continue;
|
|
372
|
-
if (operation.action === 'delete') {
|
|
373
|
-
try {
|
|
374
|
-
await unlink(operation.target);
|
|
375
|
-
} catch (error) {
|
|
376
|
-
if (error?.code !== 'ENOENT') throw error;
|
|
377
|
-
}
|
|
378
|
-
continue;
|
|
379
|
-
}
|
|
380
|
-
await applyFileOperation(operation);
|
|
381
|
-
}
|
|
382
|
-
if (plan.stateChanged) await writeInstalledState(plan.projectRoot, plan.nextState);
|
|
383
|
-
return plan;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
export async function syncProject(projectRoot, manifests) {
|
|
387
|
-
const plan = await previewSync(projectRoot, manifests);
|
|
388
|
-
await applySyncPlan(plan);
|
|
389
|
-
return plan;
|
|
390
|
-
}
|