tylerlong / yarn-upgrade-all

This is a command line utility program to upgrade all the packages in your `package.json` to the latest version (potentially upgrading packages across major versions).
96 stars 7 forks source link

Why not make it WAYYY faster and simpler #9

Closed agrublev closed 5 years ago

agrublev commented 5 years ago
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const childProcess = require("child_process");
const chalk = require("chalk");
const logError = message => {
    console.log(chalk.red("[Error]: " + message));
};
const logInfo = message => {
    console.log(chalk.blue("[Start]: " + message));
};
const logSuccess = message => {
    console.log(chalk.green("[Done]: " + message));
};

let global = "";
let packagePath = path.resolve(process.cwd(), "package.json");

if (!fs.existsSync(packagePath)) {
    logError("Cannot find package.json file in the current directory");
    process.exit(1);
}

const packageJson = require(packagePath);
const options = {
    dependencies: "",
    devDependencies: " --dev",
    peerDependencies: " --peer"
};
let ignorePkgs = [];
if (packageJson["ignore-upgrade"]) {
    ignorePkgs = packageJson["ignore-upgrade"];
}
let upgraded = { before: {}, after: {} };
for (let element of ["dependencies", "devDependencies", "peerDependencies"]) {
    if (packageJson[element]) {
        const packages = Object.keys(packageJson[element]);
        let packagesList = packages
            .filter(pkk => !ignorePkgs.includes(pkk))
            .map(pkk => {
                upgraded["before"][pkk] = packageJson[element][pkk];
                return pkk + "@latest";
            })
            .join(" ");
        let command = `yarn${global} add --dev ${packagesList}`;
        try {
            // logInfo(command);
            childProcess.execSync(command, { stdio: [] });
            const packageJsonAfter = JSON.parse(fs.readFileSync(packagePath));
            const packagesAfter = Object.keys(packageJsonAfter[element]);
            let packagesListAfter = packagesAfter
                .filter(pkk => !ignorePkgs.includes(pkk))
                .map(pkk => {
                    upgraded["after"][pkk] = packageJsonAfter[element][pkk];
                    return upgraded["before"][pkk] !== upgraded["after"][pkk]
                        ? `Updated: ${pkk} from: ${upgraded["before"][pkk]} | to: ${
                              upgraded["after"][pkk]
                          }\n`
                        : ``;
                })
                .join("");
            logSuccess(packagesListAfter);
        } catch (e) {
            logError(`${command} - ${e}`);
        }
    }
}
tylerlong commented 5 years ago

This issue has been discussed here: https://github.com/tylerlong/yarn-upgrade-all/issues/2