1
+ import { type } from 'arktype' ;
1
2
import fs from 'fs-extra' ;
2
3
import kleur from 'kleur' ;
3
4
import path from 'path' ;
4
5
import yargs from 'yargs' ;
5
- import { type Options , type Target } from './types ' ;
6
+ import { config , type Config , type Target , type TargetOptions } from './schema ' ;
6
7
import { loadConfig } from './utils/loadConfig' ;
7
8
import * as logger from './utils/logger' ;
8
9
import { run } from './utils/workerize' ;
@@ -39,46 +40,21 @@ export async function build(argv: Argv) {
39
40
) ;
40
41
}
41
42
42
- const options : Options = result ! . config ;
43
+ const parsed = config ( result . config ) ;
43
44
44
- if ( ! options . targets ?. length ) {
45
+ if ( parsed instanceof type . errors ) {
45
46
throw new Error (
46
- `No 'targets' found in the configuration in '${ path . relative (
47
- root ,
48
- result ! . filepath
49
- ) } '.`
50
- ) ;
51
- }
52
-
53
- const source = options . source ;
54
-
55
- if ( ! source ) {
56
- throw new Error (
57
- `No 'source' option found in the configuration in '${ path . relative (
58
- root ,
59
- result ! . filepath
60
- ) } '.`
47
+ `Invalid configuration in ${ result . filepath } : ${ parsed . summary } `
61
48
) ;
62
49
}
63
50
64
- const output = options . output ;
51
+ const { source , output, targets , exclude } = parsed ;
65
52
66
- if ( ! output ) {
67
- throw new Error (
68
- `No 'output' option found in the configuration in '${ path . relative (
69
- root ,
70
- result ! . filepath
71
- ) } '.`
72
- ) ;
73
- }
74
-
75
- const exclude = options . exclude ?? '**/{__tests__,__fixtures__,__mocks__}/**' ;
76
-
77
- const commonjs = options . targets ?. some ( ( t ) =>
53
+ const commonjs = targets . some ( ( t ) =>
78
54
Array . isArray ( t ) ? t [ 0 ] === 'commonjs' : t === 'commonjs'
79
55
) ;
80
56
81
- const module = options . targets ? .some ( ( t ) =>
57
+ const module = targets . some ( ( t ) =>
82
58
Array . isArray ( t ) ? t [ 0 ] === 'module' : t === 'module'
83
59
) ;
84
60
@@ -94,68 +70,69 @@ export async function build(argv: Argv) {
94
70
source,
95
71
output,
96
72
exclude,
97
- options ,
73
+ config : parsed ,
98
74
variants,
99
75
} ) ;
100
76
} else {
101
77
await Promise . all (
102
- options . targets ? .map ( ( target ) =>
78
+ targets . map ( ( target ) =>
103
79
buildTarget ( {
104
80
root,
105
- target,
81
+ target : Array . isArray ( target ) ? target [ 0 ] : target ,
106
82
source,
107
83
output,
108
84
exclude,
109
- options ,
85
+ config : parsed ,
110
86
variants,
111
87
} )
112
88
)
113
89
) ;
114
90
}
115
91
}
116
92
117
- async function buildTarget ( {
93
+ async function buildTarget < T extends Target > ( {
118
94
root,
119
95
target,
120
96
source,
121
97
output,
122
98
exclude,
123
- options ,
99
+ config ,
124
100
variants,
125
101
} : {
126
102
root : string ;
127
- target : Exclude < Options [ 'targets' ] , undefined > [ number ] ;
103
+ target : T ;
128
104
source : string ;
129
105
output : string ;
130
106
exclude : string ;
131
- options : Options ;
107
+ config : Config ;
132
108
variants : {
133
109
commonjs ?: boolean ;
134
110
module ?: boolean ;
135
111
} ;
136
112
} ) {
137
- const targetName = Array . isArray ( target ) ? target [ 0 ] : target ;
138
- const targetOptions = Array . isArray ( target ) ? target [ 1 ] : undefined ;
113
+ const options = config . targets
114
+ . map ( ( t ) => ( Array . isArray ( t ) ? t : ( [ t , undefined ] as const ) ) )
115
+ . find ( ( t ) => t [ 0 ] === target ) ?. [ 1 ] ;
139
116
140
- const report = logger . grouped ( targetName ) ;
117
+ const report = logger . grouped ( target ) ;
141
118
142
- switch ( targetName ) {
119
+ switch ( target ) {
143
120
case 'commonjs' :
144
121
case 'module' :
145
- await run ( targetName , {
122
+ await run ( target , {
146
123
root,
147
124
source : path . resolve ( root , source ) ,
148
- output : path . resolve ( root , output , targetName ) ,
125
+ output : path . resolve ( root , output , target ) ,
149
126
exclude,
150
- options : targetOptions ,
127
+ options : options as TargetOptions < 'commonjs' | 'module' > ,
151
128
variants,
152
129
report,
153
130
} ) ;
154
131
break ;
155
132
case 'typescript' :
156
133
{
157
134
const esm =
158
- options . targets ?. some ( ( t ) => {
135
+ config . targets ?. some ( ( t ) => {
159
136
if ( Array . isArray ( t ) ) {
160
137
const [ name , options ] = t ;
161
138
@@ -171,7 +148,7 @@ async function buildTarget({
171
148
root,
172
149
source : path . resolve ( root , source ) ,
173
150
output : path . resolve ( root , output , 'typescript' ) ,
174
- options : targetOptions ,
151
+ options : options as TargetOptions < 'typescript' > ,
175
152
esm,
176
153
variants,
177
154
report,
@@ -182,19 +159,18 @@ async function buildTarget({
182
159
await run ( 'codegen' , {
183
160
root,
184
161
source : path . resolve ( root , source ) ,
185
- output : path . resolve ( root , output , 'typescript' ) ,
186
162
report,
187
163
} ) ;
188
164
break ;
189
165
case 'custom' :
190
166
await run ( 'custom' , {
191
- options : targetOptions ,
167
+ root ,
192
168
source : path . resolve ( root , source ) ,
169
+ options : options as TargetOptions < 'custom' > ,
193
170
report,
194
- root,
195
171
} ) ;
196
172
break ;
197
173
default :
198
- throw new Error ( `Invalid target ${ kleur . blue ( targetName ) } .` ) ;
174
+ throw new Error ( `Invalid target ${ kleur . blue ( target ) } .` ) ;
199
175
}
200
176
}
0 commit comments