Files
waggle-os/scripts/check-workspace-deps.mjs
shumeiko.it 46687a823a chore: remove the packages excluded from the community edition (P2.2)
Delete apps/www, packages/admin-web, every packages/hive-mind-* package,
packages/memory-mcp and packages/wiki-compiler (ADR-0002), with what
existed only for them: their tests kept outside the packages, the hook
runtime build, the hive-mind OSS subtree-split and drift scripts, the
www deploy and hive-mind-cli workflows, the staged hook lifecycle step
in the Windows desktop workflows, and the root dev:www, build:www and
build:hook-runtime scripts.

Drop the matching excludes from the Vitest, ESLint and workspace
dependency checks, the next override, and the extraneous workspace
entries from package-lock.json. The smart-router qualification seed
now writes its spend to waggle.db, where the sidecar has read cost
traces since P1.5.
2026-11-01 00:21:10 +01:00

109 lines
3.9 KiB
JavaScript

#!/usr/bin/env node
/**
* Fails when a workspace package imports a package it does not declare.
*
* npm hoists every dependency to the root node_modules, so an undeclared
* import still works inside the monorepo and only breaks once the package is
* installed on its own. This check reads the runtime sources (`src/`) of each
* community-edition package and compares the bare import specifiers with the
* package's dependencies, devDependencies, peerDependencies and
* optionalDependencies.
*
* Usage: node scripts/check-workspace-deps.mjs
*/
import fs from 'node:fs';
import path from 'node:path';
import { builtinModules } from 'node:module';
const ROOT = path.resolve(path.dirname(new URL(import.meta.url).pathname), '..');
// Known undeclared imports that a work package removes. Each entry needs a reason.
const PENDING = {};
const SOURCE_EXT = /\.(?:[cm]?[jt]sx?)$/;
// Valid npm package names, so specifier-like text inside strings is ignored.
const PACKAGE_NAME = /^(?:@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*$/i;
const BUILTINS = new Set(builtinModules);
function workspaceDirs() {
const dirs = [];
for (const parent of ['apps', 'packages']) {
for (const name of fs.readdirSync(path.join(ROOT, parent))) {
const rel = `${parent}/${name}`;
if (fs.existsSync(path.join(ROOT, rel, 'package.json'))) dirs.push(rel);
}
}
return dirs.sort();
}
function sourceFiles(dir) {
const out = [];
const walk = (d) => {
for (const entry of fs.readdirSync(d, { withFileTypes: true })) {
if (entry.name === 'node_modules' || entry.name === 'dist') continue;
const full = path.join(d, entry.name);
if (entry.isDirectory()) walk(full);
else if (SOURCE_EXT.test(entry.name) && !/\.(?:test|spec)\./.test(entry.name)) out.push(full);
}
};
if (fs.existsSync(dir)) walk(dir);
return out;
}
function stripComments(code) {
return code
.replace(/\/\*[\s\S]*?\*\//g, '')
.replace(/(^|[^:'"`\\])\/\/.*$/gm, '$1');
}
const SPECIFIER_PATTERNS = [
/\bfrom\s*['"]([^'"\n]+)['"]/g,
/\bimport\s*['"]([^'"\n]+)['"]/g,
/\bimport\s*\(\s*['"]([^'"\n]+)['"]\s*\)/g,
/\brequire\s*\(\s*['"]([^'"\n]+)['"]\s*\)/g,
];
function packageName(specifier) {
if (specifier.startsWith('.') || specifier.startsWith('/')) return null;
if (specifier.startsWith('node:') || specifier.startsWith('@/') || specifier.startsWith('virtual:')) return null;
const parts = specifier.split('/');
const name = specifier.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
if (BUILTINS.has(name) || !PACKAGE_NAME.test(name)) return null;
return name;
}
let failures = 0;
for (const rel of workspaceDirs()) {
const manifest = JSON.parse(fs.readFileSync(path.join(ROOT, rel, 'package.json'), 'utf8'));
const declared = new Set([
manifest.name,
...Object.keys(manifest.dependencies ?? {}),
...Object.keys(manifest.devDependencies ?? {}),
...Object.keys(manifest.peerDependencies ?? {}),
...Object.keys(manifest.optionalDependencies ?? {}),
]);
const pending = PENDING[rel] ?? {};
const missing = new Map();
for (const file of sourceFiles(path.join(ROOT, rel, 'src'))) {
const code = stripComments(fs.readFileSync(file, 'utf8'));
for (const pattern of SPECIFIER_PATTERNS) {
for (const match of code.matchAll(pattern)) {
const name = packageName(match[1]);
if (!name || declared.has(name) || declared.has(`@types/${name}`) || name in pending) continue;
if (!missing.has(name)) missing.set(name, path.relative(ROOT, file));
}
}
}
if (missing.size > 0) {
failures += missing.size;
console.error(`${rel} (${manifest.name}) imports undeclared packages:`);
for (const [name, file] of missing) console.error(` ${name} (first seen in ${file})`);
}
}
if (failures > 0) {
console.error(`\n${failures} undeclared import(s). Declare them in the package's package.json.`);
process.exit(1);
}
console.log('All workspace imports are declared.');