"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.installPrePushHook = exports.lintFix = exports.installPackages = void 0;
const child_process_1 = require("child_process");
const path_1 = __importDefault(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const cmd_1 = require("../utils/cmd");
const helpers_1 = require("../utils/helpers");
/**
 * Executes packages installation, depending on the environment
 * @param {string} projectFolder path to the app folder
 * @param {boolean} [silent] suppress logs
 */
const installPackages = (projectFolder, silent) => {
    silent || console.log(chalk_1.default.cyan('Installing packages...'));
    if ((0, helpers_1.isDevEnvironment)(projectFolder)) {
        silent || console.log(chalk_1.default.yellow('Detected development environment.'));
        (0, cmd_1.run)('yarn', ['install'], {
            cwd: projectFolder,
            encoding: 'utf8',
        }, silent);
    }
    else {
        (0, cmd_1.run)('npm', ['install'], {
            cwd: projectFolder,
            encoding: 'utf8',
        }, silent);
    }
};
exports.installPackages = installPackages;
/**
 * Fixes possible linting issues
 * @param {string} projectFolder path to the app folder
 * @param {boolean} [silent] suppress logs
 */
const lintFix = (projectFolder, silent) => {
    var _a;
    const pkg = getPackageJson(projectFolder);
    if (!((_a = pkg === null || pkg === void 0 ? void 0 : pkg.scripts) === null || _a === void 0 ? void 0 : _a.lint)) {
        return;
    }
    silent || console.log(chalk_1.default.cyan('Linting app...'));
    (0, cmd_1.run)('npm', ['run', 'lint', '--', '--fix'], {
        cwd: projectFolder,
        encoding: 'utf8',
    }, silent);
};
exports.lintFix = lintFix;
/**
 * Install pre-push hook for lint check
 * @param {string} destination path to the app folder
 * @param {boolean} [silent] suppress logs
 */
const installPrePushHook = (destination, silent) => __awaiter(void 0, void 0, void 0, function* () {
    const pkg = getPackageJson(destination);
    if (!(pkg === null || pkg === void 0 ? void 0 : pkg.scripts) || !pkg.scripts['install-pre-push-hook']) {
        return;
    }
    silent || console.log(chalk_1.default.cyan('Installing pre-push hook...'));
    yield new Promise((resolve, reject) => {
        (0, child_process_1.exec)(`cd ${destination} && git init && npm run install-pre-push-hook`, (err) => {
            if (err) {
                console.log(chalk_1.default.yellow(`Warning: Pre-push hook may not be working due to error ${err}`));
                reject(err);
            }
            else {
                silent || console.log(chalk_1.default.cyan('Pre-push hook installed successfully!'));
                resolve();
            }
        });
    });
});
exports.installPrePushHook = installPrePushHook;
const getPackageJson = (projectFolder) => {
    const packagePath = path_1.default.join(projectFolder, 'package.json');
    return (0, helpers_1.openJsonFile)(packagePath);
};
