2018-04-14 22:50:16 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2018-09-23 19:26:01 +00:00
|
|
|
import crypto from 'crypto'
|
|
|
|
import fs from 'fs'
|
|
|
|
import pify from 'pify'
|
|
|
|
import path from 'path'
|
|
|
|
import { themes } from '../routes/_static/themes.js'
|
|
|
|
import { fromPairs } from 'lodash-es'
|
|
|
|
|
2018-04-14 22:50:16 +00:00
|
|
|
const readFile = pify(fs.readFile.bind(fs))
|
|
|
|
const writeFile = pify(fs.writeFile.bind(fs))
|
|
|
|
|
|
|
|
async function main () {
|
2018-09-23 19:26:01 +00:00
|
|
|
let inlineScriptPath = path.join(__dirname, '../inline-script.js')
|
|
|
|
let code = await readFile(inlineScriptPath, 'utf8')
|
|
|
|
|
|
|
|
code = code.replace('process.env.THEME_COLORS', JSON.stringify(fromPairs(themes.map(_ => ([_.name, _.color])))))
|
|
|
|
code = `(function () {'use strict'\n${code}})()`
|
2018-04-14 22:50:16 +00:00
|
|
|
|
2018-09-23 19:26:01 +00:00
|
|
|
let checksum = crypto.createHash('sha256').update(code).digest('base64')
|
2018-04-14 22:50:16 +00:00
|
|
|
|
|
|
|
let checksumFilepath = path.join(__dirname, '../inline-script-checksum.json')
|
2018-08-30 04:42:57 +00:00
|
|
|
await writeFile(checksumFilepath, JSON.stringify({ checksum }), 'utf8')
|
2018-04-14 22:50:16 +00:00
|
|
|
|
|
|
|
let html2xxFilepath = path.join(__dirname, '../templates/2xx.html')
|
|
|
|
let html2xxFile = await readFile(html2xxFilepath, 'utf8')
|
|
|
|
html2xxFile = html2xxFile.replace(
|
|
|
|
/<!-- insert inline script here -->[\s\S]+<!-- end insert inline script here -->/,
|
2018-09-23 19:26:01 +00:00
|
|
|
'<!-- insert inline script here --><script>' + code + '</script><!-- end insert inline script here -->'
|
2018-04-14 22:50:16 +00:00
|
|
|
)
|
|
|
|
await writeFile(html2xxFilepath, html2xxFile, 'utf8')
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|