Files
2026-11-23 11:34:52 +00:00

130 lines
4.0 KiB
JavaScript

#!/usr/bin/env node
// Keeps released contract versions frozen.
//
// Each `src/v<N>/` directory is one contract version. The generated
// declarations of a released version are stored in `api/v<N>.d.ts`. This
// script fails if they changed, if a released version was removed, or if
// `HooksApi` no longer offers a released version.
//
// node scripts/check-api.mjs check every version
// node scripts/check-api.mjs --freeze also store versions that have no snapshot yet
import { spawnSync } from 'node:child_process';
import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const packageDir = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const srcDir = join(packageDir, 'src');
const snapshotDir = join(packageDir, 'api');
const freeze = process.argv.includes('--freeze');
const VERSION_DIR = /^v(\d+)$/;
const HEADER =
'// Generated by scripts/check-api.mjs. Do not edit.\n' +
'// This contract version is released and frozen. Add a new version instead.\n';
const outDir = mkdtempSync(join(tmpdir(), 'hooks-api-'));
const failures = [];
try {
const tsc = spawnSync(
'tsc',
[
'-p',
'tsconfig.build.json',
'--composite',
'false',
'--incremental',
'false',
'--declaration',
'--emitDeclarationOnly',
'--declarationMap',
'false',
'--sourceMap',
'false',
'--outDir',
outDir,
],
{ cwd: packageDir, stdio: 'inherit', shell: process.platform === 'win32' },
);
if (tsc.error) {
console.error(
`Could not run tsc: ${tsc.error.message}. Run this script with \`pnpm check:api\`.`,
);
process.exit(1);
}
if (tsc.status !== 0) process.exit(tsc.status ?? 1);
const versions = readdirSync(srcDir)
.filter((name) => VERSION_DIR.test(name))
.sort((a, b) => Number(a.slice(1)) - Number(b.slice(1)));
for (const version of versions) {
const actual = renderVersion(join(outDir, version));
const snapshotPath = join(snapshotDir, `${version}.d.ts`);
if (!existsSync(snapshotPath)) {
if (freeze) {
writeFileSync(snapshotPath, actual);
console.log(`Froze ${version} in api/${version}.d.ts`);
} else {
failures.push(
`${version} has no snapshot. Run \`pnpm api:freeze\` when you release ${version}.`,
);
}
continue;
}
const expected = readFileSync(snapshotPath, 'utf8');
if (actual !== expected) {
const actualPath = join(outDir, `${version}.actual.d.ts`);
writeFileSync(actualPath, actual);
spawnSync('git', ['diff', '--no-index', '--no-color', snapshotPath, actualPath], {
stdio: 'inherit',
});
failures.push(
`Released contract ${version} changed. Revert the change, and add ` +
`v${Number(version.slice(1)) + 1} for the new shape.`,
);
}
}
const rootDeclarations = readFileSync(join(outDir, 'index.d.ts'), 'utf8');
for (const snapshot of readdirSync(snapshotDir).filter((name) => name.endsWith('.d.ts'))) {
const version = snapshot.replace(/\.d\.ts$/, '');
if (!versions.includes(version)) {
failures.push(`Released contract ${version} was removed. Released versions stay.`);
}
const n = version.slice(1);
if (!rootDeclarations.includes(`v${n}?: HooksApiV${n};`)) {
failures.push(`HooksApi must keep offering released contract ${version} as \`v${n}?\`.`);
}
}
} finally {
rmSync(outDir, { recursive: true, force: true });
}
if (failures.length > 0) {
console.error(`\n@n8n/hooks-api contract check failed:\n- ${failures.join('\n- ')}`);
process.exit(1);
}
console.log('@n8n/hooks-api contract check passed');
/** Joins all declaration files of one version into one stable text. */
function renderVersion(dir) {
const files = readdirSync(dir)
.filter((name) => name.endsWith('.d.ts'))
.sort();
const body = files
.map((name) => {
const content = readFileSync(join(dir, name), 'utf8')
.replace(/^\/\/# sourceMappingURL=.*$/gm, '')
.trimEnd();
return `// ---- ${name} ----\n${content}\n`;
})
.join('\n');
return `${HEADER}\n${body}`;
}