microsoft/typespec

Public

mirrored from https://github.com/microsoft/typespecAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3040a83d6de0cc6876163b48ec9be61eefa3ebdd

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

packages/bundler/src/cli.ts

75lines · modecode

1/* eslint-disable no-console */
2import { resolvePath, typespecVersion } from "@typespec/compiler";
3import yargs from "yargs";
4import { bundleTypeSpecLibrary } from "./bundler.js";
5
6try {
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
14async 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
56function 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
70process.on("unhandledRejection", (error: unknown) => {
71 console.error("Unhandled promise rejection!");
72 internalError(error);
73});
74
75main().catch(internalError);