-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
301 lines (254 loc) · 9.02 KB
/
cli.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env node
let fs = require('fs-extra');
let argv = require('minimist')(process.argv.slice(2));
let colors = require('colors');
let inquirer = require('inquirer');
let package = __dirname;
let cwd = process.cwd();
let apimConfig = null;
let operation = argv._[0] ?? '';
let version = argv._[1] ?? '';
let childProcess = require('child_process');
(function() {
var oldSpawn = childProcess.spawn;
function mySpawn() {
var result = oldSpawn.apply(this, arguments);
return result;
}
childProcess.spawn = mySpawn;
})();
let child = childProcess.spawn('gulp', ['serve'], {
stdio: 'inherit',
cwd: package,
shell: true,
env: {
...process.env,
REAL_CWD: cwd
}
});
child.on('error', function (err) {
throw err
});
child.on('close', function (code) {
console.log(code)
});
child.on('data', function (code) {
console.log(code)
});
try {
switch (operation) {
case 'init':
init();
return;
case 'help':
help();
return;
}
loadConfig();
switch (operation) {
case 'list':
list();
break;
default:
if (operation !== '') {
console.log(colors.red('Unknown command \'' + operation + '\''));
}
help();
}
} catch (err) {
console.error(colors.red(err.message));
}
function help() {
let cli = 'apim';
let opt_file = '{openapi}'.bold;
let opt_base = '--base={version}'.gray;
console.log('Usage: ' + cli + ' ' + '{operation} '.bold + opt_file + ' ['.gray + opt_base + ']'.gray);
console.log('');
console.log(_ident(4) + cli + ' ' + 'help'.bold + _ident(36) + ' » prints this message');
console.log(_ident(4) + cli + ' ' + 'init'.bold + _ident(36) + ' » initialize the manager on this project');
console.log(_ident(4) + cli + ' ' + 'list'.bold + _ident(36) + ' » list all versions managed');
console.log(_ident(4) + cli + ' ' + 'generate'.bold + _ident(2) + opt_file + _ident(1) + ' ['.gray + opt_base + ']'.gray + _ident(1) + ' » (re)generate the API based on specified OpenAPI schema');
console.log('');
}
function init() {
const apimConfig = {};
inquirer.prompt({
name: 'side',
message: 'Which side is this project?',
type: 'list',
choices: ['server', 'client']
}).then(({side}) => {
apimConfig.side = side;
const languages = side === 'server' ? [
'php'
] : [
'php'
];
inquirer.prompt([
{
name: 'lang',
message: 'Which language should be used?',
type: 'list',
choices: languages
},
{
name: 'root',
message: 'Where API\'s should be located?',
type: 'input',
default: '/api'
}
]).then(({lang, root}) => {
apimConfig.lang = lang;
apimConfig.root = root;
console.log('Take a look on your chooses:');
console.log(JSON.stringify(apimConfig, null, 4));
inquirer.prompt({
name: 'confirm',
message: 'Everything is ok?',
type: 'confirm'
}).then(({confirm}) => {
if (!confirm) {
console.log('\n\n\n');
return init();
}
fs.writeFile(cwd + '/apim.config.json', JSON.stringify(apimConfig, null, 4));
})
});
});
}
function loadConfig() {
if (fs.existsSync(cwd + '/apim.config.json')) {
apimConfig = JSON.parse(fs.readFileSync(cwd + '/apim.config.json').toString());
return;
}
throw new Error('Configuration file not found. Did you initialize the API manager by calling \'apim init\'?')
}
async function list() {
if (!fs.pathExistsSync(cwd + apimConfig.root)) {
console.log('No managed APIs found'.gray);
return;
}
console.log('Managed API versions:'.bold);
const versions = await (await fs.readdir(cwd + apimConfig.root, {withFileTypes: true}))
.filter(dirent => dirent.isDirectory() && dirent.name.charAt(0) !== '.' && fs.pathExistsSync(cwd + apimConfig.root + dirent.name + '/api.json'))
.map(dirent => dirent.name);
for (let version of versions) {
console.log(version.green);
}
}
async function _generate(api) {
const root = cwd + apimConfig.root + '/' + api.info.version;
const rootLang = apimConfig.lang.charAt(0) === '/'
? cwd + apimConfig.lang
: __dirname + "/" + apimConfig.lang;
console.log(rootLang);
if (!fs.pathExistsSync(rootLang + '/structure.json')) {
console.error('Tree map not found. Aborting...'.red);
process.exit();
}
const structure = await fs.readFile(rootLang + '/structure.json');
const { treeMap, noRegenerate } = JSON.parse(structure);
await _createFolder(root);
iterateTreeMap(treeMap, null, api).then(() => fs.writeFile(root + '/api.json', JSON.stringify(api, null, 4)));
async function iterateTreeMap(map, currentKey, data, path, dataKey) {
if (!path) path = [];
for (let i in map) {
const options = map[i];
if (i.charAt(0) === '$') {
await iterateTreeMap(map[i], i, data[i.replace('$', '')], path);
continue;
}
if (i.charAt(0) === '[') {
for (let k in data) {
if (i.indexOf('.') !== -1) {
let single = {};
single[i.replace('[', '').replace(']', '')] = map[i];
await iterateTreeMap(single, currentKey, data[k], path, k);
continue;
}
const tag = k.substr(1, k.length).split('/').shift();
let single = {};
single[tag] = map[i];
await iterateTreeMap(single, currentKey, data[k], path, k);
}
continue;
}
if (i.indexOf('.') !== -1) {
const parsed = await parseArgs(i, data, dataKey);
if (parsed !== false) {
const concat = path.join('/') + '/' + parsed;
await fs.writeFile(root + concat, JSON.stringify(data, null, 4));
continue;
}
let rename = options.rename ?? null;
if (rename) {
const parsed = await parseArgs(rename, data, dataKey);
if (parsed !== false) {
rename = parsed;
}
}
const concat = path.join('/') + '/' + (rename ?? i);
let canRegenerate = true;
for (let nr of noRegenerate) {
if ((nr.charAt(0) === '/' && concat.indexOf(nr) === 0) ||
((rename ?? i) === nr)) {
canRegenerate = false;
break;
}
}
if (!canRegenerate) {
const fileAlreadyCreated = await fs.pathExists(root + concat);
if (fileAlreadyCreated) {
continue;
}
}
const template = await fs.readFile(rootLang + '/' + i);
let d = data;
d.key = dataKey;
await fs.writeFile(root + concat, '')(d);
continue;
} else {
await createFolder(i, data);
}
}
async function parseArgs(original, data, parentKey) {
let parsed = original;
const placeholders = parsed.match(/({.*})/g);
if (placeholders !== null) {
for (let p of placeholders) {
const dataTreePath = p.replace('{', '').replace('}', '').split('.');
let value = data;
for (let deep of dataTreePath) {
value = deep === 'key' ? parentKey : (value[deep] ?? '');
}
parsed = parsed.replace(p, value);
}
return parsed;
}
return false;
}
async function createFolder(i, data) {
path = [path.join('/')];
path.push(i);
const concat = path.join('/');
await _createFolder(root + concat);
await iterateTreeMap(map[i], i, data, path);
path = [path[0]];
}
}
}
async function _createFolder(path) {
const exists = await fs.pathExists(path);
if (!exists) {
await fs.mkdir(path, {recursive: true});
}
return true;
}
function _ident(times) {
let space = ' ';
let identation = '';
for (let i = 0; i < times; i++) {
identation += space;
}
return identation;
}