"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.executePipe = exports.buildHooks = void 0;var _inquirer = _interopRequireDefault(require("inquirer"));
var _path = _interopRequireDefault(require("path"));
var _esbuild = require("esbuild");
var _logger = require("../systemManager/logger");
var _fileutils = require("../systemManager/fileutils");
var _common = require("../common");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { "default": obj };}function _toConsumableArray(arr) {return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();}function _nonIterableSpread() {throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");}function _unsupportedIterableToArray(o, minLen) {if (!o) return;if (typeof o === "string") return _arrayLikeToArray(o, minLen);var n = Object.prototype.toString.call(o).slice(8, -1);if (n === "Object" && o.constructor) n = o.constructor.name;if (n === "Map" || n === "Set") return Array.from(o);if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);}function _iterableToArray(iter) {if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);}function _arrayWithoutHoles(arr) {if (Array.isArray(arr)) return _arrayLikeToArray(arr);}function _arrayLikeToArray(arr, len) {if (len == null || len > arr.length) len = arr.length;for (var i = 0, arr2 = new Array(len); i < len; i++) {arr2[i] = arr[i];}return arr2;}function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {try {var info = gen[key](arg);var value = info.value;} catch (error) {reject(error);return;}if (info.done) {resolve(value);} else {Promise.resolve(value).then(_next, _throw);}}function _asyncToGenerator(fn) {return function () {var self = this,args = arguments;return new Promise(function (resolve, reject) {var gen = fn.apply(self, args);function _next(value) {asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);}function _throw(err) {asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);}_next(undefined);});};}

var executePipe = /*#__PURE__*/function () {var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(c, key) {var pipe;return regeneratorRuntime.wrap(function _callee$(_context) {while (1) {switch (_context.prev = _context.next) {case 0:
            (0, _logger.logHook)('executePipe', "('".concat(key, "')"));_context.next = 3;return (

              buildHooks(c));case 3:

            pipe = c.buildPipes ? c.buildPipes[key] : null;if (!

            Array.isArray(pipe)) {_context.next = 9;break;}_context.next = 7;return (
              pipe.reduce(
              function (accumulatorPromise, next) {
                (0, _logger.logHook)("buildHook.".concat(next === null || next === void 0 ? void 0 : next.name), '(EXECUTING)');
                return accumulatorPromise.then(function () {return next(c);});
              },
              Promise.resolve()));case 7:_context.next = 13;break;case 9:if (!

            pipe) {_context.next = 13;break;}
            (0, _logger.logHook)("buildHook.".concat(pipe === null || pipe === void 0 ? void 0 : pipe.name), '(EXECUTING)');_context.next = 13;return (
              pipe(c));case 13:case "end":return _context.stop();}}}, _callee);}));return function executePipe(_x, _x2) {return _ref.apply(this, arguments);};}();



/* eslint-disable import/no-dynamic-require, global-require */exports.executePipe = executePipe;
var buildHooks = /*#__PURE__*/function () {var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(c) {var _c$files$project$conf;var enableHookRebuild, shouldBuildHook, _yield$inquirer$promp, confirm, h;return regeneratorRuntime.wrap(function _callee2$(_context2) {while (1) {switch (_context2.prev = _context2.next) {case 0:
            (0, _logger.logDebug)('buildHooks');

            enableHookRebuild = (0, _common.getConfigProp)(c, c.platform, 'enableHookRebuild');

            shouldBuildHook = c.program.reset || c.program.resetHard || c.program.resetAssets ||
            c.program.hooks || !(0, _fileutils.fsExistsSync)(c.paths.buildHooks.dist.dir) || enableHookRebuild === true ||
            c.runtime.forceBuildHookRebuild;if (!(

            !(0, _fileutils.fsExistsSync)(c.paths.buildHooks.index) && c.program.ci || c.runtime.skipBuildHooks)) {_context2.next = 6;break;}
            (0, _logger.logInfo)('No buld hooks found and in --ci mode. SKIPPING');return _context2.abrupt("return",
            true);case 6:if (



            (0, _fileutils.fsExistsSync)(c.paths.buildHooks.index)) {_context2.next = 20;break;}if (!
            c.program.ci) {_context2.next = 10;break;}
            c.runtime.skipBuildHooks = true;return _context2.abrupt("return");case 10:_context2.next = 12;return (


              _inquirer["default"].prompt({
                type: 'confirm',
                name: 'confirm',
                message: 'Build hooks not configured in this project. Configure?' }));case 12:_yield$inquirer$promp = _context2.sent;confirm = _yield$inquirer$promp.confirm;if (!

            confirm) {_context2.next = 18;break;}
            (0, _fileutils.copyFolderContentsRecursiveSync)(
            _path["default"].join(c.paths.rnv.dir, 'coreTemplateFiles/buildHooks/src'),
            c.paths.buildHooks.dir);_context2.next = 20;break;case 18:


            c.runtime.skipBuildHooks = true;return _context2.abrupt("return");case 20:if (!(




            !c.runtime.isFirstRunAfterNew && !((_c$files$project$conf = c.files.project.config) !== null && _c$files$project$conf !== void 0 && _c$files$project$conf.isNew))) {_context2.next = 36;break;}if (!(
            shouldBuildHook && !c.isBuildHooksReady)) {_context2.next = 32;break;}_context2.prev = 22;

            (0, _logger.logHook)('buildHooks', 'Build hooks not complied. BUILDING...');_context2.next = 26;return (
              (0, _esbuild.build)({
                entryPoints: ["".concat(c.paths.buildHooks.dir, "/index.js")],
                bundle: true,
                platform: 'node',
                external: [].concat(_toConsumableArray(Object.keys(c.files.project["package"].dependencies || {})), _toConsumableArray(
                Object.keys(c.files.project["package"].devDependencies || {}))), // exclude everything that's present in node_modules
                outfile: "".concat(c.paths.buildHooks.dist.dir, "/index.js") }));case 26:_context2.next = 31;break;case 28:_context2.prev = 28;_context2.t0 = _context2["catch"](22);return _context2.abrupt("return",



            Promise.reject("BUILD_HOOK Failed with error: ".concat(_context2.t0)));case 31:

            c.isBuildHooksReady = true;case 32:


            h = require(c.paths.buildHooks.dist.index);

            c.buildHooks = h.hooks;
            c.buildPipes = h.pipes;
            h = require(c.paths.buildHooks.dist.index);case 36:return _context2.abrupt("return",


            true);case 37:case "end":return _context2.stop();}}}, _callee2, null, [[22, 28]]);}));return function buildHooks(_x3) {return _ref2.apply(this, arguments);};}();exports.buildHooks = buildHooks;
//# sourceMappingURL=buildHooks.js.map