2018-01-05 02:09:59 +00:00
|
|
|
"use strict";
|
|
|
|
|
2018-03-02 18:28:54 +00:00
|
|
|
const colors = require("chalk");
|
2018-01-05 02:09:59 +00:00
|
|
|
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();
|
2018-02-08 05:19:44 +00:00
|
|
|
const packagesConfig = path.join(packagesPath, "package.json");
|
2018-01-06 06:34:41 +00:00
|
|
|
const packageWasNotInstalled = `${colors.green(packageName)} was not installed.`;
|
|
|
|
|
|
|
|
if (!fs.existsSync(packagesConfig)) {
|
|
|
|
log.warn(packageWasNotInstalled);
|
2018-01-13 07:00:37 +00:00
|
|
|
process.exit(1);
|
2018-01-06 06:34:41 +00:00
|
|
|
}
|
2018-01-05 02:09:59 +00:00
|
|
|
|
2018-01-19 10:50:33 +00:00
|
|
|
const packages = JSON.parse(fs.readFileSync(packagesConfig, "utf-8"));
|
|
|
|
|
|
|
|
if (!packages.dependencies || !packages.dependencies.hasOwnProperty(packageName)) {
|
|
|
|
log.warn(packageWasNotInstalled);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2018-01-13 07:00:37 +00:00
|
|
|
const errorHandler = (error) => {
|
|
|
|
log.error(
|
|
|
|
`Failed to uninstall ${colors.green(packageName)}. ` +
|
|
|
|
`${typeof x === "number" ? "Exit code" : "Error"}: ${error}`
|
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
};
|
|
|
|
|
2018-01-19 10:50:33 +00:00
|
|
|
const yarn = path.join(
|
|
|
|
__dirname,
|
|
|
|
"..",
|
|
|
|
"..",
|
|
|
|
"node_modules",
|
|
|
|
"yarn",
|
|
|
|
"bin",
|
|
|
|
"yarn.js"
|
|
|
|
);
|
|
|
|
|
|
|
|
let success = false;
|
|
|
|
const remove = child.spawn(
|
|
|
|
process.execPath,
|
2018-01-05 02:09:59 +00:00
|
|
|
[
|
2018-01-19 10:50:33 +00:00
|
|
|
yarn,
|
|
|
|
"remove",
|
|
|
|
"--json",
|
|
|
|
"--ignore-scripts",
|
|
|
|
"--non-interactive",
|
|
|
|
"--cwd",
|
2018-02-08 05:19:44 +00:00
|
|
|
packagesPath,
|
2018-01-05 02:09:59 +00:00
|
|
|
packageName,
|
2018-01-19 10:50:33 +00:00
|
|
|
]
|
2018-01-05 02:09:59 +00:00
|
|
|
);
|
|
|
|
|
2018-01-19 10:50:33 +00:00
|
|
|
remove.stdout.on("data", (data) => {
|
|
|
|
data.toString().trim().split("\n").forEach((line) => {
|
|
|
|
line = JSON.parse(line);
|
2018-01-05 03:13:46 +00:00
|
|
|
|
2018-01-19 10:50:33 +00:00
|
|
|
if (line.type === "success") {
|
|
|
|
success = true;
|
2018-01-13 07:00:37 +00:00
|
|
|
}
|
2018-01-19 10:50:33 +00:00
|
|
|
});
|
|
|
|
});
|
2018-01-13 07:00:37 +00:00
|
|
|
|
2018-03-27 20:55:18 +00:00
|
|
|
remove.stderr.on("data", (data) => {
|
|
|
|
log.error(data.toString());
|
|
|
|
});
|
|
|
|
|
2018-01-19 10:50:33 +00:00
|
|
|
remove.on("error", errorHandler);
|
2018-01-13 07:00:37 +00:00
|
|
|
|
2018-01-19 10:50:33 +00:00
|
|
|
remove.on("close", (code) => {
|
|
|
|
if (!success || code !== 0) {
|
|
|
|
return errorHandler(code);
|
|
|
|
}
|
2018-01-05 02:09:59 +00:00
|
|
|
|
2018-01-19 10:50:33 +00:00
|
|
|
log.info(`${colors.green(packageName)} has been successfully uninstalled.`);
|
2018-01-05 02:09:59 +00:00
|
|
|
});
|
|
|
|
});
|