diff --git a/make.ts b/make.ts index 6d62be5..8b3f2ad 100755 --- a/make.ts +++ b/make.ts @@ -9,6 +9,8 @@ const targetLangFiles: LanguageInstance[] = [ {suffix: "en_us_nsfw", file: "NSFW (Default Pack)/en_us.json", overrideSuffix: "en_us_sfw", ro: true}, {suffix: "en_us_sfw", file: "SFW (baked in NoN)/en_us.json", ro: true}, {suffix: "zh_cn_nsfw", file: "NSFW (Default Pack)/zh_cn.json", overrideSuffix: "zh_cn_sfw"}, + {suffix: "zh_cn_nsfw_nomeme", file: "zh_cn_nomeme.json", overrideSuffix: "zh_cn_nsfw_nomeme"}, + {suffix: "zh_cn_nsfw_meme", file: "zh_cn_meme.json", overrideSuffix: "zh_cn_nsfw_meme"}, {suffix: "zh_cn_sfw", file: "SFW (baked in NoN)/zh_cn.json"} ] const workFile = "zh_cn.json" @@ -17,6 +19,7 @@ type LanguageInstance = { suffix: string, file: string, data?: Record, + outputBuffer?: KVPair[]; overrideSuffix?: string, override?: LanguageInstance, ro?: boolean // read only, don't edit. @@ -24,6 +27,8 @@ type LanguageInstance = { const mainLangFile = "SFW (baked in NoN)/en_us.json"; +const KEEP_SAME_OVERRIDE_LINES = !(process.env.KEEP_SAME_OVERRIDE_LINES ?? "").match(/^(0|false)$/i) + const targetLangFilesBySuffix: Record = {} for (let l of targetLangFiles) { if (!l.suffix.match(/^[a-z0-9-_]+$/i)) throw new Error(`bad Suffix: ${JSON.stringify(l.suffix)} that not match ^[a-z0-9-_]+$ (ignore case)`) @@ -66,6 +71,7 @@ function genMergedLang(): void { for (let l of targetLangFiles) { let value = l.data?.[key]; if (l.override?.data?.[key] === value) value = undefined + if (typeof value === "undefined" && KEEP_SAME_OVERRIDE_LINES) value = l.override?.data?.[key] if (value) output.push([`${key}.${l.suffix}`, value]) } } @@ -73,21 +79,22 @@ function genMergedLang(): void { } function writeBackLang() { - let mixed = parseOrdered(fs.readFileSync(workFile, "utf-8")); - const buffer: Record = {} - for (const key of Object.keys(targetLangFilesBySuffix)) buffer[key] = [] - - for (const [k, v] of mixed) { - const [key, suffix] = k.split(/\.(?=[^.]*$)/); - if (!buffer[suffix]) throw new Error(`can't parse the suffix: ${JSON.stringify(suffix)} from key: ${JSON.stringify(k)}`) - buffer[suffix].push([key, v]) + const mergedKeys: Record = JSON.parse(fs.readFileSync(workFile, "utf-8")); + for (let l of targetLangFiles) { + l.outputBuffer = [] + } + for (let key of keyOrder) { + for (let l of targetLangFiles) { + let s = mergedKeys[`${key}.${l.suffix}`] + if (typeof s !== "string" && KEEP_SAME_OVERRIDE_LINES && l.override?.suffix) s = mergedKeys[`${key}.${l.override.suffix}`] + if (typeof s === "string") l.outputBuffer?.push([key, s]) + } } - for (let suffix in buffer) { - if (targetLangFilesBySuffix[suffix].ro) continue - fs.writeFileSync(targetLangFilesBySuffix[suffix].file, stringify(buffer[suffix])) + for (let l of targetLangFiles) { + if (l.ro || !l.outputBuffer) continue + fs.writeFileSync(l.file, stringify(l.outputBuffer)) } - } ;(() => {