diff --git a/src/bin.ts b/src/bin.ts old mode 100644 new mode 100755 index df4cd050..1413c383 --- a/src/bin.ts +++ b/src/bin.ts @@ -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] [ [ ...]]' + usage: 'glob [options] [ [ ...]]', }) .description( ` @@ -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', @@ -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)) diff --git a/test/bin.ts b/test/bin.ts index 6d56251e..6cd242c8 100644 --- a/test/bin.ts +++ b/test/bin.ts @@ -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`) +})