@nodearch/command
Version:
NodeArch Command Toolkit
54 lines • 2.55 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { AppContext, Service } from '@nodearch/core';
import { CommandParser } from './command-parser.js';
import { CommandConfig } from './command.config.js';
import { CommandDecorator } from '../index.js';
let CommandService = class CommandService {
constructor(commandConfig, commandParser, appContext) {
this.commandConfig = commandConfig;
this.commandParser = commandParser;
this.appContext = appContext;
}
async start(options) {
const commands = this.appContext.getContainer().getById(CommandDecorator.COMMAND);
if (options?.commands)
commands.push(...options.commands);
let filteredCommands = [], exclude = options?.exclude || [];
filteredCommands = commands.filter(cmd => {
if (typeof cmd.command !== 'string')
return true;
return !exclude.some(ex => ex instanceof RegExp ? ex.test(cmd.command) : ex === cmd.command);
});
const yargsCommands = this.commandParser.getYargsCommands(filteredCommands);
const args = yargs(hideBin(process.argv))
.scriptName(this.commandConfig.name)
.usage(this.commandConfig.usage);
if (this.commandConfig.options) {
args.options(this.commandConfig.options);
}
args.demandCommand();
yargsCommands
.forEach(cmd => args.command(cmd));
args.alias('h', 'help')
.alias('v', 'version');
args.argv;
}
};
CommandService = __decorate([
Service({ export: true }),
__metadata("design:paramtypes", [CommandConfig,
CommandParser,
AppContext])
], CommandService);
export { CommandService };
//# sourceMappingURL=command.service.js.map