Make a separate function to execute yarn commands; fallback to global yarn

Fixes #2301
Fixes #2348
This commit is contained in:
Pavel Djundik 2018-04-24 21:38:54 +03:00
parent 73b1293522
commit f7d34739b5
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,59 +52,20 @@ program
}, null, "\t")); }, null, "\t"));
} }
const yarn = path.join( return Utils.executeYarnCommand(
__dirname, "add",
"..", "--json",
"..", "--exact",
"node_modules", "--production",
"yarn", "--ignore-scripts",
"bin", "--non-interactive",
"yarn.js" "--cwd",
); packagesPath,
`${json.name}@${json.version}`
let success = false; ).then(() => {
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;
}
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( "remove",
`Failed to uninstall ${colors.green(packageName)}. ` + "--json",
`${typeof x === "number" ? "Exit code" : "Error"}: ${error}` "--ignore-scripts",
); "--non-interactive",
process.exit(1); "--cwd",
}; packagesPath,
packageName
const yarn = path.join( ).then(() => {
__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);
}
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;