|
11 | 11 | * long spew of "this string is not translated" and replacing it with a |
12 | 12 | * summary of how many strings are missing with respect to the source locale. |
13 | 13 | * |
14 | | - * @import { ConfigFile } from "@lit/localize-tools/lib/types/config.js" |
15 | 14 | * @import { Stats } from "node:fs"; |
| 15 | + * @import { RuntimeOutputConfig } from "@lit/localize-tools/lib/types/modes.js" |
16 | 16 | */ |
17 | 17 |
|
18 | | -import { spawnSync } from "node:child_process"; |
19 | | -import { readFileSync, statSync } from "node:fs"; |
20 | | -import path from "node:path"; |
| 18 | +import * as fs from "node:fs/promises"; |
| 19 | +import path, { resolve } from "node:path"; |
21 | 20 |
|
| 21 | +import { generatePseudoLocaleModule } from "./pseudolocalize.mjs"; |
| 22 | + |
| 23 | +// import localizeRules from "../lit-localize.json" with { type: "json" }; |
| 24 | +import { ConsoleLogger } from "#logger/node"; |
22 | 25 | import { PackageRoot } from "#paths/node"; |
23 | 26 |
|
24 | | -/** |
25 | | - * @type {ConfigFile} |
26 | | - */ |
27 | | -const localizeRules = JSON.parse( |
28 | | - readFileSync(path.join(PackageRoot, "lit-localize.json"), "utf-8"), |
| 27 | +import { readConfigFileAndWriteSchema } from "@lit/localize-tools/lib/config.js"; |
| 28 | +import { RuntimeLitLocalizer } from "@lit/localize-tools/lib/modes/runtime.js"; |
| 29 | + |
| 30 | +const logger = ConsoleLogger.child({ name: "Locales" }); |
| 31 | + |
| 32 | +const localizeRules = readConfigFileAndWriteSchema(path.join(PackageRoot, "lit-localize.json")); |
| 33 | + |
| 34 | +if (localizeRules.interchange.format !== "xliff") { |
| 35 | + logger.error("Unsupported interchange type, expected 'xliff'"); |
| 36 | + process.exit(1); |
| 37 | +} |
| 38 | + |
| 39 | +const XLIFFPath = resolve(PackageRoot, localizeRules.interchange.xliffDir); |
| 40 | + |
| 41 | +const EmittedLocalesDirectory = resolve( |
| 42 | + PackageRoot, |
| 43 | + /** @type {string} */ (localizeRules.output.outputDir), |
29 | 44 | ); |
30 | 45 |
|
| 46 | +async function cleanEmittedLocales() { |
| 47 | + logger.info("♻️ Cleaning previously emitted locales..."); |
| 48 | + logger.info(`♻️ ${EmittedLocalesDirectory}`); |
| 49 | + |
| 50 | + await fs.rm(EmittedLocalesDirectory, { |
| 51 | + recursive: true, |
| 52 | + force: true, |
| 53 | + }); |
| 54 | + |
| 55 | + await fs.mkdir(EmittedLocalesDirectory, { |
| 56 | + recursive: true, |
| 57 | + }); |
| 58 | + |
| 59 | + logger.info(`♻️ Done!`); |
| 60 | +} |
| 61 | + |
31 | 62 | /** |
| 63 | + * Returns false if: the expected XLF file doesn't exist, The expected |
| 64 | + * generated file doesn't exist, or the XLF file is newer (has a higher date) |
| 65 | + * than the generated file. The missing XLF file is important enough it |
| 66 | + * generates a unique error message and halts the build. |
32 | 67 | * |
33 | | - * @param {string} loc |
34 | | - * @returns {boolean} |
| 68 | + * @param {string} localeCode |
| 69 | + * @returns {Promise<boolean>} |
35 | 70 | */ |
36 | | -function generatedFileIsUpToDateWithXliffSource(loc) { |
37 | | - const xliff = path.join("./xliff", `${loc}.xlf`); |
38 | | - const gened = path.join("./src/locales", `${loc}.ts`); |
39 | | - |
40 | | - // Returns false if: the expected XLF file doesn't exist, The expected |
41 | | - // generated file doesn't exist, or the XLF file is newer (has a higher date) |
42 | | - // than the generated file. The missing XLF file is important enough it |
43 | | - // generates a unique error message and halts the build. |
| 71 | +async function checkIfEmittedFileCurrent(localeCode) { |
| 72 | + const xliffPath = path.join(XLIFFPath, `${localeCode}.xlf`); |
| 73 | + const emittedPath = path.join(EmittedLocalesDirectory, `${localeCode}.ts`); |
44 | 74 |
|
45 | 75 | /** |
46 | 76 | * @type {Stats} |
47 | 77 | */ |
48 | | - let xlfStat; |
| 78 | + let xliffStat; |
49 | 79 |
|
50 | 80 | try { |
51 | | - xlfStat = statSync(xliff); |
| 81 | + xliffStat = await fs.stat(xliffPath); |
52 | 82 | } catch (_error) { |
53 | | - console.error(`lit-localize expected '${loc}.xlf', but XLF file is not present`); |
| 83 | + logger.error(`XLIFF source file missing for locale '${localeCode}': ${xliffPath}`); |
54 | 84 | process.exit(1); |
55 | 85 | } |
56 | 86 |
|
57 | 87 | /** |
58 | 88 | * @type {Stats} |
59 | 89 | */ |
60 | | - let genedStat; |
| 90 | + let emittedStat; |
61 | 91 |
|
62 | 92 | // If the generated file doesn't exist, of course it's not up to date. |
63 | 93 | try { |
64 | | - genedStat = statSync(gened); |
| 94 | + emittedStat = await fs.stat(emittedPath); |
65 | 95 | } catch (_error) { |
66 | 96 | return false; |
67 | 97 | } |
68 | 98 |
|
69 | | - // if the generated file is the same age or newer (date is greater) than the xliff file, it's |
| 99 | + // Possible if the script was interrupted between clearing and generating. |
| 100 | + if (emittedStat.size === 0) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + // If the emitted file is the same age or newer (date is greater) than the xliff file, it's |
70 | 105 | // presumed to have been generated by that file and is up-to-date. |
71 | | - return genedStat.mtimeMs >= xlfStat.mtimeMs; |
| 106 | + return emittedStat.mtimeMs >= xliffStat.mtimeMs; |
72 | 107 | } |
73 | 108 |
|
74 | | -// For all the expected files, find out if any aren't up-to-date. |
75 | | -const upToDate = localizeRules.targetLocales.reduce( |
76 | | - (acc, loc) => acc && generatedFileIsUpToDateWithXliffSource(loc), |
77 | | - true, |
78 | | -); |
| 109 | +/** |
| 110 | + * Checks if all the locale source files are up-to-date with their XLIFF sources. |
| 111 | + * @returns {Promise<boolean>} |
| 112 | + */ |
| 113 | +async function checkIfLocalesAreCurrent() { |
| 114 | + logger.info("Reading locale configuration..."); |
| 115 | + |
| 116 | + const targetLocales = localizeRules.targetLocales.filter((localeCode) => { |
| 117 | + return localeCode !== "pseudo-LOCALE"; |
| 118 | + }); |
| 119 | + |
| 120 | + logger.info(`Checking ${targetLocales.length} source files...`); |
| 121 | + |
| 122 | + let outOfDateCount = 0; |
| 123 | + |
| 124 | + await Promise.all( |
| 125 | + targetLocales.map(async (localeCode) => { |
| 126 | + const current = await checkIfEmittedFileCurrent(localeCode); |
79 | 127 |
|
80 | | -if (!upToDate) { |
81 | | - const status = spawnSync("npm", ["run", "build-locales:build"], { encoding: "utf8" }); |
| 128 | + if (!current) { |
| 129 | + logger.info(`Locale '${localeCode}' is out-of-date.`); |
| 130 | + outOfDateCount++; |
| 131 | + } |
| 132 | + }), |
| 133 | + ); |
82 | 134 |
|
83 | | - // Count all the missing message warnings |
84 | | - const counts = status.stderr.split("\n").reduce((acc, line) => { |
85 | | - const match = /^([\w-]+) message/.exec(line); |
86 | | - if (!match) { |
87 | | - return acc; |
88 | | - } |
89 | | - acc.set(match[1], (acc.get(match[1]) || 0) + 1); |
90 | | - return acc; |
91 | | - }, new Map()); |
| 135 | + return outOfDateCount === 0; |
| 136 | +} |
| 137 | + |
| 138 | +export async function generateLocaleModules() { |
| 139 | + logger.info("Updating pseudo-locale..."); |
| 140 | + await generatePseudoLocaleModule(); |
| 141 | + |
| 142 | + logger.info("Generating locale modules..."); |
92 | 143 |
|
93 | | - const locales = Array.from(counts.keys()); |
94 | | - locales.sort(); |
| 144 | + const localizer = new RuntimeLitLocalizer({ |
| 145 | + ...localizeRules, |
| 146 | + output: /** @type {RuntimeOutputConfig} */ (localizeRules.output), |
| 147 | + }); |
95 | 148 |
|
96 | | - const report = locales |
97 | | - .map((locale) => `Locale '${locale}' has ${counts.get(locale)} missing translations`) |
98 | | - .join("\n"); |
| 149 | + await localizer.build(); |
99 | 150 |
|
100 | | - console.log(`Translation tables rebuilt.\n${report}\n`); |
| 151 | + logger.info("Complete."); |
101 | 152 | } |
102 | 153 |
|
103 | | -console.log("Locale ./src is up-to-date"); |
| 154 | +async function delegateCommand() { |
| 155 | + const command = process.argv[2]; |
| 156 | + |
| 157 | + switch (command) { |
| 158 | + case "--clean": |
| 159 | + return cleanEmittedLocales(); |
| 160 | + case "--check": |
| 161 | + return checkIfLocalesAreCurrent(); |
| 162 | + case "--force": |
| 163 | + return generateLocaleModules(); |
| 164 | + } |
| 165 | + |
| 166 | + const upToDate = await checkIfLocalesAreCurrent(); |
| 167 | + |
| 168 | + if (upToDate) { |
| 169 | + logger.info("Locale is up-to-date!"); |
| 170 | + |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + logger.info("Locale ./src is out-of-date, rebuilding..."); |
| 175 | + |
| 176 | + return generateLocaleModules(); |
| 177 | +} |
| 178 | + |
| 179 | +await delegateCommand() |
| 180 | + .then(() => { |
| 181 | + process.exit(0); |
| 182 | + }) |
| 183 | + .catch((error) => { |
| 184 | + logger.error(`Error during locale build: ${error}`); |
| 185 | + process.exit(1); |
| 186 | + }); |
0 commit comments