48 lines
2.1 KiB
JavaScript
48 lines
2.1 KiB
JavaScript
#!/usr/bin/env node
|
|
import { readFile, writeFile } from "node:fs/promises";
|
|
import {
|
|
generateTypeScriptBindings,
|
|
generatePortableTypeScriptBindings,
|
|
generateRemoteTypeScriptBindings,
|
|
} from "./index.js";
|
|
import path from "node:path";
|
|
import { reactPlatformTypes } from "./react-platform.js";
|
|
import { generateReactBindings } from "./react.js";
|
|
|
|
const main = async () => {
|
|
const [schema, revision, output, options, ...rest] = process.argv.slice(2);
|
|
if (!schema || !revision || !output || rest.length)
|
|
throw new Error("usage: quixos-codegen-ts SCHEMA.json PACKAGE_REVISION OUTPUT.ts [OPTIONS.json]");
|
|
const config = options ? JSON.parse(await readFile(options, "utf8")) : {};
|
|
const contracts = JSON.parse(await readFile(schema, "utf8"));
|
|
if ([config.portable, config.remote, config.react].filter(Boolean).length > 1)
|
|
throw new Error("Portable, remote and React bindings are separate artifacts");
|
|
if (config.remote !== undefined && config.remote !== true) throw new Error("Remote binding mode must be true");
|
|
if (config.portable && typeof config.portable.target !== "string")
|
|
throw new Error("Portable bindings require an explicit target");
|
|
const generated = config.remote
|
|
? generateRemoteTypeScriptBindings(
|
|
contracts,
|
|
revision,
|
|
config.runtimeModule ? { runtimeModule: config.runtimeModule } : {},
|
|
)
|
|
: config.portable
|
|
? generatePortableTypeScriptBindings(contracts, revision, {
|
|
...config.portable,
|
|
...(config.runtimeModule ? { runtimeModule: config.runtimeModule } : {}),
|
|
})
|
|
: generateTypeScriptBindings(contracts, revision, config);
|
|
await writeFile(output, generated);
|
|
if (config.react) {
|
|
await writeFile(path.join(path.dirname(output), "web-studio-react-runtime.d.ts"), reactPlatformTypes);
|
|
await writeFile(
|
|
path.join(path.dirname(output), "react-props.gen.ts"),
|
|
generateReactBindings(contracts, revision, config.react.propsExports, config.react.components),
|
|
);
|
|
}
|
|
};
|
|
main().catch((error: unknown) => {
|
|
console.error(error instanceof Error ? error.message : error);
|
|
process.exitCode = 1;
|
|
});
|