"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const ChildProcess = tslib_1.__importStar(require("child_process"));
const Path = tslib_1.__importStar(require("path"));
const ShellQuote = tslib_1.__importStar(require("shell-quote"));
const v = tslib_1.__importStar(require("villa"));
async function writeTextFileToVolume(dockerComposeProjectName, volume, path, text, mode) {
    if (Path.isAbsolute(path)) {
        throw new Error('The provided `path` must be relative');
    }
    let quotedPath = ShellQuote.quote([Path.posix.join('/volume', path)]);
    let commands = [`echo ${ShellQuote.quote(['-n', text])} > ${quotedPath}`];
    if (mode) {
        commands.push(`chmod ${ShellQuote.quote([mode])} ${quotedPath}`);
    }
    let subprocess = ChildProcess.spawn('docker', [
        'run',
        '--rm',
        '--volume',
        `${dockerComposeProjectName}_${volume}:/volume`,
        'alpine',
        'sh',
        '-c',
        commands.join(' && '),
    ]);
    await v.awaitable(subprocess);
}
exports.writeTextFileToVolume = writeTextFileToVolume;
function createVolume(name) {
    ChildProcess.spawnSync('docker', ['volume', 'create', '--name', name]);
}
exports.createVolume = createVolume;
function parseGitURL(url) {
    let [, host, project] = (url.match(/@(.+?):(.+)\.git/) || []);
    return host && project ? { host, project } : undefined;
}
exports.parseGitURL = parseGitURL;
//# sourceMappingURL=@utils.js.map