-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
181 lines (169 loc) · 5.84 KB
/
scripts.js
File metadata and controls
181 lines (169 loc) · 5.84 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const fs = require('fs-extra')
const path = require('path')
const script = process.argv[2]
if (!script) {
console.log('Usage: node ./scripts <script>')
process.exit(1)
}
const fns = {
clean () {
fs.removeSync('dist/js')
fs.removeSync('dist/css')
fs.removeSync('dist.tgz')
fs.removeSync('dist/compat.json')
fs.removeSync('dist/build.json')
fs.removeSync('lib')
try {
const htmlFiles = fs.readdirSync('dist').filter(f => f.endsWith('.html'))
for (const f of htmlFiles) fs.removeSync(`dist/${f}`)
// eslint-disable-next-line no-empty
} catch (e) {}
},
'clean:cache' () {
fs.removeSync('.webpack-cache')
},
async pack () {
const tar = require('tar')
try {
const htmlFiles = fs.readdirSync('dist').filter(f => f.endsWith('.html')).map(f => `dist/${f}`)
const files = process.env.PUBLIC_PATH ? [ ...htmlFiles, 'dist/build.json', 'lib' ] : [ 'dist/js', 'dist/css', 'lib', ...htmlFiles ]
await tar.c({ gzip: true, file: 'dist.tgz' }, files)
} catch (e) {
console.log(`Failed creating pack: ${e}`)
process.exit(-1)
}
},
'kas-mock' () {
const Koa = require('koa')
const Router = require('koa-router')
const app = new Koa()
const router = new Router()
app.use(router.routes()).use(router.allowedMethods())
router.post('/api/auth/query_information', ctx => {
console.log('query-information')
ctx.set('Access-Control-Allow-Origin', '*')
// I have no idea why KAS responses are so ugly
ctx.body = {
status: 0,
result: {
status: 0,
result: {
avatar: 'https://keeer.net/img/logo/dark-square.jpg',
nickname: 'KEEER',
keeer_id: 'keeer',
},
},
}
})
router.post('/api/auth/examine_token', ctx => ctx.body = {
status: 0,
result: { status: 0 },
})
app.listen(8081)
console.log('Listening on http://localhost:8081/')
},
async 'build:html' () {
const ejs = require('ejs')
const { messages } = require('./locale')
const languages = Object.keys(messages)
let lang
const $t = m => {
const slices = m.split('.')
let message = messages[lang]
for (const s of slices) message = message[s] || m
return message
}
const ejsEntries = [ 'index', 'set-id', '404', '500' ]
const data = { $t }
const opts = {
root: './ejs',
rmWhitespace: true,
async: true,
}
for (const entry of ejsEntries) {
for (lang of languages) {
fs.writeFileSync(`./dist/${lang}-${entry}.html`, await ejs.renderFile(`./ejs/${entry}.ejs`, data, opts))
}
}
},
'build:compat' () {
require('./src/bootstrap')(true)
const plugins = require('./src/plugin').plugins
const themes = require('./src/theme').themes
const Form = require('./src/form').Form
const form = new Form({ pages: [] })
const requiredPlugins = plugins.filter(p => p.config.required)
const nonRequiredPlugins = plugins.filter(p => !p.config.required)
const objectsToDfs = [ ...nonRequiredPlugins, ...themes ]
const table = {}
const sort = ([ theme, ...plugin ]) => ([ theme, ...plugin.sort((a, b) => plugins.indexOf(a) - plugins.indexOf(b)) ])
const stringify = ([ theme, ...plugin ]) => ([ `theme:${theme.config.code}`, ...plugin.map(o => o.config.code) ].join('/'))
const dfs = (base = [ themes.find(t => t.config.default), ...requiredPlugins ]) => {
base = sort(base)
const stringBase = stringify(base)
console.log('[dfs]', stringBase)
const [ baseTheme, ...basePlugins ] = base
for (const object of objectsToDfs) {
form.options.plugins = basePlugins
form.options.theme = baseTheme.config.code
if (!table[stringBase]) table[stringBase] = []
if (table[stringBase].includes(object)) continue
const contains = base.includes(object)
if (!contains && form.isApplicable(object)) {
table[stringBase].push(object)
if (object.is === 'theme') {
dfs([ object, ...basePlugins ])
} else if (object.is === 'plugin') {
dfs([ ...base, object ])
}
}
if (contains && object.is === 'plugin' && !form.isRequired(object)) {
table[stringBase].push(object)
dfs([ baseTheme, ...basePlugins.filter(p => p !== object) ])
}
}
}
dfs()
for (const k in table) table[k] = table[k].map(o => o.is === 'theme' ? `theme:${o.config.code}` : o.config.code).join('/')
require('fs-extra').ensureDirSync('dist')
require('fs').writeFileSync('dist/compat.json', JSON.stringify(table))
},
async 'build:meta' () {
await fs.ensureDir('dist')
await fs.writeFile('dist/build.json', JSON.stringify({
js: await fs.readdir('dist/js'),
css: await fs.readdir('dist/css'),
}))
},
async 'deploy-oss' () {
const oss = require('ali-oss')
const cfg = {
accessKeyId: process.env.ACCESS_KEY_ID,
accessKeySecret: process.env.ACCESS_KEY_SECRET,
region: process.env.REGION,
bucket: process.env.BUCKET,
}
if (Object.values(cfg).some(x => typeof x !== 'string')) throw new Error('Invalid config')
const store = new oss(cfg)
const deploy = async (localPath, remotePath) => {
for (const file of await fs.readdir(localPath)) {
await store.put(path.join(remotePath, file), path.join(localPath, file), {
headers: { 'Cache-Control': 'public, max-age=31536000' },
})
}
}
await deploy(path.resolve(__dirname, 'dist/js'), 'js')
await deploy(path.resolve(__dirname, 'dist/css'), 'css')
},
}
if (!(script in fns)) {
console.log(`Script ${script} not found. All scripts:`)
console.log(Object.keys(fns).join(', '))
process.exit(1)
}
;(async () => {
try { await fns[script]() } catch (e) {
console.error(e)
process.exit(1)
}
})()