"use strict";
/*
 * Copyright 2018, collaboration Factory AG. All rights reserved.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const LessCompiler_1 = require("./LessCompiler");
const CplaceTypescriptCompiler_1 = require("./CplaceTypescriptCompiler");
const interfaces_1 = require("./interfaces");
const utils_1 = require("../utils");
const CompressCssCompiler_1 = require("./CompressCssCompiler");
const E2ETypescriptCompiler_1 = require("./E2ETypescriptCompiler");
const OpenAPIYamlCompiler_1 = require("./OpenAPIYamlCompiler");
const VendorCompiler_1 = require("./VendorCompiler");
const CombineJavascriptsCompiler_1 = require("./CombineJavascriptsCompiler");
/* ==================
 *      This file will be called as main process by `ExecutorService` as specified by
 *      `ExecutorService#COMPILER_ENTRY_POINT`.
 * ================== */
if (require.main === module) {
    process.on('message', (request) => {
        handleRequest(request)
            .then((result) => {
            if (!process.send) {
                throw Error('must be called as a worker');
            }
            const response = {
                state: interfaces_1.ProcessState.DONE,
                result,
            };
            process.send(response);
        })
            .catch((e) => {
            console.error();
            console.error(utils_1.cerr `${e}`);
            console.error();
            if (process.send) {
                const response = {
                    state: interfaces_1.ProcessState.FAILED,
                };
                process.send(response);
            }
        });
    });
    function handleRequest(request) {
        utils_1.enableDebug(request.verbose);
        // verify that all required values are present
        if (!request.pluginName || !request.assetsPath) {
            throw Error('Invalid request');
        }
        let CompilerConstructor;
        if (request.vendor) {
            CompilerConstructor = VendorCompiler_1.VendorCompiler;
        }
        else if (request.ts) {
            CompilerConstructor = CplaceTypescriptCompiler_1.CplaceTypescriptCompiler;
        }
        else if (request.tsE2E) {
            CompilerConstructor = E2ETypescriptCompiler_1.E2ETypescriptCompiler;
        }
        else if (request.less) {
            CompilerConstructor = LessCompiler_1.LessCompiler;
        }
        else if (request.openAPIYaml) {
            CompilerConstructor = OpenAPIYamlCompiler_1.OpenAPIYamlCompiler;
        }
        else if (request.compressCss) {
            CompilerConstructor = CompressCssCompiler_1.CompressCssCompiler;
        }
        else if (request.combineJs) {
            CompilerConstructor = CombineJavascriptsCompiler_1.CombineJavascriptsCompiler;
        }
        else {
            console.error(utils_1.cerr `unknown compile type - neither ts nor less`);
            throw Error(`unknown compile type - neither ts nor less`);
        }
        let compiler;
        try {
            compiler = new CompilerConstructor(request.pluginName, request.dependencyPaths, request.assetsPath, request.mainRepoDir, request.isProduction);
        }
        catch (e) {
            console.error(utils_1.cerr `${e.message}`);
            throw Error(e);
        }
        return compiler.compile();
    }
}
//# sourceMappingURL=index.js.map