Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
chore: wip
  • Loading branch information
chrisbbreuer committed Oct 17, 2024
1 parent 08f015a commit 98f08ef
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 53 deletions.
50 changes: 0 additions & 50 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,53 +89,3 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
// Apply final formatting
return formatDeclarations(declarations, false)
}

export async function extractConfigTypeFromSource(filePath: string): Promise<string> {
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<string> {
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'
}
4 changes: 2 additions & 2 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 98f08ef

Please sign in to comment.