Skip to content

Commit

Permalink
match prettier on codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
schlawg committed Oct 15, 2024
1 parent 1bf6386 commit 70d2d1e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
57 changes: 33 additions & 24 deletions ui/.build/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ const isFormat = /%(?:[\d]\$)?s/;

const tsPrelude = `// Generated
interface I18nFormat {
(...args: (string | number)[]): string;
asArray: <T>(...args: T[]) => (T | string)[], // vdom
(...args: (string | number)[]): string; // formatted
asArray: <T>(...args: T[]) => (T | string)[]; // vdom
}
interface I18nPlural {
(quantity: number, ...args: (string | number)[]): string, // pluralSame
raw: (quantity: number, ...args: (string | number)[]) => string, // plural
asArray: <T>(quantity: number, ...args: T[]) => (T | string)[], // vdomPlural
(quantity: number, ...args: (string | number)[]): string; // pluralSame
raw: (quantity: number, ...args: (string | number)[]) => string; // plural
asArray: <T>(quantity: number, ...args: T[]) => (T | string)[]; // vdomPlural
}
interface I18n {
/** Global noarg key lookup (only if absolutely necessary). */
Expand Down Expand Up @@ -98,8 +98,9 @@ export async function i18n(isBoot = true): Promise<void> {
}

async function compileTypings(): Promise<void> {
const typingsPathname = path.join(env.typesDir, 'lichess', `i18n.d.ts`);
const [tstat] = await Promise.all([
fs.promises.stat(path.join(env.typesDir, 'lichess', `i18n.d.ts`)).catch(() => undefined),
fs.promises.stat(typingsPathname).catch(() => undefined),
fs.promises.mkdir(env.i18nJsDir).catch(() => {}),
]);
const catStats = await Promise.all(cats.map(d => updated(d)));
Expand All @@ -114,24 +115,32 @@ async function compileTypings(): Promise<void> {
),
),
);
const code =
await fs.promises.writeFile(
typingsPathname,
tsPrelude +
[...dicts]
.map(
([cat, dict]) =>
` ${cat}: {\n` +
[...dict.entries()]
.map(([k, v]) => {
const tpe = typeof v !== 'string' ? 'I18nPlural' : isFormat.test(v) ? 'I18nFormat' : 'string';
const comment = typeof v === 'string' ? v.split('\n')[0] : v['other']?.split('\n')[0];
return ` /** ${comment} */\n ${k}: ${tpe};`;
})
.join('\n') +
'\n };\n',
)
.join('') +
'}\n';
return fs.promises.writeFile(path.join(env.typesDir, 'lichess', `i18n.d.ts`), code);
[...dicts]
.map(
([cat, dict]) =>
` ${cat}: {\n` +
[...dict.entries()]
.map(([k, v]) => {
if (!/^[A-Za-z_]\w*$/.test(k)) k = `'${k}'`;
const tpe =
typeof v !== 'string' ? 'I18nPlural' : isFormat.test(v) ? 'I18nFormat' : 'string';
const comment = typeof v === 'string' ? v.split('\n')[0] : v['other']?.split('\n')[0];
return ` /** ${comment} */\n ${k}: ${tpe};`;
})
.join('\n') +
'\n };\n',
)
.join('') +
'}\n',
);
const mstat = catStats.reduce(
(a, b) => (a && b && quantize(a.mtimeMs) > quantize(b.mtimeMs) ? a : b),
tstat || false,
);
if (mstat) await fs.promises.utimes(typingsPathname, mstat.mtime, mstat.mtime);
}
}

Expand Down Expand Up @@ -205,7 +214,7 @@ async function updated(cat: string, locale?: string): Promise<fs.Stats | false>
const jsPath = path.join(env.i18nJsDir, `${cat}.${locale ?? 'en-GB'}.js`);
const [xml, js] = await Promise.allSettled([fs.promises.stat(xmlPath), fs.promises.stat(jsPath)]);
return xml.status === 'rejected' ||
(js.status !== 'rejected' && quantize(xml.value.mtimeMs, 2000) <= quantize(js.value.mtimeMs, 2000))
(js.status !== 'rejected' && quantize(xml.value.mtimeMs) <= quantize(js.value.mtimeMs))
? false
: xml.value;
}
Expand Down
2 changes: 1 addition & 1 deletion ui/@types/lichess/i18n.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Generated
interface I18nFormat {
(...args: (string | number)[]): string;
(...args: (string | number)[]): string; // formatted
asArray: <T>(...args: T[]) => (T | string)[]; // vdom
}
interface I18nPlural {
Expand Down

0 comments on commit 70d2d1e

Please sign in to comment.