|
| 1 | +// Expects to be run with tsx |
| 2 | +// @ts-check |
| 3 | + |
| 4 | +import ts from "typescript"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { fileURLToPath } from "node:url"; |
| 7 | +import { Logger, Options, TSConfigReader } from "typedoc"; |
| 8 | +import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 9 | +import { ok } from "node:assert"; |
| 10 | + |
| 11 | +const browserBundleFolders = [ |
| 12 | + "/utils-common/", |
| 13 | + "/models/", |
| 14 | + "/serialization/", |
| 15 | +]; |
| 16 | + |
| 17 | +const localesDir = join( |
| 18 | + fileURLToPath(import.meta.url), |
| 19 | + "../../src/lib/internationalization/locales", |
| 20 | +); |
| 21 | + |
| 22 | +const distDir = join( |
| 23 | + fileURLToPath(import.meta.url), |
| 24 | + "../../dist/browser-locales", |
| 25 | +); |
| 26 | + |
| 27 | +const options = new Options(); |
| 28 | +const tsconfigReader = new TSConfigReader(); |
| 29 | +tsconfigReader.read(options, new Logger(), process.cwd()); |
| 30 | + |
| 31 | +/** @type {ts.LanguageServiceHost} */ |
| 32 | +const host = { |
| 33 | + getScriptFileNames: () => options.getFileNames().slice(), |
| 34 | + getScriptVersion: () => "unused", |
| 35 | + getScriptSnapshot: (fileName) => { |
| 36 | + if (!existsSync(fileName)) return undefined; |
| 37 | + return ts.ScriptSnapshot.fromString( |
| 38 | + readFileSync(fileName, "utf-8"), |
| 39 | + ); |
| 40 | + }, |
| 41 | + getCurrentDirectory: () => process.cwd(), |
| 42 | + getCompilationSettings: () => options.getCompilerOptions(), |
| 43 | + getDefaultLibFileName: (opts) => ts.getDefaultLibFilePath(opts), |
| 44 | + fileExists: ts.sys.fileExists, |
| 45 | + readFile: ts.sys.readFile, |
| 46 | + readDirectory: ts.sys.readDirectory, |
| 47 | + directoryExists: ts.sys.directoryExists, |
| 48 | + getDirectories: ts.sys.getDirectories, |
| 49 | +}; |
| 50 | + |
| 51 | +const service = ts.createLanguageService( |
| 52 | + host, |
| 53 | + ts.createDocumentRegistry(), |
| 54 | +); |
| 55 | + |
| 56 | +const program = service.getProgram(); |
| 57 | +ok(program, "Failed to get program for i18n analysis"); |
| 58 | + |
| 59 | +const sf = program.getSourceFile(join(localesDir, "en.cts")); |
| 60 | +ok(sf, "Failed to get source file"); |
| 61 | + |
| 62 | +const moduleSymbol = program.getTypeChecker().getSymbolAtLocation(sf); |
| 63 | +const translatable = moduleSymbol?.exports?.get( |
| 64 | + "export=" as ts.__String, |
| 65 | +); |
| 66 | +ok(translatable, "Failed to get translatable symbol"); |
| 67 | + |
| 68 | +ok(translatable.valueDeclaration && ts.isExportAssignment(translatable.valueDeclaration)); |
| 69 | +ok(ts.isAsExpression(translatable.valueDeclaration.expression)); |
| 70 | +ok( |
| 71 | + ts.isObjectLiteralExpression( |
| 72 | + translatable.valueDeclaration.expression.expression, |
| 73 | + ), |
| 74 | +); |
| 75 | +const translatableObj = translatable.valueDeclaration.expression.expression; |
| 76 | + |
| 77 | +const bundleUsedTranslationKeys: string[] = []; |
| 78 | + |
| 79 | +translatableObj.forEachChild((child) => { |
| 80 | + ok(ts.isPropertyAssignment(child)); |
| 81 | + const refs = service.getReferencesAtPosition( |
| 82 | + sf.fileName, |
| 83 | + child.getStart(), |
| 84 | + ); |
| 85 | + |
| 86 | + if (refs?.some(ref => browserBundleFolders.some(f => ref.fileName.includes(f)))) { |
| 87 | + bundleUsedTranslationKeys.push(child.name.getText()); |
| 88 | + } |
| 89 | +}); |
| 90 | + |
| 91 | +service.dispose(); |
| 92 | + |
| 93 | +const enLocale = (await import(join(localesDir, "en.cts"))).default; |
| 94 | + |
| 95 | +rmSync(distDir, { recursive: true, force: true }); |
| 96 | +mkdirSync(distDir, { recursive: true }); |
| 97 | + |
| 98 | +for (const locale of readdirSync(localesDir)) { |
| 99 | + console.log(`Processing ${locale}`); |
| 100 | + |
| 101 | + const browserTranslations = {}; |
| 102 | + const translations = (await import(join(localesDir, locale))).default; |
| 103 | + for (const key of bundleUsedTranslationKeys) { |
| 104 | + browserTranslations[key] = translations[key] || enLocale[key]; |
| 105 | + } |
| 106 | + |
| 107 | + writeFileSync( |
| 108 | + join(distDir, locale.replace(".cts", ".js")), |
| 109 | + `export default ${JSON.stringify(browserTranslations, null, 4)}\n`, |
| 110 | + ); |
| 111 | + |
| 112 | + writeFileSync( |
| 113 | + join(distDir, locale.replace(".cts", ".d.ts")), |
| 114 | + `const translations: Record<string, string>;\nexport default translations;\n`, |
| 115 | + ); |
| 116 | +} |
0 commit comments