build: npm and Deno workspace with core's gates
Same lint, format and complexity gates as core, plus a lint rule that keeps core a types-only import in plugin sources. Runtime suites are chosen from each package's engines, so declaring a runtime is what puts a package under its test. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
10 files changed
+3103
No files matched your search
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env node
|
||||
// Cognitive-complexity gate over every package's src. Fails (exit 1) if any named function exceeds the
|
||||
// threshold. Same gate as core's, walked per package because ccts-json takes one directory.
|
||||
import { execSync } from 'node:child_process';
|
||||
import { existsSync, readdirSync } from 'node:fs';
|
||||
|
||||
const THRESHOLD = 25;
|
||||
|
||||
function scores(dir) {
|
||||
try {
|
||||
return execSync(`npx ccts-json ${dir}`, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
||||
} catch (error) {
|
||||
return error.stdout?.toString() ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
const offenders = [];
|
||||
|
||||
function walk(node, dir) {
|
||||
if (!node || typeof node !== 'object') return;
|
||||
if (node.kind === 'function' && node.name && node.score > THRESHOLD) {
|
||||
offenders.push({ score: node.score, name: `${dir}: ${node.name}` });
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(node)) {
|
||||
if (key === 'inner' && Array.isArray(value)) value.forEach((child) => walk(child, dir));
|
||||
else if (value && typeof value === 'object') walk(value, dir);
|
||||
}
|
||||
}
|
||||
|
||||
const dirs = readdirSync('packages')
|
||||
.map((name) => `packages/${name}/src`)
|
||||
.filter((dir) => existsSync(dir));
|
||||
|
||||
for (const dir of dirs) {
|
||||
const raw = scores(dir);
|
||||
if (!raw.trim()) {
|
||||
console.error(`check-complexity: no output from ccts-json for ${dir}`);
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
walk(JSON.parse(raw), dir);
|
||||
}
|
||||
|
||||
if (offenders.length) {
|
||||
offenders.sort((a, b) => b.score - a.score);
|
||||
console.error(`\n✖ cognitive complexity: ${offenders.length} function(s) over ${THRESHOLD}\n`);
|
||||
for (const o of offenders) console.error(` ${String(o.score).padStart(4)} ${o.name}`);
|
||||
console.error('\nExtract helpers to bring them under the threshold. The threshold is not raised.\n');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`✓ cognitive complexity: every function ≤ ${THRESHOLD} (${dirs.length} package(s))`);
|
||||
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env node
|
||||
// Runs the contract tests of every package whose `engines` declares <runtime>, on that runtime.
|
||||
// `engines` is the one declaration (convention rule 9): a package that does not claim a runtime is not
|
||||
// tested on it, and a package that claims one cannot skip it.
|
||||
//
|
||||
// Usage: node scripts/run-runtime.mjs <deno|bun|workerd>
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { readdirSync, readFileSync } from 'node:fs';
|
||||
|
||||
const COMMANDS = {
|
||||
deno: (dirs) => ['deno', ['test', '--allow-all', '--no-check', ...dirs.map((dir) => `packages/${dir}/test/`)]],
|
||||
bun: (dirs) => ['bun', ['test', ...dirs.map((dir) => `./packages/${dir}/test/`)]],
|
||||
};
|
||||
|
||||
const runtime = process.argv[2];
|
||||
if (!(runtime in COMMANDS) && runtime !== 'workerd') {
|
||||
console.error('usage: run-runtime.mjs <deno|bun|workerd>');
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const declares = (dir) => {
|
||||
const { engines = {} } = JSON.parse(readFileSync(`packages/${dir}/package.json`, 'utf8'));
|
||||
return runtime in engines;
|
||||
};
|
||||
const dirs = readdirSync('packages').filter(declares);
|
||||
|
||||
if (dirs.length === 0) {
|
||||
console.log(`no package declares ${runtime}; nothing to run`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// There is no workerd harness for plugins yet (spec, section 4). A package that declares workerd must
|
||||
// not pass CI by having nothing run against it — copy core's test/edge harness first.
|
||||
if (runtime === 'workerd') {
|
||||
console.error(`${dirs.join(', ')} declare workerd, and there is no workerd harness yet.`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const [command, args] = COMMANDS[runtime](dirs);
|
||||
const { status } = spawnSync(command, args, { stdio: 'inherit' });
|
||||
process.exit(status ?? 1);
|
||||
Reference in new issue
Block a user