2026-03-31 13:02:29 +08:00
|
|
|
import { parse } from 'csv-parse/sync';
|
|
|
|
|
import { parseSchema, createValidator, parseValue } from '../index.js';
|
2026-04-11 22:56:01 +08:00
|
|
|
import type { Schema, ReferenceSchema } from '../types.js';
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import * as path from 'path';
|
2026-03-31 13:02:29 +08:00
|
|
|
|
|
|
|
|
export interface CsvLoaderOptions {
|
|
|
|
|
delimiter?: string;
|
|
|
|
|
quote?: string;
|
|
|
|
|
escape?: string;
|
2026-03-31 14:45:02 +08:00
|
|
|
bom?: boolean;
|
|
|
|
|
comment?: string | false;
|
|
|
|
|
trim?: boolean;
|
2026-03-31 15:19:03 +08:00
|
|
|
/** Generate TypeScript declaration file (.d.ts) */
|
|
|
|
|
emitTypes?: boolean;
|
|
|
|
|
/** Output directory for generated type files (relative to output path) */
|
|
|
|
|
typesOutputDir?: string;
|
2026-03-31 15:54:38 +08:00
|
|
|
/** Write .d.ts files to disk (useful for dev server) */
|
|
|
|
|
writeToDisk?: boolean;
|
2026-04-11 22:56:01 +08:00
|
|
|
/** Base directory for resolving referenced CSV files (default: directory of current file) */
|
|
|
|
|
refBaseDir?: string;
|
|
|
|
|
/** Primary key field name for referenced tables (default: 'id') */
|
|
|
|
|
defaultPrimaryKey?: string;
|
|
|
|
|
/** Current file path (used to resolve relative references) */
|
|
|
|
|
currentFilePath?: string;
|
2026-03-31 13:02:29 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 17:32:13 +08:00
|
|
|
export interface CsvParseResult {
|
|
|
|
|
/** Parsed CSV data as array of objects */
|
|
|
|
|
data: Record<string, unknown>[];
|
|
|
|
|
/** Generated TypeScript type definition string (if emitTypes is true) */
|
|
|
|
|
typeDefinition?: string;
|
|
|
|
|
/** Property configurations for the CSV columns */
|
|
|
|
|
propertyConfigs: PropertyConfig[];
|
2026-04-11 22:56:01 +08:00
|
|
|
/** Referenced table names */
|
|
|
|
|
references: Set<string>;
|
2026-04-02 17:32:13 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 13:02:29 +08:00
|
|
|
interface PropertyConfig {
|
|
|
|
|
name: string;
|
|
|
|
|
schema: any;
|
|
|
|
|
validator: (value: unknown) => boolean;
|
|
|
|
|
parser: (valueString: string) => unknown;
|
2026-04-11 22:56:01 +08:00
|
|
|
/** Whether this property is a reference to another table */
|
|
|
|
|
isReference?: boolean;
|
|
|
|
|
/** Referenced table name (if isReference is true) */
|
|
|
|
|
referenceTableName?: string;
|
|
|
|
|
/** Whether it's an array reference */
|
|
|
|
|
referenceIsArray?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Cache for loaded referenced tables */
|
|
|
|
|
const referenceTableCache = new Map<string, Record<string, unknown>[]>();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parse and resolve a reference value.
|
|
|
|
|
* Loads the referenced table and replaces IDs with actual objects.
|
|
|
|
|
*/
|
|
|
|
|
function parseReferenceValue(
|
|
|
|
|
schema: ReferenceSchema,
|
|
|
|
|
valueString: string,
|
|
|
|
|
refBaseDir: string | undefined,
|
|
|
|
|
defaultPrimaryKey: string,
|
|
|
|
|
currentFilePath: string | undefined
|
|
|
|
|
): unknown {
|
|
|
|
|
// Determine the directory to search for referenced files
|
|
|
|
|
const baseDir = refBaseDir || (currentFilePath ? path.dirname(currentFilePath) : process.cwd());
|
|
|
|
|
|
|
|
|
|
// Build the referenced file path
|
|
|
|
|
const fileName = `${schema.tableName}.csv`;
|
|
|
|
|
const refFilePath = path.isAbsolute(fileName)
|
|
|
|
|
? fileName
|
|
|
|
|
: path.join(baseDir, fileName);
|
|
|
|
|
|
|
|
|
|
// Load the referenced table (use cache if already loaded)
|
|
|
|
|
let refTable: Record<string, unknown>[];
|
|
|
|
|
if (referenceTableCache.has(refFilePath)) {
|
|
|
|
|
refTable = referenceTableCache.get(refFilePath)!;
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
const refContent = fs.readFileSync(refFilePath, 'utf-8');
|
|
|
|
|
const refResult = parseCsv(refContent, {
|
|
|
|
|
currentFilePath: refFilePath,
|
|
|
|
|
emitTypes: false,
|
|
|
|
|
});
|
|
|
|
|
refTable = refResult.data;
|
|
|
|
|
referenceTableCache.set(refFilePath, refTable);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Failed to load referenced table "${schema.tableName}" from ${refFilePath}: ${error instanceof Error ? error.message : String(error)}`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build a lookup map by primary key
|
|
|
|
|
const primaryKeyMap = new Map<string, Record<string, unknown>>();
|
|
|
|
|
refTable.forEach(row => {
|
|
|
|
|
const pkValue = row[defaultPrimaryKey];
|
|
|
|
|
if (pkValue !== undefined) {
|
|
|
|
|
primaryKeyMap.set(String(pkValue), row);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Parse the value string to extract IDs
|
|
|
|
|
const valueParser = new ReferenceValueParser(valueString.trim());
|
|
|
|
|
const ids = valueParser.parseIds(schema.isArray);
|
|
|
|
|
|
|
|
|
|
// Resolve IDs to actual objects
|
|
|
|
|
if (schema.isArray) {
|
|
|
|
|
return ids.map(id => {
|
|
|
|
|
const obj = primaryKeyMap.get(id);
|
|
|
|
|
if (!obj) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Reference to "${schema.tableName}" with ${defaultPrimaryKey}="${id}" not found`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// Single reference (first ID if array provided)
|
|
|
|
|
const id = ids[0];
|
|
|
|
|
const obj = primaryKeyMap.get(id);
|
|
|
|
|
if (!obj) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Reference to "${schema.tableName}" with ${defaultPrimaryKey}="${id}" not found`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parser for reference values (extracts IDs from value string)
|
|
|
|
|
*/
|
|
|
|
|
class ReferenceValueParser {
|
|
|
|
|
private input: string;
|
|
|
|
|
private pos: number = 0;
|
|
|
|
|
|
|
|
|
|
constructor(input: string) {
|
|
|
|
|
this.input = input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private peek(): string {
|
|
|
|
|
return this.input[this.pos] || '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private consume(): string {
|
|
|
|
|
return this.input[this.pos++] || '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private skipWhitespace(): void {
|
|
|
|
|
while (this.pos < this.input.length && /\s/.test(this.input[this.pos])) {
|
|
|
|
|
this.pos++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private consumeStr(str: string): boolean {
|
|
|
|
|
if (this.input.slice(this.pos, this.pos + str.length) === str) {
|
|
|
|
|
this.pos += str.length;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parseIds(isArray: boolean): string[] {
|
|
|
|
|
this.skipWhitespace();
|
|
|
|
|
|
|
|
|
|
if (isArray) {
|
|
|
|
|
// Parse array format: [id1; id2; id3]
|
|
|
|
|
if (this.peek() === '[') {
|
|
|
|
|
this.consume();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.skipWhitespace();
|
|
|
|
|
|
|
|
|
|
if (this.peek() === ']') {
|
|
|
|
|
this.consume();
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ids: string[] = [];
|
|
|
|
|
while (true) {
|
|
|
|
|
this.skipWhitespace();
|
|
|
|
|
let id = '';
|
|
|
|
|
while (this.pos < this.input.length && this.peek() !== ';' && this.peek() !== ']') {
|
|
|
|
|
id += this.consume();
|
|
|
|
|
}
|
|
|
|
|
const trimmedId = id.trim();
|
|
|
|
|
if (trimmedId) {
|
|
|
|
|
ids.push(trimmedId);
|
|
|
|
|
}
|
|
|
|
|
this.skipWhitespace();
|
|
|
|
|
|
|
|
|
|
if (!this.consumeStr(';')) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.skipWhitespace();
|
|
|
|
|
if (this.peek() === ']') {
|
|
|
|
|
this.consume();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
|
} else {
|
|
|
|
|
// Parse single ID
|
|
|
|
|
let id = '';
|
|
|
|
|
while (this.pos < this.input.length) {
|
|
|
|
|
const char = this.peek();
|
|
|
|
|
if (char === ';' || char === ']' || char === ',') {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
id += this.consume();
|
|
|
|
|
}
|
|
|
|
|
return [id.trim()];
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-31 13:02:29 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 15:19:03 +08:00
|
|
|
/**
|
|
|
|
|
* Convert a schema to TypeScript type string
|
|
|
|
|
*/
|
2026-04-11 22:56:01 +08:00
|
|
|
function schemaToTypeString(schema: Schema, resourceNames?: Map<string, string>): string {
|
2026-03-31 15:19:03 +08:00
|
|
|
switch (schema.type) {
|
|
|
|
|
case 'string':
|
|
|
|
|
return 'string';
|
|
|
|
|
case 'number':
|
2026-04-04 17:08:29 +08:00
|
|
|
case 'int':
|
|
|
|
|
case 'float':
|
2026-03-31 15:19:03 +08:00
|
|
|
return 'number';
|
|
|
|
|
case 'boolean':
|
|
|
|
|
return 'boolean';
|
2026-04-11 22:56:01 +08:00
|
|
|
case 'reference': {
|
|
|
|
|
// Use the resource name mapping if provided, otherwise capitalize table name
|
|
|
|
|
const typeName = resourceNames?.get(schema.tableName) ||
|
|
|
|
|
schema.tableName.charAt(0).toUpperCase() + schema.tableName.slice(1);
|
|
|
|
|
return schema.isArray ? `readonly ${typeName}[]` : typeName;
|
|
|
|
|
}
|
2026-03-31 15:19:03 +08:00
|
|
|
case 'array':
|
|
|
|
|
if (schema.element.type === 'tuple') {
|
2026-03-31 16:57:52 +08:00
|
|
|
const tupleElements = schema.element.elements.map((el) => {
|
2026-04-11 22:56:01 +08:00
|
|
|
const typeStr = schemaToTypeString(el.schema, resourceNames);
|
2026-04-05 12:38:33 +08:00
|
|
|
return el.name ? `readonly ${el.name}: ${typeStr}` : typeStr;
|
2026-03-31 16:57:52 +08:00
|
|
|
});
|
2026-04-05 12:38:33 +08:00
|
|
|
return `readonly [${tupleElements.join(', ')}]`;
|
2026-03-31 15:19:03 +08:00
|
|
|
}
|
2026-04-11 22:56:01 +08:00
|
|
|
return `readonly ${schemaToTypeString(schema.element, resourceNames)}[]`;
|
2026-03-31 15:19:03 +08:00
|
|
|
case 'tuple':
|
2026-03-31 16:57:52 +08:00
|
|
|
const tupleElements = schema.elements.map((el) => {
|
2026-04-11 22:56:01 +08:00
|
|
|
const typeStr = schemaToTypeString(el.schema, resourceNames);
|
2026-04-05 12:38:33 +08:00
|
|
|
return el.name ? `readonly ${el.name}: ${typeStr}` : typeStr;
|
2026-03-31 16:57:52 +08:00
|
|
|
});
|
2026-04-05 12:38:33 +08:00
|
|
|
return `readonly [${tupleElements.join(', ')}]`;
|
2026-03-31 15:19:03 +08:00
|
|
|
default:
|
|
|
|
|
return 'unknown';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate TypeScript interface for the CSV data
|
|
|
|
|
*/
|
|
|
|
|
function generateTypeDefinition(
|
|
|
|
|
resourceName: string,
|
2026-04-11 22:56:01 +08:00
|
|
|
propertyConfigs: PropertyConfig[],
|
|
|
|
|
references: Set<string>,
|
|
|
|
|
currentFilePath?: string
|
2026-03-31 15:19:03 +08:00
|
|
|
): string {
|
2026-04-05 12:38:33 +08:00
|
|
|
const typeName = resourceName ? `${resourceName}Table` : 'Table';
|
2026-04-11 22:56:01 +08:00
|
|
|
|
|
|
|
|
// Generate import statements for referenced tables
|
|
|
|
|
const imports: string[] = [];
|
|
|
|
|
const resourceNames = new Map<string, string>();
|
|
|
|
|
|
|
|
|
|
references.forEach(tableName => {
|
|
|
|
|
// Convert table name to type name (parts -> Part, recipes -> Recipe)
|
|
|
|
|
// Remove trailing 's' to get singular form, then capitalize
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// Import from relative path
|
|
|
|
|
const importPath = currentFilePath
|
|
|
|
|
? `./${tableName}.csv`
|
|
|
|
|
: `../${tableName}.csv`;
|
|
|
|
|
imports.push(`import type { ${typeBase} } from '${importPath}';`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const importSection = imports.length > 0 ? imports.join('\n') + '\n\n' : '';
|
|
|
|
|
|
2026-03-31 15:19:03 +08:00
|
|
|
const properties = propertyConfigs
|
2026-04-11 22:56:01 +08:00
|
|
|
.map((config) => ` readonly ${config.name}: ${schemaToTypeString(config.schema, resourceNames)};`)
|
2026-03-31 15:19:03 +08:00
|
|
|
.join('\n');
|
2026-04-05 12:38:33 +08:00
|
|
|
|
2026-04-11 22:56:01 +08:00
|
|
|
return `${importSection}type ${typeName} = readonly {
|
2026-03-31 15:19:03 +08:00
|
|
|
${properties}
|
2026-04-05 12:38:33 +08:00
|
|
|
}[];
|
2026-03-31 15:19:03 +08:00
|
|
|
|
2026-04-05 12:38:33 +08:00
|
|
|
declare const data: ${typeName};
|
2026-03-31 16:12:17 +08:00
|
|
|
export default data;
|
2026-03-31 15:19:03 +08:00
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 17:32:13 +08:00
|
|
|
/**
|
|
|
|
|
* Parse CSV content string into structured data with schema validation.
|
|
|
|
|
* This is a standalone function that doesn't depend on webpack/rspack LoaderContext.
|
2026-04-11 22:56:01 +08:00
|
|
|
*
|
2026-04-02 17:32:13 +08:00
|
|
|
* @param content - CSV content string (must have at least headers + schema row + 1 data row)
|
|
|
|
|
* @param options - Parsing options
|
|
|
|
|
* @returns CsvParseResult containing parsed data and optional type definitions
|
|
|
|
|
*/
|
|
|
|
|
export function parseCsv(
|
|
|
|
|
content: string,
|
2026-04-05 12:38:33 +08:00
|
|
|
options: CsvLoaderOptions & { resourceName?: string } = {}
|
2026-04-02 17:32:13 +08:00
|
|
|
): CsvParseResult {
|
|
|
|
|
const delimiter = options.delimiter ?? ',';
|
|
|
|
|
const quote = options.quote ?? '"';
|
|
|
|
|
const escape = options.escape ?? '\\';
|
|
|
|
|
const bom = options.bom ?? true;
|
|
|
|
|
const comment = options.comment === false ? undefined : (options.comment ?? '#');
|
|
|
|
|
const trim = options.trim ?? true;
|
|
|
|
|
const emitTypes = options.emitTypes ?? true;
|
2026-04-11 22:56:01 +08:00
|
|
|
const refBaseDir = options.refBaseDir;
|
|
|
|
|
const defaultPrimaryKey = options.defaultPrimaryKey ?? 'id';
|
2026-03-31 13:02:29 +08:00
|
|
|
|
|
|
|
|
const records = parse(content, {
|
|
|
|
|
delimiter,
|
|
|
|
|
quote,
|
|
|
|
|
escape,
|
2026-03-31 14:45:02 +08:00
|
|
|
bom,
|
|
|
|
|
comment,
|
|
|
|
|
trim,
|
2026-04-07 12:11:01 +08:00
|
|
|
skip_empty_lines: true,
|
2026-03-31 13:02:29 +08:00
|
|
|
relax_column_count: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (records.length < 2) {
|
|
|
|
|
throw new Error('CSV must have at least 2 rows: headers and schemas');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const headers = records[0];
|
|
|
|
|
const schemas = records[1];
|
|
|
|
|
|
|
|
|
|
if (headers.length !== schemas.length) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Header count (${headers.length}) does not match schema count (${schemas.length})`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const propertyConfigs: PropertyConfig[] = headers.map((header: string, index: number) => {
|
|
|
|
|
const schemaString = schemas[index];
|
|
|
|
|
const schema = parseSchema(schemaString);
|
2026-04-11 22:56:01 +08:00
|
|
|
|
|
|
|
|
const config: PropertyConfig = {
|
2026-03-31 13:02:29 +08:00
|
|
|
name: header,
|
|
|
|
|
schema,
|
|
|
|
|
validator: createValidator(schema),
|
|
|
|
|
parser: (valueString: string) => parseValue(schema, valueString),
|
|
|
|
|
};
|
2026-04-11 22:56:01 +08:00
|
|
|
|
|
|
|
|
// Check if it's a reference type
|
|
|
|
|
if (schema.type === 'reference') {
|
|
|
|
|
config.isReference = true;
|
|
|
|
|
config.referenceTableName = schema.tableName;
|
|
|
|
|
config.referenceIsArray = schema.isArray;
|
|
|
|
|
// Override parser for reference fields
|
|
|
|
|
config.parser = (valueString: string) => {
|
|
|
|
|
return parseReferenceValue(schema, valueString, refBaseDir, defaultPrimaryKey, options.currentFilePath);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return config;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Collect all referenced tables
|
|
|
|
|
const references = new Set<string>();
|
|
|
|
|
propertyConfigs.forEach(config => {
|
|
|
|
|
if (config.isReference && config.referenceTableName) {
|
|
|
|
|
references.add(config.referenceTableName);
|
|
|
|
|
}
|
2026-03-31 13:02:29 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const dataRows = records.slice(2);
|
|
|
|
|
const objects = dataRows.map((row: string[], rowIndex: number) => {
|
|
|
|
|
const obj: Record<string, unknown> = {};
|
|
|
|
|
propertyConfigs.forEach((config, colIndex) => {
|
|
|
|
|
const rawValue = row[colIndex] ?? '';
|
|
|
|
|
try {
|
|
|
|
|
const parsed = config.parser(rawValue);
|
2026-04-11 22:56:01 +08:00
|
|
|
// Skip validation for reference fields (validation happens during reference resolution)
|
|
|
|
|
if (!config.isReference && !config.validator(parsed)) {
|
2026-03-31 13:02:29 +08:00
|
|
|
throw new Error(
|
|
|
|
|
`Validation failed for property "${config.name}" at row ${rowIndex + 3}: ${rawValue}`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
obj[config.name] = parsed;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error instanceof Error) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Failed to parse property "${config.name}" at row ${rowIndex + 3}, column ${colIndex + 1}: ${error.message}`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return obj;
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-02 17:32:13 +08:00
|
|
|
const result: CsvParseResult = {
|
|
|
|
|
data: objects,
|
|
|
|
|
propertyConfigs,
|
2026-04-11 22:56:01 +08:00
|
|
|
references,
|
2026-04-02 17:32:13 +08:00
|
|
|
};
|
2026-03-31 15:49:05 +08:00
|
|
|
|
2026-03-31 15:19:03 +08:00
|
|
|
if (emitTypes) {
|
2026-04-11 22:56:01 +08:00
|
|
|
result.typeDefinition = generateTypeDefinition(
|
|
|
|
|
options.resourceName || '',
|
|
|
|
|
propertyConfigs,
|
|
|
|
|
references,
|
|
|
|
|
options.currentFilePath
|
|
|
|
|
);
|
2026-04-02 17:32:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate JavaScript module code from CSV content.
|
|
|
|
|
* Returns a string that can be used as a module export.
|
2026-04-07 11:25:02 +08:00
|
|
|
*
|
2026-04-02 17:32:13 +08:00
|
|
|
* @param content - CSV content string
|
|
|
|
|
* @param options - Parsing options
|
|
|
|
|
* @returns JavaScript module code string
|
|
|
|
|
*/
|
|
|
|
|
export function csvToModule(
|
|
|
|
|
content: string,
|
2026-04-05 12:38:33 +08:00
|
|
|
options: CsvLoaderOptions & { resourceName?: string } = {}
|
2026-04-02 17:32:13 +08:00
|
|
|
): { js: string; dts?: string } {
|
|
|
|
|
const result = parseCsv(content, options);
|
2026-04-07 11:25:02 +08:00
|
|
|
|
2026-04-02 17:32:13 +08:00
|
|
|
const json = JSON.stringify(result.data, null, 2);
|
|
|
|
|
const js = `export default ${json};`;
|
2026-04-07 11:25:02 +08:00
|
|
|
|
2026-04-02 17:32:13 +08:00
|
|
|
return {
|
|
|
|
|
js,
|
|
|
|
|
dts: result.typeDefinition,
|
|
|
|
|
};
|
|
|
|
|
}
|