Change (un)install commands to use locally installed yarn

This commit is contained in:
Pavel Djundik 2018-01-19 12:50:33 +02:00
parent f646fbbd4f
commit 913e88185e
3 changed files with 85 additions and 79 deletions

View File

@ -57,7 +57,8 @@
"thelounge-ldapjs-non-maintained-fork": "1.0.2",
"ua-parser-js": "0.7.17",
"urijs": "1.19.1",
"web-push": "3.2.5"
"web-push": "3.2.5",
"yarn": "1.3.2"
},
"devDependencies": {
"babel-core": "6.26.0",

View File

@ -37,11 +37,10 @@ program
const packagesPath = Helper.getPackagesPath();
const packagesConfig = path.join(packagesPath, "package.json");
// Create node_modules folder, otherwise npm will start walking upwards to find one
// Create node_modules folder, otherwise yarn will start walking upwards to find one
fsextra.ensureDirSync(path.join(packagesPath, "node_modules"));
// Create package.json with private set to true to avoid npm warnings, if
// it doesn't exist already
// Create package.json with private set to true, if it doesn't exist already
if (!fs.existsSync(packagesConfig)) {
fs.writeFileSync(packagesConfig, JSON.stringify({
private: true,
@ -49,33 +48,50 @@ program
}, null, "\t"));
}
const npm = child.spawn(
process.platform === "win32" ? "npm.cmd" : "npm",
[
"install",
"--production",
"--save",
"--save-exact",
"--no-bin-links",
"--no-package-lock",
"--no-progress",
"--prefix",
packagesPath,
`${packageName}@${json.version}`,
],
{
// This is the same as `"inherit"` except `process.stdout` is ignored
stdio: [process.stdin, "ignore", process.stderr],
}
const yarn = path.join(
__dirname,
"..",
"..",
"node_modules",
"yarn",
"bin",
"yarn.js"
);
npm.on("error", (e) => {
let success = false;
const add = child.spawn(
process.execPath,
[
yarn,
"add",
"--json",
"--exact",
"--production",
"--ignore-scripts",
"--non-interactive",
"--cwd",
packagesPath,
`${packageName}@${json.version}`,
]
);
add.stdout.on("data", (data) => {
data.toString().trim().split("\n").forEach((line) => {
line = JSON.parse(line);
if (line.type === "success") {
success = true;
}
});
});
add.on("error", (e) => {
log.error(`${e}`);
process.exit(1);
});
npm.on("close", (code) => {
if (code !== 0) {
add.on("close", (code) => {
if (!success || code !== 0) {
log.error(`Failed to install ${colors.green(packageName)}. Exit code: ${code}`);
return;
}

View File

@ -30,7 +30,12 @@ program
process.exit(1);
}
const npm = process.platform === "win32" ? "npm.cmd" : "npm";
const packages = JSON.parse(fs.readFileSync(packagesConfig, "utf-8"));
if (!packages.dependencies || !packages.dependencies.hasOwnProperty(packageName)) {
log.warn(packageWasNotInstalled);
process.exit(1);
}
const errorHandler = (error) => {
log.error(
@ -40,64 +45,48 @@ program
process.exit(1);
};
// First, we check if the package is installed with `npm list`
const list = child.spawn(
npm,
[
"list",
"--depth",
"0",
"--prefix",
packagesPath,
packageName,
],
{
// This is the same as `"inherit"` except:
// - `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"],
}
const yarn = path.join(
__dirname,
"..",
"..",
"node_modules",
"yarn",
"bin",
"yarn.js"
);
list.stdout.on("data", (data) => {
// If the package name does not appear in stdout, it means it was not
// installed. We cannot rely on exit code because `npm ERR! extraneous`
// causes a status of 1 even if package exists.
if (!data.toString().includes(packageName)) {
log.warn(packageWasNotInstalled);
process.exit(1);
}
});
let success = false;
const remove = child.spawn(
process.execPath,
[
yarn,
"remove",
"--json",
"--ignore-scripts",
"--non-interactive",
"--cwd",
packagesPath,
packageName,
]
);
list.on("error", errorHandler);
remove.stdout.on("data", (data) => {
data.toString().trim().split("\n").forEach((line) => {
line = JSON.parse(line);
list.on("close", () => {
// If we get there, it means the package exists, so uninstall
const uninstall = child.spawn(
npm,
[
"uninstall",
"--save",
"--no-progress",
"--prefix",
packagesPath,
packageName,
],
{
// This is the same as `"inherit"` except `process.stdout` is silenced
stdio: [process.stdin, "ignore", process.stderr],
if (line.type === "success") {
success = true;
}
);
uninstall.on("error", errorHandler);
uninstall.on("close", (code) => {
if (code !== 0) {
errorHandler(code);
}
log.info(`${colors.green(packageName)} has been successfully uninstalled.`);
});
});
remove.on("error", errorHandler);
remove.on("close", (code) => {
if (!success || code !== 0) {
return errorHandler(code);
}
log.info(`${colors.green(packageName)} has been successfully uninstalled.`);
});
});