-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
227 lines (204 loc) · 4.44 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* Dependencie(s)
*/
const fs = require('fs')
const {
join,
relative
} = require('path')
/**
* Generate a manner tree (resources) from a folder structure.
*
* @param {String} path
* @param {Object?} options
* @api public
*/
module.exports = (path, options = {}) => {
let resources = {}
walk(path, folder => {
const samples = stories(folder, options.stories)
resources = merge(
resources,
resource(
join('/', relative(path, folder)),
options.disabled ? mock(samples) : (read(folder + '/index.js', e => {
throw new Error(e)
}) || {}),
schema(folder, options.schema),
samples
)
)
})
return resources
}
/**
* Mock service.
*
* This handlers mock services with empty function to avoid
* executing real service.
*
* @param {Object} samples
* @return {Object}
* @api private
*/
function mock (samples) {
let services = {}
Object.keys(samples).map(key => {
const service = services[key] = {}
Object.keys(samples[key]).map(path => {
service[path] = () => 'service disabled'
})
})
return services
}
/**
* Read schema if exist.
*
* @param {String} path
* @return {Object} (empty if schema does not exist)
* @api private
*/
function schema (path, name = 'schema') {
const js = read(join(path, `${name}.js`))
const json = read(join(path, `${name}.json`))
return js || json || {}
}
/**
* Read user stories if exist.
*
* @param {String} path
* @return {Object} (empty if user stories does not exist)
* @api private
*/
function stories (path, name = 'stories') {
const js = read(join(path, `${name}.js`))
const json = read(join(path, `${name}.json`))
return js || json || {}
}
/**
* Merge srouce object with manner tree.
*
* @param {Object} src
* @param {Object} tree
* @return {Object}
* @api private
*/
function merge (src, tree) {
Object.keys(tree).map(method => {
const services = tree[method]
const node = src[method] = src[method] || {}
Object.keys(services).map(path => {
node[path] = services[path]
})
})
return src
}
/**
* Walk folder recursively.
*
* @param {String} path
* @param {Function} cb
* @param {String} relative
* @api private
*/
function walk (path, cb) {
cb(path)
fs.readdirSync(path).map(file => {
const folder = join(path, file)
if (fs.statSync(folder).isDirectory()) {
walk(folder, cb)
}
})
}
/**
* Read manner resource Synchronously.
*
* @param {String} folder
* @api private
*/
function read (folder, catcher = a => a) {
var resource = null
if (fs.existsSync(folder)) {
try {
resource = require(folder)
} catch (e) {
catcher(e)
}
}
return resource
}
/**
* Create manner resource from a set of services, schema and user stories.
*
* @param {String} pathn
* @param {Object} services
* @param {Object} conf (services schema)
* @param {Object} cases (user stories)
* @return {Object}
* @api private
*/
function resource (path, services, conf = {}, cases = {}) {
const result = {}
Object.keys(services).map(method => {
const res = result[method] = result[method] || {}
const service = services[method]
const schema = conf[method] || {}
const stories = cases[method] || {}
if (typeof service === 'object') {
Object.keys(service).map(p => {
const route = trim(join(path, p))
res[posix(route)] = parse(service[p], schema[p], stories[p])
})
} else {
res[posix(path)] = parse(service, schema['/'], stories['/'])
}
})
return result
}
/**
* Transform win32 path into posix path.
*
* @param {String} path
* @return {String}
* @api private
*/
function posix (path) {
const isExtendedLengthPath = /^\\\\\?\\/.test(path)
const hasNonAscii = /[^\u0000-\u0080]+/.test(path)
if (isExtendedLengthPath || hasNonAscii) return path
return path.replace(/\\/g, '/')
}
/**
* Trim path.
*
* Remove empty characters and end backslash.
*
* @param {String} path
* @return {String}
* @api private
*/
function trim (path) {
path = path.trim()
const length = path.length - 1
if (length > 0 && path[length] === '/') {
path = path.substring(0, length)
}
return path
}
/**
* Parse function to return manner service.
*
* @param {Function} service
* @param {Object} data schema
* @param {Object} stories
* @return {Object}
* @api private
*/
function parse (service, data = {}, stories = {}) {
return {
service,
options: {},
...data,
stories
}
}