From 99175bef82b8edf5bead3882c861d6a43128402b Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Tue, 24 Dec 2019 11:31:17 +0200 Subject: [PATCH] Check if there are any packages installed in upgrade command --- src/command-line/uninstall.js | 25 ++++++------------------- src/command-line/upgrade.js | 11 +++++------ src/plugins/packages/index.js | 3 ++- 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/command-line/uninstall.js b/src/command-line/uninstall.js index 50c0b8b5..84c526ac 100644 --- a/src/command-line/uninstall.js +++ b/src/command-line/uninstall.js @@ -2,7 +2,6 @@ const log = require("../log"); const colors = require("chalk"); -const path = require("path"); const program = require("commander"); const Helper = require("../helper"); const Utils = require("./utils"); @@ -13,33 +12,21 @@ program .on("--help", Utils.extraHelp) .action(function(packageName) { const fs = require("fs"); + const path = require("path"); - if (!fs.existsSync(Helper.getConfigPath())) { - log.error(`${Helper.getConfigPath()} does not exist.`); - return; - } - - log.info(`Uninstalling ${colors.green(packageName)}...`); - - const packagesPath = Helper.getPackagesPath(); - const packagesConfig = path.join(packagesPath, "package.json"); - const packageWasNotInstalled = `${colors.green(packageName)} was not installed.`; - - if (!fs.existsSync(packagesConfig)) { - log.warn(packageWasNotInstalled); - process.exit(1); - } - - const packages = JSON.parse(fs.readFileSync(packagesConfig, "utf-8")); + const packagesConfig = path.join(Helper.getPackagesPath(), "package.json"); + const packages = JSON.parse(fs.readFileSync(packagesConfig, "utf-8")).dependencies; if ( !packages.dependencies || !Object.prototype.hasOwnProperty.call(packages.dependencies, packageName) ) { - log.warn(packageWasNotInstalled); + log.warn(`${colors.green(packageName)} is not installed.`); process.exit(1); } + log.info(`Uninstalling ${colors.green(packageName)}...`); + return Utils.executeYarnCommand("remove", packageName) .then(() => { log.info(`${colors.green(packageName)} has been successfully uninstalled.`); diff --git a/src/command-line/upgrade.js b/src/command-line/upgrade.js index da9c724f..efbbe190 100644 --- a/src/command-line/upgrade.js +++ b/src/command-line/upgrade.js @@ -15,15 +15,13 @@ program 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 packagesConfig = path.join(Helper.getPackagesPath(), "package.json"); + const packagesList = JSON.parse(fs.readFileSync(packagesConfig), "utf-8").dependencies; const argsList = ["upgrade", "--latest"]; let count = 0; - // Check if the configuration file exists - if (!fs.existsSync(packagesConfig)) { + if (!Object.entries(packagesList).length) { log.warn("There are no packages installed."); return; } @@ -55,6 +53,7 @@ program log.info("Package(s) have been successfully upgraded."); }) .catch((code) => { - throw `Failed to upgrade package(s). Exit code ${code}`; + log.error(`Failed to upgrade package(s). Exit code ${code}`); + process.exit(1); }); }); diff --git a/src/plugins/packages/index.js b/src/plugins/packages/index.js index 11c2dffc..56488896 100644 --- a/src/plugins/packages/index.js +++ b/src/plugins/packages/index.js @@ -172,6 +172,7 @@ async function outdated(cacheTimeout = TIME_TO_LIVE) { // 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), "utf-8").dependencies; const argsList = [ "outdated", "--latest", @@ -184,7 +185,7 @@ async function outdated(cacheTimeout = TIME_TO_LIVE) { ]; // Check if the configuration file exists - if (!fs.existsSync(packagesConfig)) { + if (!Object.entries(packagesList).length) { // CLI calls outdated with zero TTL, so we can print the warning there if (!cacheTimeout) { log.warn("There are no packages installed.");