"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupProject = exports.getBranch = exports.createRandomFile = exports.commit = exports.getPackageJsonVersion = exports.getLatestCommitMessage = exports.getLatestTag = exports.changePackageKey = exports.runCiToolkit = exports.goToDir = exports.mkTempDir = void 0;
const lodash_1 = require("lodash");
const shelljs_1 = require("shelljs");
const fs_1 = require("fs");
const path_1 = require("path");
const os_1 = require("os");
const helpers_1 = require("../helpers");
exports.mkTempDir = () => helpers_1.exec(os_1.platform() === "darwin" ? "mktemp -d" : "mktemp -p /tmp -d").stdout;
exports.goToDir = (workdir) => {
    if (shelljs_1.pwd() === workdir) {
        return;
    }
    shelljs_1.cd(workdir);
};
exports.runCiToolkit = (envString, rootPath, command, ...variadicArgs) => {
    return helpers_1.unsafeExec(`${envString || ""} node ${rootPath}/dist/index.js ${command} ${variadicArgs.join(" ")}`);
};
exports.changePackageKey = (workdir, key, value) => {
    const packageContent = JSON.parse(fs_1.readFileSync(path_1.join(workdir, "package.json"), "utf-8"));
    fs_1.writeFileSync(path_1.join(workdir, "package.json"), JSON.stringify(lodash_1.set(lodash_1.cloneDeep(packageContent), key, value)), "utf-8");
};
exports.getLatestTag = (workdir) => {
    exports.goToDir(workdir);
    return helpers_1.exec("git tag").stdout;
};
exports.getLatestCommitMessage = (workdir) => {
    exports.goToDir(workdir);
    return helpers_1.exec("git log -1 --pretty=%B").stdout;
};
exports.getPackageJsonVersion = (workdir) => {
    const content = JSON.parse(fs_1.readFileSync(path_1.join(workdir, "package.json"), "utf-8"));
    return content.version;
};
exports.commit = (workdir, msg) => {
    exports.goToDir(workdir);
    helpers_1.exec(`git add .; git commit --allow-empty -m "${msg}" `);
};
exports.createRandomFile = (workdir) => {
    exports.goToDir(workdir);
    helpers_1.exec(` touch ${Math.floor(Math.random() * 100000)}`);
};
exports.getBranch = (workdir) => {
    exports.goToDir(workdir);
    return helpers_1.exec("git rev-parse --abbrev-ref HEAD").stdout;
};
exports.setupProject = (branch, baseProjectPath, fixtureSourcePath) => {
    const tempDir = exports.mkTempDir();
    console.log(`Setting up temporary project at: ${tempDir}`);
    helpers_1.exec(`cp -R ${fixtureSourcePath}/. ${tempDir}/`);
    helpers_1.exec(`ln -s ${baseProjectPath}/node_modules ${tempDir}/node_modules`);
    exports.goToDir(tempDir);
    helpers_1.exec("pwd");
    helpers_1.exec("git init");
    helpers_1.exec("git config user.email kaminskobot@mail.ca");
    helpers_1.exec("git config user.name kaminskobot");
    exports.commit(tempDir, "root-commit");
    if (branch !== "master") {
        exports.goToDir(tempDir);
        helpers_1.exec(`git checkout -b ${branch}`);
    }
    helpers_1.exec(`export GIT_BRANCH=${branch}`);
    const workdir = shelljs_1.pwd().toString();
    console.log(`Temporary project set up, pwd: ${workdir}`);
    return workdir;
};
//# sourceMappingURL=projectHelper.js.map