Comparing version 0.4.1 to 0.5.0
import * as inversify_lib_interfaces_interfaces from 'inversify/lib/interfaces/interfaces'; | ||
import { Container } from 'inversify'; | ||
import { Argv as Argv$1 } from 'yargs'; | ||
import { Writable } from 'node:stream'; | ||
@@ -20,5 +21,16 @@ interface CommandStatic<Args extends CommandArgs = CommandArgs> { | ||
declare class CLIError extends Error { | ||
exitCode: number; | ||
constructor(message: string, exitCode?: number); | ||
} | ||
type ErrorHandler = (error: unknown, context: ErrorHandlerContext) => number; | ||
interface ErrorHandlerContext { | ||
stdout: Writable; | ||
stderr: Writable; | ||
} | ||
interface CLIOptions { | ||
container?: Container; | ||
yargs?: Argv$1; | ||
onError?: ErrorHandler; | ||
} | ||
@@ -29,3 +41,4 @@ declare class CLI { | ||
private isInit; | ||
constructor({ container, yargs }?: CLIOptions); | ||
private onError; | ||
constructor({ container, yargs, onError, }?: CLIOptions); | ||
register<T extends CommandStatic>(command: T): inversify_lib_interfaces_interfaces.interfaces.BindingWhenOnSyntax<T>; | ||
@@ -36,7 +49,2 @@ run(processArgs: string[]): Promise<number>; | ||
declare class CLIError extends Error { | ||
exitCode?: number | undefined; | ||
constructor(message: string, exitCode?: number | undefined); | ||
} | ||
declare const Argv = "argv"; | ||
@@ -50,2 +58,2 @@ declare const Args = "args"; | ||
export { Args, Argv, CLI, CLIError, Command, CommandArgs, CommandResult, CommandStatic, Stderr, Stdin, Stdout, array, options }; | ||
export { Args, Argv, CLI, CLIError, Command, CommandArgs, CommandResult, CommandStatic, ErrorHandler, ErrorHandlerContext, Stderr, Stdin, Stdout, array, options }; |
@@ -132,3 +132,3 @@ "use strict"; | ||
exitCode; | ||
constructor(message, exitCode) { | ||
constructor(message, exitCode = 1) { | ||
super(message); | ||
@@ -138,2 +138,13 @@ this.exitCode = exitCode; | ||
}; | ||
var defualtErrorHandler = /* @__PURE__ */ __name((error, { stderr }) => { | ||
const message = error.message; | ||
if (message != null) { | ||
stderr.write(message); | ||
stderr.write("\n"); | ||
} | ||
if (error instanceof CLIError) { | ||
return error.exitCode; | ||
} | ||
return 1; | ||
}, "defualtErrorHandler"); | ||
@@ -148,5 +159,7 @@ // src/cli.ts | ||
isInit = false; | ||
constructor({ container = createDefaultContainer(), yargs = (0, import_yargs.default)().help() } = {}) { | ||
onError; | ||
constructor({ container = createDefaultContainer(), yargs = (0, import_yargs.default)().help(), onError = defualtErrorHandler } = {}) { | ||
this.container = container; | ||
this.yargs = yargs; | ||
this.onError = onError; | ||
} | ||
@@ -157,2 +170,3 @@ register(command) { | ||
async run(processArgs) { | ||
const stdout = await this.container.getAsync(Stdout); | ||
const stderr = await this.container.getAsync(Stderr); | ||
@@ -163,6 +177,8 @@ await this.init(); | ||
if (error != null) { | ||
writeln(stderr, error.message); | ||
resolve(1); | ||
resolve(this.onError(error, { | ||
stdout, | ||
stderr | ||
})); | ||
} else if (output !== "") { | ||
writeln(stderr, output); | ||
writeln(stdout, output); | ||
resolve(0); | ||
@@ -187,15 +203,5 @@ } else { | ||
container.bind(Command2).toSelf(); | ||
const stderr = await container.getAsync(Stderr); | ||
const command = await container.getAsync(Command2); | ||
try { | ||
args["$?"] = await command.execute(); | ||
} catch (error) { | ||
if (error instanceof CLIError) { | ||
args["$?"] = error.exitCode ?? 1; | ||
stderr.write(error.message); | ||
stderr.write("\n"); | ||
return; | ||
} | ||
throw error; | ||
} | ||
const commandResult = await command.execute(); | ||
args["$?"] = commandResult ?? 0; | ||
}); | ||
@@ -202,0 +208,0 @@ this.isInit = true; |
{ | ||
"name": "sgray", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"author": "cumul <gg6123@naver.com>", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40523
433