-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.js
176 lines (156 loc) · 3.66 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
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
#!/bin/env node
import {
readdir,
readFile,
writeFile,
} from 'fs/promises';
import {exec as child_process_exec} from 'child_process';
import cssnano from 'cssnano';
const GCC_COMMAND = './node_modules/.bin/google-closure-compiler --';
const TMP_FILE = '/tmp/app.js';
const prod = !!process.env.CI;
const {version} = JSON.parse(
await readFile('./package.json', 'utf8')
);
let languages = ['en'];
if (prod) {
const files = await readdir('./locales');
languages = (
files
.filter(name => name.endsWith('.csv'))
.map(name => name.split('.')[0])
);
}
const exec = cmd => (
new Promise(resolve =>
child_process_exec(
cmd,
(...args) => resolve(args)
)
)
);
async function lang_generate(lang) {
const csv = await readFile(`./locales/${lang}.csv`, 'utf8');
let output = '';
for (const line of csv.split('\n')) {
if (!line) continue;
const index_colon = line.indexOf(': ');
const key = line.substring(0, index_colon);
const value = line.substring(index_colon + 2);
output += `export const locale_${key} = '${value}';\n`;
}
return output;
}
const env_set = async (version, debug, lang, api, api_data) => Promise.all([
writeFile(
'./src/etc/env.js',
`export const VERSION = '${version}';
export const DEBUG = ${debug};
export const LANG = '${lang}';
export const API = '${api}';
export const API_DATA = '${api_data}';
`,
'utf8'
),
writeFile(
'./src/etc/locale.js',
await lang_generate(lang),
'utf8'
),
]);
async function build_css() {
const css_raw = await readFile('./src/app.css', 'utf8');
console.log('css...');
const css_minified = '' + await (
cssnano()
.process(
css_raw,
{from: undefined}
)
);
console.log('css done.');
await writeFile(
'./dist/app.css',
css_minified,
'ascii'
);
}
async function build_js(lang) {
await env_set(
prod ? version : 'dev',
false,
lang,
prod ? '/api/minicraft/' : '//l3p3.de/api/minicraft/',
prod ? '/static/minicraft/' : '//l3p3.de/static/minicraft/'
);
console.log('js pass 1...');
console.log((await exec(
GCC_COMMAND +
[
'assume_function_wrapper',
'compilation_level ADVANCED',
'dependency_mode PRUNE',
'entry_point ./src/app.js',
'js ./src',
'js_output_file ' + TMP_FILE,
'language_in ECMASCRIPT_NEXT',
'language_out ECMASCRIPT6_STRICT',
'module_resolution BROWSER',
'rewrite_polyfills false',
'strict_mode_input',
'use_types_for_optimization',
'warning_level VERBOSE',
]
.join(' --')
))[2]);
console.log('custom transformation...');
await writeFile(
TMP_FILE,
(await readFile(TMP_FILE, 'ascii'))
.split('actions').join('a')
.split('content').join('c')
.split('loaded').join('l')
.split('downl').join('downloaded'),
'ascii'
);
console.log('js pass 2...');
console.log((await exec(
GCC_COMMAND +
[
'assume_function_wrapper',
'compilation_level SIMPLE',
'externs ./src/externs.js',
'js ' + TMP_FILE,
`js_output_file ./dist/app-${lang}.js`,
'language_in ECMASCRIPT6_STRICT',
'language_out ECMASCRIPT6_STRICT',
'rewrite_polyfills false',
'strict_mode_input',
'warning_level VERBOSE',
]
.join(' --')
))[2]);
console.log(`js ${lang} done.`);
await exec('rm ' + TMP_FILE);
}
if(
!(
await exec(GCC_COMMAND + 'version')
)[1].includes('Version: v202')
) {
throw new Error('google closure compiler required!');
}
await exec('rm ./dist/app*');
console.log(`build ${version} (${prod ? 'production' : 'dev'})...`);
try {
const lang_first = languages.shift();
await Promise.all([
build_css(),
build_js(lang_first),
]);
for (const lang of languages) await build_js(lang);
}
finally {
await env_set('dev', true, 'en', '//l3p3.de/api/minicraft/', '//l3p3.de/static/minicraft/');
}
console.log('done.');