This repository has been archived by the owner on Dec 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaffold.js
60 lines (48 loc) · 1.52 KB
/
scaffold.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
const fs = require('fs')
const path = require('path')
const _ = require('lodash')
module.exports = {
mapFilename: '.map.json',
mapData: null,
options: null,
init(endpoints, options) {
this.options = Object.assign({}, options)
const endpointsLite = endpoints.map(endpoint => {
Object.keys(endpoint.methods).forEach(method => {
endpoint.methods[method] = {operationId: endpoint.methods[method].operationId}
})
return endpoint
})
this.mapData = fs.readFileSync(getPath(this.mapFilename), {
encoding: 'utf-8',
flag: 'a+'
})
if(this.mapData) {
this.mapData = JSON.parse(this.mapData)
} else {
this.mapData = endpointsLite
}
fs.writeFileSync(options.controllersDirectory+'/'+this.mapFilename, JSON.stringify(endpointsLite), {flag: 'w'})
},
new(name, path) {
console.info('# Creating controller', name)
fs.writeFileSync(
path+'.js',
`module.exports = (req, res, next) => {\n\n}`,
{flag: 'wx'}
)
},
rename(endpoint, method) {
let oldEndpoint = _.find(this.mapData, {path: endpoint.path})
if (oldEndpoint instanceof Object && oldEndpoint.methods[method]) {
let oldName = oldEndpoint.methods[method].operationId
let newName = endpoint.methods[method].operationId
if (newName !== oldName) {
fs.renameSync(getPath(`${oldName}.js`), getPath(`${newName}.js`))
}
}
}
}
function getPath(filepath) {
return path.resolve(module.exports.options.controllersDirectory + '/' + filepath)
}