@featurevisor/core
Version:
Core package of Featurevisor for Node.js usage
69 lines • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.runCLI = runCLI;
const plugins_1 = require("./plugins");
async function runCLI(runnerOptions) {
const yargs = require("yargs");
let y = yargs(process.argv.slice(2)).usage("Usage: <command> [options]");
const registeredSubcommands = [];
const { rootDirectoryPath, projectConfig, datasource } = runnerOptions;
function registerPlugin(plugin) {
const subcommand = plugin.command.split(" ")[0];
if (registeredSubcommands.includes(subcommand)) {
console.warn(`Plugin "${subcommand}" already registered. Skipping.`);
return;
}
y = y.command({
command: plugin.command,
handler: async function (parsed) {
// @NOTE: in future, allow yargs options to be defined via plugins
if (parsed.schemaVersion && typeof parsed.schemaVersion !== "string") {
parsed.schemaVersion = parsed.schemaVersion.toString();
}
try {
const result = await plugin.handler({
rootDirectoryPath,
projectConfig,
datasource,
parsed,
});
if (result === false) {
process.exit(1);
}
}
catch (error) {
console.error(error);
process.exit(1);
}
},
});
for (const example of plugin.examples) {
y = y.example(`$0 ${example.command}`, example.description);
}
registeredSubcommands.push(subcommand);
}
// non project-based plugins
if (!projectConfig) {
for (const plugin of plugins_1.nonProjectPlugins) {
registerPlugin(plugin);
}
}
// project-based plugins
if (projectConfig) {
for (const plugin of [...plugins_1.projectBasedPlugins, ...(projectConfig.plugins || [])]) {
registerPlugin(plugin);
}
}
// common plugins
for (const plugin of plugins_1.commonPlugins) {
registerPlugin(plugin);
}
// show help if no command is provided
y.command({
command: "*",
handler() {
y.showHelp();
},
}).argv;
}
//# sourceMappingURL=cli.js.map