-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_server.js
139 lines (125 loc) · 3.34 KB
/
start_server.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
const fs = require("fs");
const server = require("./server.js");
const child_process = require("child_process");
const { LoadJsonStringByFile } = require("./tool.js");
if (!IsSystemSupport()) {
process.exit(-1);
}
const DOCS_CONFIG_FILE = "./docs_config.json";
StartDocsServer();
function IsSystemSupport() {
switch (process.platform) {
case "darwin": {
return true;
}
default: {
console.error("unsupport now!");
return false;
}
}
}
function CreateSystemTerminal(script_str) {
switch (process.platform) {
case "darwin": {
child_process.spawn("osascript", [
"-e",
`tell app "Terminal" to do script "${script_str}"`,
]);
break;
}
case "linux":
case "win32": {
return "";
}
}
}
function CheckNecessaryJsonConfig(obj) {
let lack_config_items = "";
if (obj.main_page == undefined) {
lack_config_items += "main_page, ";
}
if (obj.docs_folder == undefined) {
lack_config_items += "docs_folder, ";
}
if (obj.port == undefined) {
lack_config_items += "port, ";
}
if (lack_config_items == "") {
return true;
} else {
console.error(
`Cannot find necessary config: [${lack_config_items.slice(0, -2)}]`
);
return false;
}
}
function StartDocsServer() {
if (
process.argv.length > 2 &&
process.argv[2] != undefined &&
process.argv[3] != undefined
) {
const doc_name = process.argv[2];
const config_json = JSON.parse(atob(process.argv[3]));
if (!CheckNecessaryJsonConfig(config_json)) {
console.error(`Create ${doc_name} server failed`);
return;
}
console.log(`\nStart doc ${doc_name} server`);
StartDocServer(
config_json.main_page,
config_json.docs_folder,
config_json.html_folds,
Number(config_json.port)
);
return;
}
const config_string = LoadJsonStringByFile(DOCS_CONFIG_FILE);
if (config_string == "") {
console.error("Start server failed");
process.exit(-1);
}
const docs = JSON.parse(config_string).docs;
for (const idx in docs) {
const doc = docs[idx];
if (doc.enable == undefined || doc.enable == false) {
continue;
}
if (!CheckNecessaryJsonConfig(doc)) {
console.error(`Start ${idx} doc server failed`);
process.exit(-1);
}
const doc_json_str = btoa(`${JSON.stringify(doc)}`);
let start_script = `cd ${process.cwd()} && node start_server.js ${idx} ${doc_json_str}`;
CreateSystemTerminal(start_script);
console.log(`Start ${idx} in backend temrinal`);
}
}
function StartDocServer(main_page, docs_folder, html_folds, port) {
if (!fs.existsSync(docs_folder)) {
console.error(`file ${docs_folder} not exist`);
}
if (
main_page == null ||
docs_folder == null ||
port == null ||
main_page == undefined ||
docs_folder == undefined ||
port == undefined ||
typeof port !== "number"
) {
console.error("main_page, docs_folder, port cannot be null!");
return;
}
if (html_folds == null || html_folds == undefined) {
html_folds = [];
}
for (let i = 0; i < html_folds.length; i++) {
if (!fs.existsSync(docs_folder + "/" + html_folds[i])) {
console.error(`file ${docs_folder + "/" + html_folds[i]} not exist`);
return;
}
}
const docs_server = new server.Server(main_page, docs_folder, html_folds);
docs_server.start(port);
}