forked from victrme/Bonjourr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.config.js
397 lines (326 loc) · 10.9 KB
/
build.config.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import fs, { mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
import { cp, copyFile, writeFile, watch } from 'node:fs/promises'
import { extname } from 'node:path'
import { exec } from 'node:child_process'
import { argv } from 'node:process'
import http from 'node:http'
import esbuild from 'esbuild'
const args = argv.slice(2)
const platform = args[0]
const PLATFORMS = ['chrome', 'firefox', 'safari', 'edge', 'online']
const PLATFORM_FIREFOX = platform === 'firefox'
const PLATFORM_CHROME = platform === 'chrome'
const PLATFORM_SAFARI = platform === 'safari'
const PLATFORM_EDGE = platform === 'edge'
const PLATFORM_ONLINE = platform === 'online'
const PLATFORM_EXT = !PLATFORM_ONLINE
const env = args[1] ?? 'prod'
const ENV_DEV = env === 'dev'
const ENV_PROD = env === 'prod'
const ENV_TEST = env === 'test'
const paths = {
shared: {
scripts: ['src/scripts/index.ts', `release/${platform}/src/scripts/main.js`],
styles: ['src/styles/style.css', `release/${platform}/src/styles/style.css`],
locales: ['_locales/', `release/${platform}/_locales/`],
htmls: {
index: ['src/index.html', `release/${platform}/index.html`],
settings: ['src/settings.html', `release/${platform}/settings.html`],
},
assets: {
interface: ['src/assets/interface', `release/${platform}/src/assets/interface`],
weather: ['src/assets/weather', `release/${platform}/src/assets/weather`],
favicons: {
ico: ['src/assets/favicon.ico', `release/${platform}/src/assets/favicon.ico`],
128: ['src/assets/favicon-128x128.png', `release/${platform}/src/assets/favicon-128x128.png`],
512: ['src/assets/favicon-512x512.png', `release/${platform}/src/assets/favicon-512x512.png`],
},
},
},
extension: {
manifest: [`src/manifests/${platform}.json`, `release/${platform}/manifest.json`],
scripts: {
background: ['src/scripts/services/background.js', `release/${platform}/src/scripts/background.js`],
storage: ['src/scripts/services/webext-storage.js', `release/${platform}/src/scripts/webext-storage.js`],
},
},
online: {
icon: ['src/assets/apple-touch-icon.png', 'release/online/src/assets/apple-touch-icon.png'],
manifest: ['src/manifests/manifest.webmanifest', 'release/online/manifest.webmanifest'],
screenshots: ['src/assets/screenshots', 'release/online/src/assets/screenshots'],
serviceworker: ['src/scripts/services/service-worker.js', 'release/online/service-worker.js'],
},
edge: {
favicon: ['src/assets/monochrome.png', 'release/edge/src/assets/monochrome.png'],
},
}
// Main
console.clear()
if (args.includes('translate')) {
updateTranslations()
}
if ((ENV_DEV || ENV_TEST) && PLATFORM_ONLINE) {
liveServer()
}
if (ENV_DEV && PLATFORMS.includes(platform)) {
builder()
watcher()
}
if (ENV_PROD && PLATFORMS.includes(platform)) {
builder()
}
if (ENV_PROD && platform === undefined) {
if (fs.existsSync('./release')) {
fs.rmSync('./release/', { recursive: true })
}
for (const platform of PLATFORMS) {
exec(`node ./build.config.js ${platform} prod`, (error, stdout, _) => {
error ? console.error(error) : console.log(`${stdout.replace('\n', '')} <- ${platform}`)
})
}
}
// Build or Watch
function builder() {
console.time('Built in')
addDirectories()
html()
styles()
assets()
scripts()
locales()
manifests()
console.timeEnd('Built in')
}
function watcher() {
watchTasks('_locales', (filename) => {
locales()
})
watchTasks('src', (filename) => {
if (filename.includes('.html')) html()
if (filename.includes('styles/')) styles()
if (filename.includes('assets/')) assets()
if (filename.includes('scripts/')) scripts()
if (filename.includes('manifests/')) manifests()
})
}
function addDirectories() {
try {
if (readdirSync('release')?.includes(platform)) {
return
}
} catch (error) {
console.error('First build')
}
mkdirSync(`release/${platform}/src/assets`, { recursive: true })
mkdirSync(`release/${platform}/src/scripts`, { recursive: true })
mkdirSync(`release/${platform}/src/styles`, { recursive: true })
}
// Tasks
function html() {
let data = readFileSync(paths.shared.htmls.index[0], 'utf8')
const icon = '<link rel="apple-touch-icon" href="src/assets/apple-touch-icon.png" />'
const manifest = '<link rel="manifest" href="manifest.webmanifest">'
const storage = '<script src="src/scripts/webext-storage.js"></script>'
if (PLATFORM_ONLINE) data = data.replace('<!-- icon -->', icon)
if (PLATFORM_ONLINE) data = data.replace('<!-- manifest -->', manifest)
if (PLATFORM_EXT) data = data.replace('<!-- webext-storage -->', storage)
if (PLATFORM_EDGE) data = data.replace('favicon.ico', 'monochrome.png')
writeFile(paths.shared.htmls.index[1], data)
copyFile(...paths.shared.htmls.settings)
}
function styles() {
const [input, output] = paths.shared.styles
esbuild.buildSync({
entryPoints: [input],
outfile: output,
bundle: true,
minify: ENV_PROD,
})
}
function scripts() {
const [input, output] = paths.shared.scripts
esbuild.buildSync({
entryPoints: [input],
outfile: output,
format: 'iife',
bundle: true,
sourcemap: ENV_DEV,
minifySyntax: ENV_PROD,
minifyWhitespace: ENV_PROD,
define: {
ENV: `"${env.toUpperCase()}"`,
},
})
if (PLATFORM_ONLINE) copyFile(...paths.online.serviceworker)
if (PLATFORM_EXT) copyFile(...paths.extension.scripts.background)
if (PLATFORM_EXT) copyFile(...paths.extension.scripts.storage)
}
function assets() {
copyDir(...paths.shared.assets.interface)
copyDir(...paths.shared.assets.weather)
copyFile(...paths.shared.assets.favicons.ico)
copyFile(...paths.shared.assets.favicons[128])
copyFile(...paths.shared.assets.favicons[512])
if (PLATFORM_ONLINE) copyDir(...paths.online.screenshots)
if (PLATFORM_ONLINE) copyFile(...paths.online.icon)
if (PLATFORM_EDGE) copyFile(...paths.edge.favicon)
if (!PLATFORM_EDGE) copyFile(...paths.shared.assets.favicons.ico)
}
function manifests() {
if (PLATFORM_ONLINE) copyFile(...paths.online.manifest)
if (PLATFORM_EXT) copyFile(...paths.extension.manifest)
}
function locales() {
const langs = readdirSync('_locales').filter((dir) => dir !== '.DS_Store')
const [input, output] = paths.shared.locales
for (const lang of langs) {
mkdirSync(output + lang, { recursive: true })
copyFile(`${input}${lang}/translations.json`, `${output}${lang}/translations.json`)
if (PLATFORM_EXT) {
copyFile(`${input}${lang}/messages.json`, `${output}${lang}/messages.json`)
}
}
}
// Node stuff
async function watchTasks(path, callback) {
// debounce because IDEs do multiple fast saves which triggers watcher
const events = watch(path, { recursive: true })
let debounce = 0
for await (const event of events) {
clearInterval(debounce)
debounce = setTimeout(() => debounceCallback(event.filename), 30)
}
function debounceCallback(filename) {
console.time('Built in')
callback(filename.replaceAll('\\', '/')) // windows back slashes :(
console.timeEnd('Built in')
}
}
function liveServer() {
const server = http.createServer()
const PORT = 8080
const contentTypeList = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'text/javascript',
'.ico': 'image/x-icon',
'.svg': 'image/svg+xml',
'.png': 'image/png',
}
server.listen(PORT, () => {
console.log(`Live server: http://127.0.0.1:${PORT}`)
})
server.on('request', (req, res) => {
const path = `release/online/${req.url === '/' ? 'index.html' : req.url}`
const filePath = new URL(path, import.meta.url)
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.end('Not Found')
return
}
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/html' })
res.end('<h1>500 Internal Server Error</h1>')
return
}
const contentType = contentTypeList[extname(filePath.toString())]
res.writeHead(200, {
'Content-Type': contentType || 'application/octet-stream',
'cache-control': 'no-cache',
})
res.end(data)
})
})
})
}
function copyDir(...args) {
// cosmetic abstraction, i'm sure its fine
cp(...args, { recursive: true })
}
// Auto translator tool
async function updateTranslations() {
const supportedLangs = await fetchDeeplSupportedLangs()
const enDict = JSON.parse(readFileSync(`./_locales/en/translations.json`, 'utf8'))
const langs = readdirSync('./_locales/')
const translations = []
for (const lang of langs) {
if (lang === 'en') {
continue
}
translations.push(translateFile(lang, enDict, supportedLangs))
}
await Promise.all(translations)
}
async function translateFile(lang, enDict, supportedLangs) {
let sanitizedLang = lang
if (lang === 'gr') sanitizedLang = 'el'
if (lang === 'jp') sanitizedLang = 'ja'
if (lang === 'zh_CN') sanitizedLang = 'zh'
if (lang === 'zh_HK') sanitizedLang = 'zh'
if (lang === 'es_ES') sanitizedLang = 'es'
if (lang === 'pt_BR') sanitizedLang = 'pt-BR'
if (lang === 'pt_PT') sanitizedLang = 'pt-PT'
const supported = supportedLangs.includes(sanitizedLang)
const langDict = JSON.parse(readFileSync(`./_locales/${lang}/translations.json`, 'utf8'))
const newDict = {}
let removed = 0
let added = 0
// Remove keys not found in "english" translation file
for (const key of Object.keys(langDict)) {
if (enDict[key]) {
newDict[key] = langDict[key]
continue
}
removed++
}
// Add keys & translate new stuff
for (const key of Object.keys(enDict)) {
const trn = newDict[key]
if (!trn) {
if (supported) {
newDict[key] = await fetchDeeplTranslation(key, sanitizedLang)
} else {
newDict[key] = key
}
added++
}
}
// Order translations
const keylist = new Set()
const enKeys = [...Object.keys(enDict)]
const sortOrder = (a, b) => enKeys.indexOf(a) - enKeys.indexOf(b)
JSON.stringify(newDict, (key, value) => {
return keylist.add(key), value
})
const stringified = JSON.stringify(newDict, Array.from(keylist).sort(sortOrder), 2)
// Write to file
writeFileSync(`./_locales/${lang}/translations.json`, stringified)
// Log
console.log(`${lang.slice(0, 2)}: [removed: ${removed}, added: ${added}, translated: ${supported ? 'yes' : 'no'}]`)
}
async function fetchDeeplSupportedLangs() {
const auth = await getDeeplAuth()
const headers = { Authorization: auth, 'User-Agent': 'Bonjourr/19.2.0' }
const resp = await fetch('https://api-free.deepl.com/v2/languages?type=target', { headers })
const json = await resp.json()
return Object.values(json).map((val) => val.language.toLowerCase())
}
async function fetchDeeplTranslation(text, lang) {
const formData = new FormData()
formData.append('source_lang', 'en')
formData.append('target_lang', lang)
formData.append('text', text)
const options = {
method: 'POST',
body: formData,
headers: { 'User-Agent': 'Bonjourr/19.2.0', Authorization: await getDeeplAuth() },
}
const resp = await fetch('https://api-free.deepl.com/v2/translate', options)
const json = await resp.json()
return json.translations[0]?.text
}
async function getDeeplAuth() {
return `DeepL-Auth-Key ${await (await fetch('https://deepl.bonjourr.workers.dev/')).text()}`
}