From d9cb640c2a1754c557315e4c50bc867944572a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Thu, 4 Jan 2018 21:09:59 -0500 Subject: [PATCH 1/3] Add a `thelounge uninstall` command to remove themes and packages --- src/command-line/index.js | 1 + src/command-line/uninstall.js | 53 +++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/command-line/uninstall.js diff --git a/src/command-line/index.js b/src/command-line/index.js index beef8bef..eb8cebb1 100644 --- a/src/command-line/index.js +++ b/src/command-line/index.js @@ -65,6 +65,7 @@ if (!Helper.config.public && !Helper.config.ldap.enable) { require("./users"); } require("./install"); +require("./uninstall"); // TODO: Remove this when releasing The Lounge v3 if (process.argv[1].endsWith(`${require("path").sep}lounge`)) { diff --git a/src/command-line/uninstall.js b/src/command-line/uninstall.js new file mode 100644 index 00000000..8d2a87c0 --- /dev/null +++ b/src/command-line/uninstall.js @@ -0,0 +1,53 @@ +"use strict"; + +const colors = require("colors/safe"); +const path = require("path"); +const program = require("commander"); +const Helper = require("../helper"); +const Utils = require("./utils"); + +program + .command("uninstall ") + .description("Uninstall a theme or a package") + .on("--help", Utils.extraHelp) + .action(function(packageName) { + const fs = require("fs"); + const child = require("child_process"); + + if (!fs.existsSync(Helper.getConfigPath())) { + log.error(`${Helper.getConfigPath()} does not exist.`); + return; + } + + log.info(`Uninstalling ${colors.green(packageName)}...`); + + const packagesPath = Helper.getPackagesPath(); + const packagesParent = path.dirname(packagesPath); + + const npm = child.spawn( + process.platform === "win32" ? "npm.cmd" : "npm", + [ + "uninstall", + "--prefix", + packagesParent, + packageName, + ], + { + stdio: "inherit", + } + ); + + npm.on("error", (e) => { + log.error(`${e}`); + process.exit(1); + }); + + npm.on("close", (code) => { + if (code !== 0) { + log.error(`Failed to uninstall ${colors.green(packageName)}. Exit code: ${code}`); + return; + } + + log.info(`${colors.green(packageName)} has been successfully uninstalled.`); + }); + }); From 3971ecff63195973a26c8e9aa150a16753606e4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Thu, 4 Jan 2018 22:13:46 -0500 Subject: [PATCH 2/3] Customize a bit `thelounge install|uninstall` - Hides progress bars that flash when installing/uninstalling as it does not bring real value here, at least for now - Inform user if package being uninstalled was not actually installed - Do not display npm outputs, mention which version was installed (this will probably need refining when installing packages with dependencies) --- src/command-line/install.js | 6 ++++-- src/command-line/uninstall.js | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/command-line/install.js b/src/command-line/install.js index 90ddcb75..563df106 100644 --- a/src/command-line/install.js +++ b/src/command-line/install.js @@ -55,12 +55,14 @@ program "--no-save", "--no-bin-links", "--no-package-lock", + "--no-progress", "--prefix", packagesParent, packageName, ], { - stdio: "inherit", + // This is the same as `"inherit"` except `process.stdout` is ignored + stdio: [process.stdin, "ignore", process.stderr], } ); @@ -75,7 +77,7 @@ program return; } - log.info(`${colors.green(packageName)} has been successfully installed.`); + log.info(`${colors.green(packageName + " v" + json.version)} has been successfully installed.`); }); }).catch((e) => { log.error(`${e}`); diff --git a/src/command-line/uninstall.js b/src/command-line/uninstall.js index 8d2a87c0..1c402d77 100644 --- a/src/command-line/uninstall.js +++ b/src/command-line/uninstall.js @@ -28,15 +28,23 @@ program process.platform === "win32" ? "npm.cmd" : "npm", [ "uninstall", + "--no-progress", "--prefix", packagesParent, packageName, ], { - stdio: "inherit", + // This is the same as `"inherit"` except `process.stdout` is piped + stdio: [process.stdin, "pipe", process.stderr], } ); + let hasUninstalled = false; + + npm.stdout.on("data", () => { + hasUninstalled = true; + }); + npm.on("error", (e) => { log.error(`${e}`); process.exit(1); @@ -48,6 +56,10 @@ program return; } - log.info(`${colors.green(packageName)} has been successfully uninstalled.`); + if (hasUninstalled) { + log.info(`${colors.green(packageName)} has been successfully uninstalled.`); + } else { + log.warn(`${colors.green(packageName)} was not installed.`); + } }); }); From d2388dc623c98faa47c4f5806256837c7de9b512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Sat, 6 Jan 2018 01:34:41 -0500 Subject: [PATCH 3/3] Bail when uninstalling if package.json for TL packages does not exist It is on purpose that the message is the same than when a package was not installed. From a user standpoint, it only matters that this specific package was not installed. --- src/command-line/uninstall.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/command-line/uninstall.js b/src/command-line/uninstall.js index 1c402d77..71c63538 100644 --- a/src/command-line/uninstall.js +++ b/src/command-line/uninstall.js @@ -23,6 +23,13 @@ program const packagesPath = Helper.getPackagesPath(); const packagesParent = path.dirname(packagesPath); + const packagesConfig = path.join(packagesParent, "package.json"); + const packageWasNotInstalled = `${colors.green(packageName)} was not installed.`; + + if (!fs.existsSync(packagesConfig)) { + log.warn(packageWasNotInstalled); + return; + } const npm = child.spawn( process.platform === "win32" ? "npm.cmd" : "npm", @@ -59,7 +66,7 @@ program if (hasUninstalled) { log.info(`${colors.green(packageName)} has been successfully uninstalled.`); } else { - log.warn(`${colors.green(packageName)} was not installed.`); + log.warn(packageWasNotInstalled); } }); });