Comparing version 0.2.0 to 0.2.1
@@ -1,8 +0,8 @@ | ||
import { Container, interfaces } from 'inversify'; | ||
import { Container } from 'inversify'; | ||
import { Argv as Argv$1 } from 'yargs'; | ||
import { Writable, Readable } from 'node:stream'; | ||
interface CommandStatic<Args extends CommandArgs = CommandArgs> { | ||
new (...args: any[]): Command; | ||
command: string | readonly string[]; | ||
path?: string[]; | ||
command: string; | ||
description: string; | ||
@@ -31,9 +31,2 @@ options: (y: Argv$1<CommandArgs>) => Argv$1<Args>; | ||
interface ContainerOptions extends interfaces.ContainerOptions { | ||
stdout?: Writable; | ||
stderr?: Writable; | ||
stdin?: Readable; | ||
} | ||
declare function createDefaultContainer({ stdout, stderr, stdin, ...options }?: ContainerOptions): Container; | ||
declare const Argv = "argv"; | ||
@@ -45,2 +38,2 @@ declare const Args = "args"; | ||
export { Args, Argv, CLI, Command, CommandArgs, CommandResult, CommandStatic, Stderr, Stdin, Stdout, createDefaultContainer as createContainer, options }; | ||
export { Args, Argv, CLI, Command, CommandArgs, CommandResult, CommandStatic, Stderr, Stdin, Stdout, options }; |
@@ -40,3 +40,2 @@ "use strict"; | ||
Stdout: () => Stdout, | ||
createContainer: () => createDefaultContainer, | ||
options: () => options | ||
@@ -49,2 +48,48 @@ }); | ||
// src/internal/argv.ts | ||
var ArgvBuilder = class { | ||
static { | ||
__name(this, "ArgvBuilder"); | ||
} | ||
store = {}; | ||
add(path, name, node) { | ||
let target = this.store; | ||
for (const name2 of path) { | ||
let parentNode = target[name2]; | ||
if (parentNode == null) { | ||
parentNode = target[name2] = { | ||
command: null, | ||
children: {} | ||
}; | ||
} | ||
target = parentNode.children; | ||
} | ||
if (target[name] != null) { | ||
throw new Error(`command conflicts: "${path.join(" ")}"`); | ||
} | ||
target[name] = node; | ||
} | ||
build(yargs, handle, store = this.store) { | ||
for (const [name, { command, children }] of Object.entries(store)) { | ||
if (command != null) { | ||
yargs.command(name, command.description, command.options, (args) => handle(command, args)); | ||
} else { | ||
yargs.command(name, "", (y) => this.build(y, handle, children)); | ||
} | ||
} | ||
} | ||
static from(commands) { | ||
const instance = new this(); | ||
for (const command of commands) { | ||
const path = command.path ?? []; | ||
const [name] = command.command.split(" ", 2); | ||
instance.add(path, name, { | ||
command, | ||
children: {} | ||
}); | ||
} | ||
return instance; | ||
} | ||
}; | ||
// src/internal/container.ts | ||
@@ -62,7 +107,7 @@ var import_inversify = require("inversify"); | ||
// src/internal/container.ts | ||
function createDefaultContainer({ stdout, stderr, stdin, ...options2 } = {}) { | ||
function createDefaultContainer(options2) { | ||
const container = new import_inversify.Container(options2); | ||
container.bind(Stdout).toConstantValue(stdout ?? process.stdout); | ||
container.bind(Stderr).toConstantValue(stderr ?? process.stderr); | ||
container.bind(Stdin).toConstantValue(stdin ?? process.stdin); | ||
container.bind(Stdout).toConstantValue(process.stdout); | ||
container.bind(Stderr).toConstantValue(process.stderr); | ||
container.bind(Stdin).toConstantValue(process.stdin); | ||
return container; | ||
@@ -79,3 +124,3 @@ } | ||
// src/cli.ts | ||
var CLI = class { | ||
var CLI = class _CLI { | ||
static { | ||
@@ -98,13 +143,13 @@ __name(this, "CLI"); | ||
const stderr = await this.container.getAsync(Stderr); | ||
for (const Command2 of await this.container.getAllAsync(Command)) { | ||
this.yargs.command(Command2.command, Command2.description, (y) => Command2.options(y), async (args) => { | ||
const commandContainer = this.container.createChild(); | ||
commandContainer.bind(Argv).toConstantValue(this.yargs); | ||
commandContainer.bind(Args).toConstantValue(args); | ||
commandContainer.bind(Command2).toSelf(); | ||
const command = await commandContainer.getAsync(Command2); | ||
const commandResult = await command.execute(); | ||
args["$?"] = commandResult; | ||
}); | ||
} | ||
const commands = await this.container.getAllAsync(Command); | ||
ArgvBuilder.from(commands).build(this.yargs, async (Command2, args) => { | ||
const container = this.container.createChild(); | ||
container.bind(_CLI).toConstantValue(this); | ||
container.bind(Argv).toConstantValue(this.yargs); | ||
container.bind(Args).toConstantValue(args); | ||
container.bind(Command2).toSelf(); | ||
const command = await container.getAsync(Command2); | ||
const commandResult = await command.execute(); | ||
args["$?"] = commandResult; | ||
}); | ||
return new Promise((resolve) => { | ||
@@ -137,5 +182,4 @@ this.yargs.parse(processArgs, {}, (error, argv, output) => { | ||
Stdout, | ||
createContainer, | ||
options | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "sgray", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
32840
331
0