Skip to content

Commit

Permalink
Improve options parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 10, 2019
1 parent 5962093 commit 2eb9c8e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
9 changes: 1 addition & 8 deletions src/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,8 @@ export const exec = function(input, opts) {
return execCommand(input, optsA)
}

// Same but delayed.
// Options are parsed right away, in case there are validation errors.
export const execBind = function(input, opts) {
const optsA = parseOpts(opts)
return execCommand.bind(null, input, optsA)
}

// Fire the command with `execa()`
const execCommand = async function(input, opts) {
export const execCommand = async function(input, opts) {
printEcho({ input, opts })

try {
Expand Down
10 changes: 6 additions & 4 deletions src/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { callbackify } from 'util'

import through from 'through2-concurrent'

import { exec } from './exec.js'
import { parseOpts } from './options.js'
import { execCommand } from './exec.js'

// Creates a stream to use in Gulp e.g.
// src(...).pipe(stream(({ path }) => ['command', [path]]))
Expand All @@ -11,15 +12,16 @@ import { exec } from './exec.js'
// call to those functions would be more efficient that creating lots of
// child processes through streaming.
export const stream = function(mapFunc, opts) {
const { maxConcurrency, ...optsA } = { ...DEFAULT_OPTS, ...opts }
const optsA = { ...DEFAULT_OPTS, ...opts }
const { maxConcurrency, ...optsB } = parseOpts(optsA)

// `maxConcurrency` `through2` option is not specified because `gulp.src()`
// always has a `highWaterMark` of `16` meaning only 16 files are processed
// at a time in parallel. `maxConcurrency` can then only be used to decrease
// that level of parallelism but `16` is already quite low.
return through.obj(
{ maxConcurrency },
execVinyl.bind(null, { mapFunc, opts: optsA }),
execVinyl.bind(null, { mapFunc, opts: optsB }),
)
}

Expand Down Expand Up @@ -54,7 +56,7 @@ const fireCommand = function({ input, opts }) {
return
}

return exec(input, opts)
return execCommand(input, opts)
}

const addToVinyl = function({ file, result }) {
Expand Down
8 changes: 6 additions & 2 deletions src/task.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import renameFn from 'rename-fn'

import { execBind } from './exec.js'
import { parseOpts } from './options.js'
import { execCommand } from './exec.js'

// Create a Gulp task
export const task = function(input, opts) {
const gulpTask = execBind(input, { ...opts, ...FORCED_OPTS })
const optsA = { ...opts, ...FORCED_OPTS }
const optsB = parseOpts(optsA)

const gulpTask = execCommand.bind(null, input, optsB)

// Log the command and arguments as the inner function name
renameFn(gulpTask, input)
Expand Down

0 comments on commit 2eb9c8e

Please sign in to comment.