Skip to content

Commit

Permalink
Fix: fix for latest yarn
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Nov 3, 2017
1 parent 528d105 commit dfb9dcb
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 23 deletions.
38 changes: 17 additions & 21 deletions lib/run-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,25 @@ module.exports = function runTask(task, options) {
))
}

if (path.extname(options.npmPath || "a.js") === ".js") {
const npmPath = options.npmPath || process.env.npm_execpath //eslint-disable-line no-process-env
const execPath = npmPath ? process.execPath : "npm"
const spawnArgs = [].concat(
npmPath ? [npmPath, "run"] : ["run"],
options.prefixOptions,
parseArgs(task)
)

// Execute.
cp = spawn(execPath, spawnArgs, spawnOptions)
// Execute.
const npmPath = options.npmPath || process.env.npm_execpath //eslint-disable-line no-process-env
const npmPathIsJs = typeof npmPath === "string" && /\.m?js/.test(path.extname(npmPath))
const execPath = (npmPathIsJs ? process.execPath : npmPath || "npm")
const isYarn = path.basename(npmPath || "npm").startsWith("yarn")
const spawnArgs = ["run"]

if (npmPathIsJs) {
spawnArgs.unshift(npmPath)
}
else {
const execPath = options.npmPath
const spawnArgs = [].concat(
["run"],
options.prefixOptions,
parseArgs(task)
)

// Execute.
cp = spawn(execPath, spawnArgs, spawnOptions)
if (!isYarn) {
Array.prototype.push.apply(spawnArgs, options.prefixOptions)
}
else if (options.prefixOptions.indexOf("--silent") !== -1) {
spawnArgs.push("--silent")
}
Array.prototype.push.apply(spawnArgs, parseArgs(task))

cp = spawn(execPath, spawnArgs, spawnOptions)

// Piping stdio.
if (stdinKind === "pipe") {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"mocha": "^3.5.0",
"nyc": "^11.1.0",
"power-assert": "^1.4.4",
"rimraf": "^2.6.1"
"rimraf": "^2.6.1",
"yarn": "^1.2.1"
},
"repository": "mysticatea/npm-run-all",
"keywords": [
Expand Down
3 changes: 2 additions & 1 deletion test-workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"test-task:nest-append:npm-run-all": "node ../bin/npm-run-all/index.js test-task:append",
"test-task:nest-append:run-s": "node ../bin/run-s/index.js test-task:append",
"test-task:nest-append:run-p": "node ../bin/run-p/index.js test-task:append",
"test-task:delayed": "node tasks/output-with-delay.js"
"test-task:delayed": "node tasks/output-with-delay.js",
"test-task:yarn": "node ../bin/npm-run-all/index.js test-task:append:{a,b}"
},
"repository": {
"type": "git",
Expand Down
66 changes: 66 additions & 0 deletions test/yarn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const spawn = require("cross-spawn")
const assert = require("power-assert")
const BufferStream = require("./lib/buffer-stream")
const util = require("./lib/util")
const result = util.result
const removeResult = util.removeResult

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* Execute a command.
* @param {string} command A command to execute.
* @param {string[]} args Arguments for the command.
* @returns {Promise<string>} The result of child process's stdout.
*/
function exec(command, args) {
return new Promise((resolve, reject) => {
const stdout = new BufferStream()
const stderr = new BufferStream()
const cp = spawn(command, args, { stdio: ["inherit", "pipe", "pipe"] })

cp.stdout.pipe(stdout)
cp.stderr.pipe(stderr)
cp.on("exit", (exitCode) => {
if (exitCode) {
reject(new Error(`Exited with ${exitCode}: ${stderr.value}`))
return
}
resolve(stdout.value)
})
cp.on("error", reject)
})
}

//------------------------------------------------------------------------------
// Test
//------------------------------------------------------------------------------

describe("[yarn]", () => {
before(() => process.chdir("test-workspace"))
after(() => process.chdir(".."))

beforeEach(removeResult)

describe("'yarn run' command", () => {
it("should run 'npm-run-all' in scripts with yarn.", async () => {
const stdout = await exec("yarn", ["run", "test-task:yarn"])
const matches = stdout.match(/^\$ node .+$/gm)
assert(result() === "aabb")
assert(matches.length === 3)
})
})
})

0 comments on commit dfb9dcb

Please sign in to comment.