-
Notifications
You must be signed in to change notification settings - Fork 5
/
find-controllers.ts
43 lines (37 loc) · 1.08 KB
/
find-controllers.ts
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
import * as glob from 'glob'
import { IStateAndTarget, getStateAndTarget } from './state-util'
/**
* Find Controllers result.
*/
export type FindControllersResult = Array<IStateAndTarget>
/**
* Finds classes using the specified pattern and options.
*
* @param pattern Glob pattern
* @param opts Glob options
*/
export function findControllers(
pattern: string,
opts?: glob.IOptions
): FindControllersResult {
const result = glob.sync(pattern, opts)
return result
.map(path => {
const items: Array<IStateAndTarget | null> = []
let required = require(path)
if (required) {
const stateAndTarget = getStateAndTarget(required)
if (stateAndTarget) {
items.push(stateAndTarget)
return items
}
// loop through exports - this will cover named as well as a default export
for (const key of Object.keys(required)) {
items.push(getStateAndTarget(required[key]))
}
}
return items
})
.reduce((acc, cur) => acc.concat(cur), [])
.filter(x => x !== null) as FindControllersResult
}