diff --git a/.gitignore b/.gitignore index a572eb1..0561f4c 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ node_modules temp docs/.vitepress/cache storage +fixtures/generated diff --git a/bin/cli.ts b/bin/cli.ts index 2d2f39b..8b8d394 100644 --- a/bin/cli.ts +++ b/bin/cli.ts @@ -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') diff --git a/dts.config.ts b/dts.config.ts index 39fb029..1bf696f 100644 --- a/dts.config.ts +++ b/dts.config.ts @@ -1,6 +1,9 @@ -export default { +import type { DtsGenerationOption } from './src/types' + +const config: DtsGenerationOption = { cwd: './', root: './src', + entrypoints: ['**/*.ts'], outdir: './dist', keepComments: true, clean: true, @@ -8,3 +11,5 @@ export default { // bundle: true, // minify: true, } + +export default config diff --git a/src/config.ts b/src/config.ts index b8a79f9..2ad3a89 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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, diff --git a/src/generate.ts b/src/generate.ts index 98980bc..ae6c002 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -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 { + // console.log('Generating declaration files...', options) try { // Check for isolatedModules setting const isIsolatedDeclarations = await checkIsolatedDeclarations(options) @@ -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) } @@ -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 }) diff --git a/src/types.ts b/src/types.ts index defc7b8..02b02a1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,7 +6,7 @@ export interface DtsGenerationConfig { cwd: string root: string - file: string + entrypoints: string[] outdir: string keepComments: boolean clean: boolean diff --git a/test/dts.test.ts b/test/dts.test.ts index 24abfad..d3260bc 100644 --- a/test/dts.test.ts +++ b/test/dts.test.ts @@ -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'), } @@ -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'), } @@ -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'), } @@ -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'), } @@ -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'), } @@ -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) }