-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglob.js
58 lines (49 loc) · 1.38 KB
/
glob.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
52
53
54
55
56
57
58
import { SpanStatusCode } from '@opentelemetry/api'
import { glob as globFn } from 'glob'
import onetime from 'onetime'
import { Readable } from 'readable-stream'
import tracer from './lib/tracer.js'
/**
* @this {import('barnard59-core').Context}
* @param {{ pattern: string } & Omit<import('glob').GlobOptions, 'withFileTypes'>} options
* @return {Readable}
*/
function glob({ pattern, ...options }) {
const { logger } = this
/** @type {string[]} */
let filenames = []
const span = tracer.startSpan('glob')
const init = onetime(async () => {
span.addEvent('init')
filenames = await globFn(pattern, options)
return filenames.length === 0
})
/** @type {Readable} */
const stream = new Readable({
objectMode: true,
read: async () => {
try {
const noneMatched = await init()
if (filenames.length === 0) {
if (noneMatched) {
logger.warn(`No files matched by glob '${pattern}'`)
}
return stream.push(null)
}
if (!stream.push(filenames.shift())) {
return
}
// @ts-ignore
stream._read()
} catch (/** @type {any} */ err) {
span.recordException(err)
span.setStatus({ code: SpanStatusCode.ERROR, message: err.message })
stream.destroy(err)
} finally {
span.end()
}
},
})
return stream
}
export default glob