Merge pull request #2385 from thelounge/xpaw/yarn

Make a separate function to execute yarn commands; fallback to global yarn
This commit is contained in:
Pavel Djundik 2018-04-26 10:14:02 +03:00 committed by GitHub
commit 4f85779e78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 108 deletions

View file

@ -13,7 +13,6 @@ program
const fs = require("fs");
const fsextra = require("fs-extra");
const path = require("path");
const child = require("child_process");
const packageJson = require("package-json");
if (!fs.existsSync(Helper.getConfigPath())) {
@ -53,59 +52,20 @@ program
}, null, "\t"));
}
const yarn = path.join(
__dirname,
"..",
"..",
"node_modules",
"yarn",
"bin",
"yarn.js"
);
let success = false;
const add = child.spawn(
process.execPath,
[
yarn,
"add",
"--json",
"--exact",
"--production",
"--ignore-scripts",
"--non-interactive",
"--cwd",
packagesPath,
`${json.name}@${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.stderr.on("data", (data) => {
log.error(data.toString());
});
add.on("error", (e) => {
log.error(`${e}`);
process.exit(1);
});
add.on("close", (code) => {
if (!success || code !== 0) {
log.error(`Failed to install ${colors.green(json.name + " v" + json.version)}. Exit code: ${code}`);
return;
}
return Utils.executeYarnCommand(
"add",
"--json",
"--exact",
"--production",
"--ignore-scripts",
"--non-interactive",
"--cwd",
packagesPath,
`${json.name}@${json.version}`
).then(() => {
log.info(`${colors.green(json.name + " v" + json.version)} has been successfully installed.`);
}).catch((code) => {
throw `Failed to install ${colors.green(json.name + " v" + json.version)}. Exit code: ${code}`;
});
}).catch((e) => {
log.error(`${e}`);

View file

@ -12,7 +12,6 @@ program
.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.`);
@ -37,60 +36,18 @@ program
process.exit(1);
}
const errorHandler = (error) => {
log.error(
`Failed to uninstall ${colors.green(packageName)}. ` +
`${typeof x === "number" ? "Exit code" : "Error"}: ${error}`
);
process.exit(1);
};
const yarn = path.join(
__dirname,
"..",
"..",
"node_modules",
"yarn",
"bin",
"yarn.js"
);
let success = false;
const remove = child.spawn(
process.execPath,
[
yarn,
"remove",
"--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;
}
});
});
remove.stderr.on("data", (data) => {
log.error(data.toString());
});
remove.on("error", errorHandler);
remove.on("close", (code) => {
if (!success || code !== 0) {
return errorHandler(code);
}
return Utils.executeYarnCommand(
"remove",
"--json",
"--ignore-scripts",
"--non-interactive",
"--cwd",
packagesPath,
packageName
).then(() => {
log.info(`${colors.green(packageName)} has been successfully uninstalled.`);
}).catch((code) => {
log.error(`Failed to uninstall ${colors.green(packageName)}. Exit code: ${code}`);
process.exit(1);
});
});

View file

@ -105,6 +105,65 @@ class Utils {
return memo;
}
static executeYarnCommand(...parameters) {
// First off, try to find yarn inside of The Lounge
let yarn = path.join(
__dirname, "..", "..", "node_modules",
"yarn", "bin", "yarn.js"
);
if (!fs.existsSync(yarn)) {
// Now try to find yarn in the same parent folder as The Lounge (flat install)
yarn = path.join(
__dirname, "..", "..", "..",
"yarn", "bin", "yarn.js"
);
if (!fs.existsSync(yarn)) {
// Fallback to global installation
yarn = "yarn";
}
}
return new Promise((resolve, reject) => {
let success = false;
const add = require("child_process").spawn(process.execPath, [yarn, ...parameters]);
add.stdout.on("data", (data) => {
data.toString().trim().split("\n").forEach((line) => {
line = JSON.parse(line);
if (line.type === "success") {
success = true;
}
});
});
add.stderr.on("data", (data) => {
data.toString().trim().split("\n").forEach((line) => {
const json = JSON.parse(line);
if (json.type === "error") {
log.error(json.data);
}
});
});
add.on("error", (e) => {
log.error(`${e}`);
process.exit(1);
});
add.on("close", (code) => {
if (!success || code !== 0) {
return reject(code);
}
resolve();
});
});
}
}
module.exports = Utils;