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 75233fa commit f2fc599
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ node_modules
temp
docs/.vitepress/cache
storage
fixtures/generated
2 changes: 1 addition & 1 deletion bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { version } from '../package.json'

// import { generate } from '../src/generate'

const cli = new CAC('dts')
const cli = new CAC('dtsx')

cli
.command('generate', 'Start the Reverse Proxy Server')
Expand Down
7 changes: 6 additions & 1 deletion dts.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
export default {
import type { DtsGenerationOption } from './src/types'

const config: DtsGenerationOption = {
cwd: './',
root: './src',
entrypoints: ['**/*.ts'],
outdir: './dist',
keepComments: true,
clean: true,

// bundle: true,
// minify: true,
}

export default config
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const config: DtsGenerationConfig = (await loadConfig({
defaultConfig: {
cwd: process.cwd(),
root: './src',
file: '**/*.ts',
entrypoints: ['**/*.ts'],
outdir: './dist',
keepComments: true,
clean: true,
Expand Down
10 changes: 6 additions & 4 deletions src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { DtsGenerationConfig, DtsGenerationOption } from './types'
import { rm, mkdir } from 'node:fs/promises'
import { join, relative, dirname } from 'node:path'
import { join, relative, dirname, parse } 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): Promise<void> {
// console.log('Generating declaration files...', options)
try {
// Check for isolatedModules setting
const isIsolatedDeclarations = await checkIsolatedDeclarations(options)
Expand All @@ -21,8 +22,8 @@ export async function generateDeclarationsFromFiles(options: DtsGenerationConfig
}

let files: string[]
if (options.file) {
files = await glob(options.file, { cwd: options.root ?? options.cwd, absolute: true })
if (options.entrypoints) {
files = await glob(options.entrypoints, { cwd: options.root ?? options.cwd, absolute: true })
} else {
files = await getAllTypeScriptFiles(options.root)
}
Expand All @@ -35,7 +36,8 @@ export async function generateDeclarationsFromFiles(options: DtsGenerationConfig

if (fileDeclarations) {
const relativePath = relative(options.root, file)
const outputPath = join(options.outdir, relativePath.replace(/\.ts$/, '.d.ts'))
const parsedPath = parse(relativePath)
const outputPath = join(options.outdir, `${parsedPath.name}.d.ts`)

// Ensure the directory exists
await mkdir(dirname(outputPath), { recursive: true })
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
export interface DtsGenerationConfig {
cwd: string
root: string
file: string
entrypoints: string[]
outdir: string
keepComments: boolean
clean: boolean
Expand Down
21 changes: 10 additions & 11 deletions test/dts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ describe('dts-generation', () => {
const example = 'example-1'

const config: DtsGenerationOption = {
file: join(inputDir, `${example}.ts`),
entrypoints: [join(inputDir, `${example}.ts`)],
outdir: generatedDir,
clean: true,
clean: false,
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
}

Expand All @@ -35,9 +35,9 @@ describe('dts-generation', () => {
const example = 'example-2'

const config: DtsGenerationOption = {
file: join(inputDir, `${example}.ts`),
entrypoints: [join(inputDir, `${example}.ts`)],
outdir: generatedDir,
clean: true,
clean: false,
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
}

Expand All @@ -56,9 +56,9 @@ describe('dts-generation', () => {
const example = 'example-3'

const config: DtsGenerationOption = {
file: join(inputDir, `${example}.ts`),
entrypoints: [join(inputDir, `${example}.ts`)],
outdir: generatedDir,
clean: true,
clean: false,
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
}

Expand All @@ -77,9 +77,9 @@ describe('dts-generation', () => {
const example = 'example-4'

const config: DtsGenerationOption = {
file: join(inputDir, `${example}.ts`),
entrypoints: [join(inputDir, `${example}.ts`)],
outdir: generatedDir,
clean: true,
clean: false,
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
}

Expand All @@ -98,9 +98,9 @@ describe('dts-generation', () => {
const example = 'example-5'

const config: DtsGenerationOption = {
file: join(inputDir, `${example}.ts`),
entrypoints: [join(inputDir, `${example}.ts`)],
outdir: generatedDir,
clean: true,
clean: false,
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
}

Expand All @@ -119,7 +119,6 @@ describe('dts-generation', () => {
// 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)
}
Expand Down

0 comments on commit f2fc599

Please sign in to comment.