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 7c2ced6 commit f15f20c
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 30 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"eslint-plugin-yml": "^1.14.0",
"local-pkg": "^0.5.0",
"neverthrow": "^8.0.0",
"tinyglobby": "^0.2.9",
"vitepress": "^1.4.0"
},
"simple-git-hooks": {
Expand Down
14 changes: 14 additions & 0 deletions scripts/generate-dts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import path from 'path'
import { generate } from '../src'

console.log('Generating output...', path.join(__dirname, '..'))

generate({
cwd: path.join(__dirname, '..'),
root: path.join(__dirname, '..', 'src'),
outdir: path.join(__dirname, '..', 'dist'),
clean: true,
tsconfigPath: path.join(__dirname, '..', 'tsconfig.json'),
})

console.log('Generated')
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const config: DtsGenerationConfig = (await loadConfig({
defaultConfig: {
cwd: process.cwd(),
root: './src',
file: '**/*.ts',
outdir: './dist',
keepComments: true,
clean: true,
Expand Down
11 changes: 9 additions & 2 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { join, relative, dirname } from 'node:path'
import { config } from './config'
import { writeToFile, getAllTypeScriptFiles, checkIsolatedDeclarations } from './utils'
import { extractTypeFromSource } from './extract'
import { glob } from 'tinyglobby'

export async function generateDeclarationsFromFiles(options: DtsGenerationConfig = config): Promise<void> {
export async function generateDeclarationsFromFiles(options: DtsGenerationConfig): Promise<void> {
try {
// Check for isolatedModules setting
const isIsolatedDeclarations = await checkIsolatedDeclarations(options)
Expand All @@ -19,7 +20,13 @@ export async function generateDeclarationsFromFiles(options: DtsGenerationConfig
await rm(options.outdir, { recursive: true, force: true })
}

const files = await getAllTypeScriptFiles(options.root)
let files: string[]
if (options.file) {
files = await glob(options.file, { cwd: options.root ?? options.cwd, absolute: true })
} else {
files = await getAllTypeScriptFiles(options.root)
}

// console.log('Found the following TypeScript files:', files)

for (const file of files) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
export interface DtsGenerationConfig {
cwd: string
root: string
file: string
outdir: string
keepComments: boolean
clean: boolean
Expand Down
75 changes: 47 additions & 28 deletions test/dts.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,59 @@
import { describe, it, expect } from 'bun:test'
import { describe, it, expect, afterEach } from 'bun:test'
import { join } from 'node:path'
import { readdir } from 'node:fs/promises'
import { generate } from '../src/generate'
import type { DtsGenerationConfig } from '../src/types'
import type { DtsGenerationOption } from '../src/types'
import { rm } from 'node:fs/promises'

describe('dts-generation', () => {
const cwdDir = join(__dirname, '..')
const inputDir = join(cwdDir, 'fixtures/input')
const outputDir = join(cwdDir, 'fixtures/output')

const config: DtsGenerationConfig = {
cwd: cwdDir,
root: inputDir,
outdir: outputDir,
keepComments: true,
clean: false,
tsconfigPath: join(cwdDir, 'tsconfig.json'),
}

it('should generate correct type declarations for all input files', async () => {
// Generate the declaration files
const testDir = join(__dirname, '../fixtures')
const inputDir = join(testDir, 'input')
const outputDir = join(testDir, 'output')
const generatedDir = join(testDir, 'generated')

it('should properly generate types for example-1', async () => {
const config: DtsGenerationOption = {
file: join(__dirname, '..', 'tsconfig.json'),
outdir: generatedDir,
clean: true,
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
}

await generate(config)

// Get all input files
const inputFiles = await readdir(inputDir)
const example = 'example-1'
const outputPath = join(outputDir, `${example}.d.ts`)
const generatedPath = join(generatedDir, `${example}.d.ts`)

const expectedContent = await Bun.file(outputPath).text()
const generatedContent = await Bun.file(generatedPath).text()

for (const file of inputFiles) {
const outputPath = join(outputDir, file.replace('.ts', '.d.ts'))
const generatedPath = join(outputDir, file.replace('.ts', '.d.ts'))
expect(generatedContent).toBe(expectedContent)

// Read expected and generated content
const expectedContent = await Bun.file(outputPath).text()
const generatedContent = await Bun.file(generatedPath).text()
})

it('should properly generate types for example-2', async () => {
await testExample('example-2')

Check failure on line 35 in test/dts.test.ts

View workflow job for this annotation

GitHub Actions / test

ReferenceError: Can't find variable: testExample

at /home/runner/work/dts-generation/dts-generation/test/dts.test.ts:35:11 at /home/runner/work/dts-generation/dts-generation/test/dts.test.ts:34:54
})

it('should properly generate types for example-3', async () => {
await testExample('example-3')

Check failure on line 39 in test/dts.test.ts

View workflow job for this annotation

GitHub Actions / test

ReferenceError: Can't find variable: testExample

at /home/runner/work/dts-generation/dts-generation/test/dts.test.ts:39:11 at /home/runner/work/dts-generation/dts-generation/test/dts.test.ts:38:54
})

it('should properly generate types for example-4', async () => {
await testExample('example-4')

Check failure on line 43 in test/dts.test.ts

View workflow job for this annotation

GitHub Actions / test

ReferenceError: Can't find variable: testExample

at /home/runner/work/dts-generation/dts-generation/test/dts.test.ts:43:11 at /home/runner/work/dts-generation/dts-generation/test/dts.test.ts:42:54
})

it('should properly generate types for example-5', async () => {
await testExample('example-5')

Check failure on line 47 in test/dts.test.ts

View workflow job for this annotation

GitHub Actions / test

ReferenceError: Can't find variable: testExample

at /home/runner/work/dts-generation/dts-generation/test/dts.test.ts:47:11 at /home/runner/work/dts-generation/dts-generation/test/dts.test.ts:46:54
})

// Compare the contents
expect(generatedContent.trim()).toBe(expectedContent.trim())
afterEach(async () => {
// Clean up generated files
try {
await rm(generatedDir, { recursive: true, force: true })
console.log('Cleaned up generated files')
} catch (error) {
console.error('Error cleaning up generated files:', error)
}
})
})

0 comments on commit f15f20c

Please sign in to comment.