UNPKG

@aivec/wp-docker-dev-factory

Version:

Spin up local WordPress environments with Docker.

234 lines (233 loc) 11.5 kB
"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 (g && (g = 0, op[0] && (_ = 0)), _) 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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.showPrompts = void 0; var fs_1 = require("fs"); var path_1 = __importDefault(require("path")); var prompts_1 = __importDefault(require("prompts")); var process_1 = require("process"); var dumpfiles_1 = require("./actions/dumpfiles"); var container_1 = require("./docker/container"); var validateAll_1 = __importDefault(require("./validation/validateAll")); var buildAll_1 = __importDefault(require("./buildFinalConfig/buildAll")); var instance_1 = __importDefault(require("./actions/instance")); var stop_1 = __importDefault(require("./actions/stop")); var savesnapshot_1 = __importDefault(require("./actions/savesnapshot")); var logContainer_1 = __importDefault(require("./actions/logContainer")); var ngrok_1 = __importDefault(require("./actions/ngrok")); var logger_1 = __importDefault(require("./logger")); var environmentSelect = function (config) { return __awaiter(this, void 0, void 0, function () { var instances_1, index; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!Array.isArray(config)) return [3 /*break*/, 2]; instances_1 = []; config.forEach(function (cobj, index) { instances_1 = __spreadArray(__spreadArray([], instances_1, true), [{ title: cobj.instanceName, value: index }], false); }); return [4 /*yield*/, (0, prompts_1.default)({ type: 'select', name: 'index', message: 'Select an instance', choices: instances_1, initial: 0, })]; case 1: index = (_a.sent()).index; config = config[index]; return [2 /*return*/, config]; case 2: return [2 /*return*/, config]; } }); }); }; var actionSelect = function (config) { return __awaiter(this, void 0, void 0, function () { var actionMap; return __generator(this, function (_a) { switch (_a.label) { case 0: actionMap = [ { title: 'Start WordPress', value: { shouldBeRunning: false, requiresValidation: true, func: instance_1.default, }, }, { title: 'Stop WordPress', value: { requiresValidation: false, func: stop_1.default, }, }, { title: 'Save snapshot', value: { shouldBeRunning: true, requiresValidation: false, func: savesnapshot_1.default, }, }, { title: 'Launch NGROK (local SSL)', value: { shouldBeRunning: true, requiresValidation: false, func: ngrok_1.default, }, }, { title: 'Show server logs', value: { shouldBeRunning: true, requiresValidation: false, func: logContainer_1.default, }, }, { title: 'Update dumpfile', value: { shouldBeRunning: true, requiresValidation: false, func: dumpfiles_1.overwriteDumpfile, }, }, { title: 'Create new dumpfile', value: { shouldBeRunning: true, requiresValidation: false, func: dumpfiles_1.createNewDump, }, }, { title: 'Import database', value: { shouldBeRunning: true, requiresValidation: false, func: dumpfiles_1.redumpWithSelectedDumpfile, }, }, ]; return [4 /*yield*/, (0, prompts_1.default)({ type: 'select', name: 'action', message: "Select an operation for ".concat(config.instanceName), choices: actionMap, initial: 0, })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; var showPrompts = function (argv) { return __awaiter(void 0, void 0, void 0, function () { var configfolder, configfile, topdir, workingdir, config, chosenConfig, _a, func, shouldBeRunning, requiresValidation, finalConfig, running, e_1; return __generator(this, function (_b) { switch (_b.label) { case 0: configfolder = argv._[0] ? String(argv._[0]) : null; configfile = './wp-instances.json'; topdir = path_1.default.resolve(path_1.default.join(__dirname, '..')); workingdir = (0, process_1.cwd)(); if (configfolder) { workingdir = path_1.default.resolve((0, process_1.cwd)(), configfolder); configfile = "".concat(workingdir, "/wp-instances.json"); } if (!(0, fs_1.existsSync)(configfile)) { logger_1.default.error("".concat(logger_1.default.yellow(configfile), " doesnt exist.")); process.exit(1); } config = JSON.parse((0, fs_1.readFileSync)(configfile, 'utf8')); _b.label = 1; case 1: _b.trys.push([1, 4, , 5]); return [4 /*yield*/, environmentSelect(config)]; case 2: chosenConfig = _b.sent(); chosenConfig.workingdir = workingdir; chosenConfig.topdir = topdir; return [4 /*yield*/, actionSelect(chosenConfig)]; case 3: _a = (_b.sent()).action, func = _a.func, shouldBeRunning = _a.shouldBeRunning, requiresValidation = _a.requiresValidation; if (requiresValidation) { try { (0, validateAll_1.default)(chosenConfig); } catch (error) { process.exit(1); } } finalConfig = (0, buildAll_1.default)(chosenConfig, workingdir, topdir); running = (0, container_1.isRunning)(finalConfig.containerName); if (!running && shouldBeRunning === true) { logger_1.default.error("Container ".concat(logger_1.default.yellow(finalConfig.containerName), " isn't running.")); process.exit(1); } if (running && shouldBeRunning === false) { logger_1.default.error("Container ".concat(logger_1.default.yellow(finalConfig.containerName), " is already running.")); process.exit(1); } func(finalConfig); return [3 /*break*/, 5]; case 4: e_1 = _b.sent(); console.log('\nBye.'); return [3 /*break*/, 5]; case 5: return [2 /*return*/]; } }); }); }; exports.showPrompts = showPrompts;