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", "thelounge-ldapjs-non-maintained-fork": "1.0.2",
"ua-parser-js": "0.7.17", "ua-parser-js": "0.7.17",
"urijs": "1.19.1", "urijs": "1.19.1",
"web-push": "3.2.5" "web-push": "3.2.5",
"yarn": "1.3.2"
}, },
"devDependencies": { "devDependencies": {
"babel-core": "6.26.0", "babel-core": "6.26.0",

View file

@ -37,11 +37,10 @@ program
const packagesPath = Helper.getPackagesPath(); const packagesPath = Helper.getPackagesPath();
const packagesConfig = path.join(packagesPath, "package.json"); 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")); fsextra.ensureDirSync(path.join(packagesPath, "node_modules"));
// Create package.json with private set to true to avoid npm warnings, if // Create package.json with private set to true, if it doesn't exist already
// it doesn't exist already
if (!fs.existsSync(packagesConfig)) { if (!fs.existsSync(packagesConfig)) {
fs.writeFileSync(packagesConfig, JSON.stringify({ fs.writeFileSync(packagesConfig, JSON.stringify({
private: true, private: true,
@ -49,33 +48,50 @@ program
}, null, "\t")); }, null, "\t"));
} }
const npm = child.spawn( const yarn = path.join(
process.platform === "win32" ? "npm.cmd" : "npm", __dirname,
[ "..",
"install", "..",
"--production", "node_modules",
"--save", "yarn",
"--save-exact", "bin",
"--no-bin-links", "yarn.js"
"--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],
}
); );
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}`); log.error(`${e}`);
process.exit(1); process.exit(1);
}); });
npm.on("close", (code) => { add.on("close", (code) => {
if (code !== 0) { if (!success || code !== 0) {
log.error(`Failed to install ${colors.green(packageName)}. Exit code: ${code}`); log.error(`Failed to install ${colors.green(packageName)}. Exit code: ${code}`);
return; return;
} }

View file

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