-
Notifications
You must be signed in to change notification settings - Fork 3
/
service-gen.js
107 lines (89 loc) · 3.6 KB
/
service-gen.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
const fs = require('fs');
const os = require('os');
const utils = require("./utils.js");
const cmd = require('commander');
cmd
.version('0.1.0', '-v, --version')
.option('--type <string>', 'provider(p) or consumer(c)')
.option('--lang <string>', 'code language')
.option('-s, --service-name <string>', 'service and package name')
.option('--schemas [string]', 'path of jsonschema', './schemas.json')
.option('-o, --output [string]', 'path of output dir', './output')
.parse(process.argv);
// 1 Receive parameters
let type = cmd.type
let lang = cmd.lang
let service_name = cmd.serviceName
let schemasPath = cmd.schemas
let output_dir = cmd.output
if (type === "p") type = "provider"
if (type === "c") type = "consumer"
if (type !== "provider" && type !== "consumer") {
console.log("Please enter correct type: consumer | provider.");
return
}
if (lang !== "go" && lang !== "java" && lang !== "js") {
console.log("Only support go, java, js.");
return
}
if (/[A-Z]/g.test(service_name)) {
console.log("************************\nNote: Uppercase letters are converted to lowercase!\n************************")
service_name = service_name.toLowerCase()
}
if (typeof (service_name) == "undefined") {
console.log("Please enter service name");
return
}
if (fs.existsSync(schemasPath) === false) {
console.log("Schemas not exist.")
return
}
if (fs.existsSync(output_dir) === false) {
fs.mkdirSync(output_dir)
}
// Record template path
let template_path = fs.realpathSync('.') + "/templates/" + type + "/" + lang
// Record schemas path
const schemas = JSON.parse(fs.readFileSync(schemasPath).toString().trim());
console.log("Complete initialization.")
// 2 Copy the specified template to the specified project path
utils.CopyDir(template_path, output_dir)
if (lang === "java") {
utils.DeleteDir(output_dir + "/lib")
fs.mkdirSync(output_dir + "/lib")
utils.copyFile(template_path + "/lib/service-sdk-1.0-SNAPSHOT-jar-with-dependencies.jar", output_dir + "/lib/service-sdk-1.0-SNAPSHOT-jar-with-dependencies.jar")
}
// 3 Modify template variables
// Modify folder name
if (lang === "go") {
fs.mkdirSync(output_dir + "/" + service_name)
fs.renameSync(output_dir + "/{{service_name}}/callback.go", output_dir + "/" + service_name + "/callback.go")
fs.rmdirSync(output_dir + "/{{service_name}}")
} else if (lang === "java") {
fs.mkdirSync(output_dir + "/src/main/java/service/" + type + "/" + service_name)
fs.renameSync(output_dir + "/src/main/java/service/" + type + "/{{service_name}}/CallbackImpl.java", output_dir + "/src/main/java/service/" + type + "/" + service_name + "/CallbackImpl.java")
fs.rmdirSync(output_dir + "/src/main/java/service/" + type + "/{{service_name}}")
}
// Modify the service name
utils.ReplaceTemp(output_dir, "{{service_name}}", service_name)
if (service_name.indexOf("-") !== -1) {
if (lang === "go") {
utils.ReplaceTemp(output_dir + "/" + service_name, service_name, service_name.replace(/-/g, "_"))
}
if (lang === "java") {
utils.ReplaceTemp(output_dir + "/src/main/java/service/" + type + service_name, service_name, service_name.replaceAll(/-/g, "_"))
}
}
console.log("Complete copying config and replacing template.")
// 4 Install converter, read schema.json, convert to the corresponding language structure
// Create temporary folder
fs.mkdirSync(output_dir + "/.temp")
if (lang === "go") {
utils.GoParseJson(output_dir, schemas)
} else if (lang === "java") {
utils.JavaParseJson(output_dir, schemas, type)
}
console.log("Complete parsing json.")
// Remove temporary folder
utils.DeleteDir(output_dir + "/.temp");
console.log("Complete installation project dependencies.")