wp-setup
Version:
Easily create replicable local WordPress environments with Docker
52 lines • 2.46 kB
JavaScript
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 { join } from 'node:path';
import { download, exists, extract, path, rm, setupDir } from './fs.js';
export const parseVolume = (value) => {
if (typeof value !== 'string') {
return value;
}
const parts = value.split(':');
const [host, container] = parts.length > 2
? [parts.slice(0, -1).join(':'), parts[parts.length - 1]]
: parts;
const response = { host, container };
if (host.startsWith('.') || (!host.startsWith('http') && host.match(/^[a-z0-9]/i))) {
response.host = join(process.cwd(), host);
}
return response;
};
export const getExternalVolumeFiles = (volumes, type, beforeCallback) => __awaiter(void 0, void 0, void 0, function* () {
const tmpDir = path(`build/tmp/${type}`);
const destination = join(setupDir(), type);
const promises = volumes.map(parseVolume).map((volume) => __awaiter(void 0, void 0, void 0, function* () {
if (!volume.host.startsWith('http')) {
return volume;
}
const url = volume.host;
const fileName = url.split('/').pop();
if (!fileName) {
throw new Error(`Invalid file URL: ${url}`);
}
const tmpFile = `${tmpDir}/${fileName}`;
const dirName = fileName.split('.').shift();
const dest = `${destination}/${dirName}`;
if (exists(dest)) {
return Object.assign(Object.assign({}, volume), { host: dest });
}
beforeCallback(fileName, tmpFile);
const file = yield download(url, tmpFile);
yield extract(file.path.toString(), destination);
yield rm(tmpFile);
return Object.assign(Object.assign({}, volume), { host: dest });
}));
return Promise.all(promises);
});
//# sourceMappingURL=docker.js.map