Skip to content

Commit 3fe3f26

Browse files
committed
feat: make paths (async)iterable or string
1 parent c9d057a commit 3fe3f26

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/files/glob-source.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ const fs = require('fs-extra')
44
const glob = require('it-glob')
55
const Path = require('path')
66
const errCode = require('err-code')
7+
const kindOf = require('kind-of')
78

89
/**
910
* Create an async iterator that yields paths that match requested file paths.
1011
*
11-
* @param {String} ...paths File system path(s) to glob from
12+
* @param {Iterable|AsyncIterable|String} paths File system path(s) to glob from
1213
* @param {Object} [options] Optional options
1314
* @param {Boolean} [options.recursive] Recursively glob all paths in directories
1415
* @param {Boolean} [options.hidden] Include .dot files in matched paths
1516
* @param {Array<String>} [options.ignore] Glob paths to ignore
1617
* @param {Boolean} [options.followSymlinks] follow symlinks
1718
* @yields {Object} File objects in the form `{ path: String, content: AsyncIterator<Buffer> }`
1819
*/
19-
module.exports = async function * globSource (...args) {
20-
const options = typeof args[args.length - 1] === 'string' ? {} : args.pop()
21-
const paths = args
20+
module.exports = async function * globSource (paths, options) {
21+
options = options || {}
22+
23+
if (kindOf(paths) === 'string') {
24+
paths = [paths]
25+
}
2226

2327
const globSourceOptions = {
2428
recursive: options.recursive,
@@ -30,7 +34,7 @@ module.exports = async function * globSource (...args) {
3034
}
3135

3236
// Check the input paths comply with options.recursive and convert to glob sources
33-
for (const path of paths) {
37+
for await (const path of paths) {
3438
if (typeof path !== 'string') {
3539
throw errCode(
3640
new Error(`Path must be a string`),

test/files/glob-source.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ describe('glob-source', () => {
105105
return this.skip()
106106
}
107107

108-
const result = await all(globSource(
108+
const result = await all(globSource([
109109
path.relative(process.cwd(), path.join(__dirname, '..', 'fixtures', 'dir', 'file-1.txt')),
110110
path.relative(process.cwd(), path.join(__dirname, '..', 'fixtures', 'dir', 'file-2.js'))
111-
))
111+
]))
112112

113113
expect(result.length).to.equal(2)
114114
expect(result[0].path).to.equal('file-1.txt')

0 commit comments

Comments
 (0)