"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = require("fs-extra");
const workerpool_1 = require("workerpool");
const linkNodeModules_1 = require("../../utils/linkNodeModules");
const common_worker_1 = require("./common.worker");
const utils_1 = require("./utils");
const removePackageJson_1 = require("./utils/removePackageJson");
(0, workerpool_1.worker)({
    install,
    validateLayout: common_worker_1.validateLayout,
    initTemplate,
    linkModules,
    updateSettings: common_worker_1.updateSettings,
    build: common_worker_1.build,
    archive: common_worker_1.archive,
});
function install({ baseDir, dest, options }) {
    return __awaiter(this, void 0, void 0, function* () {
        yield (0, fs_extra_1.ensureDir)(dest);
        yield (0, utils_1.writePackageJson)(baseDir, dest, options.masterRepoInfo);
        yield (0, utils_1.runInstallAction)(dest, options.npmRegistryConfig);
        yield (0, removePackageJson_1.removePackageJson)(dest);
    });
}
function initTemplate({ baseDir, layoutPath, dest, layoutName, options }) {
    return __awaiter(this, void 0, void 0, function* () {
        const { fromMasterTemplate, masterRepoInfo } = options;
        const systemSettings = yield (0, utils_1.getSourceSystemSettings)(baseDir, layoutPath);
        yield (0, fs_extra_1.emptyDir)(dest);
        yield (0, utils_1.writeSystemSettings)(dest, masterRepoInfo.repoName, fromMasterTemplate ? '' : layoutName, systemSettings);
        yield (0, utils_1.copyFiles)(baseDir, layoutPath, dest, systemSettings.path);
        yield (0, utils_1.writeExtentions)(baseDir, dest);
        yield (0, utils_1.writePackageJson)(layoutPath, dest, masterRepoInfo);
    });
}
function linkModules({ buildDir, dest }) {
    return __awaiter(this, void 0, void 0, function* () {
        yield (0, linkNodeModules_1.linkNodeModules)(buildDir, dest);
    });
}
//# sourceMappingURL=ewizard-template.worker.js.map