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 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.`);

View File

@ -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);
});
});

View File

@ -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.");