"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cac_1 = require("cac");
const chalk_1 = __importDefault(require("chalk"));
const logger_1 = require("./logger");
const _1 = require(".");
const cli = cac_1.cac('vite');
/**
 * removing global flags before passing as command specific sub-configs
 */
function cleanOptions(options) {
    const ret = { ...options };
    delete ret['--'];
    delete ret.debug;
    delete ret.d;
    delete ret.filter;
    delete ret.f;
    delete ret.config;
    delete ret.c;
    delete ret.root;
    delete ret.r;
    delete ret.mode;
    delete ret.m;
    delete ret.logLevel;
    delete ret.l;
    return ret;
}
cli
    .option('-c, --config <file>', `[string] use specified config file`)
    .option('-r, --root <path>', `[string] use specified config file`)
    .option('-l, --logLevel <level>', `[string] silent | error | warn | all`)
    .option('-d, --debug [feat]', `[string | boolean] show debug logs`)
    .option('-f, --filter <filter>', `[string] filter debug logs`);
// dev
cli
    .command('[root]') // default command
    .alias('serve')
    .option('--host <host>', `[string] specify hostname`)
    .option('--port <port>', `[number] specify port`)
    .option('--https', `[boolean] use TLS + HTTP/2`)
    .option('--open [browser]', `[boolean | string] open browser on startup`)
    .option('--cors', `[boolean] enable CORS`)
    .option('-m, --mode <mode>', `[string] set env mode`, {
    default: 'development'
})
    .option('--force', `[boolean]  force the optimizer to ignore the cache and re-bundle`)
    .action(async (root, options) => {
    // output structure is preserved even after bundling so require()
    // is ok here
    const { createServer } = await Promise.resolve().then(() => __importStar(require('./server')));
    try {
        const server = await createServer({
            root,
            mode: options.mode,
            logLevel: options.logLevel,
            server: cleanOptions(options)
        }, options.config);
        await server.listen();
    }
    catch (e) {
        const logError = logger_1.createLogger(options.logLevel).error;
        logError(chalk_1.default.red(`error when starting dev server:\n${e.stack}`));
        process.exit(1);
    }
});
// build
cli
    .command('build [root]')
    .option('--entry <file>', `[string]  entry file for build (default: index.html)`)
    .option('--base <path>', `[string]  public base path (default: /)`)
    .option('--outDir <dir>', `[string]  output directory (default: dist)`)
    .option('--assetsDir <dir>', `[string]  directory under outDir to place assets in (default: _assets)`)
    .option('--assetsInlineLimit <number>', `[number]  static asset base64 inline threshold in bytes (default: 4096)`)
    .option('--ssr', `[boolean]  build for server-side rendering`)
    .option('--sourcemap', `[boolean]  output source maps for build (default: false)`)
    .option('--minify [minifier]', `[boolean | "terser" | "esbuild"] enable/disable minification, ` +
    `or specify minifier to use (default: terser)`)
    .option('--manifest', `[boolean] emit build manifest json`)
    .option('-m, --mode <mode>', `[string]  set env mode`, {
    default: 'production'
})
    .action(async (root, options) => {
    const { build } = await Promise.resolve().then(() => __importStar(require('./build')));
    try {
        await build({
            root,
            mode: options.mode,
            logLevel: options.logLevel,
            build: cleanOptions(options)
        }, options.config);
    }
    catch (e) {
        const logError = logger_1.createLogger(options.logLevel).error;
        logError(chalk_1.default.red(`error during build:\n${e.stack}`));
        process.exit(1);
    }
});
// optimize
cli
    .command('optimize [root]')
    .option('--force', `[boolean]  force the optimizer to ignore the cache and re-bundle`)
    .action(async (root, options) => {
    const { optimizeDeps } = await Promise.resolve().then(() => __importStar(require('./optimizer')));
    try {
        const config = await _1.resolveConfig({
            root,
            logLevel: options.logLevel
        }, 'build', 'development', options.config);
        await optimizeDeps(config, options.force, true);
    }
    catch (e) {
        const logError = logger_1.createLogger(options.logLevel).error;
        logError(chalk_1.default.red(`error when optimizing deps:\n${e.stack}`));
        process.exit(1);
    }
});
cli.help();
cli.version(require('../../package.json').version);
cli.parse();
//# sourceMappingURL=cli.js.map