forked from Script-Hub-Org/Script-Hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preview.js
96 lines (89 loc) · 2.98 KB
/
preview.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
const fs = require('fs')
const path = require('path')
const Koa = require('koa')
const { scriptMap, scriptMapBeta } = require('./scriptMap.js')
const PORT = process.env.PORT || 9000
const BETA_PORT = process.env.BETA_PORT || 9001
const HOST = process.env.HOST || '0.0.0.0'
const EXPORT_HTML = process.env.EXPORT_HTML
const generateHTML = (config, info = '') => {
const content = fs.readFileSync(config[1], { encoding: 'utf8' })
let html =
content.match(/<!DOCTYPE html>([\s\S]*?<body style="margin-bottom: 80px;"><script>)/i)[1] +
'</script><script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>' +
content.match(/(<div id="app">[\s\S]*?)<\/html>/i)[1].replace("${$.getEnv() || ''}", 'Node.js' + info)
const rewriteParser = fs.readFileSync(config[2], { encoding: 'utf8' })
const ruleParser = fs.readFileSync(config[3], { encoding: 'utf8' })
const scriptConverter = fs.readFileSync(config[4], { encoding: 'utf8' })
return html.replace(
'"__SCRIPT__"',
JSON.stringify({ scriptConverter, rewriteParser, ruleParser, scriptMap: config[5] })
)
}
if (EXPORT_HTML) {
// fs.rmdirSync(path.join(__dirname, './dist'), { recursive: true })
fs.mkdirSync(path.join(__dirname, './dist/beta'), { recursive: true })
fs.writeFileSync(
path.join(__dirname, './dist/index.html'),
generateHTML([
scriptMap,
path.join(__dirname, './script-hub.js'),
path.join(__dirname, 'Rewrite-Parser.js'),
path.join(__dirname, 'rule-parser.js'),
path.join(__dirname, 'script-converter.js'),
]),
{
encoding: 'utf8',
}
)
fs.writeFileSync(
path.join(__dirname, './dist/beta/index.html'),
generateHTML(
[
scriptMapBeta,
path.join(__dirname, './script-hub.beta.js'),
path.join(__dirname, 'Rewrite-Parser.beta.js'),
path.join(__dirname, 'rule-parser.beta.js'),
path.join(__dirname, 'script-converter.beta.js'),
],
'(β)'
),
{
encoding: 'utf8',
}
)
} else {
const app = new Koa()
app.use(async ctx => {
const html = generateHTML([
scriptMap,
path.join(__dirname, './script-hub.js'),
path.join(__dirname, 'Rewrite-Parser.js'),
path.join(__dirname, 'rule-parser.js'),
path.join(__dirname, 'script-converter.js'),
])
ctx.type = 'html'
ctx.body = html
})
app.listen(PORT, HOST, async ctx => {
console.log(`listening on port ${HOST}:${PORT}, http://127.0.0.1:${PORT}`)
})
const appBeta = new Koa()
appBeta.use(async ctx => {
const html = generateHTML(
[
scriptMapBeta,
path.join(__dirname, './script-hub.beta.js'),
path.join(__dirname, 'Rewrite-Parser.beta.js'),
path.join(__dirname, 'rule-parser.beta.js'),
path.join(__dirname, 'script-converter.beta.js'),
],
'(β)'
)
ctx.type = 'html'
ctx.body = html
})
appBeta.listen(BETA_PORT, HOST, async ctx => {
console.log(`β listening on port ${HOST}:${BETA_PORT}, http://127.0.0.1:${BETA_PORT}`)
})
}