-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_worker.js
96 lines (73 loc) · 2.92 KB
/
generate_worker.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
#!/usr/bin/env node
import fs from 'node:fs'
import pages from './includes/pages.js'
import versions from './includes/versions.js'
import {pagePath, generatePage, generateWithIncludes, getStylesheet} from './includes/generatePage.js'
function escapeTemplateLiteral(value) {
return value
.replace(/\\/g, '\\\\')
.replace(/`/g, '\\`')
.replace(/\$\{/g, '\\${')
}
let worker = fs.readFileSync('worker_template.js').toString()
worker = worker.replace('__VERSIONS_LATEST__', `\`${versions.latest}\``)
let versionsHashesString = []
for (let version in versions.hashes) {
const hash = versions.hashes[version]
versionsHashesString.push(`versions.hashes['${version}'] = \`${hash}\``)
}
versionsHashesString = versionsHashesString.join(`\n`)
worker = worker.replace(`__VERSIONS_HASHES__`, versionsHashesString)
let versionsContentString = []
for (let version in versions.content) {
const content = versions.content[version]
versionsContentString.push(`versions.content['${version}'] = \`${escapeTemplateLiteral(content)}\``)
}
versionsContentString = versionsContentString.join(`\n\n`)
worker = worker.replace(`__VERSIONS_CONTENT__`, versionsContentString)
let scriptsString = []
const scriptsDir = fs.readdirSync(`scripts`)
scriptsDir.forEach((file) => {
const script = fs.readFileSync(`scripts/${file}`).toString()
scriptsString.push(`scripts['${file}'] = \`${escapeTemplateLiteral(script)}\``)
})
scriptsString = scriptsString.join(`\n\n`)
worker = worker.replace(`__SCRIPTS__`, scriptsString)
const stylesheet = getStylesheet()
worker = worker.replace(`__STYLESHEET__`, `\`${escapeTemplateLiteral(stylesheet)}\``)
let pagesString = []
for (let pageKey in pages) {
const pageValue = pages[pageKey].replace(/src="\/trusted-by\//g, 'src="https://assets.instant.page/trusted-by/')
pagesString.push(`pages['${pageKey}'] = \`${escapeTemplateLiteral(pageValue)}\``)
}
pagesString = pagesString.join(`\n\n`)
worker = worker.replace('__PAGES__', pagesString)
const generatePageFunctionsString = [pagePath.toString(), generatePage.toString(), generateWithIncludes.toString()].join('\n\n')
worker = worker.replace('__GENERATE_PAGE__', generatePageFunctionsString)
let configPath
if (fs.existsSync('./config.json')) {
configPath = './config.json'
}
else {
configPath = './config.sample.json'
}
const config = fs.readFileSync(configPath, {encoding: 'utf-8'})
worker = worker.replace('__CONFIG__', config)
if (!fs.existsSync('build')) {
fs.mkdirSync('build')
}
;(function renamePreviousWorkerBuild() {
const oldPath = 'build/worker.js'
if (!fs.existsSync(oldPath)) {
return
}
const oldWorker = fs.readFileSync(oldPath).toString()
if (oldWorker == worker) {
return
}
const {mtime} = fs.statSync(oldPath)
const isoMtime = mtime.toISOString().replaceAll(/[-:]/g, '').replace(/\.[0-9]{3}Z$/, 'Z')
const newPath = `build/worker ${isoMtime}.js`
fs.renameSync(oldPath, newPath)
})()
fs.writeFileSync('build/worker.js', worker)