Merge pull request #1938 from thelounge/astorije/uninstall-packages

Add a `thelounge uninstall` command to remove themes and packages
This commit is contained in:
Jérémie Astori 2018-01-09 00:51:27 -05:00 committed by GitHub
commit b3702b0550
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 2 deletions

View File

@ -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`)) {

View File

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

View File

@ -0,0 +1,72 @@
"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 <package>")
.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 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",
[
"uninstall",
"--no-progress",
"--prefix",
packagesParent,
packageName,
],
{
// 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);
});
npm.on("close", (code) => {
if (code !== 0) {
log.error(`Failed to uninstall ${colors.green(packageName)}. Exit code: ${code}`);
return;
}
if (hasUninstalled) {
log.info(`${colors.green(packageName)} has been successfully uninstalled.`);
} else {
log.warn(packageWasNotInstalled);
}
});
});