Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 89 additions & 1 deletion lib/build.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fileURLToPath } from 'node:url';
import { styleText } from 'node:util';
import fs from 'fs';
import jsonschema from 'jsonschema';
Expand All @@ -6,6 +7,7 @@ import shell from 'shelljs';
import YAML from 'js-yaml';
import marky from 'marky';
import { createRequire } from 'module';
import { compile, toSafeIdentifier } from 'json-schema-to-typescript-lite';

import fetchTranslations from './translations.js';

Expand Down Expand Up @@ -186,7 +188,8 @@ function processData(options, type) {
minifyJSON(distDir + '/preset_defaults.json', distDir + '/preset_defaults.min.json'),
minifyJSON(distDir + '/deprecated.json', distDir + '/deprecated.min.json'),
minifyJSON(distDir + '/discarded.json', distDir + '/discarded.min.json'),
minifyJSON(distDir + '/translations/' + sourceLocale + '.json', distDir + '/translations/' + sourceLocale + '.min.json')
minifyJSON(distDir + '/translations/' + sourceLocale + '.json', distDir + '/translations/' + sourceLocale + '.min.json'),
generateTypeDefs(distDir),
];

if (doFetchTranslations) {
Expand Down Expand Up @@ -746,6 +749,91 @@ function generateIconsList(presets, fields, categories) {
return Object.keys(icons).sort();
}

/** @param {string} string */
const toPascalCase = string => string.replace(/(_.|^.)/g, match => match.at(-1).toUpperCase());

/** @param {string} string */
const createTypeIdentifier = (string) => toPascalCase(toSafeIdentifier(string));


/** @param {string} distDir */
async function generateTypeDefs(distDir) {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const inputFolder = path.join(__dirname, '../schemas');

/**
* Some generated files use plural names because they
* export an object, e.g. `Fields = Record<string, Field>`
* @type {Record<string, string>}
*/
const KEY_MAP = {
field: 'fields',
preset: 'presets',
preset_category: 'preset_categories',
};
const fileNames = fs.globSync(path.join(inputFolder, '/**/*.json'));

/** @param {string} fileName */
async function processFile(fileName) {
const key = path.parse(fileName).name;
const pluralKey = KEY_MAP[key];

const mainExport = createTypeIdentifier(pluralKey || key);

const output = [''];

if (pluralKey) {
output.push(
`export interface ${createTypeIdentifier(pluralKey)} {`,
` [id: string]: ${createTypeIdentifier(key)}`,
'}'
);
}

output.push(
`declare const json: ${mainExport};`,
'export default json;'
);

const fileContent = JSON.parse(await fs.promises.readFile(fileName, 'utf8'));
if (key === 'field') delete fileContent.anyOf;

const tsFile = await compile(fileContent, mainExport, {
additionalProperties: false,
ignoreMinAndMaxItems: true,
cwd: path.join(__dirname, '../schemas'),

// ensure that the default export uses a consistent name
customName: (schema, fallback) =>
schema.$schema && schema.$id
? createTypeIdentifier(path.parse(schema.$id).name)
: (schema.$id || schema.title || fallback || ''),
});


output.unshift(tsFile);
output.push('');
await fs.promises.writeFile(
path.join(distDir, `${pluralKey || key}.d.json.ts`),
output.join('\n'),
);
}
await Promise.all(fileNames.map(processFile));

// finally, create the index file which re-exports everything
// as named types.
const indexFile = fileNames
.map((fileName) => {
const key = path.parse(fileName).name;
return `export type * from './${KEY_MAP[key] || key}.d.json.ts';`;
})
.join('\n');

await fs.promises.writeFile(
path.join(distDir, 'index.d.ts'),
indexFile + '\n',
);
}

function validateCategoryPresets(categories, presets) {
Object.keys(categories).forEach(id => {
Expand Down
53 changes: 49 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"@transifex/api": "^7.1.0",
"js-yaml": "^4.0.0",
"json-schema-to-typescript-lite": "^15.0.0",
"jsonschema": "^1.1.0",
"marky": "^1.2.4",
"node-fetch": "^3.2.10",
Expand Down