@tsed/cli-core
Version:
Build your CLI with TypeScript and Decorators
65 lines (64 loc) • 2.52 kB
JavaScript
import { __decorate } from "tslib";
import { constant, inject, Injectable } from "@tsed/di";
import chalk from "chalk";
import { CommandStoreKeys } from "../domains/CommandStoreKeys.js";
import { PackageManagersModule } from "../packageManagers/PackageManagersModule.js";
import { createSubTasks } from "../utils/createTasksRunner.js";
import { loadPlugins } from "../utils/loadPlugins.js";
import { CliHooks } from "./CliHooks.js";
import { NpmRegistryClient } from "./NpmRegistryClient.js";
import { ProjectPackageJson } from "./ProjectPackageJson.js";
function mapPlugins({ package: { name, description = "", ...otherProps } }) {
return {
name: `${name} ${description}`.trim(),
value: name,
...otherProps
};
}
let CliPlugins = class CliPlugins {
constructor() {
this.name = constant("name", "");
this.loadPlugins = loadPlugins;
this.npmRegistryClient = inject(NpmRegistryClient);
this.cliHooks = inject(CliHooks);
this.packageJson = inject(ProjectPackageJson);
this.packageManagers = inject(PackageManagersModule);
}
async searchPlugins(keyword = "", options = {}) {
const result = await this.npmRegistryClient.search(this.getKeyword(keyword), options);
return result.filter(({ package: { name } }) => this.isPlugin(name)).map(mapPlugins);
}
addPluginsDependencies(ctx) {
const plugins = Object.keys(this.packageJson.devDependencies).filter((name) => this.isPlugin(name));
const tasks = plugins.map((plugin) => {
return {
title: `Run plugin '${chalk.cyan(plugin)}'`,
task: () => {
return this.cliHooks.emit(CommandStoreKeys.ADD, plugin, ctx);
}
};
});
return [
...tasks,
{
title: "Install",
task: createSubTasks(() => {
return this.packageManagers.install(ctx);
}, { ...ctx, concurrent: false })
}
];
}
getKeyword(keyword) {
return `@${this.name}/cli-plugin-${this.cleanKeyword(keyword)}`;
}
cleanKeyword(keyword) {
return keyword.replace(this.name, "").replace("@", "").replace("/", "").replace("cli-plugin-", "");
}
isPlugin(name) {
return name.startsWith(`@${this.name}/cli-plugin`) || name.includes(`${this.name}-cli-plugin`);
}
};
CliPlugins = __decorate([
Injectable()
], CliPlugins);
export { CliPlugins };