2023-07-09 10:19:07 +02:00
|
|
|
import * as fs from 'node:fs';
|
2023-09-10 12:31:01 +02:00
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { dirname } from 'node:path';
|
2023-07-09 10:19:07 +02:00
|
|
|
import * as yaml from 'js-yaml';
|
2023-09-10 12:31:01 +02:00
|
|
|
import ts from 'typescript';
|
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = dirname(__filename);
|
2024-01-18 23:58:07 +01:00
|
|
|
const parameterRegExp = /\{(\w+)\}/g;
|
|
|
|
|
|
|
|
function createMemberType(item) {
|
|
|
|
if (typeof item !== 'string') {
|
|
|
|
return ts.factory.createTypeLiteralNode(createMembers(item));
|
|
|
|
}
|
|
|
|
const parameters = Array.from(
|
|
|
|
item.matchAll(parameterRegExp),
|
|
|
|
([, parameter]) => parameter,
|
|
|
|
);
|
|
|
|
if (!parameters.length) {
|
|
|
|
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
|
|
|
|
}
|
|
|
|
return ts.factory.createTypeReferenceNode(
|
|
|
|
ts.factory.createIdentifier('ParameterizedString'),
|
|
|
|
[
|
|
|
|
ts.factory.createUnionTypeNode(
|
|
|
|
parameters.map((parameter) =>
|
|
|
|
ts.factory.createLiteralTypeNode(
|
|
|
|
ts.factory.createStringLiteral(parameter),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2023-05-24 10:12:38 +02:00
|
|
|
|
|
|
|
function createMembers(record) {
|
2024-01-18 23:58:07 +01:00
|
|
|
return Object.entries(record).map(([k, v]) =>
|
|
|
|
ts.factory.createPropertySignature(
|
2023-05-24 10:12:38 +02:00
|
|
|
undefined,
|
|
|
|
ts.factory.createStringLiteral(k),
|
|
|
|
undefined,
|
2024-01-18 23:58:07 +01:00
|
|
|
createMemberType(v),
|
|
|
|
),
|
|
|
|
);
|
2023-05-24 10:12:38 +02:00
|
|
|
}
|
|
|
|
|
2023-07-09 10:19:07 +02:00
|
|
|
export default function generateDTS() {
|
2023-05-24 10:12:38 +02:00
|
|
|
const locale = yaml.load(fs.readFileSync(`${__dirname}/ja-JP.yml`, 'utf-8'));
|
|
|
|
const members = createMembers(locale);
|
|
|
|
const elements = [
|
|
|
|
ts.factory.createVariableStatement(
|
|
|
|
[ts.factory.createToken(ts.SyntaxKind.DeclareKeyword)],
|
|
|
|
ts.factory.createVariableDeclarationList(
|
2024-01-18 23:58:07 +01:00
|
|
|
[
|
|
|
|
ts.factory.createVariableDeclaration(
|
|
|
|
ts.factory.createIdentifier('kParameters'),
|
|
|
|
undefined,
|
|
|
|
ts.factory.createTypeOperatorNode(
|
|
|
|
ts.SyntaxKind.UniqueKeyword,
|
|
|
|
ts.factory.createKeywordTypeNode(ts.SyntaxKind.SymbolKeyword),
|
|
|
|
),
|
|
|
|
undefined,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
ts.NodeFlags.Const,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
ts.factory.createInterfaceDeclaration(
|
|
|
|
[ts.factory.createToken(ts.SyntaxKind.ExportKeyword)],
|
|
|
|
ts.factory.createIdentifier('ParameterizedString'),
|
|
|
|
[
|
|
|
|
ts.factory.createTypeParameterDeclaration(
|
|
|
|
undefined,
|
|
|
|
ts.factory.createIdentifier('T'),
|
|
|
|
ts.factory.createTypeReferenceNode(
|
|
|
|
ts.factory.createIdentifier('string'),
|
|
|
|
undefined,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
undefined,
|
|
|
|
[
|
|
|
|
ts.factory.createPropertySignature(
|
|
|
|
undefined,
|
|
|
|
ts.factory.createComputedPropertyName(
|
|
|
|
ts.factory.createIdentifier('kParameters'),
|
|
|
|
),
|
2023-05-24 10:12:38 +02:00
|
|
|
undefined,
|
2024-01-18 23:58:07 +01:00
|
|
|
ts.factory.createTypeReferenceNode(
|
|
|
|
ts.factory.createIdentifier('T'),
|
2023-05-24 10:12:38 +02:00
|
|
|
undefined,
|
2024-01-18 23:58:07 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
ts.factory.createInterfaceDeclaration(
|
|
|
|
[ts.factory.createToken(ts.SyntaxKind.ExportKeyword)],
|
|
|
|
ts.factory.createIdentifier('ILocale'),
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
[
|
|
|
|
ts.factory.createIndexSignature(
|
|
|
|
undefined,
|
|
|
|
[
|
|
|
|
ts.factory.createParameterDeclaration(
|
2023-05-24 10:12:38 +02:00
|
|
|
undefined,
|
|
|
|
undefined,
|
2024-01-18 23:58:07 +01:00
|
|
|
ts.factory.createIdentifier('_'),
|
2023-05-24 10:12:38 +02:00
|
|
|
undefined,
|
|
|
|
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
|
|
|
|
undefined,
|
2024-01-18 23:58:07 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
ts.factory.createUnionTypeNode([
|
|
|
|
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
|
2023-05-24 10:12:38 +02:00
|
|
|
ts.factory.createTypeReferenceNode(
|
2024-01-18 23:58:07 +01:00
|
|
|
ts.factory.createIdentifier('ParameterizedString'),
|
|
|
|
[ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)],
|
|
|
|
),
|
|
|
|
ts.factory.createTypeReferenceNode(
|
|
|
|
ts.factory.createIdentifier('ILocale'),
|
2023-05-24 10:12:38 +02:00
|
|
|
undefined,
|
|
|
|
),
|
2024-01-18 23:58:07 +01:00
|
|
|
]),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
ts.factory.createInterfaceDeclaration(
|
|
|
|
[ts.factory.createToken(ts.SyntaxKind.ExportKeyword)],
|
|
|
|
ts.factory.createIdentifier('Locale'),
|
|
|
|
undefined,
|
|
|
|
[
|
|
|
|
ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [
|
|
|
|
ts.factory.createExpressionWithTypeArguments(
|
|
|
|
ts.factory.createIdentifier('ILocale'),
|
|
|
|
undefined,
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
members,
|
|
|
|
),
|
|
|
|
ts.factory.createVariableStatement(
|
|
|
|
[ts.factory.createToken(ts.SyntaxKind.DeclareKeyword)],
|
|
|
|
ts.factory.createVariableDeclarationList(
|
|
|
|
[
|
|
|
|
ts.factory.createVariableDeclaration(
|
|
|
|
ts.factory.createIdentifier('locales'),
|
|
|
|
undefined,
|
|
|
|
ts.factory.createTypeLiteralNode([
|
|
|
|
ts.factory.createIndexSignature(
|
|
|
|
undefined,
|
|
|
|
[
|
|
|
|
ts.factory.createParameterDeclaration(
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
ts.factory.createIdentifier('lang'),
|
|
|
|
undefined,
|
|
|
|
ts.factory.createKeywordTypeNode(
|
|
|
|
ts.SyntaxKind.StringKeyword,
|
|
|
|
),
|
|
|
|
undefined,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
ts.factory.createTypeReferenceNode(
|
|
|
|
ts.factory.createIdentifier('Locale'),
|
|
|
|
undefined,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
undefined,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
ts.NodeFlags.Const,
|
2023-05-24 10:12:38 +02:00
|
|
|
),
|
|
|
|
),
|
2023-11-30 06:48:02 +01:00
|
|
|
ts.factory.createFunctionDeclaration(
|
|
|
|
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
|
|
|
|
undefined,
|
|
|
|
ts.factory.createIdentifier('build'),
|
|
|
|
undefined,
|
|
|
|
[],
|
|
|
|
ts.factory.createTypeReferenceNode(
|
|
|
|
ts.factory.createIdentifier('Locale'),
|
|
|
|
undefined,
|
|
|
|
),
|
|
|
|
undefined,
|
|
|
|
),
|
2023-07-14 13:53:09 +02:00
|
|
|
ts.factory.createExportDefault(ts.factory.createIdentifier('locales')),
|
2023-05-24 10:12:38 +02:00
|
|
|
];
|
2024-01-18 23:58:07 +01:00
|
|
|
const printed = ts
|
|
|
|
.createPrinter({
|
|
|
|
newLine: ts.NewLineKind.LineFeed,
|
|
|
|
})
|
|
|
|
.printList(
|
|
|
|
ts.ListFormat.MultiLine,
|
|
|
|
ts.factory.createNodeArray(elements),
|
|
|
|
ts.createSourceFile(
|
|
|
|
'index.d.ts',
|
|
|
|
'',
|
|
|
|
ts.ScriptTarget.ESNext,
|
|
|
|
true,
|
|
|
|
ts.ScriptKind.TS,
|
|
|
|
),
|
|
|
|
);
|
2023-05-24 10:12:38 +02:00
|
|
|
|
2024-01-18 23:58:07 +01:00
|
|
|
fs.writeFileSync(
|
|
|
|
`${__dirname}/index.d.ts`,
|
|
|
|
`/* eslint-disable */
|
2023-05-24 10:12:38 +02:00
|
|
|
// This file is generated by locales/generateDTS.js
|
|
|
|
// Do not edit this file directly.
|
2024-01-18 23:58:07 +01:00
|
|
|
${printed}`,
|
|
|
|
'utf-8',
|
|
|
|
);
|
2023-05-24 10:12:38 +02:00
|
|
|
}
|