-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathDiscovery.js
36 lines (31 loc) · 1.08 KB
/
Discovery.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
import path from 'path'
import fs from 'fs'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const __onWindows = process.platform === 'win32' || process.platform === 'win64'
if (import.meta.url === `file://${process.argv[1]}`) {
throw new Error(
'Sorry, purescript-spec-discovery only supports NodeJS environments!'
)
}
export function getSpecs(pattern) {
return (onError, onSuccess) => {
const regex = new RegExp(pattern)
const modulePromises = fs
.readdirSync(path.join(__dirname, '..'))
.filter(directory => regex.test(directory))
.map(name => {
const fullPath = __onWindows
? path.join('file://', __dirname, '..', name, 'index.js')
: path.join(__dirname, '..', name, 'index.js')
return import(fullPath).then(module =>
module && typeof module.spec !== 'undefined' ? module.spec : null
)
})
Promise.all(modulePromises)
.then(specs => onSuccess(specs.filter(x => x)))
.catch(onError)
return () => {}
}
}