Repository navigation
Expand file tree
/
Copy pathverify-workspace-boundaries.js
More file actions
214 lines (199 loc) · 9.61 KB
/
Copy pathverify-workspace-boundaries.js
File metadata and controls
214 lines (199 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
const repositoryRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const REQUIRED_WORKSPACES = [
"apps/cli",
"apps/web",
"apps/desktop",
"packages/core",
"packages/contracts",
"packages/core-client",
"packages/app-ui",
"packages/design-system",
"packages/test-fixtures"
];
const DEPENDENCY_FIELDS = ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"];
async function readJson(relativePath) {
return JSON.parse(await fs.readFile(path.join(repositoryRoot, relativePath), "utf8"));
}
function assert(condition, message) {
if (!condition) throw new Error(message);
}
function assertExactDependencies(manifest, label) {
for (const field of DEPENDENCY_FIELDS) {
for (const [name, specification] of Object.entries(manifest[field] ?? {})) {
assert(
typeof specification === "string"
&& /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(specification),
`${label} ${field}.${name} must use an exact version; found ${String(specification)}.`
);
}
}
}
async function sourceFiles(relativeRoot) {
const root = path.join(repositoryRoot, relativeRoot);
const result = [];
async function visit(directory) {
for (const entry of await fs.readdir(directory, { withFileTypes: true })) {
const fullPath = path.join(directory, entry.name);
if (entry.isDirectory()) await visit(fullPath);
else if (/\.(?:js|mjs|ts|tsx)$/.test(entry.name)) result.push(fullPath);
}
}
await visit(root);
return result;
}
const rootManifest = await readJson("package.json");
assert(rootManifest.name === "@dailin521/codex-provider-sync", "Root npm package name changed.");
assert(rootManifest.bin?.["codex-provider"] === "src/cli.js", "Root CLI bin changed.");
assert(rootManifest.engines?.node === ">=16.20.2", "Root Node 16 compatibility contract changed.");
assert(Array.isArray(rootManifest.workspaces), "Root npm workspaces are not enabled.");
assert(rootManifest.workspaces.includes("apps/*") && rootManifest.workspaces.includes("packages/*"), "Root workspace globs are incomplete.");
assert(Object.keys(rootManifest.dependencies ?? {}).length === 0, "Root runtime dependencies must stay Core-only.");
assertExactDependencies(rootManifest, "root package");
const rootDependencyNames = DEPENDENCY_FIELDS.flatMap((field) => Object.keys(rootManifest[field] ?? {}));
assert(
!rootDependencyNames.some((name) => name === "electron" || name.startsWith("electron-")),
"Root npm manifest must not contain Electron dependencies."
);
const rootPublishAllowlist = new Set([
"README.md",
"CHANGELOG.md",
"CONTRIBUTING.md",
"CONTRIBUTORS.md",
"AGENTS.md",
"docs",
"images/README",
"src",
"web/dist",
"packages/contracts/dist",
"packages/core/src"
]);
for (const entry of rootManifest.files ?? []) {
assert(rootPublishAllowlist.has(entry), `Root tarball allowlist contains an unapproved path: ${entry}`);
}
for (const required of rootPublishAllowlist) {
assert(rootManifest.files?.includes(required), `Root tarball allowlist is missing ${required}.`);
}
const manifests = new Map();
for (const workspace of REQUIRED_WORKSPACES) {
const manifest = await readJson(`${workspace}/package.json`);
manifests.set(workspace, manifest);
assert(manifest.private === true, `${workspace} must remain private.`);
assertExactDependencies(manifest, workspace);
}
const coreSource = await fs.readFile(path.join(repositoryRoot, "packages/core/src/index.js"), "utf8");
assert(!/(?:\.\.\/)+src\//.test(coreSource), "CoreFacade must not import the legacy root directly.");
assert(!coreSource.includes("legacy-public-api-port"), "CoreFacade must not route through a legacy public API port.");
const allowedRootCoreImports = new Map([
["src/web-core-adapter.js", new Set([
"../packages/core/src/index.js"
])],
["src/service.js", new Set([
"../packages/core/src/application/service-runtime.js"
])],
["src/diagnostics.js", new Set([
"../packages/core/src/application/diagnostics.js"
])],
["src/watch.js", new Set([
"../packages/core/src/application/watch-runtime.js"
])]
]);
for (const filePath of await sourceFiles("src")) {
const source = await fs.readFile(filePath, "utf8");
const relativePath = path.relative(repositoryRoot, filePath).replaceAll("\\", "/");
const packageCoreImports = [...source.matchAll(/from\s+["']([^"']*packages\/core\/src\/[^"']+)["']/g)]
.map((match) => match[1]);
const allowed = allowedRootCoreImports.get(relativePath) ?? new Set();
const actual = new Set(packageCoreImports);
assert(
actual.size === allowed.size
&& [...actual].every((specifier) => allowed.has(specifier)),
`${relativePath} has an unapproved Core implementation import.`
);
}
for (const relativePath of [
"packages/core/src/application/concurrency-guard.js",
"packages/core/src/application/operation-runtime.js",
"packages/core/src/application/plan-apply-guard.js"
]) {
const source = await fs.readFile(path.join(repositoryRoot, relativePath), "utf8");
assert(!/(?:\.\.\/)+src\//.test(source), `${relativePath} imports the legacy root.`);
assert(!/node:(?:fs|sqlite)/.test(source), `${relativePath} imports storage infrastructure.`);
assert(!/backup|journal|session-files|state-db-lock/.test(source), `${relativePath} owns a data-mutation concern.`);
}
for (const boundary of [
"packages/contracts/src",
"packages/core-client/src",
"packages/app-ui/src",
"packages/design-system/src"
]) {
for (const filePath of await sourceFiles(boundary)) {
const source = await fs.readFile(filePath, "utf8");
assert(!/from\s+["'](?:node:|electron)/.test(source), `${path.relative(repositoryRoot, filePath)} imports a forbidden platform module.`);
assert(!/\.\.\/.*src\//.test(source), `${path.relative(repositoryRoot, filePath)} deep-imports implementation source.`);
}
}
const desktopManifest = manifests.get("apps/desktop");
const desktopDependencies = {
...(desktopManifest.dependencies ?? {}),
...(desktopManifest.devDependencies ?? {}),
...(desktopManifest.optionalDependencies ?? {})
};
assert(
desktopManifest.dependencies?.["@codex-provider-sync/test-fixtures"] === undefined
&& desktopManifest.devDependencies?.["@codex-provider-sync/test-fixtures"] === "0.0.0",
"Desktop fault fixtures must remain an exact private devDependency, never a production dependency."
);
const approvedDesktopElectronDependencies = new Map([
["electron", { version: "44.9.3", field: "devDependencies", checkpoint: "C6" }],
["electron-vite", { version: "5.0.0", field: "devDependencies", checkpoint: "C6" }],
["electron-builder", { version: "26.15.7", field: "devDependencies", checkpoint: "C6" }],
["electron-updater", { version: "6.8.9", field: "dependencies", checkpoint: "C8" }],
["better-sqlite3", { version: "13.0.3", field: "dependencies", checkpoint: "C9" }],
["@electron/asar", { version: "4.3.0", field: "devDependencies", checkpoint: "C9" }],
["@electron/fuses", { version: "2.1.3", field: "devDependencies", checkpoint: "C9" }],
["plist", { version: "5.0.0", field: "devDependencies", checkpoint: "C9" }],
["resedit", { version: "3.1.0", field: "devDependencies", checkpoint: "C9" }]
]);
for (const [name, approval] of approvedDesktopElectronDependencies) {
assert(
desktopManifest[approval.field]?.[name] === approval.version,
`apps/desktop must pin ${name} to the reviewed ${approval.checkpoint} version ${approval.version}.`
);
}
for (const name of Object.keys(desktopDependencies)) {
if (name === "electron" || name.startsWith("electron-") || name.startsWith("@electron/")) {
assert(approvedDesktopElectronDependencies.has(name), `Unreviewed Electron dependency: ${name}`);
}
}
for (const [workspace, manifest] of manifests) {
if (workspace === "apps/desktop") continue;
const dependencies = DEPENDENCY_FIELDS.flatMap((field) => Object.keys(manifest[field] ?? {}));
assert(
!dependencies.some((name) => name === "electron" || name.startsWith("electron-")),
`${workspace} must not depend on Electron.`
);
}
for (const filePath of await sourceFiles("apps/desktop/src/renderer")) {
const source = await fs.readFile(filePath, "utf8");
assert(!/from\s+["'](?:node:|electron)/.test(source), `${path.relative(repositoryRoot, filePath)} imports Node or Electron.`);
assert(!/@codex-provider-sync\/core(?:["'/])/.test(source), `${path.relative(repositoryRoot, filePath)} imports Core directly.`);
}
for (const filePath of await sourceFiles("apps/desktop/src/preload")) {
const source = await fs.readFile(filePath, "utf8");
assert(!/from\s+["']node:/.test(source), `${path.relative(repositoryRoot, filePath)} imports Node in sandboxed preload.`);
assert(!/@codex-provider-sync\/core(?:["'/])/.test(source), `${path.relative(repositoryRoot, filePath)} imports Core in preload.`);
}
for (const filePath of await sourceFiles("apps/desktop/src/main")) {
const source = await fs.readFile(filePath, "utf8");
assert(!/@codex-provider-sync\/core(?:["'/])/.test(source), `${path.relative(repositoryRoot, filePath)} runs Core in Main.`);
assert(!/\.\.\/\.\.\/\.\.\/src\//.test(source), `${path.relative(repositoryRoot, filePath)} deep-imports root implementation.`);
}
for (const filePath of await sourceFiles("apps/desktop/src/runtime")) {
const source = await fs.readFile(filePath, "utf8");
assert(!/\.\.\/main\//.test(source), `${path.relative(repositoryRoot, filePath)} depends on Electron Main.`);
assert(!/\.\.\/\.\.\/\.\.\/src\//.test(source), `${path.relative(repositoryRoot, filePath)} deep-imports root implementation.`);
}
process.stdout.write("Workspace package, Electron, dependency, import and root publish boundaries are valid.\n");