UNPKG

wp-setup

Version:

Easily create replicable local WordPress environments with Docker

166 lines 8.34 kB
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()); }); }; import AbstractCommand from './abstractCommand.js'; import { join } from 'node:path'; import { parseVolume } from '../helpers/docker.js'; import { confirm } from '../helpers/cli.js'; import { deleteVolume, exec, getServices } from '../services/docker.js'; import { render } from '../services/template.js'; import { runSetup } from '../services/wordpress.js'; import { getVSCodeConfig, startVSCode } from '../services/ideSupport.js'; export default class DockerCommands extends AbstractCommand { constructor() { super(...arguments); this.exec = (command_1, ...args_1) => __awaiter(this, [command_1, ...args_1], void 0, function* (command, options = {}) { const config = yield this.getConfig(); if (options.stdio === undefined) { options.stdio = this.mode === 'silent' ? 'pipe' : 'inherit'; } return yield exec(config, command, options); }); } start(_a) { return __awaiter(this, arguments, void 0, function* ({ xdebug }) { const config = yield this.getConfig(); const running = yield getServices(config); if (running.find(service => service.State === 'running')) { if (xdebug) { process.env.XDEBUG_MODE = 'debug,develop'; process.env.TEST_XDEBUG_MODE = 'coverage,develop,debug'; yield this.exec('up -d --remove-orphans', { stdio: 'inherit' }); this.success('XDebug started.'); } this.error('The project is already running.'); } if (xdebug) { process.env.XDEBUG_MODE = 'debug,develop'; process.env.TEST_XDEBUG_MODE = 'coverage,develop,debug'; } yield this.exec('up -d --build --remove-orphans', { stdio: 'inherit' }); this.log('Project started. Configuring WordPress environments...'); const setupData = Object.assign(Object.assign({}, config), { cliContainer: 'wp-cli', exec: this.exec }); try { yield Promise.all([ runSetup(setupData), runSetup(Object.assign(Object.assign({}, setupData), { host: `test.${config.host}`, cliContainer: 'wp-test-cli' })), ]); } catch (error) { this.error(error.message); } this.log(yield render('views/docker/start-success', config)); this.success('Environment started.'); }); } destroy() { return __awaiter(this, void 0, void 0, function* () { yield confirm('Are you sure you want to destroy the environment?'); yield this.exec('down -v', { stdio: 'inherit' }); this.success('Environment destroyed.'); }); } stop(_a) { return __awaiter(this, arguments, void 0, function* ({ xdebug }) { if (xdebug) { process.env.XDEBUG_MODE = 'off'; process.env.TEST_XDEBUG_MODE = 'coverage'; yield this.exec('up -d --remove-orphans', { stdio: 'inherit' }); this.success('XDebug stopped.'); } yield this.exec('down', { stdio: 'inherit' }); try { deleteVolume('wp-test', { stdio: 'pipe' }); } catch (e) { /* empty */ } this.success('Environment stopped.'); }); } run(service_1, command_1) { return __awaiter(this, arguments, void 0, function* (service, command, workdir = false) { const services = (yield this.exec('config --services', { stdio: 'pipe' })).toString().split('\n'); if (!services.includes(service)) { this.error(`The service "${service}" does not exist.`); } const config = yield this.getConfig(); const running = (yield getServices(config)) .filter(service => service.State === 'running') .map(service => service.Service); workdir = this.getWorkdir(config, workdir); const workdirCall = workdir ? `--workdir="${workdir}"` : ''; try { if (running.includes(service)) { yield this.exec(`exec ${workdirCall} ${service} ${command.join(' ')}`, { stdio: 'inherit' }); this.success(); } yield this.exec(`run --rm ${workdirCall} ${service} ${command.join(' ')}`, { stdio: 'inherit' }); this.success(); } catch (error) { this.error(error.message); } }); } wpCli(command) { return __awaiter(this, void 0, void 0, function* () { command.unshift('wp'); return this.run('wp-cli', command); }); } wpCliTest(command) { return __awaiter(this, void 0, void 0, function* () { command.unshift('wp'); return this.run('wp-test-cli', command); }); } code(_a) { return __awaiter(this, arguments, void 0, function* ({ editor, workdir = false, test = false }) { var _b, _c; const config = yield this.getConfig(); workdir = this.getWorkdir(config, workdir); editor = (_b = editor !== null && editor !== void 0 ? editor : config.editor) !== null && _b !== void 0 ? _b : 'vscode'; const serviceName = test ? 'wp-test-cli' : 'wp-cli'; const service = (yield getServices(config)) .filter(service => service.State === 'running') .find(service => service.Service === serviceName); if (!service) { this.error(`The service "${serviceName}" is not running.`); } config.editorConfig = Object.assign(Object.assign({}, (_c = config.editorConfig) !== null && _c !== void 0 ? _c : {}), { vscode: getVSCodeConfig(config) }); switch (editor) { case 'vscode': startVSCode(config, service, workdir ? workdir : '/var/www/html'); break; default: this.error(`The editor "${editor}" is not supported.`); break; } this.success(); }); } getWorkdir(setupFile, workdir) { var _a, _b, _c, _d, _e; if (workdir && setupFile && !workdir.startsWith('/')) { const setContainerPath = (volume, basePath = '') => ({ host: volume.host.includes('${PWD}') ? volume.host.replace('${PWD}', process.cwd()) : volume.host, container: basePath + volume.container, }); const plugins = ((_a = setupFile.plugins) !== null && _a !== void 0 ? _a : []).map(parseVolume) .map(volume => setContainerPath(volume, '/var/www/html/wp-content/plugins/')); const themes = ((_b = setupFile.themes) !== null && _b !== void 0 ? _b : []).map(parseVolume) .map(volume => setContainerPath(volume, '/var/www/html/wp-content/themes/')); const volumes = ((_c = setupFile.themes) !== null && _c !== void 0 ? _c : []).map(parseVolume) .map(volume => setContainerPath(volume)); const directory = join(process.cwd(), workdir); workdir = (_e = (_d = [...plugins, ...themes, ...volumes].find(volume => volume.host === directory)) === null || _d === void 0 ? void 0 : _d.container) !== null && _e !== void 0 ? _e : workdir; } return workdir; } } //# sourceMappingURL=dockerCommands.js.map