"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const command_1 = require("@oclif/command");
const chalk_1 = __importDefault(require("chalk"));
const fs_extra_1 = require("fs-extra");
const analyzer_1 = require("../hooks/analyzer");
const logger_1 = require("../utils/logger");
const webpack_1 = require("../utils/webpack");
class Build extends command_1.Command {
    static description = 'Build your Sales App Extension Points';
    static examples = [
        '$ sales-app build {account}',
    ];
    static args = [
        {
            name: 'account',
            description: 'the account for which the dev environment should run',
            required: true,
        },
        {
            name: 'path',
            description: 'the path where the Sales App is located',
            default: 'sales-app',
            required: false,
        },
    ];
    async run() {
        const { args } = this.parse(Build);
        const projectDir = path_1.default.resolve(args.path);
        const projectSrcDir = path_1.default.join(projectDir, 'src');
        if (!(0, fs_extra_1.existsSync)(projectSrcDir)) {
            logger_1.logger.log(`${chalk_1.default.red('Error')} - Sales App project not created. Please run "sales-app create" first`);
            return;
        }
        this.log(`${chalk_1.default.blue('Info')} - Running fsp-analyzer on ${projectDir}...`);
        try {
            await (0, analyzer_1.Analyzer)(projectDir);
            this.log(`${chalk_1.default.green('Success')} - Static analysis passed.`);
        }
        catch {
            logger_1.logger.error(`${chalk_1.default.red('Error')} - Static analysis found issues. Build aborted.`);
            process.exit(1);
        }
        this.log(`${chalk_1.default.blue('Info')} - Starting Webpack compilation...`);
        const { compiler } = (0, webpack_1.getWebpackCompiler)(projectDir, 'production');
        compiler.run((err, stats) => {
            if (err) {
                logger_1.logger.error(`${chalk_1.default.red('Error')} - Failed to execute build command: ${err?.message}`);
                process.exit(1);
            }
            else {
                if (stats?.hasErrors()) {
                    const info = stats.toJson();
                    console.error(`${chalk_1.default.red('Error')} - Extensions build failed with errors:`);
                    info?.errors?.forEach((error) => {
                        console.error(error.message || error);
                    });
                    process.exit(1);
                }
                this.log(`${chalk_1.default.green('Success')} - Extensions built successfully`);
            }
            compiler.close((closeErr) => {
                logger_1.logger.log(closeErr?.message);
            });
        });
    }
}
exports.default = Build;
//# sourceMappingURL=build.js.map