Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD --mute/-m flag to mute GROUPS or TASKS #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions bin/common/parse-cli-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
const OVERWRITE_OPTION = /^--([^:]+?):([^=]+?)(?:=(.+))?$/
const CONFIG_OPTION = /^--([^=]+?)(?:=(.+))$/
const PACKAGE_CONFIG_PATTERN = /^npm_package_config_(.+)$/
const CONCAT_OPTIONS = /^-[clnprs]+$/
const CONCAT_OPTIONS = /^-[clmnprs]+$/

/**
* Overwrites a specified package config.
Expand Down Expand Up @@ -182,6 +182,17 @@ function parseCLIArgsCore(set, args) { // eslint-disable-line complexity
addGroup(set.groups, {parallel: true})
break

case "-m":
case "--mute":
var muteTask = !!set.lastGroup.patterns.length
if (muteTask) {
var task = set.lastGroup.patterns.pop()
set.lastGroup.patterns.push([task, '--mute'])
} else {
set.lastGroup.mute = true
}
break

case "--npm-path":
set.npmPath = args[++i] || null
break
Expand Down Expand Up @@ -209,7 +220,8 @@ function parseCLIArgsCore(set, args) { // eslint-disable-line complexity
throw new Error(`Invalid Option: ${arg}`)
}
else {
set.lastGroup.patterns.push(arg)
var isMute = set.lastGroup.mute
set.lastGroup.patterns.push(isMute ? [arg, '--mute'] : arg)
}

break
Expand Down
8 changes: 6 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ module.exports = function npmRunAll(patternOrPatterns, options) {
return readPackageJson()
})
.then(x => {
const tasks = matchTasks(x.taskList, patterns)
const opts = patterns.map(x=>{
if (x[1] === '--mute') return { mute: true }
})
const pats = patterns.map(x=> typeof x === 'string' ? x : x[0])
const tasks = matchTasks(x.taskList, pats)
const labelWidth = tasks.reduce(maxLength, 0)

return runTasks(tasks, {
Expand All @@ -273,7 +277,7 @@ module.exports = function npmRunAll(patternOrPatterns, options) {
race,
maxParallel,
npmPath,
})
}, opts)
})
}
catch (err) {
Expand Down
16 changes: 8 additions & 8 deletions lib/run-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ function selectColor(taskName) {
* @param {object} labelState - An label state for the transform stream.
* @returns {stream.Writable} `source` or the created wrapped stream.
*/
function wrapLabeling(taskName, source, labelState) {
function wrapLabeling(taskName, source, labelState, opts) {
if (source == null || !labelState.enabled) {
return source
}

const label = padEnd(taskName, labelState.width)
const color = source.isTTY ? selectColor(taskName) : (x) => x
const prefix = color(`[${label}] `)
var isMuted = (opts||{}).mute ? ' MUTED' : ''
const prefix = color(`[${label + isMuted}] `)
const stream = createPrefixTransform(prefix, labelState)

stream.pipe(source)
Expand Down Expand Up @@ -115,12 +116,12 @@ function detectStreamKind(stream, std) {
* This promise object has an extra method: `abort()`.
* @private
*/
module.exports = function runTask(task, options) {
module.exports = function runTask(task, options, opts) {
let cp = null
const promise = new Promise((resolve, reject) => {
const stdin = options.stdin
const stdout = wrapLabeling(task, options.stdout, options.labelState)
const stderr = wrapLabeling(task, options.stderr, options.labelState)
const stdout = wrapLabeling(task, options.stdout, options.labelState, opts)
const stderr = wrapLabeling(task, options.stderr, options.labelState, opts)
const stdinKind = detectStreamKind(stdin, process.stdin)
const stdoutKind = detectStreamKind(stdout, process.stdout)
const stderrKind = detectStreamKind(stderr, process.stderr)
Expand All @@ -145,7 +146,7 @@ module.exports = function runTask(task, options) {
)

// Execute.
cp = spawn(execPath, spawnArgs, spawnOptions)
cp = spawn(execPath, spawnArgs, spawnOptions, opts)
}
else {
const execPath = options.npmPath
Expand All @@ -156,7 +157,7 @@ module.exports = function runTask(task, options) {
)

// Execute.
cp = spawn(execPath, spawnArgs, spawnOptions)
cp = spawn(execPath, spawnArgs, spawnOptions, opts)
}

// Piping stdio.
Expand Down Expand Up @@ -187,6 +188,5 @@ module.exports = function runTask(task, options) {
cp = null
}
}

return promise
}
8 changes: 5 additions & 3 deletions lib/run-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ function remove(array, x) {
* @returns {Promise} A promise object which becomes fullfilled when all npm-scripts are completed.
* @private
*/
module.exports = function runTasks(tasks, options) {
module.exports = function runTasks(tasks, options, opts) {
return new Promise((resolve, reject) => {
if (tasks.length === 0) {
resolve([])
return
}

const results = tasks.map(task => ({name: task, code: undefined}))
const queue = tasks.map((task, index) => ({name: task, index}))
const queue = tasks.map((task, index) => ({
name: task, index, opts: opts[index]
}))
const promises = []
let error = null
let aborted = false
Expand Down Expand Up @@ -105,7 +107,7 @@ module.exports = function runTasks(tasks, options) {
return
}
const task = queue.shift()
const promise = runTask(task.name, options)
const promise = runTask(task.name, options, task.opts)

promises.push(promise)
promise.then(
Expand Down
9 changes: 9 additions & 0 deletions lib/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@
module.exports = require(
process.platform === "win32" ? "./spawn-win32" : "./spawn-posix"
)

var fn = require(process.platform === "win32" ? "./spawn-win32" : "./spawn-posix")
module.exports = function spawn (command, args, options, opts) {
if (opts && opts.mute) {
args = ['--silent'].concat(`${[command].concat(args).join(' ')}`)
command = require.resolve('execr').replace('index.js', 'runner.js')
}
return fn(command, args, options)
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"chalk": "^1.1.3",
"cross-spawn": "^5.0.1",
"execr": "^1.0.1",
"minimatch": "^3.0.2",
"ps-tree": "^1.0.1",
"read-pkg": "^2.0.0",
Expand Down