"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());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Hook = void 0;
var fs_1 = __importDefault(require("fs"));
var path_1 = __importDefault(require("path"));
var utils_1 = require("../../utils");
var plugin_1 = require("../plugin");
var spawnSync = require('child_process').spawnSync;
var Hook = /** @class */ (function () {
    function Hook(extendsParams) {
        var _this = this;
        if (extendsParams === void 0) { extendsParams = []; }
        this.preHooks = [];
        this.afterHooks = [];
        extendsParams.forEach(function (_extend) {
            if (_extend.Pre) {
                _this.preHooks.push(_extend);
            }
            else {
                _this.afterHooks.push(_extend);
            }
        });
    }
    Hook.prototype.executePreHook = function () {
        return __awaiter(this, void 0, void 0, function () {
            var i;
            var _this = this;
            return __generator(this, function (_a) {
                if (this.preHooks.length > 0) {
                    utils_1.logger.info('Start the pre-hook');
                    for (i = 0; i < this.preHooks.length; i++) {
                        (function (index) { return __awaiter(_this, void 0, void 0, function () {
                            var hookIndex;
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0:
                                        hookIndex = index;
                                        utils_1.logger.info("[Hook / Plugin] " + (this.preHooks[hookIndex].Hook || this.preHooks[hookIndex].Plugin));
                                        return [4 /*yield*/, this.executeByConfig(this.preHooks[hookIndex])];
                                    case 1:
                                        _a.sent();
                                        return [2 /*return*/];
                                }
                            });
                        }); })(i);
                    }
                    utils_1.logger.info('End the pre-hook');
                }
                return [2 /*return*/];
            });
        });
    };
    Hook.prototype.executeAfterHook = function () {
        return __awaiter(this, void 0, void 0, function () {
            var i;
            var _this = this;
            return __generator(this, function (_a) {
                if (this.afterHooks.length > 0) {
                    utils_1.logger.info('Start the after-hook');
                    for (i = 0; i < this.afterHooks.length; i++) {
                        // logger.info(`[Hook / Plugin] ${this.afterHooks[i].Hook || this.afterHooks[i].Plugin}`);
                        // try {
                        (function (index) { return __awaiter(_this, void 0, void 0, function () {
                            var hookIndex;
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0:
                                        hookIndex = index;
                                        return [4 /*yield*/, this.executeByConfig(this.afterHooks[hookIndex])];
                                    case 1: return [4 /*yield*/, _a.sent()];
                                    case 2:
                                        _a.sent();
                                        return [2 /*return*/];
                                }
                            });
                        }); })(i);
                        // } catch (ex) {
                        //   process.env['project_error'] = String(true)
                        //   const thisMessage = `> Execute Error: ${this.preHooks[i].Hook || this.preHooks[i].Plugin}\n${ex}`
                        //   const tempMessage = process.env['project_error_message'] ? process.env['project_error_message'] + "\n" : ""
                        //   process.env['project_error_message'] = tempMessage + thisMessage
                        //   logger.error(`[Hook / Plugin] [Error]: ${ex.stdout || ex.stderr}`);
                        // }
                    }
                    utils_1.logger.info('End the after-hook');
                }
                return [2 /*return*/];
            });
        });
    };
    Hook.prototype.commandExecute = function (command, executePath) {
        return __awaiter(this, void 0, void 0, function () {
            var currentDir, cwdPath, result;
            return __generator(this, function (_a) {
                currentDir = process.cwd();
                cwdPath = executePath ? path_1.default.resolve(currentDir, executePath) : currentDir;
                if (fs_1.default.existsSync(cwdPath) && fs_1.default.lstatSync(cwdPath).isDirectory()) {
                    process.env['next-command-execute-flag'] = 'true';
                    utils_1.logger.info('Executing ...');
                    result = spawnSync(command, [], { cwd: cwdPath, stdio: 'inherit', shell: true });
                    if (result && result.status !== 0) {
                        throw Error("> Execute Error");
                    }
                }
                return [2 /*return*/];
            });
        });
    };
    Hook.prototype.pluginExecute = function (name) {
        return __awaiter(this, void 0, void 0, function () {
            var pluginInstance, pluginModule;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        pluginInstance = new plugin_1.PluginExeCute({ name: name });
                        return [4 /*yield*/, pluginInstance.init()];
                    case 1:
                        _a.sent();
                        return [4 /*yield*/, pluginInstance.loadPlugin()];
                    case 2:
                        pluginModule = _a.sent();
                        pluginModule.apply(null, []);
                        return [2 /*return*/];
                }
            });
        });
    };
    Hook.prototype.executeByConfig = function (hookConfig) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!hookConfig.Hook) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.commandExecute(hookConfig.Hook, hookConfig.Path)];
                    case 1:
                        _a.sent();
                        return [3 /*break*/, 4];
                    case 2:
                        if (!hookConfig.Plugin) return [3 /*break*/, 4];
                        return [4 /*yield*/, this.pluginExecute(hookConfig.Plugin)];
                    case 3:
                        _a.sent();
                        _a.label = 4;
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    return Hook;
}());
exports.Hook = Hook;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaG9vay5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9jb3JlL2NvbXBvbmVudC9ob29rLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OztBQUFBLDBDQUFvQjtBQUNwQiw4Q0FBd0I7QUFDeEIscUNBQXFDO0FBQ3JDLG9DQUEwQztBQUMxQyxJQUFNLFNBQVMsR0FBRyxPQUFPLENBQUMsZUFBZSxDQUFDLENBQUMsU0FBUyxDQUFDO0FBUXJEO0lBSUUsY0FBWSxhQUFnQztRQUE1QyxpQkFRQztRQVJXLDhCQUFBLEVBQUEsa0JBQWdDO1FBSDVDLGFBQVEsR0FBaUIsRUFBRSxDQUFDO1FBQzVCLGVBQVUsR0FBaUIsRUFBRSxDQUFDO1FBRzVCLGFBQWEsQ0FBQyxPQUFPLENBQUMsVUFBQyxPQUFtQjtZQUN4QyxJQUFJLE9BQU8sQ0FBQyxHQUFHLEVBQUU7Z0JBQ2YsS0FBSSxDQUFDLFFBQVEsQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDLENBQUM7YUFDN0I7aUJBQU07Z0JBQ0wsS0FBSSxDQUFDLFVBQV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