From d1295aa4ad362df88dbe7b2fddb54a66ab91763b Mon Sep 17 00:00:00 2001 From: Wanjohi <71614375+wanjohiryan@users.noreply.github.com> Date: Wed, 5 Nov 2025 16:05:07 +0300 Subject: [PATCH] feat: Fix yargs cli --- bun.lock | 3 +++ packages/studio/package.json | 3 +++ packages/studio/src/error.ts | 26 ++++++++++++++++++++ packages/studio/src/index.ts | 47 +++++++++++++++++++++++++++++++++++- 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 packages/studio/src/error.ts diff --git a/bun.lock b/bun.lock index eddedef2..8492265e 100644 --- a/bun.lock +++ b/bun.lock @@ -190,6 +190,9 @@ }, "packages/studio": { "name": "@nestri/studio", + "dependencies": { + "yargs": "^18.0.0", + }, "devDependencies": { "@tsconfig/bun": "catalog:", "@types/bun": "catalog:", diff --git a/packages/studio/package.json b/packages/studio/package.json index b680ec20..cbffd78d 100644 --- a/packages/studio/package.json +++ b/packages/studio/package.json @@ -16,5 +16,8 @@ "peerDependencies": { "@nestri/core": "workspace:^", "@nestri/function": "workspace:^" + }, + "dependencies": { + "yargs": "^18.0.0" } } diff --git a/packages/studio/src/error.ts b/packages/studio/src/error.ts new file mode 100644 index 00000000..8fc3b054 --- /dev/null +++ b/packages/studio/src/error.ts @@ -0,0 +1,26 @@ +// import { ConfigMarkdown } from "@/config/markdown" +// import { Config } from "../config/config" +// import { UI } from "./ui" + +export function FormatError(input: unknown) { + // if (Config.JsonError.isInstance(input)) { + // return ( + // `Config file at ${input.data.path} is not valid JSON(C)` + + // (input.data.message ? `: ${input.data.message}` : "") + // ) + // } + // if (Config.ConfigDirectoryTypoError.isInstance(input)) { + // return `Directory "${input.data.dir}" in ${input.data.path} is not valid. Use "${input.data.suggestion}" instead. This is a common typo.` + // } + // if (ConfigMarkdown.FrontmatterError.isInstance(input)) { + // return `Failed to parse frontmatter in ${input.data.path}:\n${input.data.message}` + // } + // if (Config.InvalidError.isInstance(input)) + // return [ + // `Config file at ${input.data.path} is invalid` + + // (input.data.message ? `: ${input.data.message}` : ""), + // ...(input.data.issues?.map((issue) => "↳ " + issue.message + " " + issue.path.join(".")) ?? + // []), + // ].join("\n") + return "TODO: Format this error message"; +} diff --git a/packages/studio/src/index.ts b/packages/studio/src/index.ts index b59e81de..2183735c 100644 --- a/packages/studio/src/index.ts +++ b/packages/studio/src/index.ts @@ -1,6 +1,8 @@ import yargs from "yargs"; +import { FormatError } from "./error"; import { hideBin } from "yargs/helpers"; import { Log } from "@nestri/core/utils/log"; +import { NamedError } from "@nestri/core/utils/error"; import { Installation } from "@nestri/core/installation/index"; process.on("unhandledRejection", (e) => { @@ -16,8 +18,51 @@ process.on("uncaughtException", (e) => { }); const cli = yargs(hideBin(process.argv)) - .scriptName("nestri-studio") + .scriptName("nestri") .help("help", "Show help") .alias("h", "help") .version("version", "show version number", Installation.VERSION) .alias("v", "version"); + +try { + await cli.parse(); +} catch (e) { + let data: Record = {}; + if (e instanceof NamedError) { + const obj = e.toObject(); + Object.assign(data, { + ...obj.data, + }); + } + + if (e instanceof Error) { + Object.assign(data, { + name: e.name, + message: e.message, + cause: e.cause?.toString(), + stack: e.stack, + }); + } + + if (e instanceof ResolveMessage) { + Object.assign(data, { + name: e.name, + message: e.message, + code: e.code, + specifier: e.specifier, + referrer: e.referrer, + position: e.position, + importKind: e.importKind, + }); + } + Log.Default.error("fatal", data); + const formatted = FormatError(e); + // if (formatted) UI.error(formatted) + if (formatted === undefined) { + // UI.error("Unexpected error, check log file at " + Log.file() + " for more details" + EOL) + console.error(e); + } + process.exitCode = 1; +} finally { + process.exit(); +}