@parlajs/compiler 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +21 -2
- package/build/build.d.ts +18 -0
- package/build/build.d.ts.map +1 -0
- package/build/build.js +10 -0
- package/build/cli.d.ts +3 -0
- package/build/cli.d.ts.map +1 -0
- package/build/cli.js +8 -0
- package/build/command.d.ts +13 -0
- package/build/command.d.ts.map +1 -0
- package/build/command.js +156 -0
- package/build/compile/code.d.ts +13 -0
- package/build/compile/code.d.ts.map +1 -0
- package/build/compile/code.js +42 -0
- package/build/compile/compile.d.ts +11 -0
- package/build/compile/compile.d.ts.map +1 -0
- package/build/compile/compile.js +244 -0
- package/build/compile/error.d.ts +4 -0
- package/build/compile/error.d.ts.map +1 -0
- package/build/compile/error.js +3 -0
- package/build/compile/info.d.ts +49 -0
- package/build/compile/info.d.ts.map +1 -0
- package/build/compile/info.js +22 -0
- package/build/compile/scope.d.ts +7 -0
- package/build/compile/scope.d.ts.map +1 -0
- package/build/compile/scope.js +14 -0
- package/build/compile/single.d.ts +11 -0
- package/build/compile/single.d.ts.map +1 -0
- package/build/compile/single.js +36 -0
- package/build/compile/variants.d.ts +16 -0
- package/build/compile/variants.d.ts.map +1 -0
- package/build/compile/variants.js +48 -0
- package/build/compile.d.ts +61 -0
- package/build/compile.d.ts.map +1 -0
- package/build/compile.js +385 -0
- package/build/config/checks.d.ts +21 -0
- package/build/config/checks.d.ts.map +1 -0
- package/build/config/checks.js +97 -0
- package/build/config/config.d.ts +71 -0
- package/build/config/config.d.ts.map +1 -0
- package/build/config/config.js +7 -0
- package/build/config/fallbacks.d.ts +6 -0
- package/build/config/fallbacks.d.ts.map +1 -0
- package/build/config/fallbacks.js +13 -0
- package/build/config/import.d.ts +31 -0
- package/build/config/import.d.ts.map +1 -0
- package/build/config/import.js +115 -0
- package/build/config/load.d.ts +13 -0
- package/build/config/load.d.ts.map +1 -0
- package/build/config/load.js +59 -0
- package/build/config.d.ts +77 -0
- package/build/config.d.ts.map +1 -0
- package/build/config.js +223 -0
- package/build/diagnostics.d.ts +31 -0
- package/build/diagnostics.d.ts.map +1 -0
- package/build/diagnostics.js +41 -0
- package/build/generate/checks.d.ts +9 -0
- package/build/generate/checks.d.ts.map +1 -0
- package/build/generate/checks.js +93 -0
- package/build/generate/chunk.d.ts +11 -0
- package/build/generate/chunk.d.ts.map +1 -0
- package/build/generate/chunk.js +57 -0
- package/build/generate/digits.d.ts +8 -0
- package/build/generate/digits.d.ts.map +1 -0
- package/build/generate/digits.js +57 -0
- package/build/generate/files.d.ts +7 -0
- package/build/generate/files.d.ts.map +1 -0
- package/build/generate/files.js +59 -0
- package/build/generate/locale.d.ts +21 -0
- package/build/generate/locale.d.ts.map +1 -0
- package/build/generate/locale.js +108 -0
- package/build/generate/modules.d.ts +27 -0
- package/build/generate/modules.d.ts.map +1 -0
- package/build/generate/modules.js +61 -0
- package/build/generate/namespace.d.ts +19 -0
- package/build/generate/namespace.d.ts.map +1 -0
- package/build/generate/namespace.js +65 -0
- package/build/generate/namespaces.d.ts +5 -0
- package/build/generate/namespaces.d.ts.map +1 -0
- package/build/generate/namespaces.js +41 -0
- package/build/generate/read.d.ts +21 -0
- package/build/generate/read.d.ts.map +1 -0
- package/build/generate/read.js +115 -0
- package/build/index.d.ts +9 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +7 -0
- package/build/init/detect.d.ts +43 -0
- package/build/init/detect.d.ts.map +1 -0
- package/build/init/detect.js +69 -0
- package/build/init/init.d.ts +12 -0
- package/build/init/init.d.ts.map +1 -0
- package/build/init/init.js +126 -0
- package/build/options/date.d.ts +3 -0
- package/build/options/date.d.ts.map +1 -0
- package/build/options/date.js +77 -0
- package/build/options/literal.d.ts +10 -0
- package/build/options/literal.d.ts.map +1 -0
- package/build/options/literal.js +11 -0
- package/build/options/names.d.ts +5 -0
- package/build/options/names.d.ts.map +1 -0
- package/build/options/names.js +56 -0
- package/build/options/number.d.ts +3 -0
- package/build/options/number.d.ts.map +1 -0
- package/build/options/number.js +17 -0
- package/build/options/options.d.ts +7 -0
- package/build/options/options.d.ts.map +1 -0
- package/build/options/options.js +66 -0
- package/build/options.d.ts +9 -0
- package/build/options.d.ts.map +1 -0
- package/build/options.js +214 -0
- package/build/plurals.d.ts +6 -0
- package/build/plurals.d.ts.map +1 -0
- package/build/plurals.js +50 -0
- package/build/types/names.d.ts +9 -0
- package/build/types/names.d.ts.map +1 -0
- package/build/types/names.js +109 -0
- package/build/types/namespace.d.ts +17 -0
- package/build/types/namespace.d.ts.map +1 -0
- package/build/types/namespace.js +113 -0
- package/build/types/signature.d.ts +8 -0
- package/build/types/signature.d.ts.map +1 -0
- package/build/types/signature.js +87 -0
- package/build/types.d.ts +25 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +250 -0
- package/build/vite.d.ts +9 -0
- package/build/vite.d.ts.map +1 -0
- package/build/vite.js +125 -0
- package/build/watch.d.ts +12 -0
- package/build/watch.d.ts.map +1 -0
- package/build/watch.js +93 -0
- package/package.json +84 -3
- package/src/build.ts +31 -0
- package/src/cli.ts +9 -0
- package/src/command.ts +195 -0
- package/src/compile/code.ts +51 -0
- package/src/compile/compile.ts +289 -0
- package/src/compile/error.ts +3 -0
- package/src/compile/info.ts +63 -0
- package/src/compile/scope.ts +14 -0
- package/src/compile/single.ts +46 -0
- package/src/compile/variants.ts +68 -0
- package/src/config/checks.ts +140 -0
- package/src/config/config.ts +83 -0
- package/src/config/fallbacks.ts +18 -0
- package/src/config/import.ts +146 -0
- package/src/config/load.ts +75 -0
- package/src/diagnostics.ts +98 -0
- package/src/generate/checks.ts +103 -0
- package/src/generate/chunk.ts +73 -0
- package/src/generate/digits.ts +74 -0
- package/src/generate/files.ts +83 -0
- package/src/generate/locale.ts +145 -0
- package/src/generate/modules.ts +94 -0
- package/src/generate/namespace.ts +119 -0
- package/src/generate/namespaces.ts +59 -0
- package/src/generate/read.ts +159 -0
- package/src/index.ts +27 -0
- package/src/init/detect.ts +103 -0
- package/src/init/init.ts +158 -0
- package/src/options/date.ts +87 -0
- package/src/options/literal.ts +21 -0
- package/src/options/names.ts +64 -0
- package/src/options/number.ts +20 -0
- package/src/options/options.ts +75 -0
- package/src/plurals.ts +68 -0
- package/src/types/names.ts +115 -0
- package/src/types/namespace.ts +148 -0
- package/src/types/signature.ts +111 -0
- package/src/vite.ts +143 -0
- package/src/watch.ts +115 -0
package/build/config.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
10
|
+
import { dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
11
|
+
import { pathToFileURL } from 'node:url';
|
|
12
|
+
import { icu } from '@parlajs/format-icu';
|
|
13
|
+
import { mf2 } from '@parlajs/format-mf2';
|
|
14
|
+
import { BUILTIN_FUNCTIONS } from './compile.js';
|
|
15
|
+
export const defaultFormats = { icu, mf2 };
|
|
16
|
+
export class ConfigError extends Error {
|
|
17
|
+
name = 'ConfigError';
|
|
18
|
+
}
|
|
19
|
+
const CONFIG_FILE = 'parla.config.ts';
|
|
20
|
+
/** Types a `parla.config.ts`: `export default defineConfig({ … })`. */
|
|
21
|
+
export function defineConfig(config) {
|
|
22
|
+
return config;
|
|
23
|
+
}
|
|
24
|
+
/** The last load of each config file, for watchers. */
|
|
25
|
+
const loads = new Map();
|
|
26
|
+
/** The load to come, before the import: a file added meanwhile counts at the next one. */
|
|
27
|
+
function nextLoad(file) {
|
|
28
|
+
const last = loads.get(file);
|
|
29
|
+
if (!last) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
const names = new Map([...last.names.keys()].map((dir) => [dir, last.list(dir)]));
|
|
33
|
+
const same = [...names].every(([dir, list]) => list === last.names.get(dir));
|
|
34
|
+
const next = { ...last, names, version: last.version + (same ? 0 : 1) };
|
|
35
|
+
loads.set(file, next);
|
|
36
|
+
return next;
|
|
37
|
+
}
|
|
38
|
+
async function importConfig(file, version) {
|
|
39
|
+
// Both bust Node's module cache, so a watcher sees an edited config, or one that lists new catalogs.
|
|
40
|
+
const { mtimeNs } = statSync(file, { bigint: true });
|
|
41
|
+
const url = `${pathToFileURL(file).href}?t=${mtimeNs}&v=${version}`;
|
|
42
|
+
let mod;
|
|
43
|
+
try {
|
|
44
|
+
mod = await import(__rewriteRelativeImportExtension(url));
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
const { message, stack = '' } = error;
|
|
48
|
+
// Only the stack tells where, a syntax error's included.
|
|
49
|
+
const [position = ''] = /^(?::\d+)+/.exec(stack.split(url)[1] ?? '') ?? [];
|
|
50
|
+
throw new ConfigError(`Cannot load ${file}${position}: ${message}`);
|
|
51
|
+
}
|
|
52
|
+
const config = mod.default;
|
|
53
|
+
if (typeof config !== 'object' || config === null) {
|
|
54
|
+
throw new ConfigError(`${file} must \`export default defineConfig({ … })\``);
|
|
55
|
+
}
|
|
56
|
+
return config;
|
|
57
|
+
}
|
|
58
|
+
function readPackageJson(dir) {
|
|
59
|
+
const file = join(dir, 'package.json');
|
|
60
|
+
return existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : undefined;
|
|
61
|
+
}
|
|
62
|
+
// The adapters first: an app on one may list core too.
|
|
63
|
+
const RUNTIMES = [
|
|
64
|
+
'@parlajs/react',
|
|
65
|
+
'@parlajs/solid',
|
|
66
|
+
'@parlajs/svelte',
|
|
67
|
+
'@parlajs/vue',
|
|
68
|
+
'@parlajs/core',
|
|
69
|
+
];
|
|
70
|
+
function detectRuntime(dir) {
|
|
71
|
+
const pkg = readPackageJson(dir);
|
|
72
|
+
const deps = { ...pkg?.peerDependencies, ...pkg?.devDependencies, ...pkg?.dependencies };
|
|
73
|
+
return RUNTIMES.find((name) => name in deps) ?? '@parlajs/core';
|
|
74
|
+
}
|
|
75
|
+
function checkFunctions(functions) {
|
|
76
|
+
const shadowing = Object.keys(functions).filter((name) => BUILTIN_FUNCTIONS.has(name));
|
|
77
|
+
if (shadowing.length > 0) {
|
|
78
|
+
throw new ConfigError(`"functions" cannot redefine the built-in ${shadowing.map((n) => `:${n}`).join(', ')}`);
|
|
79
|
+
}
|
|
80
|
+
return functions;
|
|
81
|
+
}
|
|
82
|
+
/** Names that become path segments under `outDir`: no separators, no `..`. */
|
|
83
|
+
const NAMESPACE_NAME = /^[\w-]+(?:\.[\w-]+)*$/;
|
|
84
|
+
/** A tag `Intl` takes, such as `zh-Hant-TW`: any other would throw at the first plural or number. */
|
|
85
|
+
function checkLocale(locale) {
|
|
86
|
+
try {
|
|
87
|
+
Intl.getCanonicalLocales(locale);
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
const tag = locale.replaceAll('_', '-');
|
|
91
|
+
const hint = tag === locale ? ' such as "en" or "pt-BR"' : `: write "${tag}"`;
|
|
92
|
+
throw new ConfigError(`Locale "${locale}" is not a locale tag${hint}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function isInside(dir, file) {
|
|
96
|
+
const path = relative(dir, file);
|
|
97
|
+
return path !== '..' && !path.startsWith(`..${sep}`) && !isAbsolute(path);
|
|
98
|
+
}
|
|
99
|
+
/** Names of files: on macOS and Windows one would overwrite the other. */
|
|
100
|
+
function checkCase(what, names) {
|
|
101
|
+
const seen = new Map();
|
|
102
|
+
for (const name of names) {
|
|
103
|
+
const other = seen.get(name.toLowerCase()) ?? name;
|
|
104
|
+
if (other !== name) {
|
|
105
|
+
throw new ConfigError(`${what} "${other}" and "${name}" differ only in case: rename one`);
|
|
106
|
+
}
|
|
107
|
+
seen.set(name.toLowerCase(), name);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/** A fallback outside `locales` lends no text, only its `Intl` formats. */
|
|
111
|
+
function checkFallbacks(fallbacks, { locales, hasCatalog, }) {
|
|
112
|
+
for (const [locale, chain] of Object.entries(fallbacks)) {
|
|
113
|
+
if (!locales.includes(locale)) {
|
|
114
|
+
throw new ConfigError(`"fallbacks" lists "${locale}", which is not in locales`);
|
|
115
|
+
}
|
|
116
|
+
for (const fallback of chain.filter((l) => !locales.includes(l))) {
|
|
117
|
+
if (hasCatalog(fallback)) {
|
|
118
|
+
throw new ConfigError(`"${locale}" falls back to "${fallback}", whose catalogs are not read: add it to locales`);
|
|
119
|
+
}
|
|
120
|
+
if (Intl.NumberFormat.supportedLocalesOf(fallback).length === 0) {
|
|
121
|
+
throw new ConfigError(`"${locale}" falls back to "${fallback}", neither in locales nor known to Intl`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
export async function loadConfig(cwd, configFile) {
|
|
127
|
+
const file = resolve(cwd, configFile ?? CONFIG_FILE);
|
|
128
|
+
if (!configFile && !existsSync(file)) {
|
|
129
|
+
throw new ConfigError(`No ${CONFIG_FILE} in ${cwd}`);
|
|
130
|
+
}
|
|
131
|
+
const load = nextLoad(file);
|
|
132
|
+
const config = await importConfig(file, load?.version ?? 0);
|
|
133
|
+
const root = dirname(file);
|
|
134
|
+
const sourceLocale = config.sourceLocale ?? 'en';
|
|
135
|
+
const locales = config.locales ?? [sourceLocale];
|
|
136
|
+
const fallbacks = config.fallbacks ?? {};
|
|
137
|
+
for (const locale of [
|
|
138
|
+
...locales,
|
|
139
|
+
...Object.keys(fallbacks),
|
|
140
|
+
...Object.values(fallbacks).flat(),
|
|
141
|
+
]) {
|
|
142
|
+
checkLocale(locale);
|
|
143
|
+
}
|
|
144
|
+
checkCase('Locales', locales);
|
|
145
|
+
const repeated = locales.find((l, i) => locales.indexOf(l) !== i);
|
|
146
|
+
if (repeated !== undefined) {
|
|
147
|
+
throw new ConfigError(`Locale "${repeated}" is listed twice`);
|
|
148
|
+
}
|
|
149
|
+
if (!locales.includes(sourceLocale)) {
|
|
150
|
+
throw new ConfigError(`sourceLocale "${sourceLocale}" is not in locales`);
|
|
151
|
+
}
|
|
152
|
+
checkCase('Namespaces', Object.keys(config.namespaces ?? {}));
|
|
153
|
+
const outDir = resolve(root, config.outDir ?? 'src/parla');
|
|
154
|
+
const namespaces = [];
|
|
155
|
+
const catalogs = [];
|
|
156
|
+
for (const [name, ns] of Object.entries(config.namespaces ?? {})) {
|
|
157
|
+
if (!NAMESPACE_NAME.test(name)) {
|
|
158
|
+
throw new ConfigError(`Namespace "${name}" must be letters, digits, "_", "-" or inner dots: it names files`);
|
|
159
|
+
}
|
|
160
|
+
if (!ns.path.includes('{locale}')) {
|
|
161
|
+
throw new ConfigError(`Namespace "${name}" path must contain {locale}`);
|
|
162
|
+
}
|
|
163
|
+
const resolved = {
|
|
164
|
+
name,
|
|
165
|
+
format: ns.format,
|
|
166
|
+
file: (locale) => resolve(root, ns.path.replaceAll('{locale}', locale)),
|
|
167
|
+
};
|
|
168
|
+
const files = locales.map((l) => resolved.file(l));
|
|
169
|
+
if (files.some((f) => isInside(outDir, f))) {
|
|
170
|
+
throw new ConfigError(`Namespace "${name}" has its catalogs in outDir, which holds generated files only: move one of them`);
|
|
171
|
+
}
|
|
172
|
+
namespaces.push(resolved);
|
|
173
|
+
catalogs.push(...files);
|
|
174
|
+
}
|
|
175
|
+
checkFallbacks(fallbacks, {
|
|
176
|
+
locales,
|
|
177
|
+
hasCatalog: (locale) => namespaces.some((ns) => existsSync(ns.file(locale))),
|
|
178
|
+
});
|
|
179
|
+
const watchFiles = [file, ...catalogs];
|
|
180
|
+
const extensions = new Set(catalogs.map((f) => extname(f)));
|
|
181
|
+
const folders = new Set(catalogs.map((f) => dirname(f)));
|
|
182
|
+
const isCatalog = (f) => extensions.has(extname(f));
|
|
183
|
+
const list = (dir) => existsSync(dir)
|
|
184
|
+
? readdirSync(dir)
|
|
185
|
+
.filter((f) => isCatalog(f))
|
|
186
|
+
.toSorted()
|
|
187
|
+
.join('/')
|
|
188
|
+
: '';
|
|
189
|
+
loads.set(file, {
|
|
190
|
+
version: load?.version ?? 0,
|
|
191
|
+
names: new Map([...folders].map((dir) => [dir, load?.names.get(dir) ?? list(dir)])),
|
|
192
|
+
list,
|
|
193
|
+
});
|
|
194
|
+
return {
|
|
195
|
+
root,
|
|
196
|
+
configFile: file,
|
|
197
|
+
locales,
|
|
198
|
+
sourceLocale,
|
|
199
|
+
fallbacks,
|
|
200
|
+
outDir,
|
|
201
|
+
runtime: config.runtime ?? detectRuntime(root),
|
|
202
|
+
functions: checkFunctions(config.functions ?? {}),
|
|
203
|
+
markup: config.markup ?? [],
|
|
204
|
+
formats: { ...defaultFormats, ...config.formats },
|
|
205
|
+
namespaces,
|
|
206
|
+
watchFiles,
|
|
207
|
+
watchFolders: [...folders],
|
|
208
|
+
watches: (f) => watchFiles.includes(f) || (folders.has(dirname(f)) && isCatalog(f)),
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
/** The locales listed for `locale`, then its parents: `zh-Hant-TW` → `zh-Hant`, `zh`. */
|
|
212
|
+
export function parents(config, locale) {
|
|
213
|
+
const subtags = locale.split('-');
|
|
214
|
+
const bases = subtags.map((_, i) => subtags.slice(0, -1 - i).join('-'));
|
|
215
|
+
return [
|
|
216
|
+
...(config.fallbacks[locale] ?? []),
|
|
217
|
+
...bases.filter((base) => config.locales.includes(base)),
|
|
218
|
+
];
|
|
219
|
+
}
|
|
220
|
+
/** Locales to try, in order, when `locale` lacks a key. */
|
|
221
|
+
export function fallbackChain(config, locale) {
|
|
222
|
+
return [...new Set([...parents(config, locale), config.sourceLocale])].filter((l) => l !== locale);
|
|
223
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
interface Located {
|
|
2
|
+
message: string;
|
|
3
|
+
namespace?: string;
|
|
4
|
+
locale?: string;
|
|
5
|
+
key?: string;
|
|
6
|
+
file?: string;
|
|
7
|
+
}
|
|
8
|
+
/** Fails the build. A missing file is one only in the source locale. */
|
|
9
|
+
export interface ErrorDiagnostic extends Located {
|
|
10
|
+
level: 'error';
|
|
11
|
+
code: 'invalid-config' | 'invalid-syntax' | 'invalid-resource' | 'missing-file' | 'param-mismatch' | 'key-conflict' | 'unknown-format';
|
|
12
|
+
}
|
|
13
|
+
/** Fails only `--strict`. */
|
|
14
|
+
export interface WarningDiagnostic extends Located {
|
|
15
|
+
level: 'warning';
|
|
16
|
+
code: 'missing-file' | 'missing-translation' | 'missing-param' | 'missing-plural' | 'unknown-key' | 'namespace-name';
|
|
17
|
+
}
|
|
18
|
+
export type Diagnostic = ErrorDiagnostic | WarningDiagnostic;
|
|
19
|
+
/** A diagnostic before its namespace and locale are known. */
|
|
20
|
+
export type Unplaced = Omit<ErrorDiagnostic, 'namespace' | 'locale'> | Omit<WarningDiagnostic, 'namespace' | 'locale'>;
|
|
21
|
+
export declare function formatDiagnostic(d: Diagnostic, root?: string): string;
|
|
22
|
+
export interface Report {
|
|
23
|
+
level: Diagnostic['level'];
|
|
24
|
+
line: string;
|
|
25
|
+
}
|
|
26
|
+
/** One line per problem: diagnostics that differ only in their key share it. */
|
|
27
|
+
export declare function formatDiagnostics(diagnostics: ReadonlyArray<Diagnostic>, root?: string): Array<Report>;
|
|
28
|
+
/** Filters out what the previous call returned, so a rebuild repeats no warning. */
|
|
29
|
+
export declare function unreported(): (diagnostics: ReadonlyArray<Diagnostic>) => Array<Diagnostic>;
|
|
30
|
+
export {};
|
|
31
|
+
//# sourceMappingURL=diagnostics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diagnostics.d.ts","sourceRoot":"","sources":["../src/diagnostics.ts"],"names":[],"mappings":"AAEA,UAAU,OAAO;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,wEAAwE;AACxE,MAAM,WAAW,eAAgB,SAAQ,OAAO;IAC9C,KAAK,EAAE,OAAO,CAAC;IACf,IAAI,EACA,gBAAgB,GAChB,gBAAgB,GAChB,kBAAkB,GAClB,cAAc,GACd,gBAAgB,GAChB,cAAc,GACd,gBAAgB,CAAC;CACtB;AAED,6BAA6B;AAC7B,MAAM,WAAW,iBAAkB,SAAQ,OAAO;IAChD,KAAK,EAAE,SAAS,CAAC;IACjB,IAAI,EACA,cAAc,GACd,qBAAqB,GACrB,eAAe,GACf,gBAAgB,GAChB,aAAa,GACb,gBAAgB,CAAC;CACtB;AAED,MAAM,MAAM,UAAU,GAAG,eAAe,GAAG,iBAAiB,CAAC;AAE7D,8DAA8D;AAC9D,MAAM,MAAM,QAAQ,GAChB,IAAI,CAAC,eAAe,EAAE,WAAW,GAAG,QAAQ,CAAC,GAC7C,IAAI,CAAC,iBAAiB,EAAE,WAAW,GAAG,QAAQ,CAAC,CAAC;AASpD,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,UAAU,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAOrE;AAED,MAAM,WAAW,MAAM;IACrB,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;IAC3B,IAAI,EAAE,MAAM,CAAC;CACd;AAID,gFAAgF;AAChF,wBAAgB,iBAAiB,CAC/B,WAAW,EAAE,aAAa,CAAC,UAAU,CAAC,EACtC,IAAI,CAAC,EAAE,MAAM,GACZ,KAAK,CAAC,MAAM,CAAC,CAiBf;AAED,oFAAoF;AACpF,wBAAgB,UAAU,IAAI,CAAC,WAAW,EAAE,aAAa,CAAC,UAAU,CAAC,KAAK,KAAK,CAAC,UAAU,CAAC,CAQ1F"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
/** Catalogs are others' text: no new lines, which a CI may read as commands, nor terminal codes. */
|
|
3
|
+
const printable = (text) => text.replaceAll(/\p{Cc}/gu, (char) => {
|
|
4
|
+
const code = char.codePointAt(0) ?? 0;
|
|
5
|
+
return `\\u${code.toString(16).padStart(4, '0')}`;
|
|
6
|
+
});
|
|
7
|
+
export function formatDiagnostic(d, root) {
|
|
8
|
+
const where = [d.namespace, d.locale, d.key].filter(Boolean).join(' › ');
|
|
9
|
+
// `/` on Windows too, where its terminals open both.
|
|
10
|
+
const file = d.file && root ? ` (${path.relative(root, d.file).replaceAll(path.sep, '/')})` : '';
|
|
11
|
+
return printable(`${d.level === 'error' ? 'error ' : 'warning'} [${d.code}] ${where}: ${d.message}${file}`);
|
|
12
|
+
}
|
|
13
|
+
const SHOWN_KEYS = 5;
|
|
14
|
+
/** One line per problem: diagnostics that differ only in their key share it. */
|
|
15
|
+
export function formatDiagnostics(diagnostics, root) {
|
|
16
|
+
const groups = new Map();
|
|
17
|
+
for (const d of diagnostics) {
|
|
18
|
+
const id = JSON.stringify([d.level, d.code, d.namespace, d.locale, d.file, d.message]);
|
|
19
|
+
const group = groups.get(id) ?? { d, keys: [] };
|
|
20
|
+
groups.set(id, group);
|
|
21
|
+
if (d.key) {
|
|
22
|
+
group.keys.push(d.key);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return [...groups.values()].map(({ d, keys }) => {
|
|
26
|
+
const more = keys.length - SHOWN_KEYS;
|
|
27
|
+
// "and 1 more" is no shorter than the key itself
|
|
28
|
+
const key = more > 1 ? `${keys.slice(0, SHOWN_KEYS).join(', ')} and ${more} more` : keys.join(', ');
|
|
29
|
+
return { level: d.level, line: formatDiagnostic({ ...d, key }, root) };
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/** Filters out what the previous call returned, so a rebuild repeats no warning. */
|
|
33
|
+
export function unreported() {
|
|
34
|
+
let last = new Set();
|
|
35
|
+
return (diagnostics) => {
|
|
36
|
+
const ids = diagnostics.map((d) => JSON.stringify(d));
|
|
37
|
+
const fresh = diagnostics.filter((_, i) => !last.has(ids[i] ?? ''));
|
|
38
|
+
last = new Set(ids);
|
|
39
|
+
return fresh;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { MessageInfo } from '../compile/info.ts';
|
|
2
|
+
import type { ResolvedConfig } from '../config/config.ts';
|
|
3
|
+
/** A locale with a parent (a fallback or its base) leaves gaps by design; the parent reports its own. */
|
|
4
|
+
export declare function inherits(config: ResolvedConfig, locale: string): boolean;
|
|
5
|
+
/** What the source message has and a translation leaves out. */
|
|
6
|
+
export declare function omissions(own: MessageInfo, source: MessageInfo): Array<string>;
|
|
7
|
+
/** A translation's params and markup against the source message's. */
|
|
8
|
+
export declare function mismatches(own: MessageInfo, source: MessageInfo): Array<string>;
|
|
9
|
+
//# sourceMappingURL=checks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checks.d.ts","sourceRoot":"","sources":["../../src/generate/checks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAa,MAAM,oBAAoB,CAAC;AACjE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAI1D,yGAAyG;AACzG,wBAAgB,QAAQ,CAAC,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAExE;AAsCD,gEAAgE;AAChE,wBAAgB,SAAS,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,WAAW,GAAG,KAAK,CAAC,MAAM,CAAC,CAe9E;AAED,sEAAsE;AACtE,wBAAgB,UAAU,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,WAAW,GAAG,KAAK,CAAC,MAAM,CAAC,CAqC/E"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { parents } from '../config/fallbacks.js';
|
|
2
|
+
import { paramType } from '../types/signature.js';
|
|
3
|
+
/** A locale with a parent (a fallback or its base) leaves gaps by design; the parent reports its own. */
|
|
4
|
+
export function inherits(config, locale) {
|
|
5
|
+
return parents(config, locale).some((l) => l !== locale && config.locales.includes(l));
|
|
6
|
+
}
|
|
7
|
+
const selectKeys = (kinds) => kinds.flatMap((k) => (k.kind === 'select' ? k.keys : []));
|
|
8
|
+
/** Whether a param typed by the source's kinds suits the translation's. */
|
|
9
|
+
function accepts(own, source) {
|
|
10
|
+
const [ownType, sourceType] = [paramType(own), paramType(source)];
|
|
11
|
+
if (ownType === sourceType || ownType === 'string | number') {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
const isText = [...own, ...source].every((k) => k.kind === 'select' || k.kind === 'string');
|
|
15
|
+
// A language may need fewer branches, down to none.
|
|
16
|
+
return (isText &&
|
|
17
|
+
source.some((k) => k.kind === 'select') &&
|
|
18
|
+
selectKeys(own).every((key) => selectKeys(source).includes(key)));
|
|
19
|
+
}
|
|
20
|
+
/** `$p when $k is "a"`, per param the source needs in a branch and the translation leaves out. */
|
|
21
|
+
function branchOmissions(own, source) {
|
|
22
|
+
const { branches } = source;
|
|
23
|
+
const out = new Map();
|
|
24
|
+
if (branches && own.branches?.param === branches.param) {
|
|
25
|
+
for (const [key, needs] of branches.needs) {
|
|
26
|
+
const ownNeeds = own.branches.needs.get(key) ?? own.branches.needs.get(null);
|
|
27
|
+
for (const name of needs) {
|
|
28
|
+
if (own.params.has(name) && ownNeeds?.has(name) === false && !out.has(name)) {
|
|
29
|
+
const when = key === null ? 'another value' : JSON.stringify(key);
|
|
30
|
+
out.set(name, `$${name} when $${branches.param} is ${when}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return [...out.values()];
|
|
36
|
+
}
|
|
37
|
+
/** What the source message has and a translation leaves out. */
|
|
38
|
+
export function omissions(own, source) {
|
|
39
|
+
const left = [];
|
|
40
|
+
for (const [name, kinds] of source.params) {
|
|
41
|
+
// A language may not need a select, as of gender.
|
|
42
|
+
if (!own.params.has(name) && !kinds.some((k) => k.kind === 'select')) {
|
|
43
|
+
left.push(`$${name}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
left.push(...branchOmissions(own, source));
|
|
47
|
+
for (const tag of source.markup) {
|
|
48
|
+
if (!own.markup.has(tag)) {
|
|
49
|
+
left.push(`markup "${tag}"`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return left.map((what) => `leaves out ${what}, which the source message has`);
|
|
53
|
+
}
|
|
54
|
+
/** A translation's params and markup against the source message's. */
|
|
55
|
+
export function mismatches(own, source) {
|
|
56
|
+
const out = [];
|
|
57
|
+
for (const [param, kinds] of own.params) {
|
|
58
|
+
const expected = source.params.get(param);
|
|
59
|
+
if (!expected) {
|
|
60
|
+
out.push(`uses $${param}, which the source message does not have`);
|
|
61
|
+
}
|
|
62
|
+
else if (!accepts(kinds, expected)) {
|
|
63
|
+
out.push(`$${param} is ${paramType(kinds)} here but ${paramType(expected)} in the source`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
for (const tag of own.markup) {
|
|
67
|
+
if (!source.markup.has(tag)) {
|
|
68
|
+
out.push(`uses markup "${tag}", which the source message does not have`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const { branches } = source;
|
|
72
|
+
if (branches) {
|
|
73
|
+
// The source's type lets a call leave out what its value's branch does not read.
|
|
74
|
+
const ownBranches = own.branches?.param === branches.param ? own.branches : undefined;
|
|
75
|
+
const needs = (info, key) => {
|
|
76
|
+
const map = info === source ? branches : ownBranches;
|
|
77
|
+
return map?.needs.get(key) ?? map?.needs.get(null) ?? new Set(info.params.keys());
|
|
78
|
+
};
|
|
79
|
+
const keys = new Set([...branches.needs.keys(), ...(ownBranches?.needs.keys() ?? [])]);
|
|
80
|
+
const reported = new Set();
|
|
81
|
+
for (const key of keys) {
|
|
82
|
+
const allowed = needs(source, key);
|
|
83
|
+
for (const param of needs(own, key)) {
|
|
84
|
+
if (source.params.has(param) && !allowed.has(param) && !reported.has(param)) {
|
|
85
|
+
reported.add(param);
|
|
86
|
+
const when = key === null ? 'another value' : JSON.stringify(key);
|
|
87
|
+
out.push(`needs $${param} when $${branches.param} is ${when}, but the source does not`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface ChunkTree extends Map<string, ChunkTree | string> {
|
|
2
|
+
}
|
|
3
|
+
/** Dotted keys as nested groups; `key-conflict` already rules out a key that is also a group. */
|
|
4
|
+
export declare function treeOf(messages: ReadonlyArray<[key: string, fn: string]>): ChunkTree;
|
|
5
|
+
/**
|
|
6
|
+
* A chunk's factory. In parentheses, so V8 compiles it at once instead of parsing it twice; bigger
|
|
7
|
+
* groups are built in its own body, as V8 runs a function nested there ~5× slower.
|
|
8
|
+
*/
|
|
9
|
+
export declare function emitFactory(tree: ChunkTree): string;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=chunk.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chunk.d.ts","sourceRoot":"","sources":["../../src/generate/chunk.ts"],"names":[],"mappings":"AACA,UAAU,SAAU,SAAQ,GAAG,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAAC;CAAG;AAE9D,iGAAiG;AACjG,wBAAgB,MAAM,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,SAAS,CAiBpF;AAyCD;;;GAGG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,CAMnD"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** Dotted keys as nested groups; `key-conflict` already rules out a key that is also a group. */
|
|
2
|
+
export function treeOf(messages) {
|
|
3
|
+
const root = new Map();
|
|
4
|
+
for (const [key, fn] of messages) {
|
|
5
|
+
const path = key.split('.');
|
|
6
|
+
const leaf = path.pop() ?? key;
|
|
7
|
+
let node = root;
|
|
8
|
+
for (const segment of path) {
|
|
9
|
+
let child = node.get(segment);
|
|
10
|
+
if (typeof child !== 'object') {
|
|
11
|
+
child = new Map();
|
|
12
|
+
node.set(segment, child);
|
|
13
|
+
}
|
|
14
|
+
node = child;
|
|
15
|
+
}
|
|
16
|
+
node.set(leaf, fn);
|
|
17
|
+
}
|
|
18
|
+
return root;
|
|
19
|
+
}
|
|
20
|
+
// A literal `"__proto__": …` would set the prototype; a computed key defines a property.
|
|
21
|
+
const literalKey = (key) => key === '__proto__' ? '["__proto__"]' : JSON.stringify(key);
|
|
22
|
+
// V8 builds a literal of up to 127 properties from a template; at 128 it takes a path ~100× slower.
|
|
23
|
+
const MAX_LITERAL = 127;
|
|
24
|
+
/** A group as an expression: a literal or, when larger, a const that `statements` builds. */
|
|
25
|
+
function emitGroup(node, { indent, statements }) {
|
|
26
|
+
const inner = `${indent} `;
|
|
27
|
+
const value = (v) => typeof v === 'string' ? v : emitGroup(v, { indent: inner, statements });
|
|
28
|
+
// `__proto__` must stay in the literal: an assignment would set the prototype.
|
|
29
|
+
const entries = [...node].toSorted(([a], [b]) => Number(b === '__proto__') - Number(a === '__proto__'));
|
|
30
|
+
const literal = entries
|
|
31
|
+
.slice(0, MAX_LITERAL)
|
|
32
|
+
.map(([key, v]) => `${inner}${literalKey(key)}: ${value(v)},`)
|
|
33
|
+
.join('\n');
|
|
34
|
+
if (entries.length <= MAX_LITERAL) {
|
|
35
|
+
return `{\n${literal}\n${indent}}`;
|
|
36
|
+
}
|
|
37
|
+
const rest = entries
|
|
38
|
+
.slice(MAX_LITERAL)
|
|
39
|
+
.map(([key, v]) => [JSON.stringify(key), value(v)]);
|
|
40
|
+
const name = `g${statements.length}`;
|
|
41
|
+
statements.push([
|
|
42
|
+
`const ${name} = {\n${literal}\n};`,
|
|
43
|
+
...rest.map(([key, v]) => `${name}[${key}] = ${v};`),
|
|
44
|
+
].join('\n'));
|
|
45
|
+
return name;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A chunk's factory. In parentheses, so V8 compiles it at once instead of parsing it twice; bigger
|
|
49
|
+
* groups are built in its own body, as V8 runs a function nested there ~5× slower.
|
|
50
|
+
*/
|
|
51
|
+
export function emitFactory(tree) {
|
|
52
|
+
const statements = [];
|
|
53
|
+
const root = emitGroup(tree, { indent: '', statements });
|
|
54
|
+
return statements.length === 0
|
|
55
|
+
? `(($) => (${root}))`
|
|
56
|
+
: `(($) => {\n${statements.join('\n')}\nreturn ${root};\n})`;
|
|
57
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Whether `Intl` writes 0 to 999 as JavaScript does: asking it in a browser costs what this saves. */
|
|
2
|
+
export declare function hasPlainDigits(locale: string): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* What every engine puts between the groups of three of `locale`'s integers, or nothing where they
|
|
5
|
+
* may differ: its parents, and for a language some engines lack its `fallbacks` too, must agree.
|
|
6
|
+
*/
|
|
7
|
+
export declare function groupSeparator(locale: string, fallbacks: ReadonlyArray<string>): string | undefined;
|
|
8
|
+
//# sourceMappingURL=digits.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"digits.d.ts","sourceRoot":"","sources":["../../src/generate/digits.ts"],"names":[],"mappings":"AAcA,uGAAuG;AACvG,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAOtD;AAsCD;;;GAGG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,aAAa,CAAC,MAAM,CAAC,GAC/B,MAAM,GAAG,SAAS,CAMpB"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// A locale of another script, such as `ar`, has latin digits in some engines and its own in others.
|
|
2
|
+
const DIGITLESS_SCRIPTS = new Set([
|
|
3
|
+
'Armn',
|
|
4
|
+
'Cyrl',
|
|
5
|
+
'Geor',
|
|
6
|
+
'Grek',
|
|
7
|
+
'Hans',
|
|
8
|
+
'Hant',
|
|
9
|
+
'Hebr',
|
|
10
|
+
'Jpan',
|
|
11
|
+
'Kore',
|
|
12
|
+
'Latn',
|
|
13
|
+
]);
|
|
14
|
+
/** Whether `Intl` writes 0 to 999 as JavaScript does: asking it in a browser costs what this saves. */
|
|
15
|
+
export function hasPlainDigits(locale) {
|
|
16
|
+
const { script = '' } = new Intl.Locale(locale).maximize();
|
|
17
|
+
const numbers = new Intl.NumberFormat(locale);
|
|
18
|
+
return (DIGITLESS_SCRIPTS.has(script) &&
|
|
19
|
+
Array.from({ length: 1000 }, (_, n) => n).every((n) => numbers.format(n) === String(n)));
|
|
20
|
+
}
|
|
21
|
+
// Languages every engine has data for: another may be written as one of its fallbacks.
|
|
22
|
+
const COMMON_LANGUAGES = new Set('af am ar bg bn ca cs da de el en es et fa fi fil fr gu he hi hr hu id it ja kn ko lt lv ml mr ms nb nl pl pt ro ru sk sl sr sv sw ta te th tr uk ur vi zh'.split(' '));
|
|
23
|
+
/** The integers checked against `Intl`: at each power of ten up to the runtime's limit, and between. */
|
|
24
|
+
const PROBES = Array.from({ length: 13 }, (_, i) => 10 ** (i + 3)).flatMap((power) => [
|
|
25
|
+
power,
|
|
26
|
+
power + 1,
|
|
27
|
+
power + Math.floor(power / 7),
|
|
28
|
+
power * 9 - 1,
|
|
29
|
+
]);
|
|
30
|
+
/** What `Intl` puts between the groups of three of every integer from 1000 up, if that is all it does. */
|
|
31
|
+
function separatorOf(locale) {
|
|
32
|
+
if (Intl.NumberFormat.supportedLocalesOf(locale).length === 0) {
|
|
33
|
+
// It would answer for the machine's own locale.
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
const numbers = new Intl.NumberFormat(locale);
|
|
37
|
+
// Not from 10 000, as Italian does since 2022: engines differ on which locales do.
|
|
38
|
+
const [separator] = /\D+/.exec(numbers.format(1000)) ?? [];
|
|
39
|
+
return separator !== undefined &&
|
|
40
|
+
PROBES.every((n) => numbers.format(n) === String(n).replaceAll(/\B(?=(?:\d{3})+$)/g, separator))
|
|
41
|
+
? separator
|
|
42
|
+
: undefined;
|
|
43
|
+
}
|
|
44
|
+
/** The locale, then what an engine without its region or script reads instead. */
|
|
45
|
+
function lineage(locale) {
|
|
46
|
+
const subtags = new Intl.Locale(locale).baseName.split('-');
|
|
47
|
+
return [locale, ...subtags.slice(1).map((_, i) => subtags.slice(0, -1 - i).join('-'))];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* What every engine puts between the groups of three of `locale`'s integers, or nothing where they
|
|
51
|
+
* may differ: its parents, and for a language some engines lack its `fallbacks` too, must agree.
|
|
52
|
+
*/
|
|
53
|
+
export function groupSeparator(locale, fallbacks) {
|
|
54
|
+
const common = COMMON_LANGUAGES.has(new Intl.Locale(locale).language);
|
|
55
|
+
const [own, ...rest] = [...lineage(locale), ...(common ? [] : fallbacks)].map((tag) => separatorOf(tag));
|
|
56
|
+
return rest.every((other) => other === own) ? own : undefined;
|
|
57
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ResolvedConfig } from '../config/config.ts';
|
|
2
|
+
/** Writes what changed of `files` and the catalog, then removes what an earlier build left. */
|
|
3
|
+
export declare function writeOutput(config: ResolvedConfig, files: ReadonlyMap<string, string>): {
|
|
4
|
+
written: Array<string>;
|
|
5
|
+
removed: Array<string>;
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=files.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.d.ts","sourceRoot":"","sources":["../../src/generate/files.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAoD1D,+FAA+F;AAC/F,wBAAgB,WAAW,CACzB,MAAM,EAAE,cAAc,EACtB,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GACjC;IAAE,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAAC,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAA;CAAE,CAcpD"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync, } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { catalogDeclaration, catalogModule, MARK } from './modules.js';
|
|
4
|
+
/**
|
|
5
|
+
* Writes through a dotted temp file and a rename: builds run at once (one per app of a monorepo, the
|
|
6
|
+
* dev server beside `tsc --watch`) must never read a half-written chunk.
|
|
7
|
+
*/
|
|
8
|
+
function writeIfChanged(file, content) {
|
|
9
|
+
if (existsSync(file) && readFileSync(file, 'utf8') === content) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
13
|
+
const tmp = path.join(path.dirname(file), `.${path.basename(file)}.${process.pid}.tmp`);
|
|
14
|
+
writeFileSync(tmp, content);
|
|
15
|
+
renameSync(tmp, file);
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
/** Not a symlink: it leads out of `outDir`. */
|
|
19
|
+
const isFolder = (dir) => lstatSync(dir, { throwIfNoEntry: false })?.isDirectory() ?? false;
|
|
20
|
+
function isGenerated(file) {
|
|
21
|
+
try {
|
|
22
|
+
return readFileSync(file, 'utf8').startsWith(MARK);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
// a parallel build removed it first
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Removes the files in `out/rel` that an earlier build generated and this one did not. */
|
|
30
|
+
function removeStale(out, { rel, kept }) {
|
|
31
|
+
const removed = [];
|
|
32
|
+
const dir = path.join(out, rel);
|
|
33
|
+
for (const entry of isFolder(dir) ? readdirSync(dir, { withFileTypes: true }) : []) {
|
|
34
|
+
const file = path.join(dir, entry.name);
|
|
35
|
+
// a dotted file is another build's write in progress
|
|
36
|
+
const isStale = entry.isFile() && !entry.name.startsWith('.') && !kept.has(path.join(rel, entry.name));
|
|
37
|
+
if (isStale && isGenerated(file)) {
|
|
38
|
+
rmSync(file, { force: true });
|
|
39
|
+
removed.push(file);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return removed;
|
|
43
|
+
}
|
|
44
|
+
/** Writes what changed of `files` and the catalog, then removes what an earlier build left. */
|
|
45
|
+
export function writeOutput(config, files) {
|
|
46
|
+
const out = config.outDir;
|
|
47
|
+
const all = new Map([
|
|
48
|
+
...files,
|
|
49
|
+
['catalog.js', catalogModule(config)],
|
|
50
|
+
['catalog.d.ts', catalogDeclaration(config)],
|
|
51
|
+
]);
|
|
52
|
+
const written = [...all].filter(([rel, code]) => writeIfChanged(path.join(out, rel), code));
|
|
53
|
+
const removed = removeStale(out, { rel: 'namespaces', kept: all });
|
|
54
|
+
const messagesDir = path.join(out, 'messages');
|
|
55
|
+
for (const locale of isFolder(messagesDir) ? readdirSync(messagesDir) : []) {
|
|
56
|
+
removed.push(...removeStale(out, { rel: path.join('messages', locale), kept: all }));
|
|
57
|
+
}
|
|
58
|
+
return { written: written.map(([rel]) => path.join(out, rel)), removed };
|
|
59
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { MessageInfo } from '../compile/info.ts';
|
|
2
|
+
import type { ResolvedConfig, ResolvedNamespace } from '../config/config.ts';
|
|
3
|
+
import type { Diagnostic } from '../diagnostics.ts';
|
|
4
|
+
import type { Entry } from './read.ts';
|
|
5
|
+
type Messages = Map<string, {
|
|
6
|
+
info: MessageInfo;
|
|
7
|
+
source: string;
|
|
8
|
+
}>;
|
|
9
|
+
export interface LocaleInput {
|
|
10
|
+
readonly config: ResolvedConfig;
|
|
11
|
+
readonly ns: ResolvedNamespace;
|
|
12
|
+
readonly catalogs: ReadonlyMap<string, ReadonlyMap<string, Entry>>;
|
|
13
|
+
readonly reported: ReadonlySet<string>;
|
|
14
|
+
/** The source locale's messages, filled by its own call, which comes first. */
|
|
15
|
+
readonly sourceInfo: Messages;
|
|
16
|
+
readonly diagnostics: Array<Diagnostic>;
|
|
17
|
+
}
|
|
18
|
+
/** One locale's chunk of a namespace: every source key, translated or borrowed. */
|
|
19
|
+
export declare function compileLocale(locale: string, input: LocaleInput): string;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=locale.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"locale.d.ts","sourceRoot":"","sources":["../../src/generate/locale.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAmB,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEvE,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAE7E,OAAO,KAAK,EAAE,UAAU,EAAY,MAAM,mBAAmB,CAAC;AAM9D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAEvC,KAAK,QAAQ,GAAG,GAAG,CAAC,MAAM,EAAE;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAEnE,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC;IAChC,QAAQ,CAAC,EAAE,EAAE,iBAAiB,CAAC;IAC/B,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;IACnE,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACvC,+EAA+E;IAC/E,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC;IAC9B,QAAQ,CAAC,WAAW,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC;CACzC;AAwDD,mFAAmF;AACnF,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,GAAG,MAAM,CA+DxE"}
|