Skip to content

Commit

Permalink
feat: add default cli flag
Browse files Browse the repository at this point in the history
PR-URL: #534
Credit: @nohehf
Close: #534
Reviewed-by: @isaacs
  • Loading branch information
nohehf authored and isaacs committed Jun 21, 2023
1 parent 2a4c3ea commit 1b2bc07
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/bin.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import { foregroundChild } from 'foreground-child'
import { existsSync } from 'fs'
import { jack } from 'jackspeak'
import { globStream } from './index.js'
import { version } from '../package.json'
import { globStream } from './index.js'

const j = jack({
usage: 'glob [options] [<pattern> [<pattern> ...]]'
usage: 'glob [options] [<pattern> [<pattern> ...]]',
})
.description(
`
Expand All @@ -24,6 +24,14 @@ const j = jack({
matches as arguments.`,
},
})
.opt({
default: {
short: 'p',
hint: 'pattern',
description: `If no positional arguments are provided, glob will use
this pattern`,
},
})
.flag({
all: {
short: 'A',
Expand Down Expand Up @@ -219,7 +227,10 @@ try {
console.log(j.usage())
process.exit(0)
}
if (positionals.length === 0) throw 'No patterns provided'
if (positionals.length === 0 && !values.default)
throw 'No patterns provided'
if (positionals.length === 0 && values.default)
positionals.push(values.default)
const patterns = values.all
? positionals
: positionals.filter(p => !existsSync(p))
Expand Down
21 changes: 21 additions & 0 deletions test/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,24 @@ t.test('prioritizes exact match if exists, unless --all', async t => {
t.match(all.stdout, 'routes/i.tsx\n')
t.match(all.stdout, 'routes/d.tsx\n')
})

t.test('uses default pattern if none provided', async t => {
const cwd = t.testdir({
a: {
'x.y': '',
'x.a': '',
b: {
'z.y': '',
'z.a': '',
},
},
})

const def = await run(['-p', '**/*.y'], { cwd })
t.match(def.stdout, `a${sep}x.y\n`)
t.match(def.stdout, `a${sep}b${sep}z.y\n`)

const exp = await run(['-p', '**/*.y', '**/*.a'], { cwd })
t.match(exp.stdout, `a${sep}x.a\n`)
t.match(exp.stdout, `a${sep}b${sep}z.a\n`)
})

0 comments on commit 1b2bc07

Please sign in to comment.