microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
packages/bundler/src/cli.ts
75lines · modecode
| 1 | /* eslint-disable no-console */ |
| 2 | import { resolvePath, typespecVersion } from "@typespec/compiler"; |
| 3 | import yargs from "yargs"; |
| 4 | import { bundleTypeSpecLibrary } from "./bundler.js"; |
| 5 | |
| 6 | try { |
| 7 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 8 | // @ts-ignore |
| 9 | await import("source-map-support/register.js"); |
| 10 | } catch { |
| 11 | // package only present in dev. |
| 12 | } |
| 13 | |
| 14 | async function main() { |
| 15 | console.log(`TypeSpec Developer Tools v${typespecVersion}\n`); |
| 16 | |
| 17 | await yargs(process.argv.slice(2)) |
| 18 | .scriptName("tspd") |
| 19 | .help() |
| 20 | .strict() |
| 21 | .parserConfiguration({ |
| 22 | "greedy-arrays": false, |
| 23 | "boolean-negation": false, |
| 24 | }) |
| 25 | .option("debug", { |
| 26 | type: "boolean", |
| 27 | description: "Output debug log messages.", |
| 28 | default: false, |
| 29 | }) |
| 30 | .command( |
| 31 | "bundle <entrypoint>", |
| 32 | "Generate browser compatible bundle.", |
| 33 | (cmd) => { |
| 34 | return cmd |
| 35 | .positional("entrypoint", { |
| 36 | description: "Path to the library entrypoint.", |
| 37 | type: "string", |
| 38 | demandOption: true, |
| 39 | }) |
| 40 | .option("output-dir", { |
| 41 | type: "string", |
| 42 | }); |
| 43 | }, |
| 44 | async (args) => { |
| 45 | const resolvedRoot = resolvePath(process.cwd(), args.entrypoint); |
| 46 | await bundleTypeSpecLibrary( |
| 47 | resolvedRoot, |
| 48 | args["output-dir"] ?? resolvePath(resolvedRoot, "out/browser") |
| 49 | ); |
| 50 | } |
| 51 | ) |
| 52 | .version(typespecVersion) |
| 53 | .demandCommand(1, "You must use one of the supported commands.").argv; |
| 54 | } |
| 55 | |
| 56 | function internalError(error: unknown): never { |
| 57 | // NOTE: An expected error, like one thrown for bad input, shouldn't reach |
| 58 | // here, but be handled somewhere else. If we reach here, it should be |
| 59 | // considered a bug and therefore we should not suppress the stack trace as |
| 60 | // that risks losing it in the case of a bug that does not repro easily. |
| 61 | |
| 62 | console.error("Internal error!"); |
| 63 | console.error("File issue at https://github.com/microsoft/typespec"); |
| 64 | console.error(); |
| 65 | console.error(error); |
| 66 | |
| 67 | process.exit(1); |
| 68 | } |
| 69 | |
| 70 | process.on("unhandledRejection", (error: unknown) => { |
| 71 | console.error("Unhandled promise rejection!"); |
| 72 | internalError(error); |
| 73 | }); |
| 74 | |
| 75 | main().catch(internalError); |