-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (45 loc) · 1.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const { Observable } = require('rxjs/Observable')
const glob = require('glob')
const toAbsGlob = require('to-absolute-glob')
/**
* convert relative ignore path to absoulte.
* @param {Array<string> | string} ignore
* @returns {Array<string> | string}
* @api private
*/
const toAbs = (ignore, opts) => {
if (typeof ignore === 'string') {
return toAbsGlob(ignore, opts)
} else if (ignore instanceof Array) {
return ignore.map(i => toAbsGlob(i, opts))
}
}
/**
* wrap glob stream into Observable
* each value of Observable emited is just a absoulte path.
* @param {string} pattern
* @param {Object} opts
* @returns {Observable<string>}
*/
module.exports = (pattern, opts) =>
Observable.create(observer => {
opts = opts || {}
pattern = toAbsGlob(pattern, opts)
if (opts.ignore) opts.ignore = toAbs(opts.ignore, opts)
if (opts && opts.sync) {
delete opts.sync
try {
const files = glob.sync(pattern, opts)
files.forEach(f => observer.next(f))
observer.complete()
} catch (err) {
observer.error(err)
}
} else {
const globber = glob(pattern, opts)
globber.on('match', m => observer.next(m))
globber.once('end', () => observer.complete())
globber.once('error', err => observer.error(err))
return () => globber.abort()
}
})