2023-07-09 10:19:07 +02:00
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import * as yaml from 'js-yaml';
|
|
|
|
import * as ts from 'typescript';
|
2023-05-24 10:12:38 +02:00
|
|
|
|
|
|
|
function createMembers(record) {
|
|
|
|
return Object.entries(record)
|
|
|
|
.map(([k, v]) => ts.factory.createPropertySignature(
|
|
|
|
undefined,
|
|
|
|
ts.factory.createStringLiteral(k),
|
|
|
|
undefined,
|
|
|
|
typeof v === 'string'
|
|
|
|
? ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
|
|
|
|
: ts.factory.createTypeLiteralNode(createMembers(v)),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
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.createInterfaceDeclaration(
|
|
|
|
[ts.factory.createToken(ts.SyntaxKind.ExportKeyword)],
|
|
|
|
ts.factory.createIdentifier('Locale'),
|
|
|
|
undefined,
|
|
|
|
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 | ts.NodeFlags.Ambient | ts.NodeFlags.ContextFlags,
|
|
|
|
),
|
|
|
|
),
|
2023-07-14 13:53:09 +02:00
|
|
|
ts.factory.createExportDefault(ts.factory.createIdentifier('locales')),
|
2023-05-24 10:12:38 +02: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),
|
|
|
|
);
|
|
|
|
|
|
|
|
fs.writeFileSync(`${__dirname}/index.d.ts`, `/* eslint-disable */
|
|
|
|
// This file is generated by locales/generateDTS.js
|
|
|
|
// Do not edit this file directly.
|
|
|
|
${printed}`, 'utf-8');
|
|
|
|
}
|