"use strict";
/** @format */
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, hookIndex;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!(this.preHooks.length > 0)) return [3 /*break*/, 5];
                        utils_1.logger.info('Start the pre-action');
                        i = 0;
                        _a.label = 1;
                    case 1:
                        if (!(i < this.preHooks.length)) return [3 /*break*/, 4];
                        hookIndex = i;
                        utils_1.logger.info("Action: " + (this.preHooks[hookIndex].Hook || this.preHooks[hookIndex].Plugin));
                        return [4 /*yield*/, this.executeByConfig(this.preHooks[hookIndex])];
                    case 2:
                        _a.sent();
                        _a.label = 3;
                    case 3:
                        i++;
                        return [3 /*break*/, 1];
                    case 4:
                        utils_1.logger.info('End the pre-action');
                        _a.label = 5;
                    case 5: return [2 /*return*/];
                }
            });
        });
    };
    Hook.prototype.executeAfterHook = function () {
        return __awaiter(this, void 0, void 0, function () {
            var i, hookIndex;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!(this.afterHooks.length > 0)) return [3 /*break*/, 5];
                        utils_1.logger.info('Start the after-action');
                        i = 0;
                        _a.label = 1;
                    case 1:
                        if (!(i < this.afterHooks.length)) return [3 /*break*/, 4];
                        hookIndex = i;
                        utils_1.logger.info("Action: " + (this.afterHooks[hookIndex].Hook || this.afterHooks[hookIndex].Plugin));
                        return [4 /*yield*/, this.executeByConfig(this.afterHooks[hookIndex])];
                    case 2:
                        _a.sent();
                        _a.label = 3;
                    case 3:
                        i++;
                        return [3 /*break*/, 1];
                    case 4:
                        utils_1.logger.info('End the after-action');
                        _a.label = 5;
                    case 5: 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';
                    result = spawnSync(command, [], { cwd: cwdPath, stdio: 'inherit', shell: true });
                    if (result && result.status !== 0) {
                        throw new Error("Action [" + command + "] run 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaG9vay5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9jb3JlL2NvbXBvbmVudC9ob29rLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxjQUFjOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFFZCwwQ0FBb0I7QUFDcEIsOENBQXdCO0FBQ3hCLHFDQUFxQztBQUNyQyxvQ0FBMEM7QUFFMUMsSUFBTSxTQUFTLEdBQUcsT0FBTyxDQUFDLGVBQWUsQ0FBQyxDQUFDLFNBQVMsQ0FBQztBQVNyRDtJQUlFLGNBQVksYUFBZ0M7UUFBNUMsaUJBUUM7UUFSVyw4QkFBQSxFQUFBLGtCQUFnQztRQUg1QyxhQUFRLEdBQWlCLEVBQUUsQ0FBQztRQUM1QixlQUFVLEdBQWlCLEVBQUUsQ0FBQztRQUc1QixhQUFhLENBQUMsT0FBTyxDQUFDLFVBQUMsT0FBbUI7WUFDeEMsSUFBSSxPQUFPLENBQUMsR0FBRyxFQUFFO2dCQUNmLEtBQUksQ0FBQyxRQUFRLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxDQUFDO2FBQzdCO2lCQUFNO2dCQUNMLEtBQUksQ0FBQyxVQUFVLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxDQUFDO2FBQy9CO1FBQ0gsQ0FBQyxDQUFDLENBQUM7SUFDTCxDQUFDO0lBRUssNkJBQWMsR0FBcEI7Ozs7Ozs2QkFDTSxDQUFBLElBQUksQ0FBQyxRQUFRLENBQUMsTUFBTSxHQUFHLENBQUMsQ0FBQSxFQUF4Qix3QkFBd0I7d0JBQzFCLGNB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