-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.js
74 lines (70 loc) · 2.06 KB
/
build.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
const path = require('path');
const {JSDOM} = require('jsdom');
const fs = require('fs-extra');
const klaw = require('klaw');
const templates = [
'base.html',
'challenge.html',
'challenges.html',
'confirm.html',
'errors/403.html',
'errors/404.html',
'errors/429.html',
'errors/500.html',
'errors/502.html',
'login.html',
'notifications.html',
'page.html',
'register.html',
'reset_password.html',
'scoreboard.html',
'settings.html',
'setup.html',
'teams/join_team.html',
'teams/new_team.html',
'teams/private.html',
'teams/public.html',
'teams/teams.html',
'teams/team_enrollment.html',
'users/private.html',
'users/public.html',
'users/users.html',
];
(async () => {
if (!(await fs.pathExists('dist'))) {
// eslint-disable-next-line no-console
console.error('Build contents by `nuxt generate` first');
return;
}
await fs.remove('tsgctf');
await fs.mkdirp('tsgctf/static');
await fs.mkdirp('tsgctf/templates');
const walker = klaw('dist');
walker.on('data', async (item) => {
if (!item.stats.isDirectory() && !item.path.endsWith('.html')) {
const newPath = item.path.includes('_nuxt')
? path.join('tsgctf/static', path.relative(path.join(__dirname, 'dist/themes/tsgctf/static'), item.path))
: path.join('tsgctf/static', path.relative(path.join(__dirname, 'dist'), item.path));
if (item.path.endsWith('OneSignalSDKWorker.js')) {
const worker = await fs.readFile(item.path);
const newWorker = worker.toString().replace('/sw.js', '/themes/tsgctf/static/sw.js');
await fs.outputFile(newPath, newWorker);
} else {
fs.copy(item.path, newPath);
}
}
});
await new Promise((resolve) => {
walker.on('end', resolve);
});
const data = await fs.readFile('dist/index.html');
const dom = new JSDOM(data);
const meta = dom.window.document.createElement('meta');
meta.name = 'csrf-token';
meta.content = '{{Session.nonce}}';
dom.window.document.querySelector('head').appendChild(meta);
const html = dom.serialize();
for (const template of templates) {
await fs.outputFile(path.join('tsgctf/templates', template), html);
}
})();