Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 21, 2024
1 parent 23937c3 commit 98a7bd6
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,18 @@ function processConstDeclaration(declaration: string): string {
const name = firstLine.split('export const')[1].split('=')[0].trim().split(':')[0].trim()

const properties = lines.slice(1, -1).map((line) => {
const [key, ...valueParts] = line.split(':')
const value = valueParts.join(':').trim().replace(',', '')
return ` ${key.trim()}: ${value};`
const commentIndex = line.indexOf('//')
const hasComment = commentIndex !== -1
const mainPart = hasComment ? line.slice(0, commentIndex) : line
const comment = hasComment ? line.slice(commentIndex) : ''

const [key, ...valueParts] = mainPart.split(':')
let value = valueParts.join(':').trim()
if (value.endsWith(',')) {
value = value.slice(0, -1)
}

return ` ${key.trim()}: ${value}${comment};`
}).join('\n')

return `export declare const ${name}: {\n${properties}\n};`
Expand Down Expand Up @@ -193,6 +202,9 @@ function cleanOutput(output: string): string {
.replace(/;\n\}/g, ';\n}')
.replace(/\{;/g, '{')
.replace(/\};\n/g, '}\n\n') // Add an extra line break after each declaration
.replace(/\}\n(?!$)/g, '}\n\n') // Add an extra line break after closing braces, except for the last one
.replace(/\n{3,}/g, '\n\n') // Replace three or more consecutive newlines with two newlines
.replace(/;\n(\s*)\}/g, ';\n$1\n$1}') // Ensure closing bracket is on its own line
.trim()
console.log('Cleaned output:', result)
return result
Expand Down

0 comments on commit 98a7bd6

Please sign in to comment.