@jovotech/cli
Version:
Jovo Command Line Interface (Beta)
75 lines • 2.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Collector = void 0;
const cli_core_1 = require("@jovotech/cli-core");
const config_1 = require("@oclif/config");
const path_1 = require("path");
class Collector extends config_1.Plugin {
constructor() {
super(...arguments);
this.hooks = {};
this.commands = [];
}
get topics() {
return [];
}
async install(commandId) {
const emitter = new cli_core_1.EventEmitter();
await this.loadPlugins(commandId, emitter);
}
/**
* Loads plugins from project and installs respective commands and hooks.
* @param argCmd - The current command id passed from process.argv.
* @param project - The instantiated project.
* @param emitter - The Event Emitter.
*/
async loadPlugins(commandId, emitter) {
try {
cli_core_1.Log.verbose('Initiating Jovo CLI');
// Set exec path before loading plugins to prevent
// locally installed CLI modules from colliding with the global one
process.env.JOVO_CLI_EXEC_PATH = path_1.dirname(__dirname);
const cli = cli_core_1.JovoCli.getInstance();
const plugins = cli.loadPlugins();
// Fill in default context values
const context = {
command: commandId,
};
cli_core_1.Log.verbose('Installing CLI plugins');
for (const plugin of plugins) {
plugin.install(cli, emitter, context);
this.commands.push(...plugin.getCommands());
}
// Load hooks from project configuration.
if (cli.isInProjectDirectory()) {
const hooks = cli.project.config.getParameter('hooks');
if (hooks) {
for (const [eventKey, events] of Object.entries(hooks)) {
for (const event of events) {
emitter.on(eventKey, event.bind(null, context));
}
}
}
}
if (!this.commands.length) {
return;
}
// Run install middleware for currently executed command.
const currentCommand = this.commands.find((command) => command.id === commandId);
if (currentCommand) {
const { id: command, flags, args } = currentCommand;
await emitter.run('install', {
command,
flags,
args,
});
}
}
catch (error) {
cli_core_1.JovoCliError.print(error);
process.exit(1);
}
}
}
exports.Collector = Collector;
//# sourceMappingURL=Collector.js.map