Generate the typed registry of an fw-shaped package
Generate the typed module registry of your fw package, and fail the check when it drifts.
Apache-2.0Command line
Package: @awacloud/tool-fw-codegen
Run it
Command line Guide
On a two-module fw-shaped package: preview and apply the deps injection,
generate types/registry.generated.d.ts, then let registry --check pass
and, after an edit, fail.
Uses @awacloud/tool-fw-codegen (Apache-2.0). This example's code is Apache-2.0.
When to use it
Your own package follows the fw module pattern ({ name, dependencies, factory } per file under src/). The deps injector fills the
bundler-friendly deps: [...] companion from the string dependencies, and
the registry generator gives resolve('name') its precise type for editors
and tsc; --check in CI catches a stale registry.
Copy-paste steps
npm install --save-dev @awacloud/tool-fw-codegen- From your package root:
bunx fw-codegen deps --dry-run, thenbunx fw-codegen deps, thenbunx fw-codegen registry; addbunx fw-codegen registry --checkto CI. - Or copy
main.jsand runbun main.js: it builds a throwaway package in the OS temp directory and prints the four exit codes.
What it shows
runCli([...])from@awacloud/tool-fw-codegen/depsand@awacloud/tool-fw-codegen/registry, in process, returning the exit codes (0 ok, 1 stale or unresolvable, 2 usage).deps --dry-runreports without writing;depsaddsimport { alpha } from './alpha.js'anddeps: [alpha]to a module that only named its dependency.registrywritestypes/registry.generated.d.tswith aModuleInstanceMapfor the scanned modules;--checkfails once a module is added without regenerating.
Code
main.js
// SPDX-License-Identifier: Apache-2.0
// Run: bun main.js (Bun only: Bun.Glob) — builds a throwaway package in the OS temp dir. Equivalent CLI, from
// the package root: bunx fw-codegen deps --dry-run; bunx fw-codegen deps; bunx fw-codegen registry [--check]
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { runCli as deps } from '@awacloud/tool-fw-codegen/deps';
import { runCli as registry } from '@awacloud/tool-fw-codegen/registry';
const ALPHA = `export const alpha = {
name: 'alpha',
version: '1.0.0',
dependencies: [],
factory() {
return { greet: (name) => \`hello, \${name}\` };
},
};
`;
// `gamma` declares its dependency by name only: the deps injector adds the import and the deps field.
const GAMMA = `export const gamma = {
name: 'gamma',
version: '1.0.0',
dependencies: ['alpha'],
factory(alpha) {
return { shout: (name) => alpha.greet(name).toUpperCase() };
},
};
`;
/** A minimal fw-shaped package: `package.json` + `src/<area>/<module>.js`. @param {string} dir */
export function scaffold(dir) {
mkdirSync(join(dir, 'src/mod'), { recursive: true });
const manifest = { name: '@example/mini', version: '0.0.0', type: 'module' };
writeFileSync(join(dir, 'package.json'), JSON.stringify(manifest, null, 2));
writeFileSync(join(dir, 'src/mod/alpha.js'), ALPHA);
writeFileSync(join(dir, 'src/mod/gamma.js'), GAMMA);
return dir;
}
/**
* The four steps, in order, as the tool's exit codes.
* @param {string} dir
* @returns {{ dryRun: number, apply: number, registry: number, check: number, types: string }}
*/
export function generate(dir) {
const dryRun = deps(['--pkg', dir, '--dry-run']);
const apply = deps(['--pkg', dir]);
const reg = registry(['--pkg', dir]);
const check = registry(['--pkg', dir, '--check']);
const types = readFileSync(join(dir, 'types/registry.generated.d.ts'), 'utf8');
return { dryRun, apply, registry: reg, check, types };
}
/** Add a module without regenerating: `--check` must now fail (exit 1). @param {string} dir */
export function drift(dir) {
writeFileSync(join(dir, 'src/mod/delta.js'), ALPHA.replace(/alpha/g, 'delta'));
return registry(['--pkg', dir, '--check']);
}
/** @returns {Promise<string[]>} report lines */
export async function main() {
const dir = scaffold(mkdtempSync(join(tmpdir(), 'awa-example-codegen-')));
try {
const g = generate(dir);
const gamma = readFileSync(join(dir, 'src/mod/gamma.js'), 'utf8');
return [
`deps --dry-run: exit ${g.dryRun}; deps: exit ${g.apply}; `
+ `registry: exit ${g.registry}; registry --check: exit ${g.check}`,
`gamma.js now has: ${gamma.includes('deps: [alpha]') ? 'deps: [alpha]' : 'no deps field'}`,
`registry --check after adding delta.js: exit ${drift(dir)}`,
];
} finally {
rmSync(dir, { recursive: true, force: true });
}
}
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
for (const line of await main()) console.log(line);
}