From cd81a2bcec7a5d77a35261f0a7885c61398557fb Mon Sep 17 00:00:00 2001 From: Jake Walker Date: Sun, 29 Apr 2018 08:47:39 +0100 Subject: [PATCH] Added upgrade command Using suggested changes Changes suggested by astorije Upgrade parameter removed and allow multiple packages to upgrade Fixed Minor fix --- src/command-line/index.js | 1 + src/command-line/upgrade.js | 66 +++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/command-line/upgrade.js diff --git a/src/command-line/index.js b/src/command-line/index.js index 8a7a7f26..e711e9ec 100644 --- a/src/command-line/index.js +++ b/src/command-line/index.js @@ -48,6 +48,7 @@ if (!Helper.config.public && !Helper.config.ldap.enable) { require("./install"); require("./uninstall"); +require("./upgrade"); // `parse` expects to be passed `process.argv`, but we need to remove to give it // a version of `argv` that does not contain options already parsed by diff --git a/src/command-line/upgrade.js b/src/command-line/upgrade.js new file mode 100644 index 00000000..4b540a1d --- /dev/null +++ b/src/command-line/upgrade.js @@ -0,0 +1,66 @@ +"use strict"; + +const colors = require("chalk"); +const program = require("commander"); +const Helper = require("../helper"); +const Utils = require("./utils"); + +program + .command("upgrade [packages...]") + .description("Upgrade installed themes and packages to their latest versions.") + .on("--help", Utils.extraHelp) + .action(function(packages) { + const fs = require("fs"); + const path = require("path"); + + // Get paths to the location of packages directory + const packagesPath = Helper.getPackagesPath(); + const packagesConfig = path.join(packagesPath, "package.json"); + const packagesList = JSON.parse(fs.readFileSync(packagesConfig)).dependencies; + const argsList = [ + "upgrade", + "--latest", + "--json", + "--production", + "--ignore-scripts", + "--non-interactive", + "--cwd", + packagesPath, + ]; + + let count = 0; + + // Check if the configuration file exists + if (!fs.existsSync(packagesConfig)) { + log.warn("There are no packages installed."); + return; + } + + // If a package names are supplied, check they exist + if (packages.length) { + log.info("Upgrading the following packages:"); + packages.forEach((p) => { + log.info(`- ${colors.green(p)}`); + + if (packagesList.hasOwnProperty(p)) { + argsList.splice(1, 0, p); + count++; + } else { + log.error(`${colors.green(p)} is not installed.`); + } + }); + } else { + log.info("Upgrading all packages..."); + } + + if (count === 0 && packages.length) { + log.warn("There are not any packages to upgrade."); + return; + } + + return Utils.executeYarnCommand(...argsList).then(() => { + log.info("Package(s) have been successfully upgraded."); + }).catch((code) => { + throw `Failed to upgrade package(s). Exit code ${code}`; + }); + });