microsoft/typespec

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/fix-8106

Branches

Tags

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

Clone

HTTPS

Download ZIP

packages/bundler/src/cli.ts

74lines · modecode

1/* eslint-disable no-console */
2import { resolvePath } 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`);
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 .demandCommand(1, "You must use one of the supported commands.").argv;
53}
54
55function internalError(error: unknown): never {
56 // NOTE: An expected error, like one thrown for bad input, shouldn't reach
57 // here, but be handled somewhere else. If we reach here, it should be
58 // considered a bug and therefore we should not suppress the stack trace as
59 // that risks losing it in the case of a bug that does not repro easily.
60
61 console.error("Internal error!");
62 console.error("File issue at https://github.com/microsoft/typespec");
63 console.error();
64 console.error(error);
65
66 process.exit(1);
67}
68
69process.on("unhandledRejection", (error: unknown) => {
70 console.error("Unhandled promise rejection!");
71 internalError(error);
72});
73
74main().catch(internalError);
75