#!/usr/bin/env node /** * Type-checks the declarations puter.js publishes. * * The JSDoc in src/puter-js/src is the source of truth for the SDK's types; * src/puter-js/types is `tsc --emitDeclarationOnly` output, generated by the * SDK build and shipped in the npm tarball but never committed. So this * generates it and checks the result, rather than diffing against something * checked in. * * The check runs *without* `skipLibCheck`, which is the point: that flag is on * everywhere else, and it is how the hand-maintained declarations used to hide * broken re-exports — the root index.d.ts named types no module exported, and * nothing ever looked. * * Usage: * node tools/checkPuterjsTypes.mjs */ import { execFileSync } from 'node:child_process'; import { dirname, join, sep } from 'node:path'; import { fileURLToPath } from 'node:url'; const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..'); const SDK = join(ROOT, 'src', 'puter-js'); const tsc = (args) => { try { execFileSync('npx', ['tsc', ...args], { cwd: ROOT, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], }); return { ok: true, output: '' }; } catch (e) { if (e.stdout === undefined && e.stderr === undefined) throw e; return { ok: false, output: `${e.stdout ?? ''}${e.stderr ?? ''}` }; } }; const generated = tsc(['-p', join(SDK, 'tsconfig.types.json')]); if (!generated.ok) { console.error('Generating declarations from the puter.js JSDoc failed:\n'); console.error(generated.output); process.exit(1); } const checked = tsc([ '--noEmit', '--strict', '--target', 'es2022', '--module', 'nodenext', '--moduleResolution', 'nodenext', join(SDK, 'index.d.ts'), ]); // Errors inside third-party `@types` packages are not this project's to fix. const errors = checked.output .split('\n') .filter((line) => /error TS\d+/.test(line)) .filter((line) => !line.includes(`node_modules${sep}@types`)); if (errors.length) { console.error( 'The declarations generated from the puter.js JSDoc do not type-check.' + ' Fix the JSDoc in src/puter-js/src — src/puter-js/types is generated' + ' output and editing it there would be overwritten by the next build.\n', ); for (const line of errors) console.error(` ${line}`); process.exit(1); } console.log('puter.js declarations generate and type-check cleanly.');