@backstage/cli
Version:
CLI for developing Backstage plugins and apps
87 lines (83 loc) • 2.75 kB
JavaScript
;
var InternalCliModule = require('../cli-internal/src/InternalCliModule.cjs.js');
require('../cli-internal/src/InternalCommandNode.cjs.js');
require('node:fs');
require('node:os');
require('node:path');
require('../cli-internal/src/knownPluginPackages.cjs.js');
var cliNode = require('@backstage/cli-node');
var version = require('./version.cjs.js');
var types = require('node:util/types');
class CliInitializer {
#uninitiazedFeatures = [];
add(feature) {
if (types.isPromise(feature)) {
this.#uninitiazedFeatures.push(
feature.then((f) => {
const unwrapped = unwrapFeature(f.default);
if (Array.isArray(unwrapped)) {
return unwrapped.map((m) => ({ feature: m, fromArray: true }));
}
return [{ feature: unwrapped, fromArray: false }];
})
);
} else if (Array.isArray(feature)) {
this.#uninitiazedFeatures.push(
Promise.resolve(feature.map((m) => ({ feature: m, fromArray: true })))
);
} else {
this.#uninitiazedFeatures.push(
Promise.resolve([{ feature, fromArray: false }])
);
}
}
async #doInit() {
const resolvedGroups = await Promise.all(this.#uninitiazedFeatures);
const allFeatures = resolvedGroups.flat();
const individualPaths = /* @__PURE__ */ new Set();
for (const { feature, fromArray } of allFeatures) {
if (!fromArray && InternalCliModule.OpaqueCliModule.isType(feature)) {
const cmds = await InternalCliModule.OpaqueCliModule.toInternal(feature).commands;
for (const cmd of cmds) {
individualPaths.add(cmd.path.join(" "));
}
}
}
const modules = [];
for (const { feature, fromArray } of allFeatures) {
if (!InternalCliModule.OpaqueCliModule.isType(feature)) {
throw new Error(`Unsupported feature type: ${feature.$$type}`);
}
if (fromArray) {
const cmds = await InternalCliModule.OpaqueCliModule.toInternal(feature).commands;
if (cmds.some((cmd) => individualPaths.has(cmd.path.join(" ")))) {
continue;
}
}
modules.push(feature);
}
return modules;
}
/**
* Actually parse argv and pass it to the command.
*/
async run() {
const modules = await this.#doInit();
await cliNode.runCli({ modules, name: "backstage-cli", version: version.version });
}
}
function unwrapFeature(feature) {
if (Array.isArray(feature)) {
return feature;
}
if ("$$type" in feature) {
return feature;
}
if ("default" in feature) {
return feature.default;
}
return feature;
}
exports.CliInitializer = CliInitializer;
exports.unwrapFeature = unwrapFeature;
//# sourceMappingURL=CliInitializer.cjs.js.map