Merge pull request #1974 from thelounge/astorije/fix-uninstall

Fix `thelounge uninstall` command
This commit is contained in:
Jérémie Astori 2018-01-13 13:13:19 -05:00 committed by GitHub
commit 503c1538f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,45 +28,75 @@ program
if (!fs.existsSync(packagesConfig)) { if (!fs.existsSync(packagesConfig)) {
log.warn(packageWasNotInstalled); log.warn(packageWasNotInstalled);
return; process.exit(1);
} }
const npm = child.spawn( const npm = process.platform === "win32" ? "npm.cmd" : "npm";
process.platform === "win32" ? "npm.cmd" : "npm", const errorHandler = (error) => {
log.error(
`Failed to uninstall ${colors.green(packageName)}. ` +
`${typeof x === "number" ? "Exit code" : "Error"}: ${error}`
);
process.exit(1);
};
// First, we check if the package is installed with `npm list`
const list = child.spawn(
npm,
[ [
"uninstall", "list",
"--no-progress", "--depth",
"0",
"--prefix", "--prefix",
packagesParent, packagesParent,
packageName, packageName,
], ],
{ {
// This is the same as `"inherit"` except `process.stdout` is piped // This is the same as `"inherit"` except:
stdio: [process.stdin, "pipe", process.stderr], // - `process.stdout` is piped so we can test if the output mentions the
// package was found
// - `process.stderr` is ignored to silence `npm ERR! extraneous` errors
stdio: [process.stdin, "pipe", "ignore"],
} }
); );
let hasUninstalled = false; list.stdout.on("data", (data) => {
// If the package name does not appear in stdout, it means it was not
npm.stdout.on("data", () => { // installed. We cannot rely on exit code because `npm ERR! extraneous`
hasUninstalled = true; // causes a status of 1 even if package exists.
}); if (!data.toString().includes(packageName)) {
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); log.warn(packageWasNotInstalled);
process.exit(1);
} }
}); });
list.on("error", errorHandler);
list.on("close", () => {
// If we get there, it means the package exists, so uninstall
const uninstall = child.spawn(
npm,
[
"uninstall",
"--no-progress",
"--prefix",
packagesParent,
packageName,
],
{
// This is the same as `"inherit"` except `process.stdout` is silenced
stdio: [process.stdin, "ignore", process.stderr],
}
);
uninstall.on("error", errorHandler);
uninstall.on("close", (code) => {
if (code !== 0) {
errorHandler(code);
}
log.info(`${colors.green(packageName)} has been successfully uninstalled.`);
});
});
}); });