-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarsius-init.js
207 lines (187 loc) · 6.8 KB
/
tarsius-init.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env node
const p = require('commander');
const pkg = require('./package.json');
const s = require('shelljs');
const logger = require('./utils').log;
const manifest = require('./utils').manifest;
const writeFile = require('./utils').writeFile;
const os = require('os');
const path = require('path');
const i = require('inquirer');
const tshome = os.homedir() + "/.tarsius";
const tsdbconfig = tshome + "/db.json"
const tsdir = path.resolve(__dirname);
const tsstackdir = tsdir + "/template/stacks";
const pg = require('pg');
const async = require('async');
const fs = require('fs');
const ejs = require('ejs');
const spawn = require('child_process').spawn;
p
.version(pkg.version)
.option('-s, --stack <langauge_stack>', 'set language stack, eg: go', 'go')
.option('-p, --project-name <project_name>', 'set project name, eg: example', '.')
.option('-f, --file <tarsius.yml>', 'set specific sdml tarsius db model [tarsius.yml]', "tarsius.yml")
.parse(process.argv)
String.prototype.capitalize = function () {
return this.charAt(0).toUpperCase() + this.slice(1);
}
let tsManifest = manifest(p, "ERROR : db model file not found");
let tables = tsManifest.tables;
let cache = tsManifest.cache;
let acls = tsManifest.acls;
let readParams = tsManifest.readParams;
let listParams = tsManifest.listParams;
let stack = p.stack;
let project = p.projectName;
let tableList = Object.keys(tables);
let queryList = []
tableList.forEach((table) => {
let fields = tables[table];
let doCached = {};
let doReadParams = [];
let doListParams = [];
let ACLs = acls[table];
if (table in cache) {
doCached = cache[table]
}
if (table in readParams){
doReadParams = readParams[table];
}
if (table in listParams){
doListParams = listParams[table];
}
let tableName = table;
let queryFields = [];
Object.keys(fields).forEach((f) => {
let field = {
name: null,
type: null,
relationTable: null,
relationField: null,
relationListField: null,
notNull: false,
isIdentifier: false,
graphQL: {
tableName: null,
fieldName: null,
type: null
}
}
field.name = f;
field.type = fields[f].type.replace(/\(([0-9\s])+\)/gm, "").replace(" ", "");
// check forigein key or not
let checkFK = field.type.split(".");
if (checkFK.length == 2) {
if (tables[checkFK[0]][checkFK[1]].type) {
field.type = tables[checkFK[0]][checkFK[1]].type;
field.relationTable = checkFK[0];
field.relationField = checkFK[1];
field.relationListField = Object.keys(tables[checkFK[0]]);
}
else {
console.log(logger.error("Can't find table relation " + fields[f].type))
process.exit(0);
}
}
let fieldType;
let graphQLType;
switch (field.type) {
case 'varchar':
fieldType = 'string';
graphQLType = 'String';
break;
case 'text':
fieldType = 'string';
graphQLType = 'String';
break;
case 'int':
fieldType = 'int';
graphQLType = 'Int';
break;
case 'date':
fieldType = 'time.Time';
graphQLType = 'String';
break;
case 'serial':
fieldType = 'int';
graphQLType = 'Int';
break;
case 'uuid':
fieldType = 'string';
graphQLType = 'String';
break;
case 'timestamp':
fieldType = 'time.Time';
graphQLType = 'DateTime';
break;
case 'timestamptz':
fieldType = 'time.Time';
graphQLType = 'DateTime';
break;
case 'bool':
fieldType = 'bool';
graphQLType = 'Boolean';
break;
default:
console.log(logger.error("ERROR: parsing data type on table ") + logger.warn(tableName) + logger.error(" and field ") + logger.warn(field.name));
process.exit(0);
}
field.notNull = fields[f].notNull ? true : false;
field.isIdentifier = fields[f].primaryKey ? true : false;
field.type = fieldType;
field.graphQL.tableName = tableName.capitalize();
field.graphQL.fieldName = f.capitalize();
if (fields[f].primaryKey) {
field.graphQL.type = "ID";
}
else {
field.graphQL.type = graphQLType;
}
queryFields.push(field);
});
let queryTable = { table: tableName, field: queryFields, cache: doCached, acls: ACLs, readParams: doReadParams, listParams: doListParams };
queryList.push(queryTable);
});
//console.log(JSON.stringify(tsManifest,null,2));
//console.log(JSON.stringify(queryList,null,2));
const pwd = path.join(process.cwd(), p.projectName);
const svcName = path.basename(pwd);
const projectName = svcName.replace("svc-", "");
let appJSON = require(tsstackdir + "/" + stack + "/app.json");
let appListTmp = [];
appJSON.templates.forEach((f) => {
appListTmp.push(tsstackdir + "/" + stack + "/" + f);
});
console.log(logger.green("COPY template to project directory"));
s.cp("-rf", appListTmp, project);
s.cp("-rf", tsstackdir + "/" + stack + "/gitignore", project + "/.gitignore")
s.sed('-i', '<project_name>', projectName, project + "/.gitignore");
console.log(logger.green("SET project name"));
appJSON.templates.forEach((file) => {
s.sed('-i', '<project_name>', projectName, file);
s.sed('-i', '<service_name>', svcName, file);
})
console.log(logger.green("CHECK orcinus env"));
if(!fs.existsSync("orcinus.yml")){
s.cp("-rf",tsstackdir + "/" + stack + "/orcinus.yml",project+"/orcinus.yml")
s.sed('-i', '<project_name>', projectName, "orcinus.yml");
s.sed('-i', '<service_name>', svcName, "orcinus.yml");
}
console.log(logger.green("GENERATE code......"));
templateString = fs.readFileSync(tsstackdir + "/" + stack + "/main.ejs", 'utf-8');
let templateRender = ejs.render(templateString, { data: queryList });
if (stack == "go") {
writeFile(project + "/main.go", templateRender);
console.log(logger.green("INITIALIZE Go lang dep......"));
const init = spawn("make", ["init-dev"]);
init.stdout.on('data', (data) => {
console.log(logger.green(`stdout: ${data}`));
});
init.stderr.on('data', (data) => {
console.log(logger.red(`stderr: ${data}`));
});
init.on('close', (code) => {
console.log(logger.green(`child process exited with code ${code}`));
});
}