forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-json-files.ts
More file actions
94 lines (82 loc) · 2.88 KB
/
prepare-json-files.ts
File metadata and controls
94 lines (82 loc) · 2.88 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
import type { LocaleObject } from '@nuxtjs/i18n'
import * as path from 'node:path'
import * as fs from 'node:fs/promises'
import { currentLocales, lunariaJSONFiles } from '../config/i18n.ts'
import { deepCopy } from '@intlify/shared'
const destFolder = path.resolve('lunaria/files')
const localesFolder = path.resolve('i18n/locales')
const defaultLocale = currentLocales.find(l => l.code === 'en-US')
if (!defaultLocale?.name) {
throw new Error('Default locale en-US not found or has no name')
}
export { lunariaJSONFiles }
export const sourceLocale = {
label: defaultLocale.name,
lang: defaultLocale.code,
}
const filteredLocales = currentLocales.filter(
(l): l is typeof l & { name: string } => l.code !== 'en-US' && typeof l.name === 'string',
)
const firstLocale = filteredLocales[0]
if (!firstLocale) {
throw new Error('No locales found besides en-US')
}
export const locales: [{ label: string; lang: string }, ...{ label: string; lang: string }[]] = [
{ label: firstLocale.name, lang: firstLocale.code },
...filteredLocales.slice(1).map(l => ({
label: l.name,
lang: l.code,
})),
]
export async function prepareJsonFiles(): Promise<void> {
await fs.rm(destFolder, { recursive: true, force: true })
await fs.mkdir(destFolder)
await Promise.all(currentLocales.map(l => mergeLocale(l)))
}
type NestedObject = Record<string, unknown>
export async function mergeLocaleObject(
locale: LocaleObject,
options: { copy?: boolean } = {},
): Promise<NestedObject | undefined> {
const { copy = false } = options
const files = locale.files ?? []
if (locale.file || files.length === 1) {
const json =
(locale.file ? getFileName(locale.file) : undefined) ??
(files[0] ? getFileName(files[0]) : undefined)
if (!json) return undefined
if (copy) {
await fs.cp(path.resolve(`${localesFolder}/${json}`), path.resolve(`${destFolder}/${json}`))
return undefined
}
return await loadJsonFile<NestedObject>(json)
}
const firstFile = files[0]
if (!firstFile) return undefined
const source = await loadJsonFile<NestedObject>(getFileName(firstFile))
let currentSource: unknown
for (let i = 1; i < files.length; i++) {
const file = files[i]
if (!file) continue
currentSource = await loadJsonFile(getFileName(file))
deepCopy(currentSource, source)
}
return source
}
async function loadJsonFile<T = unknown>(name: string): Promise<T> {
return JSON.parse(await fs.readFile(path.resolve(`${localesFolder}/${name}`), 'utf8'))
}
function getFileName(file: string | { path: string }): string {
return typeof file === 'string' ? file : file.path
}
async function mergeLocale(locale: LocaleObject): Promise<void> {
const source = await mergeLocaleObject(locale, { copy: true })
if (!source) {
return
}
await fs.writeFile(
path.resolve(`${destFolder}/${locale.code}.json`),
`${JSON.stringify(source, null, 2)}\n`,
'utf-8',
)
}