better code: split files
Massimo Melina committed
Apr 5, 2023 at 14:33 UTC
40bb9c0f0a7b020c814b3e2ebbdd1de1d0f2a587
3 files changed
+59
-52
src/api.lang.ts
+4
-51
@@ -5,14 +5,8 @@ import _ from 'lodash'
5
import glob from 'fast-glob'
6
import { readFile, rm, writeFile } from 'fs/promises'
7
import { HTTP_BAD_REQUEST, HTTP_NOT_ACCEPTABLE, HTTP_SERVER_ERROR } from './const'
8
-import { tryJson, wantArray } from './misc'
9
-import { defineConfig } from './config'
10
-import { watchLoad } from './watchLoad'
11
-import Koa from 'koa'
12
-
13
-const PREFIX = 'hfs-lang-'
14
-const SUFFIX = '.json'
15
-const EMBEDDED = 'en'
8
+import { tryJson } from './misc'
9
+import { code2file, file2code } from './lang'
10
11
const apis: ApiHandlers = {
12
@@ -21,7 +15,7 @@ const apis: ApiHandlers = {
15
doAtStart: async list => {
16
for await (let name of glob.stream(code2file('*'))) {
17
name = String(name)
24
- const code = name.slice(PREFIX.length, -SUFFIX.length)
18
+ const code = file2code(name)
19
try {
20
const data = JSON.parse(await readFile(name, 'utf8'))
21
list.add({ code, ..._.omit(data, 'translate') })
@@ -46,8 +40,7 @@ const apis: ApiHandlers = {
40
41
async add_langs({ langs }) {
42
for (let [code, content] of Object.entries(langs)) {
49
- if (code.endsWith(SUFFIX)) // filename, actually
50
- code = code.slice(PREFIX.length, -SUFFIX.length)
43
+ code = file2code(code)
44
validateCode(code)
45
const fn = code2file(code)
46
const s = content = String(content)
@@ -62,48 +55,8 @@ const apis: ApiHandlers = {
55
56
export default apis
57
65
-function code2file(code: string) {
66
- return PREFIX + code.toLowerCase() + SUFFIX
67
-}
68
-
58
function validateCode(code: string) {
59
if (!/^(\w\w)(-\w\w)*$/.test(code))
60
throw new ApiError(HTTP_BAD_REQUEST, 'bad code/filename')
61
}
62
74
-export async function getLangData(ctx: Koa.Context) {
75
- if (forceLangData)
76
- return forceLangData
77
- const ret: any = {}
78
- const csv = String(ctx.query.lang||'') || ctx.get('Accept-Language') || ''
79
- const langs = wantArray(csv.split(',').map(x => x.toLowerCase()))
80
- let i = 0
81
- while (i < langs.length) {
82
- let k = langs[i] || '' // shut up ts
83
- if (!k || k === EMBEDDED) break
84
- try { ret[k!] = JSON.parse(await readFile(`hfs-lang-${k}.json`, 'utf8')) }
85
- catch {
86
- do { k = k.substring(0, k.lastIndexOf('-'))
87
- } while (k && langs.includes(k))
88
- if (k) {
89
- langs[i] = k // overwrite and retry
90
- continue
91
- }
92
- }
93
- i++
94
- }
95
- return ret
96
-}
97
-
98
-let forceLangData: any
99
-let undo: any
100
-defineConfig('force_lang', '', v => {
101
- undo?.()
102
- forceLangData = undefined
103
- if (!v) return
104
- const res = watchLoad(code2file(v), data => {
105
- forceLangData = { [v]: JSON.parse(data) }
106
- })
107
- undo = res.unwatch
108
-})
109
-
src/lang.ts
new
+54
@@ -0,0 +1,54 @@
1
+import Koa from 'koa'
2
+import { wantArray } from './misc'
3
+import { readFile } from 'fs/promises'
4
+import { defineConfig } from './config'
5
+import { watchLoad } from './watchLoad'
6
+
7
+const PREFIX = 'hfs-lang-'
8
+const SUFFIX = '.json'
9
+const EMBEDDED = 'en'
10
+
11
+export function code2file(code: string) {
12
+ return PREFIX + code.toLowerCase() + SUFFIX
13
+}
14
+
15
+export function file2code(fn: string) {
16
+ return fn.slice(PREFIX.length, -SUFFIX.length)
17
+}
18
+
19
+export async function getLangData(ctx: Koa.Context) {
20
+ if (forceLangData)
21
+ return forceLangData
22
+ const ret: any = {}
23
+ const csv = String(ctx.query.lang||'') || ctx.get('Accept-Language') || ''
24
+ const langs = wantArray(csv.split(',').map(x => x.toLowerCase()))
25
+ let i = 0
26
+ while (i < langs.length) {
27
+ let k = langs[i] || '' // shut up ts
28
+ if (!k || k === EMBEDDED) break
29
+ try { ret[k!] = JSON.parse(await readFile(`hfs-lang-${k}.json`, 'utf8')) }
30
+ catch {
31
+ do { k = k.substring(0, k.lastIndexOf('-'))
32
+ } while (k && langs.includes(k))
33
+ if (k) {
34
+ langs[i] = k // overwrite and retry
35
+ continue
36
+ }
37
+ }
38
+ i++
39
+ }
40
+ return ret
41
+}
42
+
43
+let forceLangData: any
44
+let undo: any
45
+defineConfig('force_lang', '', v => {
46
+ undo?.()
47
+ forceLangData = undefined
48
+ if (!v) return
49
+ const res = watchLoad(code2file(v), data => {
50
+ forceLangData = { [v]: JSON.parse(data) }
51
+ })
52
+ undo = res.unwatch
53
+})
54
+
src/serveGuiFiles.ts
+1
-1
@@ -23,7 +23,7 @@ import { subscribe } from 'valtio'
23
import { customHtmlState, getSection } from './customHtml'
24
import _ from 'lodash'
25
import { defineConfig } from './config'
26
-import { getLangData } from './api.lang'
26
+import { getLangData } from './lang'
27
28
// in case of dev env we have our static files within the 'dist' folder'
29
const DEV_STATIC = process.env.DEV ? 'dist/' : ''