Skip to content

Commit

Permalink
Fix bracketed glob patterns for include/exclude options
Browse files Browse the repository at this point in the history
This should fix #39. There is certainly room to refactor this to
be simpler and less code, but at least it's a start.
  • Loading branch information
mstade committed Sep 21, 2016
1 parent 71e04f4 commit 0916a6c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/cli/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ export default async function parse(pkg, argv) {
.option('-i, --src <dir>', `the root directory from which all sources are relative [${defaults.src}]`, pkg.relative, defaults.src)
.option('-o, --out <prefix>', `write optimized output to files with the specified prefix [${defaults.out}]`, pkg.relative, defaults.out)
.option('-L, --lib <dir>', `write unoptimized files to the specified directory [${defaults.lib}]`, pkg.relative, defaults.lib)
.option('-I, --include [js|css:]<path>', `include a path or glob (relative to source root) [${defaults.include}]`, concat, [])
.option('-X, --exclude [js|css:]<path>', `exclude a path or glob (relative to source root) [${defaults.exclude}]`, concat, [])
.option('-I, --include [js|css:]<path>', `include a path or glob (relative to source root) [${defaults.include}]`, concatGlobs, [])
.option('-X, --exclude [js|css:]<path>', `exclude a path or glob (relative to source root) [${defaults.exclude}]`, concatGlobs, [])
.option('-O, --optimize <level>', `optimization level (0 = none) [${defaults.optimize}]`, setOptimization, defaults.optimize)
.option('--no-copy', `disable copying of non-code files to ${defaults.lib}`, Boolean, !defaults.copy)
.option('--no-debug', 'disable source map generation', Boolean, !defaults.debug)
.option('--log <normal|json>', `log output format [${defaults.log}]`, /^(json|normal)$/i, defaults.log)
.option('--interactive', `watch for and recompile on changes (implies -O 0)`)
.option('--production', `enable production options (implies -O 1)`)
.option('--flags <flags>', `toggle flags [${defaults.flags}]`, concat, [])
.option('--flags <flags>', `toggle flags [${defaults.flags}]`, concatFlags, [])
.option('@<path>', 'read options from the file at <path> (relative to cwd)')

const opts = cli.parse(await explode(argv))
Expand Down Expand Up @@ -127,7 +127,12 @@ function setOptimization(level) {
return Math.max(level | 0, 0)
}

function concat(val, list) {
function concatGlobs(val, list) {
let prep = val.replace(/,([^,:]+:)/g, "\0$1")
return list.concat(prep.split('\0'))
}

function concatFlags(val, list) {
return list.concat(val.split(','))
}

Expand Down
33 changes: 32 additions & 1 deletion test/unit/cli/opts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { is } from 'funkis'
import { loadUnit, readFixture } from '../test-util.js'

test('Options', async t => {
t.plan(100)
t.plan(106)

const barePkg = await readFixture('bare-project')
, typicalPkg = await readFixture('typical-project')
Expand Down Expand Up @@ -89,6 +89,37 @@ test('Options', async t => {
})
)

t.comment('Options > --include/--exclude patterns')
await Promise.all(
[ [ 'js:**/*.{js,jsx}'
, { js: [ '**/*.{js,jsx}' ]
}
]
, [ 'js:**/*.{js,jsx},css:*.css'
, { js: [ '**/*.{js,jsx}' ]
, css: [ '*.css']
}
]
, [ 'js:**/*.{js,jsx},js:*.wibble'
, { js: [ '**/*.{js,jsx}', '*.wibble' ]
}
]
, [ 'js:**/*.{js,jsx},js:*.wibble,css:*.css'
, { js: [ '**/*.{js,jsx}', '*.wibble' ]
, css: [ '*.css']
}
]
].map(async ([pattern, expected]) => {
opts = await parseOpts(barePkg, argv('--include', pattern))

t.comment(`--include ${pattern}`)

Object.keys(expected).forEach(result => {
t.deepEqual(opts.include[result], expected[result], `${result} should equal ${expected[result]}`)
})
})
)

t.comment('Options > --production')
opts = await parseOpts(barePkg, argv('--production'))
t.equal(opts.optimize, 1, 'implies -O 1')
Expand Down

0 comments on commit 0916a6c

Please sign in to comment.