"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startCompile = void 0;
const webpack_1 = __importDefault(require("webpack"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const runningCompilations = {};
function startCompile(options, cb) {
    const key = options.configPath;
    const runningProcess = runningCompilations[key];
    if (runningProcess) {
        console.log(`Compiling ${options.configPath} already running`);
        return;
    }
    console.log(`Running webpack by ${options.configPath}`);
    console.log('Running webpack mode ', options.webpackMode);
    console.log('【编译中】正在进行生产环境编译，请稍等...');
    fs_extra_1.default.emptyDirSync(path_1.default.resolve(options.appBuildDir, 'preview'));
    const watching = (0, webpack_1.default)(require(options.configPath), async (err, stats) => {
        if (err) {
            console.error('webpack config error', err.stack || err);
            if (err.details) {
                console.error('webpack config error detail', err.details);
            }
            cb === null || cb === void 0 ? void 0 : cb(err);
            delete runningCompilations[key];
            return;
        }
        const info = stats.toJson('minimal');
        if (stats.hasErrors()) {
            console.error('Webpack compilation errors', info.errors.join('\n'));
            cb === null || cb === void 0 ? void 0 : cb(info.errors);
        }
        else {
            let { endTime = 0, startTime = 0 } = stats;
            cb === null || cb === void 0 ? void 0 : cb(null, {
                outDir: options.appBuildDir,
                timeElapsed: endTime - startTime,
            });
        }
        if (stats.hasWarnings()) {
            console.warn('webpack compiling warnings', info.warnings.join('\n'));
        }
    });
    if (!(watching instanceof webpack_1.default.Compiler)) {
        runningCompilations[key] = false;
    }
    else {
        runningCompilations[key] = true;
    }
    // if (options.webpackMode === WebpackModeType.PRODUCTION) {
    // try {
    //   console.log('【编译中】正在进行生产环境编译，请稍等...');
    //   let startTime = Date.now();
    //   runningCompilations[key] = true;
    //   const webpackProcess = spawn.sync('npm', ['run', 'build-web'], {
    //     cwd: options.appBuildDir,
    //     env: process.env,
    //     stdio: ['inherit', 'inherit', 'pipe'],
    //   });
    //   const { stderr } = webpackProcess;
    //   if (stderr) {
    //     throw new Error(stderr.toString());
    //   }
    //   cb?.(null, {
    //     outDir: options.appBuildDir,
    //     timeElapsed: Date.now() - startTime,
    //   });
    // } catch (e) {
    //   cb?.([e.message]);
    //   console.error('【失败】编译失败，请查看上面的日志修改后重试');
    // }
    // runningCompilations[key] = false;
    // }
}
exports.startCompile = startCompile;
