-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend2AngularHttpService.js
More file actions
executable file
·100 lines (87 loc) · 3.26 KB
/
Copy pathbackend2AngularHttpService.js
File metadata and controls
executable file
·100 lines (87 loc) · 3.26 KB
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
#!/user/bin/env node
var rf = require("fs")
// 参数1 是 Java 文件名
process.stdin.setEncoding('utf8')
const argument = process.argv.slice(2);
const filename = argument[0];
const lastIndexOfSlash = filename.lastIndexOf('/');
let className = filename.replace('Router.java', 'Api');
let moduleName = filename.replace('Router.java', '').toLowerCase()
className = className.slice(lastIndexOfSlash + 1);
moduleName = moduleName.slice(lastIndexOfSlash + 1);
const headerStr = `
import { Injectable } from '@angular/core'
import { SettingService } from '../core/setting/setting.service'
import { HttpService } from '../core/http/http.service'
@Injectable()
export class ${className}Service {
constructor (private httpService: HttpService, private settingService: SettingService) {
}
private post (url: string, body: any) {
return new Promise((resolve, reject) => {
const successFn = (resp) => {
resolve(resp)
}
const errorFn = (resp) => {
reject(resp)
}
this.httpService.post(this.settingService.getServerURL() + url, body, successFn, errorFn)
})
}
`
const footerStr = `
}
`
const replaceRegArray = [
/\s+\*\s<p>/,
/\s+\*\s<\/?blockquote>/,
/\s+\*\s<\/?pre>/
]
const serviceResult = []
const requestParamArray = [ '' ]
let addItemToRequestParamArray = false
serviceResult.push(headerStr)
rf.readFile(filename, 'utf-8', function (err, data) {
if (err) {
console.log("error")
} else {
const sourceArray = data.split(/[\n\r]/)
sourceArray.forEach(item => {
if (/^\s{2,}\/?\*+/.test(item)) {
for (let p of replaceRegArray) {
item = item.replace(p, '')
}
if (item) {
if (item.indexOf('* 请求') !== -1) {
addItemToRequestParamArray = true
} else if (item.indexOf('* 返回') !== -1) {
requestParamArray.push('')
addItemToRequestParamArray = false
} else if (addItemToRequestParamArray && /^\s+\*\s+"(\w+)"/.test(item)) {
/^\s+\*\s+"(\w+)"/.exec(item)
requestParamArray[ requestParamArray.length - 1] += (requestParamArray[ requestParamArray.length - 1] ? ',' : '') + RegExp.$1
}
const lineBreak = /\s+\*\//.test(item) ? '' : '\n'
serviceResult.push(item + lineBreak)
}
} else if (/^\s+public\sstatic\sRoute/.test(item)) {
const getFunctionNoReg = /Route\s(function\d+)/
getFunctionNoReg.exec(item)
const oneFuncDef = `
public ${RegExp.$1} (##requestParam##) {
return this.post('${moduleName}/${RegExp.$1}', { ##requestParam## })
}
`
serviceResult.push(oneFuncDef)
}
})
serviceResult.push(footerStr)
serviceResult.forEach((item, index) => {
if (/##requestParam##/.test(item)) {
const params = requestParamArray.shift() || ''
serviceResult[index] = item.replace(/##requestParam##/g, params)
}
})
console.log(serviceResult.join(''))
}
})