Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 15, 2019
1 parent 12f41c6 commit 398d070
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,34 @@ import { execCommand, streamCommand } 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 } = parseOpts({
const defaultOpts = getDefaultOpts({ opts })
const { maxConcurrency, result: resultOpt, ...optsA } = parseOpts({
opts,
defaultOpts,
forcedOpts,
})
const { result: resultOpt, ...optsB } = addDefaultOpts({ opts: optsA })

return through.obj(
{ maxConcurrency },
execVinyl.bind(null, { mapFunc, opts: optsB, resultOpt }),
execVinyl.bind(null, { mapFunc, opts: optsA, resultOpt }),
)
}

const defaultOpts = {
// We use `through2-concurrent` because `through2` processes files serially
// The default is 16 which is too low
maxConcurrency: 100,
// What to do with the result. Either 'save' or 'replace'
result: 'replace',
const getDefaultOpts = function({ opts: { result = 'replace' } = {} }) {
return {
// We use `through2-concurrent` because `through2` processes files serially
// The default is 16 which is too low
maxConcurrency: 100,
// What to do with the result. Either 'save' or 'replace'
result: 'replace',
...resultDefaultOpts[result],
}
}

const resultDefaultOpts = {
// `save` should retrieve output as string, but this is not needed for
// `replace`. Same thing with final newline stripping.
replace: { encoding: 'buffer', stripFinalNewline: false },
}

const forcedOpts = {
Expand All @@ -45,16 +54,6 @@ const forcedOpts = {
stdio: undefined,
}

// `save` should retrieve output as string, but this is not needed for
// `replace`. Same thing with final newline stripping.
const addDefaultOpts = function({ opts, opts: { result } }) {
if (result === 'save') {
return opts
}

return { encoding: 'buffer', stripFinalNewline: false, ...opts }
}

const cExecVinyl = async function({ mapFunc, opts, resultOpt }, file) {
const input = await mapFunc(file)

Expand Down

0 comments on commit 398d070

Please sign in to comment.