@specdest/devkit 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/catalog/README.md +11 -0
- package/catalog/modules/backend-convex/manifest.json +14 -0
- package/catalog/modules/backend-convex-expo/manifest.json +11 -0
- package/catalog/modules/backend-supabase/manifest.json +15 -0
- package/catalog/modules/backend-supabase-expo/manifest.json +11 -0
- package/catalog/modules/checker-runtime/config.ts +24 -0
- package/catalog/modules/checker-runtime/fs.ts +47 -0
- package/catalog/modules/checker-runtime/manifest.json +23 -0
- package/catalog/modules/devflow/.agents/skills/devflow/SKILL.md +33 -0
- package/catalog/modules/devflow/.devflow.json +10 -0
- package/catalog/modules/devflow/manifest.json +35 -0
- package/catalog/modules/e2e-playwright/.agents/skills/e2e-playwright/SKILL.md +85 -0
- package/catalog/modules/e2e-playwright/.agents/skills/e2e-playwright/scripts/activeScenario.mjs +130 -0
- package/catalog/modules/e2e-playwright/.agents/skills/e2e-playwright/scripts/checkLocalEnvironment.mjs +7 -0
- package/catalog/modules/e2e-playwright/.agents/skills/e2e-playwright/scripts/completeActiveScenario.mjs +36 -0
- package/catalog/modules/e2e-playwright/.agents/skills/e2e-playwright/scripts/createActiveScenario.mjs +143 -0
- package/catalog/modules/e2e-playwright/.agents/skills/e2e-playwright/scripts/localEnvironment.mjs +43 -0
- package/catalog/modules/e2e-playwright/.agents/skills/e2e-playwright/scripts/prompt.mjs +25 -0
- package/catalog/modules/e2e-playwright/.agents/skills/e2e-playwright/scripts/runActiveScenario.mjs +66 -0
- package/catalog/modules/e2e-playwright/.agents/skills/e2e-playwright/scripts/runAuthoringScenario.mjs +146 -0
- package/catalog/modules/e2e-playwright/.agents/skills/e2e-playwright/scripts/seed.mjs +56 -0
- package/catalog/modules/e2e-playwright/.agents/skills/e2e-playwright/scripts/seedActiveScenario.mjs +6 -0
- package/catalog/modules/e2e-playwright/docs/e2e-playwright/README.md +103 -0
- package/catalog/modules/e2e-playwright/manifest.json +206 -0
- package/catalog/modules/e2e-playwright/playwright.e2e.config.ts +39 -0
- package/catalog/modules/e2e-playwright/tests/e2e/support/e2ePlaywrightFlow.ts +107 -0
- package/catalog/modules/e2e-playwright/tests/e2e/support/fixtures.ts +27 -0
- package/catalog/modules/local-log/AGENTS.local-log.md +13 -0
- package/catalog/modules/local-log/docs/features/local-logging/README.md +70 -0
- package/catalog/modules/local-log/manifest.json +39 -0
- package/catalog/modules/local-log/tmp/logs/.gitignore +2 -0
- package/catalog/modules/local-log/tools/local-log/README.md +47 -0
- package/catalog/modules/local-log/tools/local-log/local-log.mjs +241 -0
- package/catalog/modules/stack-common-vite/manifest.json +16 -0
- package/catalog/modules/stack-expo-convex/manifest.json +31 -0
- package/catalog/modules/stack-expo-supabase/manifest.json +44 -0
- package/catalog/modules/stack-vite-convex/manifest.json +32 -0
- package/catalog/modules/stack-vite-supabase/manifest.json +45 -0
- package/catalog/rules/abac-permissions-convex/SKILL.md +17 -0
- package/catalog/rules/abac-permissions-convex/check.ts +86 -0
- package/catalog/rules/abac-permissions-convex/manifest.json +35 -0
- package/catalog/rules/abac-permissions-supabase/SKILL.md +17 -0
- package/catalog/rules/abac-permissions-supabase/check.ts +86 -0
- package/catalog/rules/abac-permissions-supabase/manifest.json +35 -0
- package/catalog/rules/convex-multi-presentation/SKILL.md +48 -0
- package/catalog/rules/convex-multi-presentation/boundary-test-baseline.json +3 -0
- package/catalog/rules/convex-multi-presentation/check.ts +271 -0
- package/catalog/rules/convex-multi-presentation/manifest.json +45 -0
- package/catalog/rules/convex-multi-presentation/policy.ts +261 -0
- package/catalog/rules/design-rules/SKILL.md +12 -0
- package/catalog/rules/design-rules/check.ts +83 -0
- package/catalog/rules/design-rules/config.json +5 -0
- package/catalog/rules/design-rules/manifest.json +40 -0
- package/catalog/rules/e2e-test-rules/SKILL.md +46 -0
- package/catalog/rules/e2e-test-rules/check.ts +144 -0
- package/catalog/rules/e2e-test-rules/config.json +32 -0
- package/catalog/rules/e2e-test-rules/manifest.json +40 -0
- package/catalog/rules/error-handling-rules/SKILL.md +21 -0
- package/catalog/rules/error-handling-rules/check.ts +72 -0
- package/catalog/rules/error-handling-rules/manifest.json +35 -0
- package/catalog/rules/layers-and-contracts/SKILL.md +36 -0
- package/catalog/rules/layers-and-contracts/check.ts +265 -0
- package/catalog/rules/layers-and-contracts/manifest.json +35 -0
- package/catalog/rules/layers-and-contracts-convex/SKILL.md +49 -0
- package/catalog/rules/layers-and-contracts-convex/check.ts +288 -0
- package/catalog/rules/layers-and-contracts-convex/manifest.json +40 -0
- package/catalog/rules/layers-and-contracts-convex/policy.ts +68 -0
- package/catalog/rules/layers-and-contracts-supabase/SKILL.md +52 -0
- package/catalog/rules/layers-and-contracts-supabase/check.ts +370 -0
- package/catalog/rules/layers-and-contracts-supabase/manifest.json +40 -0
- package/catalog/rules/layers-and-contracts-supabase/policy.ts +68 -0
- package/catalog/rules/module-size-rules/SKILL.md +5 -0
- package/catalog/rules/module-size-rules/check.ts +143 -0
- package/catalog/rules/module-size-rules/config.json +6 -0
- package/catalog/rules/module-size-rules/manifest.json +40 -0
- package/catalog/rules/module-size-rules-convex/SKILL.md +5 -0
- package/catalog/rules/module-size-rules-convex/check.ts +145 -0
- package/catalog/rules/module-size-rules-convex/config.json +6 -0
- package/catalog/rules/module-size-rules-convex/manifest.json +40 -0
- package/catalog/rules/module-size-rules-supabase/SKILL.md +5 -0
- package/catalog/rules/module-size-rules-supabase/check.ts +145 -0
- package/catalog/rules/module-size-rules-supabase/config.json +12 -0
- package/catalog/rules/module-size-rules-supabase/manifest.json +40 -0
- package/catalog/rules/mutation-command-rules/SKILL.md +15 -0
- package/catalog/rules/mutation-command-rules/check.ts +29 -0
- package/catalog/rules/mutation-command-rules/config.json +19 -0
- package/catalog/rules/mutation-command-rules/manifest.json +40 -0
- package/catalog/rules/nearby-contracts/SKILL.md +18 -0
- package/catalog/rules/nearby-contracts/check.ts +137 -0
- package/catalog/rules/nearby-contracts/config.json +10 -0
- package/catalog/rules/nearby-contracts/manifest.json +40 -0
- package/catalog/rules/no-raw-click-buttons/SKILL.md +5 -0
- package/catalog/rules/no-raw-click-buttons/check.ts +36 -0
- package/catalog/rules/no-raw-click-buttons/manifest.json +35 -0
- package/catalog/rules/public-api-export-barrels/SKILL.md +45 -0
- package/catalog/rules/public-api-export-barrels/check.ts +72 -0
- package/catalog/rules/public-api-export-barrels/config.json +4 -0
- package/catalog/rules/public-api-export-barrels/manifest.json +40 -0
- package/catalog/rules/route-tree-fresh/SKILL.md +16 -0
- package/catalog/rules/route-tree-fresh/check.ts +64 -0
- package/catalog/rules/route-tree-fresh/manifest.json +35 -0
- package/catalog/rules/strict-typescript/SKILL.md +3 -0
- package/catalog/rules/strict-typescript/check.ts +111 -0
- package/catalog/rules/strict-typescript/manifest.json +35 -0
- package/catalog/rules/supabase-contracts/SKILL.md +3 -0
- package/catalog/rules/supabase-contracts/check.ts +171 -0
- package/catalog/rules/supabase-contracts/manifest.json +35 -0
- package/catalog/rules/supabase-declarative-schema/SKILL.md +30 -0
- package/catalog/rules/supabase-declarative-schema/check.ts +261 -0
- package/catalog/rules/supabase-declarative-schema/config.json +8 -0
- package/catalog/rules/supabase-declarative-schema/manifest.json +40 -0
- package/catalog/rules/tanstack-form-rules/SKILL.md +11 -0
- package/catalog/rules/tanstack-form-rules/check.ts +57 -0
- package/catalog/rules/tanstack-form-rules/manifest.json +35 -0
- package/catalog/rules/unit-test-rules/SKILL.md +55 -0
- package/catalog/rules/unit-test-rules/check.ts +131 -0
- package/catalog/rules/unit-test-rules/config.json +22 -0
- package/catalog/rules/unit-test-rules/manifest.json +40 -0
- package/catalog/rules/unit-test-rules-convex/SKILL.md +41 -0
- package/catalog/rules/unit-test-rules-convex/check.ts +417 -0
- package/catalog/rules/unit-test-rules-convex/manifest.json +35 -0
- package/catalog/rules/unit-test-rules-supabase/SKILL.md +40 -0
- package/catalog/rules/unit-test-rules-supabase/check.ts +491 -0
- package/catalog/rules/unit-test-rules-supabase/manifest.json +35 -0
- package/catalog/rules/workstation-controller-rules/SKILL.md +22 -0
- package/catalog/rules/workstation-controller-rules/check.ts +107 -0
- package/catalog/rules/workstation-controller-rules/manifest.json +35 -0
- package/package.json +22 -0
- package/src/catalog-snapshot.js +39 -0
- package/src/catalog.js +117 -0
- package/src/cli.js +11 -0
- package/src/index.js +30 -0
- package/src/installer-primitives.js +198 -0
- package/src/project-operations.js +126 -0
- package/src/setup-requirements.js +82 -0
- package/src/shared-file.js +131 -0
- package/src/sync-core.js +276 -0
|
@@ -0,0 +1,131 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
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 = 1;
|
|
14
|
+
export const INSTALLED_RELATIVE_PATH = '.specdest/installed.json';
|
|
15
|
+
export const CHECKS_RELATIVE_PATH = '.specdest/checks.json';
|
|
16
|
+
|
|
17
|
+
const EMPTY_INSTALLED = Object.freeze({ schemaVersion: 1, installed: {} });
|
|
18
|
+
const EMPTY_CHECKS = Object.freeze({ schemaVersion: 1, checkedAt: null, results: {} });
|
|
19
|
+
|
|
20
|
+
function clone(value) {
|
|
21
|
+
return JSON.parse(JSON.stringify(value));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function readJsonOrDefault(filePath, fallback) {
|
|
25
|
+
try {
|
|
26
|
+
return JSON.parse(await readFile(filePath, 'utf8'));
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (error?.code === 'ENOENT') return clone(fallback);
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function writeJsonAtomic(filePath, value) {
|
|
34
|
+
await mkdir(dirname(filePath), { recursive: true });
|
|
35
|
+
const temp = `${filePath}.${process.pid}.tmp`;
|
|
36
|
+
await writeFile(temp, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
|
|
37
|
+
await rename(temp, filePath);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function validateInstalledState(state) {
|
|
41
|
+
if (state?.schemaVersion !== STATE_SCHEMA_VERSION || !state.installed || Array.isArray(state.installed) || typeof state.installed !== 'object') {
|
|
42
|
+
throw new Error('Invalid .specdest/installed.json state');
|
|
43
|
+
}
|
|
44
|
+
return state;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function validateChecksState(state) {
|
|
48
|
+
if (state?.schemaVersion !== STATE_SCHEMA_VERSION || !state.results || Array.isArray(state.results) || typeof state.results !== 'object') {
|
|
49
|
+
throw new Error('Invalid .specdest/checks.json state');
|
|
50
|
+
}
|
|
51
|
+
if (state.checkedAt !== null && typeof state.checkedAt !== 'string') {
|
|
52
|
+
throw new Error('Invalid .specdest/checks.json checkedAt');
|
|
53
|
+
}
|
|
54
|
+
return state;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function readInstalledState(projectRoot) {
|
|
58
|
+
const state = await readJsonOrDefault(join(projectRoot, INSTALLED_RELATIVE_PATH), EMPTY_INSTALLED);
|
|
59
|
+
return validateInstalledState(state);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function writeInstalledState(projectRoot, state) {
|
|
63
|
+
validateInstalledState(state);
|
|
64
|
+
await writeJsonAtomic(join(projectRoot, INSTALLED_RELATIVE_PATH), state);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function readChecksState(projectRoot) {
|
|
68
|
+
const state = await readJsonOrDefault(join(projectRoot, CHECKS_RELATIVE_PATH), EMPTY_CHECKS);
|
|
69
|
+
return validateChecksState(state);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export async function writeChecksState(projectRoot, state) {
|
|
73
|
+
validateChecksState(state);
|
|
74
|
+
await writeJsonAtomic(join(projectRoot, CHECKS_RELATIVE_PATH), state);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function validId(value) {
|
|
78
|
+
return Boolean(value && value === value.toLowerCase() && value.split('-').every((part) => part && /^[a-z0-9_]+$/.test(part)));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function validateManifest(manifest) {
|
|
82
|
+
if (!manifest || typeof manifest !== 'object' || Array.isArray(manifest)) throw new Error('Manifest must be an object');
|
|
83
|
+
if (!validId(manifest.id)) throw new Error(`Invalid manifest id: ${manifest.id}`);
|
|
84
|
+
if (!['rule', 'module'].includes(manifest.kind)) throw new Error(`Invalid manifest kind: ${manifest.kind}`);
|
|
85
|
+
if (!String(manifest.version ?? '').trim()) throw new Error(`Missing manifest version: ${manifest.id}`);
|
|
86
|
+
if (!Array.isArray(manifest.artifacts ?? [])) throw new Error(`Invalid artifacts: ${manifest.id}`);
|
|
87
|
+
if (!Array.isArray(manifest.obsolete ?? [])) throw new Error(`Invalid obsolete artifacts: ${manifest.id}`);
|
|
88
|
+
for (const artifact of manifest.artifacts ?? []) {
|
|
89
|
+
resolveProjectPath('/__specdest_project__', artifact.path);
|
|
90
|
+
if (['managed-file', 'scaffold'].includes(artifact?.kind)) {
|
|
91
|
+
if (typeof artifact.content !== 'string') throw new Error(`Artifact content must be text: ${manifest.id}:${artifact.path}`);
|
|
92
|
+
} else if (isSharedArtifact(artifact)) {
|
|
93
|
+
validateSharedArtifact(artifact);
|
|
94
|
+
} else throw new Error(`Unsupported artifact kind: ${artifact?.kind}`);
|
|
95
|
+
}
|
|
96
|
+
for (const artifact of manifest.obsolete ?? []) {
|
|
97
|
+
resolveProjectPath('/__specdest_project__', artifact.path);
|
|
98
|
+
if (artifact?.kind === 'file') continue;
|
|
99
|
+
if (isSharedArtifact(artifact)) validateSharedArtifact(artifact, { obsolete: true });
|
|
100
|
+
else throw new Error(`Unsupported obsolete artifact kind: ${artifact?.kind}`);
|
|
101
|
+
}
|
|
102
|
+
return manifest;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function claimFormat(artifact) {
|
|
106
|
+
if (!isSharedArtifact(artifact)) return 'whole';
|
|
107
|
+
if (artifact.kind === 'shared-json-field') return 'json';
|
|
108
|
+
if (artifact.kind === 'shared-markdown-section') return 'markdown';
|
|
109
|
+
if (artifact.kind === 'shared-line') return 'line';
|
|
110
|
+
return 'key-value';
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function buildOwnershipGraph(manifests) {
|
|
114
|
+
const ids = new Set();
|
|
115
|
+
const claims = new Map();
|
|
116
|
+
const claim = (owner, artifact) => {
|
|
117
|
+
const path = artifact.path;
|
|
118
|
+
const state = claims.get(path) ?? { whole: null, format: null, scopes: new Map() };
|
|
119
|
+
const format = claimFormat(artifact);
|
|
120
|
+
if (format === 'whole') {
|
|
121
|
+
if (state.whole || state.scopes.size) throw new Error(`Ownership conflict for ${path}`);
|
|
122
|
+
state.whole = owner;
|
|
123
|
+
} else {
|
|
124
|
+
if (state.whole) throw new Error(`Ownership conflict for ${path}`);
|
|
125
|
+
if (state.format && state.format !== format) throw new Error(`Ownership conflict for ${path}: incompatible shared formats`);
|
|
126
|
+
state.format = format;
|
|
127
|
+
const scope = sharedOwnershipKey(artifact);
|
|
128
|
+
if (state.scopes.has(scope)) throw new Error(`Ownership conflict for ${path}:${scope}`);
|
|
129
|
+
state.scopes.set(scope, owner);
|
|
130
|
+
}
|
|
131
|
+
claims.set(path, state);
|
|
132
|
+
};
|
|
133
|
+
for (const manifest of manifests) {
|
|
134
|
+
validateManifest(manifest);
|
|
135
|
+
if (ids.has(manifest.id)) throw new Error(`Duplicate manifest id: ${manifest.id}`);
|
|
136
|
+
ids.add(manifest.id);
|
|
137
|
+
for (const artifact of manifest.artifacts ?? []) claim(manifest.id, artifact);
|
|
138
|
+
for (const artifact of manifest.obsolete ?? []) claim(manifest.id, artifact);
|
|
139
|
+
}
|
|
140
|
+
return { claims };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function fileState(filePath) {
|
|
144
|
+
try {
|
|
145
|
+
const info = await stat(filePath);
|
|
146
|
+
return info.isFile() ? 'file' : 'other';
|
|
147
|
+
} catch (error) {
|
|
148
|
+
if (error?.code === 'ENOENT') return 'missing';
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function sortedInstalled(installed) {
|
|
154
|
+
return Object.fromEntries(Object.entries(installed).sort(([a], [b]) => a.localeCompare(b)));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export async function previewSync(projectRoot, manifests, options = {}) {
|
|
158
|
+
const currentState = await readInstalledState(projectRoot);
|
|
159
|
+
const catalog = new Map();
|
|
160
|
+
for (const manifest of manifests) {
|
|
161
|
+
validateManifest(manifest);
|
|
162
|
+
if (catalog.has(manifest.id)) throw new Error(`Duplicate manifest id: ${manifest.id}`);
|
|
163
|
+
catalog.set(manifest.id, manifest);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const desiredIds = options.desiredIds ?? Object.keys(currentState.installed);
|
|
167
|
+
const selected = [...new Set(desiredIds)].sort().map((id) => {
|
|
168
|
+
const manifest = catalog.get(id);
|
|
169
|
+
if (!manifest) throw new Error(`Desired item has no current manifest: ${id}`);
|
|
170
|
+
return manifest;
|
|
171
|
+
});
|
|
172
|
+
buildOwnershipGraph(selected);
|
|
173
|
+
|
|
174
|
+
const operations = [];
|
|
175
|
+
const sharedFiles = new Map();
|
|
176
|
+
const collectShared = (ownerId, artifact, obsolete) => {
|
|
177
|
+
const entry = sharedFiles.get(artifact.path) ?? { active: [], obsolete: [] };
|
|
178
|
+
entry[obsolete ? 'obsolete' : 'active'].push({ ownerId, artifact });
|
|
179
|
+
sharedFiles.set(artifact.path, entry);
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
for (const manifest of selected) {
|
|
183
|
+
for (const artifact of manifest.artifacts ?? []) {
|
|
184
|
+
if (isSharedArtifact(artifact)) {
|
|
185
|
+
collectShared(manifest.id, artifact, false);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
const operation = await planFileContent(projectRoot, artifact.path, artifact.content, {
|
|
189
|
+
createOnly: artifact.kind === 'scaffold',
|
|
190
|
+
});
|
|
191
|
+
operations.push({ ...operation, ownerId: manifest.id, artifactKind: artifact.kind });
|
|
192
|
+
}
|
|
193
|
+
for (const artifact of manifest.obsolete ?? []) {
|
|
194
|
+
if (isSharedArtifact(artifact)) {
|
|
195
|
+
collectShared(manifest.id, artifact, true);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
const target = resolveProjectPath(projectRoot, artifact.path);
|
|
199
|
+
const state = await fileState(target);
|
|
200
|
+
if (state === 'other') throw new Error(`Obsolete file target is not a file: ${artifact.path}`);
|
|
201
|
+
operations.push({
|
|
202
|
+
relativePath: artifact.path,
|
|
203
|
+
target,
|
|
204
|
+
action: state === 'file' ? 'delete' : 'unchanged',
|
|
205
|
+
ownerId: manifest.id,
|
|
206
|
+
artifactKind: 'obsolete-file',
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
for (const [relativePath, entry] of [...sharedFiles.entries()].sort(([a], [b]) => a.localeCompare(b))) {
|
|
212
|
+
const target = resolveProjectPath(projectRoot, relativePath);
|
|
213
|
+
const state = await fileState(target);
|
|
214
|
+
if (state === 'other') throw new Error(`Shared file target is not a file: ${relativePath}`);
|
|
215
|
+
const existing = await readText(target);
|
|
216
|
+
let updated = existing;
|
|
217
|
+
for (const item of [...entry.obsolete].sort((a, b) => sharedOwnershipKey(a.artifact).localeCompare(sharedOwnershipKey(b.artifact)))) {
|
|
218
|
+
updated = removeSharedArtifact(updated, item.artifact);
|
|
219
|
+
}
|
|
220
|
+
for (const item of [...entry.active].sort((a, b) => sharedOwnershipKey(a.artifact).localeCompare(sharedOwnershipKey(b.artifact)))) {
|
|
221
|
+
updated = applySharedArtifact(updated, item.artifact);
|
|
222
|
+
}
|
|
223
|
+
let operation = await planFileContent(projectRoot, relativePath, updated);
|
|
224
|
+
if (entry.active.length === 0 && state === 'missing' && updated === '') {
|
|
225
|
+
operation = { ...operation, action: 'unchanged' };
|
|
226
|
+
}
|
|
227
|
+
operations.push({
|
|
228
|
+
...operation,
|
|
229
|
+
ownerIds: [...new Set([...entry.active, ...entry.obsolete].map((item) => item.ownerId))].sort(),
|
|
230
|
+
artifactKind: 'shared-file',
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const nextInstalled = { ...currentState.installed };
|
|
235
|
+
for (const manifest of selected) {
|
|
236
|
+
nextInstalled[manifest.id] = { kind: manifest.kind, version: manifest.version };
|
|
237
|
+
}
|
|
238
|
+
const nextState = { schemaVersion: STATE_SCHEMA_VERSION, installed: sortedInstalled(nextInstalled) };
|
|
239
|
+
const normalizedCurrent = { schemaVersion: STATE_SCHEMA_VERSION, installed: sortedInstalled(currentState.installed) };
|
|
240
|
+
return {
|
|
241
|
+
projectRoot,
|
|
242
|
+
operations,
|
|
243
|
+
currentState: normalizedCurrent,
|
|
244
|
+
nextState,
|
|
245
|
+
stateChanged: JSON.stringify(normalizedCurrent) !== JSON.stringify(nextState),
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export async function applySyncPlan(plan) {
|
|
250
|
+
const supported = new Set(['create', 'update', 'unchanged', 'delete']);
|
|
251
|
+
for (const operation of plan.operations) {
|
|
252
|
+
if (!supported.has(operation.action)) {
|
|
253
|
+
throw new Error(`Unsupported sync operation: ${operation.action}`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
for (const operation of plan.operations) {
|
|
257
|
+
if (operation.action === 'unchanged') continue;
|
|
258
|
+
if (operation.action === 'delete') {
|
|
259
|
+
try {
|
|
260
|
+
await unlink(operation.target);
|
|
261
|
+
} catch (error) {
|
|
262
|
+
if (error?.code !== 'ENOENT') throw error;
|
|
263
|
+
}
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
await applyFileOperation(operation);
|
|
267
|
+
}
|
|
268
|
+
if (plan.stateChanged) await writeInstalledState(plan.projectRoot, plan.nextState);
|
|
269
|
+
return plan;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export async function syncProject(projectRoot, manifests) {
|
|
273
|
+
const plan = await previewSync(projectRoot, manifests);
|
|
274
|
+
await applySyncPlan(plan);
|
|
275
|
+
return plan;
|
|
276
|
+
}
|