From 98f08ef82e7f4408edad2018e390079446702783 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 17 Oct 2024 19:29:25 +0200 Subject: [PATCH] chore: wip chore: wip --- src/extract.ts | 50 ------------------------------------------------- src/generate.ts | 4 ++-- src/utils.ts | 2 +- 3 files changed, 3 insertions(+), 53 deletions(-) diff --git a/src/extract.ts b/src/extract.ts index 7131fcf..6e7da52 100644 --- a/src/extract.ts +++ b/src/extract.ts @@ -89,53 +89,3 @@ export async function extractTypeFromSource(filePath: string): Promise { // Apply final formatting return formatDeclarations(declarations, false) } - -export async function extractConfigTypeFromSource(filePath: string): Promise { - const fileContent = await readFile(filePath, 'utf-8') - let declarations = '' - - try { - // Handle type imports - const importRegex = /import\s+type\s*\{([^}]+)\}\s*from\s*['"]([^'"]+)['"]/g - let importMatch - while ((importMatch = importRegex.exec(fileContent)) !== null) { - const [, types, from] = importMatch - const typeList = types.split(',').map(t => t.trim()) - declarations += `import type { ${typeList.join(', ')} } from '${from}'\n` - } - - if (declarations) { - declarations += '\n' - } - - // Handle exports - const exportRegex = /export\s+const\s+(\w+)\s*:\s*([^=]+)\s*=/g - let exportMatch - while ((exportMatch = exportRegex.exec(fileContent)) !== null) { - const [, name, type] = exportMatch - declarations += `export declare const ${name}: ${type.trim()}\n` - } - - // console.log(`Extracted config declarations for ${filePath}:`, declarations) - return declarations.trim() + '\n' - } catch (error) { - console.error(`Error extracting config declarations from ${filePath}:`, error) - return '' - } -} - -export async function extractIndexTypeFromSource(filePath: string): Promise { - const fileContent = await readFile(filePath, 'utf-8') - let declarations = '' - - // Handle re-exports - const reExportRegex = /export\s*(?:\*|\{[^}]*\})\s*from\s*['"]([^'"]+)['"]/g - let match - while ((match = reExportRegex.exec(fileContent)) !== null) { - declarations += `${match[0]}\n` - } - - // console.log(`Extracted index declarations for ${filePath}:`, declarations) - - return declarations.trim() + '\n' -} diff --git a/src/generate.ts b/src/generate.ts index 8c7531c..55a22e6 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -2,8 +2,8 @@ import type { DtsGenerationConfig, DtsGenerationOption } from './types' import { rm, mkdir } from 'node:fs/promises' import { join, relative, dirname } from 'node:path' import { config } from './config' -import { writeToFile, getAllTypeScriptFiles, checkIsolatedDeclarations, formatDeclarations } from './utils' -import { extractTypeFromSource, extractConfigTypeFromSource, extractIndexTypeFromSource } from './extract' +import { writeToFile, getAllTypeScriptFiles, checkIsolatedDeclarations } from './utils' +import { extractTypeFromSource } from './extract' export async function generateDeclarationsFromFiles(options: DtsGenerationConfig = config): Promise { try { diff --git a/src/utils.ts b/src/utils.ts index 75f34bd..74a95e5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -43,7 +43,7 @@ export function formatDeclarations(declarations: string, isConfigFile: boolean): .replace(/export (interface|type) ([^\{]+)\s*\{\s*\n/g, 'export $1 $2 {\n') .replace(/\n\s*\}/g, '\n}') .replace(/\/\*\*\n([^*]*)(\n \*\/)/g, (match, content) => { - const formattedContent = content.split('\n').map(line => ` *${line.trim() ? ' ' + line.trim() : ''}`).join('\n') + const formattedContent = content.split('\n').map((line: string) => ` *${line.trim() ? ' ' + line.trim() : ''}`).join('\n') return `/**\n${formattedContent}\n */` }) .trim() + '\n'