From f7d34739b5c6f70c7503db269c1673502fd70520 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Tue, 24 Apr 2018 21:38:54 +0300 Subject: [PATCH] Make a separate function to execute yarn commands; fallback to global yarn Fixes #2301 Fixes #2348 --- src/command-line/install.js | 66 +++++++--------------------------- src/command-line/uninstall.js | 67 +++++++---------------------------- src/command-line/utils.js | 59 ++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 108 deletions(-) diff --git a/src/command-line/install.js b/src/command-line/install.js index 933bef6b..e21cef75 100644 --- a/src/command-line/install.js +++ b/src/command-line/install.js @@ -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}`); diff --git a/src/command-line/uninstall.js b/src/command-line/uninstall.js index b57e28d4..1de50041 100644 --- a/src/command-line/uninstall.js +++ b/src/command-line/uninstall.js @@ -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); }); }); diff --git a/src/command-line/utils.js b/src/command-line/utils.js index 870ac154..74d75148 100644 --- a/src/command-line/utils.js +++ b/src/command-line/utils.js @@ -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;