-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathindex.js
76 lines (65 loc) · 2.05 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict'
const glob = require('glob')
const BaseService = require('./base')
const { categories } = require('./categories')
const { assertValidServiceDefinitionExport } = require('./service-definitions')
class InvalidService extends Error {
constructor(message) {
super(message)
this.name = 'InvalidService'
}
}
function loadServiceClasses(servicePaths) {
if (!servicePaths) {
servicePaths = glob.sync(`${__dirname}/**/*.service.js`)
}
const serviceClasses = []
servicePaths.forEach(path => {
const module = require(path)
if (
!module ||
(module.constructor === Array && module.length === 0) ||
(module.constructor === Object && Object.keys(module).length === 0)
) {
throw new InvalidService(
`Expected ${path} to export a service or a collection of services`
)
} else if (module.prototype instanceof BaseService) {
serviceClasses.push(module)
} else if (module.constructor === Array || module.constructor === Object) {
for (const key in module) {
const serviceClass = module[key]
if (serviceClass.prototype instanceof BaseService) {
serviceClasses.push(serviceClass)
} else {
throw new InvalidService(
`Expected ${path} to export a service or a collection of services; one of them was ${serviceClass}`
)
}
}
} else {
throw new InvalidService(
`Expected ${path} to export a service or a collection of services; got ${module}`
)
}
})
return serviceClasses
}
function collectDefinitions() {
const services = loadServiceClasses()
// flatMap.
.map(ServiceClass => ServiceClass.getDefinition())
.reduce((accum, these) => accum.concat(these), [])
const result = { schemaVersion: '0', categories, services }
assertValidServiceDefinitionExport(result)
return result
}
function loadTesters() {
return glob.sync(`${__dirname}/**/*.tester.js`).map(path => require(path))
}
module.exports = {
InvalidService,
loadServiceClasses,
loadTesters,
collectDefinitions,
}