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 fs = require("fs");
const fsextra = require("fs-extra"); const fsextra = require("fs-extra");
const path = require("path"); const path = require("path");
const child = require("child_process");
const packageJson = require("package-json"); const packageJson = require("package-json");
if (!fs.existsSync(Helper.getConfigPath())) { if (!fs.existsSync(Helper.getConfigPath())) {
@ -53,21 +52,7 @@ program
}, null, "\t")); }, null, "\t"));
} }
const yarn = path.join( return Utils.executeYarnCommand(
__dirname,
"..",
"..",
"node_modules",
"yarn",
"bin",
"yarn.js"
);
let success = false;
const add = child.spawn(
process.execPath,
[
yarn,
"add", "add",
"--json", "--json",
"--exact", "--exact",
@ -76,36 +61,11 @@ program
"--non-interactive", "--non-interactive",
"--cwd", "--cwd",
packagesPath, packagesPath,
`${json.name}@${json.version}`, `${json.name}@${json.version}`
] ).then(() => {
);
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;
}
log.info(`${colors.green(json.name + " v" + json.version)} has been successfully installed.`); 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) => { }).catch((e) => {
log.error(`${e}`); log.error(`${e}`);

View File

@ -12,7 +12,6 @@ program
.on("--help", Utils.extraHelp) .on("--help", Utils.extraHelp)
.action(function(packageName) { .action(function(packageName) {
const fs = require("fs"); const fs = require("fs");
const child = require("child_process");
if (!fs.existsSync(Helper.getConfigPath())) { if (!fs.existsSync(Helper.getConfigPath())) {
log.error(`${Helper.getConfigPath()} does not exist.`); log.error(`${Helper.getConfigPath()} does not exist.`);
@ -37,60 +36,18 @@ program
process.exit(1); process.exit(1);
} }
const errorHandler = (error) => { return Utils.executeYarnCommand(
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", "remove",
"--json", "--json",
"--ignore-scripts", "--ignore-scripts",
"--non-interactive", "--non-interactive",
"--cwd", "--cwd",
packagesPath, packagesPath,
packageName, packageName
] ).then(() => {
);
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);
}
log.info(`${colors.green(packageName)} has been successfully uninstalled.`); 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; 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; module.exports = Utils;