Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 17, 2024
1 parent 8598729 commit c1dbbde
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
21 changes: 12 additions & 9 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
let usedTypes = new Set<string>()
let importMap = new Map<string, Set<string>>()

// Handle re-exports
const reExportRegex = /export\s*(?:\*|\{[^}]*\})\s*from\s*['"]([^'"]+)['"]/g
let reExportMatch
while ((reExportMatch = reExportRegex.exec(fileContent)) !== null) {
declarations += `${reExportMatch[0]}\n`
}

// Capture all imports
const importRegex = /import\s+(?:(type)\s+)?(?:(\{[^}]+\})|(\w+))(?:\s*,\s*(?:(\{[^}]+\})|(\w+)))?\s+from\s+['"]([^'"]+)['"]/g
let importMatch
Expand All @@ -22,7 +29,7 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
const [name, alias] = t.split(' as ').map(s => s.trim())
return { name: name.replace(/^type\s+/, ''), alias: alias || name.replace(/^type\s+/, '') }
})
types.forEach(({ name, alias }) => {
types.forEach(({ name }) => {
importMap.get(from)!.add(name)
})
}
Expand Down Expand Up @@ -80,12 +87,7 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
}

// Apply final formatting
const formattedDeclarations = formatDeclarations(declarations, false)

console.log(`Unformatted declarations for ${filePath}:`, declarations)
console.log(`Extracted declarations for ${filePath}:`, formattedDeclarations)

return formattedDeclarations
return formatDeclarations(declarations, false)
}

export async function extractConfigTypeFromSource(filePath: string): Promise<string> {
Expand Down Expand Up @@ -114,7 +116,7 @@ export async function extractConfigTypeFromSource(filePath: string): Promise<str
declarations += `export declare const ${name}: ${type.trim()}\n`
}

console.log(`Extracted config declarations for ${filePath}:`, declarations)
// console.log(`Extracted config declarations for ${filePath}:`, declarations)
return declarations.trim() + '\n'
} catch (error) {
console.error(`Error extracting config declarations from ${filePath}:`, error)
Expand All @@ -133,6 +135,7 @@ export async function extractIndexTypeFromSource(filePath: string): Promise<stri
declarations += `${match[0]}\n`
}

console.log(`Extracted index declarations for ${filePath}:`, declarations)
// console.log(`Extracted index declarations for ${filePath}:`, declarations)

return declarations.trim() + '\n'
}
20 changes: 5 additions & 15 deletions src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { Result } from 'neverthrow'
import type { DtsGenerationConfig, DtsGenerationOption } from './types'
import { rm, mkdir } from 'node:fs/promises'
import { join, relative, dirname } from 'node:path'
import { err, ok } from 'neverthrow'
import { config } from './config'
import { writeToFile, getAllTypeScriptFiles, checkIsolatedDeclarations, formatDeclarations } from './utils'
import { extractTypeFromSource, extractConfigTypeFromSource, extractIndexTypeFromSource } from './extract'
Expand All @@ -17,15 +15,15 @@ export async function generateDeclarationsFromFiles(options: DtsGenerationConfig
}

if (options.clean) {
console.log('Cleaning output directory...')
// console.log('Cleaning output directory...')
await rm(options.outdir, { recursive: true, force: true })
}

const files = await getAllTypeScriptFiles(options.root)
console.log('Found the following TypeScript files:', files)
// console.log('Found the following TypeScript files:', files)

for (const file of files) {
console.log(`Processing file: ${file}`)
// console.log(`Processing file: ${file}`)
const fileDeclarations = await extractTypeFromSource(file)

if (fileDeclarations) {
Expand All @@ -38,13 +36,13 @@ export async function generateDeclarationsFromFiles(options: DtsGenerationConfig
// Write the declarations without additional formatting
await writeToFile(outputPath, fileDeclarations)

console.log(`Generated ${outputPath}`)
// console.log(`Generated ${outputPath}`)
} else {
console.warn(`No declarations extracted for ${file}`)
}
}

console.log('Declaration file generation complete')
// console.log('Declaration file generation complete')
} catch (error) {
console.error('Error generating declarations:', error)
}
Expand All @@ -53,11 +51,3 @@ export async function generateDeclarationsFromFiles(options: DtsGenerationConfig
export async function generate(options?: DtsGenerationOption): Promise<void> {
await generateDeclarationsFromFiles({ ...config, ...options })
}

function validateOptions(options: unknown): Result<DtsGenerationOption, Error> {
if (typeof options === 'object' && options !== null) {
return ok(options as DtsGenerationOption)
}

return err(new Error('Invalid options'))
}

0 comments on commit c1dbbde

Please sign in to comment.