-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathsplitter.js
163 lines (143 loc) · 4.83 KB
/
splitter.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
const beautify = require('js-beautify').js;
const fs = require('fs');
const { transform } = require('lebab');
/**
* First letter capitalization, utility function
* @param string
* @returns {string}
*/
function capitalize(string) {
return string[0].toUpperCase() + string.slice(1);
}
/**
* Split the code to create multiple files and directories
* @param {string} source - source code generated by swagger-js-codegen (using special templates!)
* @param {string} className - specified class name
* @param {string} path - path to the project directory, that calls the code generator (__dirname)
* @param {string} dir - name of the container directory
* @returns {array} - array of objects (files and wrapper)
*/
async function split(source, className, path, dir) {
// check if everything we need is there
if (!(source && className && path && dir)) {
throw new Error('Missing some of the required parameters!');
}
try {
// convert ES5 to ES6
const { code, warnings } = transform(source, ['let', 'arrow', 'arrow-return', 'class']);
// show conversion errors and warnings
if (warnings && warnings.length && warnings.length > 0) {
console.log('> codegen-splitter @ ES6 conversion warnings:\n', warnings);
}
// create the source file
fs.writeFileSync(`${__dirname}/source.js`, code, (err) => {
if (err) {
throw new Error(err.message);
}
});
// load source file and get all of the available methods from the class
const Instance = require(`${__dirname}/source.js`)[`${capitalize(className.toLowerCase())}`];
const methods = Object.getOwnPropertyNames(Instance.prototype).filter(m => m !== 'constructor');
// abort everything if there are no methods (i. e. incorrect JSON or something went wrong)
if (methods.length === 0) {
return console.log('> Methods not found');
}
// create new instance of the class, use it as a provider of the code
const provider = new Instance();
// process all of the methods, store code in array
const destinations = [];
methods.forEach((method) => {
let standalone = provider[method].toString();
let functionName = standalone.split('(req, res)')[0];
const destination = functionName.split('_')[0];
functionName = functionName.split('_')[1];
standalone = `async function ${functionName}(req, res) ${standalone.split('(req, res)')[1]}`;
destinations.push({
content: standalone,
file: destination,
function: functionName,
});
});
// create file list
const files = [];
destinations.forEach((f) => {
if (!files.some(entry => entry.file === f.file)) {
files.push({
content: '',
file: f.file,
functions: [],
});
}
});
// building the controllers
files.forEach((file, i) => {
destinations.forEach((d) => {
if (file.file === d.file) {
files[i].content = `${files[i].content}
${d.content}`;
files[i].functions.push(d.function);
}
});
});
// add 'use strict' and exporting
files.forEach((file, i) => {
let fList = '';
file.functions.forEach((f) => {
fList = `${fList}
${f},`;
});
files[i].content = beautify(`'use strict';
/* auto-generated: ${file.file}.controller.js */
${file.content}
module.exports = {
${fList}
};`, { indent_size: 2 });
});
// delete the source file
await fs.unlink(`${__dirname}/source.js`, (err) => {
if (err) {
throw new Error(err.message);
}
});
// make sure container directory exists
const container = `${path}/${dir}`;
if (!fs.existsSync(container)) {
await fs.mkdirSync(container);
}
// create API directories and files
files.forEach((file) => {
// make sure API directory exists
if (!fs.existsSync(`${container}/${file.file}`)) {
fs.mkdirSync(`${container}/${file.file}`);
}
// create file
fs.writeFileSync(`${container}/${file.file}/${file.file}.controller.js`,
beautify(file.content, { indent_size: 2 }),
(fErr) => {
if (fErr) {
throw new Error(fErr.message);
}
});
});
// create API wrapper TODO: what should be inside of the wrapper? What to export?
let wrapper = '';
files.forEach((file) => {
const name = file.file;
wrapper = `${wrapper}
const ${name} = require('./${name}/${name}.controller.js');`;
});
fs.writeFileSync(`${container}/index.js`,
beautify(wrapper, { indent_size: 2 }),
(wErr) => {
if (wErr) {
throw new Error(wErr.message);
}
});
return console.log('> swagger-js-codegen @ Success!');
} catch (err) {
throw new Error(err.message);
}
}
module.exports = {
split,
};