import { n as prettyPrintError } from "../core-BoJTzbBt.js";
import { o as args, p as BaseCommand, u as flags } from "../main-DvUje0hm.js";
import { t as __decorate } from "../decorate-DcF3lt7P.js";
//#region commands/add.ts
/**
* The install command is used to `npm install` and `node ace configure` one or more packages
* in one go.
*
* @example
* ```
* ace add @adonisjs/lucid
* ace add @adonisjs/lucid @adonisjs/auth @adonisjs/session
* ace add @adonisjs/session --dev
* ace add vinejs --force
* ace add edge --package-manager=pnpm
* ```
*/
var Add = class extends BaseCommand {
	/**
	* The command name
	*/
	static commandName = "add";
	/**
	* The command description
	*/
	static description = "Install and configure one or more AdonisJS packages. Runs npm install followed by the package configure hook";
	static help = [
		"Use this command instead of manually running npm install + configure separately.",
		"Accepts shorthand names: \"vinejs\" for @vinejs/vine, \"edge\" for edge.js.",
		"```",
		"{{ binaryName }} add @adonisjs/lucid",
		"{{ binaryName }} add @adonisjs/auth @adonisjs/session",
		"```"
	];
	/**
	* Command options configuration
	*/
	static options = { allowUnknownFlags: true };
	/**
	* Resolve the npm package name from the user-provided name
	*/
	#resolveNpmPackageName(name) {
		if (name === "vinejs") return "@vinejs/vine";
		if (name === "edge") return "edge.js";
		return name;
	}
	/**
	* Configure the package by delegating the work to the `node ace configure` command
	*/
	async #configurePackage(packageName) {
		const flagValueArray = this.parsed.unknownFlags.filter((flag) => !!this.parsed.flags[flag]).map((flag) => [`--${flag}`, this.parsed.flags[flag].toString()]);
		const configureArgs = [
			packageName,
			this.force ? "--force" : void 0,
			this.verbose ? "--verbose" : void 0,
			...flagValueArray.flat()
		].filter(Boolean);
		return await this.kernel.exec("configure", configureArgs);
	}
	/**
	* Run method is invoked by ace automatically
	*/
	async run() {
		const packages = this.names.map((name) => ({
			name,
			npmName: this.#resolveNpmPackageName(name)
		}));
		/**
		* Install all packages
		*/
		const codemods = await this.createCodemods();
		codemods.verboseInstallOutput = !!this.verbose;
		if (!await codemods.installPackages(packages.map((pkg) => ({
			name: pkg.npmName,
			isDevDependency: !!this.dev
		})), this.packageManager)) return;
		/**
		* Configure each package sequentially
		*/
		const succeeded = [];
		const failed = [];
		for (const pkg of packages) {
			const { exitCode, error } = await this.#configurePackage(pkg.name);
			if (exitCode === 0) succeeded.push(pkg.name);
			else failed.push({
				name: pkg.name,
				error
			});
		}
		/**
		* Report results
		*/
		if (succeeded.length > 0) {
			const names = succeeded.map((name) => this.colors.green(name)).join(", ");
			this.logger.success(`Installed and configured ${names}`);
		}
		if (failed.length > 0) {
			this.exitCode = 1;
			for (const pkg of failed) {
				this.logger.error(`Unable to configure ${this.colors.green(pkg.name)}`);
				if (pkg.error) await prettyPrintError(pkg.error.cause || pkg.error);
			}
		}
	}
};
__decorate([args.spread({
	description: "Package names to install and configure",
	required: true
})], Add.prototype, "names", void 0);
__decorate([flags.boolean({ description: "Display logs in verbose mode" })], Add.prototype, "verbose", void 0);
__decorate([flags.string({ description: "Define the package manager you want to use" })], Add.prototype, "packageManager", void 0);
__decorate([flags.boolean({
	description: "Should we install the package as a dev dependency",
	alias: "D"
})], Add.prototype, "dev", void 0);
__decorate([flags.boolean({ description: "Forcefully overwrite existing files" })], Add.prototype, "force", void 0);
//#endregion
export { Add as default };
