-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·183 lines (133 loc) · 4.25 KB
/
app.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
'use strict';
const { exec } = require('child_process');
const { generateFunctionalities } = require('./features/firebase-crud')
const express = require('express')
const app = express()
const port = 8080;
var AdmZip = require('adm-zip');
const archiver = require('archiver')
const db = require('./config');
const fs = require('fs');
// end routes
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
var cors = require('cors');
app.use(cors())
app.get('/generate/:apiId', async (req, res) => {
let apiId = req.params.apiId;
try {
let api = (await db.doc('api/' + req.params.apiId).get()).data().swaggerDoc
api = JSON.stringify(JSON.parse(api), null, 4);
await fs.writeFileSync('apiId.json', api);
} catch (error) {
console.log('error writing file', error)
res.json({ success: false, message: 'There was a problem fetching the API document. ' })
}
console.log('got api file')
try {
// console.log('step', errorStep)
await exec(`java -jar openapi-generator-cli.jar generate \
-i apiId.json \
-g nodejs-express-server \
-o tmp/gen`, async (error, stdout, stderr) => {
// console.log(`stdout: ${stdout}`);
// console.error(`stderr: ${stderr}`);
// errorStep = '1'
// console.log('step', errorStep)
let e = error;
let so = stdout;
let si = stderr;
// START CODE GEN
const myPackages = `
{
"name": "${apiId}",
"version": "1.0.0",
"description": "No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)",
"main": "index.js",
"scripts": {
"prestart": "npm install",
"start": "node index.js"
},
"keywords": [
"openapi-generator",
"openapi"
],
"license": "Unlicense",
"private": true,
"dependencies": {
"body-parser": "^1.19.0",
"camelcase": "^5.3.1",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"express": "^4.16.4",
"express-openapi-validator": "^3.9.1",
"firebase-admin": "^9.4.1",
"js-yaml": "^3.3.0",
"ono": "^5.0.1",
"openapi-sampler": "^1.0.0-beta.15",
"swagger-ui-express": "^4.0.2",
"winston": "^3.2.1"
},
"devDependencies": {
"axios": "^0.19.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-plugin-import": "^2.17.2",
"mocha": "^7.1.1"
},
"eslintConfig": {
"env": {
"node": true
}
}
}
`
// where the magic happens
await generateFunctionalities(apiId)
fs.writeFileSync('tmp/gen/package.json', myPackages, 'utf-8');
// END CODE GEN
// creating archives
var zip = new AdmZip();
zip.addLocalFolder('tmp/gen');
// or write everything to disk
zip.writeZip("tmp/output.zip", (e) => {
if (e) {
console.error('ADMZIP error', e)
}
});
// let zipFileContents = zip.toBuffer();
// const fileName = 'generate.zip';
// const fileType = 'application/zip';
// // res.writeHead(200, {
// // 'Content-Disposition': `attachment; filename="${fileName}"`,
// // 'Content-Type': fileType,
// // })
// res.set({
// 'Content-Type': 'application/zip',
// 'Content-Disposition': `attachment; filename="${fileName}"`
// })
// return res.sendFile(__dirname + '/tmp/output.zip');
var stat = fs.statSync(__dirname + '/tmp/output.zip');
res.writeHead(200, {
'Content-Type': 'application/zip',
'Content-Length': stat.size
});
var readStream = fs.createReadStream(__dirname + '/tmp/output.zip');
// We replaced all the event handlers with a simple call to readStream.pipe()
readStream.pipe(res);
})
} catch (error) {
console.error(error)
res.send({ success: false, message: 'there was an error: ' + + ' ' + error })
}
});
app.get('/', (req, res) => {
res.send('API Generator Running....')
});
module.exports = {
app
};
// /Users/marcusgallegos/api-builder-71719-7bf68aea3f5b.json