From 0d8098711a4ce838841991fc6f0be94fbad105ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20H=C3=B8egh?= Date: Tue, 9 Jul 2019 22:09:24 +0200 Subject: [PATCH] Add support for `install-args` option --- bin/lernaupdate | 7 +++++++ src/index.js | 33 +++++++++++++++++++++----------- src/utils/composeCommand.js | 1 + src/utils/composeCommand.spec.js | 28 +++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 src/utils/composeCommand.js create mode 100644 src/utils/composeCommand.spec.js diff --git a/bin/lernaupdate b/bin/lernaupdate index d9d7ab7..fb3d27a 100755 --- a/bin/lernaupdate +++ b/bin/lernaupdate @@ -22,6 +22,8 @@ const { input, flags } = meow( --lazy Runs a single install command after updating package.json files, instead installing inside each individual package (Useful for workspaces) + --install-args Any additional arguments to pass to the NPM or Yarn during the installation (space-separated). See example. + Examples: $ lernaupdate ./myproject --dedupe @@ -33,6 +35,8 @@ const { input, flags } = meow( $ lernaupdate ./myproject --packages "packages/*" $ lernaupdate ./myproject --non-interactive --dependency "react@latest" --new-installs-mode dev + + $ lernaupdate ./myproject --install-args="--verbose --prefer-offline" `, { flags: { @@ -55,6 +59,9 @@ const { input, flags } = meow( lazy: { type: "boolean", }, + installArgs: { + type: "string", + }, }, } ); diff --git a/src/index.js b/src/index.js index f2b6bbb..aface10 100644 --- a/src/index.js +++ b/src/index.js @@ -17,6 +17,7 @@ const parseDependency = require("./utils/parseDependency"); const sanitizeGitBranchName = require("./utils/sanitizeGitBranchName"); const modifyPackageJson = require("./utils/modifyPackageJson"); const lines = require("./utils/lines"); +const composeCommand = require("./utils/composeCommand"); inquirer.registerPrompt( "autocomplete", @@ -383,7 +384,7 @@ module.exports = async ({ input, flags }) => { ui.log.write(chalk.green(`Using version ${targetVersionResolved} ✓\n`)); - // PROMPT: Yarn workspaces lazy installation prompt + // PROMPT: Yarn workspaces lazy installation if (workspaces && !flags.lazy && !flags.nonInteractive) { ui.logBottom(""); @@ -497,15 +498,22 @@ module.exports = async ({ input, flags }) => { chalk`{white.bold ${packageName}}: {green package.json updated ✓}\n` ); } else { - const installCmd = (dependencyManager === "yarn" - ? ["yarn", "add", sourceParam, `${targetDependency}@${targetVersion}`] - : [ - "npm", - "install", - sourceParam, - `${targetDependency}@${targetVersion}`, - ] - ).join(" "); + const installCmd = + dependencyManager === "yarn" + ? composeCommand( + "yarn", + "add", + sourceParam, + flags.installArgs, + `${targetDependency}@${targetVersion}` + ) + : composeCommand( + "npm", + "install", + sourceParam, + flags.installArgs, + `${targetDependency}@${targetVersion}` + ); await runCommand(`cd ${packageDir} && ${installCmd}`, { startMessage: `${chalk.white.bold(depName)}: ${installCmd}`, @@ -520,7 +528,10 @@ module.exports = async ({ input, flags }) => { if (flags.lazy) { ui.log.write(""); - const installCmd = dependencyManager === "yarn" ? "yarn" : "npm install"; + const installCmd = composeCommand( + dependencyManager === "yarn" ? "yarn" : "npm install", + flags.installArgs + ); await runCommand(`cd ${projectDir} && ${installCmd}`, { startMessage: `${chalk.white.bold(projectName)}: ${installCmd}`, diff --git a/src/utils/composeCommand.js b/src/utils/composeCommand.js new file mode 100644 index 0000000..17900c8 --- /dev/null +++ b/src/utils/composeCommand.js @@ -0,0 +1 @@ +module.exports = (...args) => args.filter(Boolean).join(" "); diff --git a/src/utils/composeCommand.spec.js b/src/utils/composeCommand.spec.js new file mode 100644 index 0000000..6161524 --- /dev/null +++ b/src/utils/composeCommand.spec.js @@ -0,0 +1,28 @@ +const expect = require("unexpected"); +const composeCommand = require("./composeCommand"); + +describe("composeCommand", () => { + it("composes single command", () => { + expect(composeCommand, "when called with", ["yarn"], "to equal", "yarn"); + }); + + it("composes multiple commands", () => { + expect( + composeCommand, + "when called with", + ["yarn", "add"], + "to equal", + "yarn add" + ); + }); + + it("ignores empty inputs", () => { + expect( + composeCommand, + "when called with", + ["yarn", undefined, "add", null, false], + "to equal", + "yarn add" + ); + }); +});