-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.js
executable file
·55 lines (39 loc) · 1.28 KB
/
mock.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
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs')
const { walk } = require('./util')
const RE = /^\s*\/\*[*\s]+?([^\r\n]+)[\s\S]+?@url\s+([^\n]+)[\s\S]+?\*\//im
function parseAPIs (dir) {
const routes = {} // routes list
const files = walk(dir);
(files || []).forEach(filepath => {
const content = String(fs.readFileSync(filepath, 'utf8')).trim() || '{}'
let url = filepath
let describe = 'no description'
const m = content.match(RE)
if (m) {
url = m[2].trim()
describe = m[1].replace(/(^[\s*]+|[\s*]+$)/g, '')
}
if (url[0] !== '/') {
// fix url path
url = '/' + url
}
let pathname = url
if (pathname.indexOf('?') > -1) {
pathname = pathname.split('?')[0]
}
if (routes[pathname]) {
console.warn('[Mock Warn]: [' + filepath + ': ' + pathname + '] already exists and has been covered with new data.')
}
routes[pathname] = {
url: url,
filepath: filepath,
describe: describe
}
if (/\.json$/.test(filepath)) {
routes[pathname].content = content
}
})
return routes
}
module.exports = parseAPIs