#!/usr/bin/env node
"use strict";
/**
 * Script automates reinstall of packages.
 * Packages with snapshot postfix do not change version.
 * Hence we need to reinstall them manually.
 * This script delete them then install.
 *
 * Parameters:
 *  packages      (optional)  Stream of package names separated by space character you want to reinstall.
 *                            Default: all packages with snapshot postfix.
 *
 * @type {string}
 */
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const childProcess = require("child_process");
const currentDir = process.cwd();
const packageJson = require(`${currentDir}/package.json`);
const exec = childProcess.execSync;
const dependenciesToReinstall = process.argv.slice(2);
const isFullUpdate = !Boolean(dependenciesToReinstall.length);
const dependencyFlags = new Map([
    ['dependencies', ''],
    ['devDependencies', '-D'],
    ['optionalDependencies', '-O'],
    ['peerDependencies', '-P']
]);
const dependencyTypes = Array.from(dependencyFlags.keys());
dependencyTypes.forEach(dependencyType => {
    const dependenciesMap = packageJson[dependencyType];
    if (!dependenciesMap) {
        return;
    }
    const dependenciesToUpdate = findDependenciesToUpdate(dependenciesMap);
    if (!dependenciesToUpdate.length) {
        return;
    }
    const latestDependencies = findLatestDependencies(dependenciesMap);
    // eslint-disable-next-line no-console
    console.log('dependencies to update', dependenciesToUpdate);
    const dependenciesWithVersion = getDependenciesWithVersion(dependenciesMap, dependenciesToUpdate);
    updateDependencies(dependenciesToUpdate, dependenciesWithVersion, dependencyType);
    restoreLatestDependencies(dependencyType, latestDependencies);
});
function findLatestDependencies(dependencyStore) {
    return Object.keys(dependencyStore)
        .filter(key => dependencyStore[key] === 'latest');
}
function findDependenciesToUpdate(dependencyStore) {
    if (!isFullUpdate) {
        return dependenciesToReinstall.filter(dependency => dependencyStore[dependency]);
    }
    const snapshotPattern = /-/;
    return Object.keys(dependencyStore)
        .filter(key => {
        const isSnapshot = snapshotPattern.test(dependencyStore[key]);
        const isUrl = dependencyStore[key].startsWith('http');
        const isLatest = dependencyStore[key] === 'latest';
        return isSnapshot || isUrl || isLatest;
    });
}
function getDependenciesWithVersion(dependencyStore, dependenciesToUpdate) {
    return dependenciesToUpdate.map(packageToRemove => `${packageToRemove}@${dependencyStore[packageToRemove]}`);
}
function updateDependencies(dependenciesToUpdate, dependenciesWithVersion, dependencyType) {
    const dependencies = dependenciesToUpdate.join(' ');
    run(`yarn remove ${dependencies}`);
    const depsWithVersion = dependenciesWithVersion.join(' ');
    run(`yarn add ${depsWithVersion} ${dependencyFlags.get(dependencyType)}`);
}
function restoreLatestDependencies(dependencyType, latestDependencies) {
    if (!latestDependencies.length) {
        return;
    }
    const parsedPackageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
    const latestDependenciesMap = latestDependencies.reduce((acc, curr) => {
        acc[curr] = 'latest';
        return acc;
    }, {});
    parsedPackageJson[dependencyType] = Object.assign({}, parsedPackageJson[dependencyType], latestDependenciesMap);
    const indent = 4;
    const packageJsonStringified = JSON.stringify(parsedPackageJson, undefined, indent);
    fs.writeFileSync('package.json', packageJsonStringified);
}
function run(command) {
    // eslint-disable-next-line no-console
    console.log('run command:', command);
    const stdout = exec(command, { encoding: 'utf8' });
    // eslint-disable-next-line no-console
    console.log(stdout);
}
//# sourceMappingURL=update-deps.js.map