Check if there are any packages installed in upgrade command

This commit is contained in:
Pavel Djundik 2019-12-24 11:31:17 +02:00
parent 56cc6d0b68
commit 99175bef82
3 changed files with 13 additions and 26 deletions

View File

@ -2,7 +2,6 @@
const log = require("../log"); const log = require("../log");
const colors = require("chalk"); const colors = require("chalk");
const path = require("path");
const program = require("commander"); const program = require("commander");
const Helper = require("../helper"); const Helper = require("../helper");
const Utils = require("./utils"); const Utils = require("./utils");
@ -13,33 +12,21 @@ program
.on("--help", Utils.extraHelp) .on("--help", Utils.extraHelp)
.action(function(packageName) { .action(function(packageName) {
const fs = require("fs"); const fs = require("fs");
const path = require("path");
if (!fs.existsSync(Helper.getConfigPath())) { const packagesConfig = path.join(Helper.getPackagesPath(), "package.json");
log.error(`${Helper.getConfigPath()} does not exist.`); const packages = JSON.parse(fs.readFileSync(packagesConfig, "utf-8")).dependencies;
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"));
if ( if (
!packages.dependencies || !packages.dependencies ||
!Object.prototype.hasOwnProperty.call(packages.dependencies, packageName) !Object.prototype.hasOwnProperty.call(packages.dependencies, packageName)
) { ) {
log.warn(packageWasNotInstalled); log.warn(`${colors.green(packageName)} is not installed.`);
process.exit(1); process.exit(1);
} }
log.info(`Uninstalling ${colors.green(packageName)}...`);
return Utils.executeYarnCommand("remove", packageName) return Utils.executeYarnCommand("remove", packageName)
.then(() => { .then(() => {
log.info(`${colors.green(packageName)} has been successfully uninstalled.`); log.info(`${colors.green(packageName)} has been successfully uninstalled.`);

View File

@ -15,15 +15,13 @@ program
const path = require("path"); const path = require("path");
// Get paths to the location of packages directory // Get paths to the location of packages directory
const packagesPath = Helper.getPackagesPath(); const packagesConfig = path.join(Helper.getPackagesPath(), "package.json");
const packagesConfig = path.join(packagesPath, "package.json"); const packagesList = JSON.parse(fs.readFileSync(packagesConfig), "utf-8").dependencies;
const packagesList = JSON.parse(fs.readFileSync(packagesConfig)).dependencies;
const argsList = ["upgrade", "--latest"]; const argsList = ["upgrade", "--latest"];
let count = 0; let count = 0;
// Check if the configuration file exists if (!Object.entries(packagesList).length) {
if (!fs.existsSync(packagesConfig)) {
log.warn("There are no packages installed."); log.warn("There are no packages installed.");
return; return;
} }
@ -55,6 +53,7 @@ program
log.info("Package(s) have been successfully upgraded."); log.info("Package(s) have been successfully upgraded.");
}) })
.catch((code) => { .catch((code) => {
throw `Failed to upgrade package(s). Exit code ${code}`; log.error(`Failed to upgrade package(s). Exit code ${code}`);
process.exit(1);
}); });
}); });

View File

@ -172,6 +172,7 @@ async function outdated(cacheTimeout = TIME_TO_LIVE) {
// Get paths to the location of packages directory // Get paths to the location of packages directory
const packagesPath = Helper.getPackagesPath(); const packagesPath = Helper.getPackagesPath();
const packagesConfig = path.join(packagesPath, "package.json"); const packagesConfig = path.join(packagesPath, "package.json");
const packagesList = JSON.parse(fs.readFileSync(packagesConfig), "utf-8").dependencies;
const argsList = [ const argsList = [
"outdated", "outdated",
"--latest", "--latest",
@ -184,7 +185,7 @@ async function outdated(cacheTimeout = TIME_TO_LIVE) {
]; ];
// Check if the configuration file exists // 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 // CLI calls outdated with zero TTL, so we can print the warning there
if (!cacheTimeout) { if (!cacheTimeout) {
log.warn("There are no packages installed."); log.warn("There are no packages installed.");