1
+ #!/usr/bin/env bun
2
+
3
+ import { parseArgs } from 'util'
4
+ import { generate } from './generator'
5
+ import type { DtsGenerationOption } from './types'
6
+ import { config as defaultConfig } from './config'
7
+ import { resolve } from 'node:path'
8
+
9
+ // Parse command line arguments
10
+ const { values, positionals } = parseArgs ( {
11
+ args : Bun . argv ,
12
+ options : {
13
+ help : {
14
+ type : 'boolean' ,
15
+ short : 'h' ,
16
+ } ,
17
+ version : {
18
+ type : 'boolean' ,
19
+ short : 'v' ,
20
+ } ,
21
+ root : {
22
+ type : 'string' ,
23
+ short : 'r' ,
24
+ } ,
25
+ outdir : {
26
+ type : 'string' ,
27
+ short : 'o' ,
28
+ } ,
29
+ clean : {
30
+ type : 'boolean' ,
31
+ short : 'c' ,
32
+ } ,
33
+ 'keep-comments' : {
34
+ type : 'boolean' ,
35
+ } ,
36
+ tsconfig : {
37
+ type : 'string' ,
38
+ } ,
39
+ verbose : {
40
+ type : 'boolean' ,
41
+ } ,
42
+ 'output-structure' : {
43
+ type : 'string' ,
44
+ } ,
45
+ } ,
46
+ allowPositionals : true ,
47
+ } )
48
+
49
+ // Show help
50
+ if ( values . help ) {
51
+ console . log ( `
52
+ dtsx - A modern, fast .d.ts generation tool
53
+
54
+ Usage:
55
+ dtsx [options] [entrypoints...]
56
+
57
+ Options:
58
+ -h, --help Show this help message
59
+ -v, --version Show version
60
+ -r, --root <dir> Root directory (default: ./src)
61
+ -o, --outdir <dir> Output directory (default: ./dist)
62
+ -c, --clean Clean output directory before generation
63
+ --keep-comments Keep comments in output (default: true)
64
+ --tsconfig <path> Path to tsconfig.json
65
+ --verbose Verbose output
66
+ --output-structure Output structure: 'mirror' or 'flat' (default: mirror)
67
+
68
+ Examples:
69
+ dtsx Generate .d.ts files for all .ts files in src/
70
+ dtsx -r lib -o types Generate from lib/ to types/
71
+ dtsx src/index.ts Generate only for specific file
72
+ ` )
73
+ process . exit ( 0 )
74
+ }
75
+
76
+ // Show version
77
+ if ( values . version ) {
78
+ const pkg = await import ( '../package.json' )
79
+ console . log ( pkg . version )
80
+ process . exit ( 0 )
81
+ }
82
+
83
+ // Build configuration
84
+ const options : DtsGenerationOption = {
85
+ root : values . root || defaultConfig . root ,
86
+ outdir : values . outdir || defaultConfig . outdir ,
87
+ clean : values . clean ?? defaultConfig . clean ,
88
+ keepComments : values [ 'keep-comments' ] ?? defaultConfig . keepComments ,
89
+ tsconfigPath : values . tsconfig || defaultConfig . tsconfigPath ,
90
+ verbose : values . verbose ?? defaultConfig . verbose ,
91
+ outputStructure : ( values [ 'output-structure' ] as 'mirror' | 'flat' ) || defaultConfig . outputStructure ,
92
+ }
93
+
94
+ // Handle entrypoints
95
+ if ( positionals . length > 2 ) { // First two are bun and script path
96
+ const entrypoints = positionals . slice ( 2 )
97
+ options . entrypoints = entrypoints . map ( e => {
98
+ // If it's a file path, convert to glob pattern relative to root
99
+ if ( e . endsWith ( '.ts' ) ) {
100
+ const relativePath = resolve ( process . cwd ( ) , e )
101
+ return relativePath
102
+ }
103
+ return e
104
+ } )
105
+ } else {
106
+ options . entrypoints = defaultConfig . entrypoints
107
+ }
108
+
109
+ // Run generation
110
+ try {
111
+ await generate ( options )
112
+ } catch ( error ) {
113
+ console . error ( 'Error generating .d.ts files:' , error )
114
+ process . exit ( 1 )
115
+ }
0 commit comments