forked from swagger-autogen/swagger-autogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swagger-autogen.js
93 lines (85 loc) · 4.56 KB
/
swagger-autogen.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
require('./src/prototype-functions')
const fs = require('fs')
const merge = require('lodash.merge');
const swaggerTags = require('./src/swagger-tags')
const handleFiles = require('./src/handle-files')
const statics = require('./src/statics')
const { platform } = process
const symbols = platform === 'win32' ? { success: '', failed: '' } : { success: '✔', failed: '✖' }
module.exports = function (args) {
let options = { language: null, disableLogs: false, disableWarnings: false }
let recLang = null
if (args && typeof args === 'string') // will be deprecated in a future version
recLang = args
else if (args && typeof args === 'object')
options = { ...options, ...args }
swaggerTags.setLanguage(recLang || options.language || 'en-US')
return async (outputFile, endpointsFiles, data) => {
return new Promise(async (resolve) => {
try {
if (!outputFile)
throw console.error("\nError: 'outputFile' was not specified.")
if (!endpointsFiles)
throw console.error("\nError: 'endpointsFiles' was not specified.")
// Checking if endpoint files exist
for (var idx = 0; idx < endpointsFiles.length; ++idx) {
var file = endpointsFiles[idx]
var extension = await handleFiles.getExtension(file)
endpointsFiles[idx] = file + extension
if (!fs.existsSync(file + extension)) {
throw console.error("\nError: File not found: '" + file + "'")
}
}
const objDoc = { ...statics.TEMPLATE, ...data, paths: {} }
if (!objDoc.info.version)
objDoc.info.version = statics.TEMPLATE.info.version
if (!objDoc.info.title)
objDoc.info.title = statics.TEMPLATE.info.title
if (!objDoc.info.description)
objDoc.info.description = statics.TEMPLATE.info.description
for (let file = 0; file < endpointsFiles.length; file++) {
const filePath = endpointsFiles[file]
const resp = await fs.existsSync(filePath)
if (!resp) {
console.error("\nError: Endpoint file not found => " + "'" + filePath + "'")
if (!options.disableLogs)
console.log('Swagger-autogen:', "\x1b[31m", 'Failed ' + symbols.failed, "\x1b[0m")
return resolve(false)
}
let relativePath = filePath.split('/')
if (relativePath.length > 1) {
relativePath.pop()
relativePath = relativePath.join('/')
} else
relativePath = null
let obj = await handleFiles.readEndpointFile(filePath, '', relativePath, [])
if (obj === false) {
if (!options.disableLogs)
console.log('Swagger-autogen:', "\x1b[31m", 'Failed ' + symbols.failed, "\x1b[0m")
return resolve(false)
}
objDoc.paths = merge({}, objDoc.paths, obj);
}
let constainXML = false
if (JSON.stringify(objDoc).includes('application/xml')) // TODO: improve this
constainXML = true
swaggerTags.setDefinitions(objDoc.definitions)
Object.keys(objDoc.definitions).forEach(definition => {
if (constainXML)
objDoc.definitions[definition] = { ...swaggerTags.formatDefinitions(objDoc.definitions[definition], {}, constainXML), xml: { name: definition } }
else
objDoc.definitions[definition] = { ...swaggerTags.formatDefinitions(objDoc.definitions[definition], {}, constainXML) }
})
let dataJSON = JSON.stringify(objDoc, null, 2)
fs.writeFileSync(outputFile, dataJSON)
if (!options.disableLogs)
console.log('Swagger-autogen:', "\x1b[32m", 'Success ' + symbols.success, "\x1b[0m")
return resolve({ success: true, data: objDoc })
} catch (err) {
if (!options.disableLogs)
console.log('Swagger-autogen:', "\x1b[31m", 'Failed ' + symbols.failed, "\x1b[0m")
return resolve({ success: false, data: null })
}
})
}
}