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