forked from fastify/fastify-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare-swagger-ui.js
39 lines (34 loc) · 1.14 KB
/
prepare-swagger-ui.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
const fs = require('fs')
const fse = require('fs-extra')
const swaggerUiAssetPath = require('swagger-ui-dist').getAbsoluteFSPath()
const resolve = require('path').resolve
fse.emptyDirSync(resolve('./static'))
// since the original swagger-ui-dist folder contains non UI files
const filesToCopy = ['favicon-16x16.png',
'favicon-32x32.png',
'index.html',
'oauth2-redirect.html',
'swagger-ui-bundle.js',
'swagger-ui-bundle.js.map',
'swagger-ui-standalone-preset.js',
'swagger-ui-standalone-preset.js.map',
'swagger-ui.css',
'swagger-ui.css.map',
'swagger-ui.js',
'swagger-ui.js.map']
filesToCopy.forEach(filename => {
fse.copySync(`${swaggerUiAssetPath}/${filename}`, resolve(`./static/${filename}`))
})
const newIndex = fs.readFileSync(resolve('./static/index.html'), 'utf8')
.replace('window.ui = ui', `window.ui = ui
function resolveUrl (url) {
const anchor = document.createElement('a')
anchor.href = url
return anchor.href
}`)
.replace(
/url: "(.*)",/,
`url: resolveUrl('./json'),
oauth2RedirectUrl: resolveUrl('./oauth2-redirect.html'),`
)
fse.writeFileSync(resolve('./static/index.html'), newIndex)