fix: typing and remove singularization

This commit is contained in:
hypercross 2026-04-11 23:05:22 +08:00
parent daac7badbb
commit d78ef75272
1 changed files with 23 additions and 11 deletions

View File

@ -272,19 +272,19 @@ function generateTypeDefinition(
const resourceNames = new Map<string, string>(); const resourceNames = new Map<string, string>();
references.forEach(tableName => { references.forEach(tableName => {
// Convert table name to type name (parts -> Part, recipes -> Recipe) // Convert table name to type name by capitalizing
// Remove trailing 's' to get singular form, then capitalize const typeBase = tableName.charAt(0).toUpperCase() + tableName.slice(1);
let singularName = tableName;
if (singularName.endsWith('s') && singularName.length > 1) {
singularName = singularName.slice(0, -1);
}
const typeBase = singularName.charAt(0).toUpperCase() + singularName.slice(1);
resourceNames.set(tableName, typeBase); resourceNames.set(tableName, typeBase);
// Import from relative path // Generate import path based on current file path
const importPath = currentFilePath let importPath: string;
? `./${tableName}.csv` if (currentFilePath) {
: `../${tableName}.csv`; // Both files are in the same directory, use relative path
importPath = `./${tableName}.csv`;
} else {
// Fallback for unknown path
importPath = `../${tableName}.csv`;
}
imports.push(`import type { ${typeBase} } from '${importPath}';`); imports.push(`import type { ${typeBase} } from '${importPath}';`);
}); });
@ -294,9 +294,21 @@ function generateTypeDefinition(
.map((config) => ` readonly ${config.name}: ${schemaToTypeString(config.schema, resourceNames)};`) .map((config) => ` readonly ${config.name}: ${schemaToTypeString(config.schema, resourceNames)};`)
.join('\n'); .join('\n');
// Generate both the table type and export a singular type alias for references
// e.g., for "parts" table, export both "partsTable" and "Parts" (as alias)
let exportAlias = '';
if (resourceName) {
// Capitalize resource name for the singular type
const singularType = resourceName.charAt(0).toUpperCase() + resourceName.slice(1);
// Remove trailing 's' if it looks like a plural (simple heuristic)
// Actually, let's just use the table name capitalized - users can adjust if needed
exportAlias = `\nexport type ${singularType} = ${typeName}[number];`;
}
return `${importSection}type ${typeName} = readonly { return `${importSection}type ${typeName} = readonly {
${properties} ${properties}
}[]; }[];
${exportAlias}
declare const data: ${typeName}; declare const data: ${typeName};
export default data; export default data;