"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompressCssCompiler = void 0;
const path = require("path");
const interfaces_1 = require("./interfaces");
const utils_1 = require("../utils");
const spawn = require("cross-spawn");
const fs = require("fs");
const rimraf = require("rimraf");
class CompressCssCompiler {
    constructor(pluginName, dependencyPaths, assetsPath, mainRepoDir, isProduction) {
        this.pluginName = pluginName;
        this.dependencyPaths = dependencyPaths;
        this.assetsPath = assetsPath;
        this.mainRepoDir = mainRepoDir;
        this.isProduction = isProduction;
        this.pathToEntryFile = '';
        this.pathToCssSources = path.join(this.assetsPath, CompressCssCompiler.CSS_SOURCES_DIR);
        this.pathToEntryFile = path.join(this.pathToCssSources, CompressCssCompiler.ENTRY_FILE_NAME);
    }
    static getCssOutputDir(assetsPath) {
        return path.resolve(assetsPath, this.CSS_OUTPUT_DIR);
    }
    compile() {
        return new Promise((resolve) => {
            const generatedCssDir = CompressCssCompiler.getCssOutputDir(this.assetsPath);
            const outputFile = path.join(generatedCssDir, CompressCssCompiler.OUTPUT_FILE_NAME);
            if (!fs.existsSync(this.pathToEntryFile)) {
                this.cleanOutput(outputFile);
                return resolve(interfaces_1.CompilationResult.CHANGED);
            }
            if (!fs.existsSync(generatedCssDir)) {
                fs.mkdirSync(generatedCssDir);
            }
            const start = new Date().getTime();
            console.log(`⟲ [${this.pluginName}] starting CSS compression...`);
            const cleanCssExecutable = this.getCleanCssExecutable();
            const args = [
                '-o',
                outputFile,
                this.pathToEntryFile,
                '--source-map',
            ];
            utils_1.debug(`(CompressCssCompiler) [${this.pluginName}] executing command '${cleanCssExecutable} ${args.join(' ')}'`);
            const result = spawn.sync(cleanCssExecutable, args, {
                stdio: [process.stdin, process.stdout, process.stderr],
            });
            utils_1.debug(`(CompressCssCompiler) [${this.pluginName}] clean-css return code: ${result.status}`);
            if (result.status !== 0) {
                this.cleanOutput(outputFile);
                throw Error(`[${this.pluginName}] CSS compression failed...`);
            }
            const end = new Date().getTime();
            console.log(utils_1.GREEN_CHECK, `[${this.pluginName}] CSS compression finished (${utils_1.formatDuration(end - start)})`);
            return resolve(interfaces_1.CompilationResult.CHANGED);
        });
    }
    cleanOutput(outputFile) {
        rimraf.sync(outputFile);
        rimraf.sync(outputFile.concat('.map'));
    }
    getCleanCssExecutable() {
        return path.resolve(this.mainRepoDir, 'node_modules', 'clean-css', 'bin', 'cleancss');
    }
}
exports.CompressCssCompiler = CompressCssCompiler;
CompressCssCompiler.CSS_SOURCES_DIR = 'css';
CompressCssCompiler.CSS_OUTPUT_DIR = 'generated_css';
CompressCssCompiler.ENTRY_FILE_NAME = 'imports.css';
CompressCssCompiler.OUTPUT_FILE_NAME = 'compressed.css';
//# sourceMappingURL=CompressCssCompiler.js.map