import { t as importAssembler } from "../utils-1rNipeII.js";
import { p as BaseCommand, u as flags } from "../main-DvUje0hm.js";
import { t as __decorate } from "../decorate-DcF3lt7P.js";
//#region commands/serve.ts
/**
* Serve command is used to run the AdonisJS HTTP server during development. The
* command under the hood runs the "bin/server.ts" file and watches for file
* system changes
*
* @example
* ```
* ace serve
* ace serve --watch
* ace serve --hmr
* ace serve --poll
* ace serve --no-clear
* ```
*/
var Serve = class extends BaseCommand {
	/**
	* The command name
	*/
	static commandName = "serve";
	/**
	* The command description
	*/
	static description = "Start the development HTTP server. Use --watch for auto-restart on file change or --hmr for hot module replacement";
	/**
	* Help text for the command
	*/
	static help = [
		"Start the development server with file watcher using the following command.",
		"```",
		"{{ binaryName }} serve --watch",
		"```",
		"",
		"You can also start the server with HMR support using the following command.",
		"```",
		"{{ binaryName }} serve --hmr",
		"```",
		"",
		"The assets bundler dev server runs automatically after detecting vite config or webpack config files",
		"You may pass vite CLI args using the --assets-args command line flag.",
		"```",
		"{{ binaryName }} serve --assets-args=\"--debug --base=/public\"",
		"```"
	];
	/**
	* Command options configuration
	*/
	static options = { staysAlive: true };
	/**
	* Log a development dependency is missing
	*
	* @param dependency - The name of the missing dependency
	*/
	#logMissingDevelopmentDependency(dependency) {
		this.logger.error([
			`Cannot find package "${dependency}"`,
			"",
			`The "${dependency}" package is a development dependency and therefore you should use the serve command during development only.`,
			"",
			"If you are running your application in production, then use \"node bin/server.js\" command to start the HTTP server"
		].join("\n"));
	}
	/**
	* Runs the HTTP server
	*/
	async run() {
		const assembler = await importAssembler(this.app);
		if (!assembler) {
			this.#logMissingDevelopmentDependency("@adonisjs/assembler");
			this.exitCode = 1;
			return;
		}
		if (this.watch && this.hmr) {
			this.logger.error("Cannot use --watch and --hmr flags together. Choose one of them");
			this.exitCode = 1;
			return;
		}
		this.devServer = new assembler.DevServer(this.app.appRoot, {
			hmr: this.hmr === true ? true : false,
			clearScreen: this.clear === false ? false : true,
			nodeArgs: this.parsed.nodeArgs,
			scriptArgs: [],
			metaFiles: this.app.rcFile.metaFiles,
			hooks: this.app.rcFile.hooks
		});
		/**
		* Share command logger with assembler, so that CLI flags like --no-ansi has
		* similar impact for assembler logs as well.
		*/
		this.devServer.ui.logger = this.logger;
		/**
		* Exit command when the dev server is closed
		*/
		this.devServer.onClose((exitCode) => {
			this.exitCode = exitCode;
			this.terminate();
		});
		/**
		* Exit command when the dev server crashes
		*/
		this.devServer.onError(() => {
			this.exitCode = 1;
			this.terminate();
		});
		/**
		* Start the development server
		*/
		if (this.watch) await this.devServer.startAndWatch({ poll: this.poll || false });
		else await this.devServer.start();
	}
};
__decorate([flags.boolean({ description: "Start the server with HMR support" })], Serve.prototype, "hmr", void 0);
__decorate([flags.boolean({
	description: "Watch filesystem and restart the HTTP server on file change",
	alias: "w"
})], Serve.prototype, "watch", void 0);
__decorate([flags.boolean({
	description: "Use polling to detect filesystem changes",
	alias: "p"
})], Serve.prototype, "poll", void 0);
__decorate([flags.boolean({
	description: "Clear the terminal for new logs after file change",
	showNegatedVariantInHelp: true,
	default: true
})], Serve.prototype, "clear", void 0);
//#endregion
export { Serve as default };
