-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathupdate-app-html.js
44 lines (36 loc) · 1.35 KB
/
update-app-html.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
const PLACEHOLDER_FOR_SCRIPTS = /<!-- Add js below -->.*<!-- Add js above -->/s
const PLACEHOLDER_FOR_CSS = /<!-- Insert CSS below -->.*<!-- Insert CSS above -->/s
const SCRIPT_REGEX = /<div id="root"><\/div>(.*)<\/body>/
const CSS_REGEX = /<\/title>(.*)<\/head>/
const builtFilePath = './build/index.html'
const appFilePath = './app/index.html'
const fs = require('fs')
const htmlData = fs.readFileSync(builtFilePath).toString()
const appHTML = fs.readFileSync(appFilePath).toString()
const scriptMatches = htmlData.match(SCRIPT_REGEX)
const processedScript = scriptMatches[1].replace(/\/static/g, 'static')
const cssMatches = htmlData.match(CSS_REGEX)
const processedCSS = cssMatches[1].replace(/\/static/g, 'static')
let processedAppHTML = appHTML.replace(
PLACEHOLDER_FOR_SCRIPTS,
`
<!-- Add js below -->
${processedScript}
<!-- Add js above -->
`
)
processedAppHTML = processedAppHTML.replace(
PLACEHOLDER_FOR_CSS,
`
<!-- Insert CSS below -->
${processedCSS}
<!-- Insert CSS above -->
`
)
// remove empty lines
processedAppHTML = processedAppHTML.replace(/^[\s]+$/gm, '').replace(/[\n]+/g, '\n')
fs.writeFileSync(appFilePath, processedAppHTML)
const filename = `index-v${(Math.random() * 100).toFixed(0)}.html`
const newAppHTMLPath = `./app/${filename}`
fs.writeFileSync(newAppHTMLPath, processedAppHTML)
console.log(`Widget path: /${filename}`)