@@ -2,10 +2,11 @@ import fs from 'fs-extra';
22import kleur from 'kleur' ;
33import path from 'path' ;
44import yargs from 'yargs' ;
5- import { type Options , type Target } from './types' ;
65import { loadConfig } from './utils/loadConfig' ;
76import * as logger from './utils/logger' ;
87import { run } from './utils/workerize' ;
8+ import { config , type Config , type Target , type TargetOptions } from './schema' ;
9+ import { type } from 'arktype' ;
910
1011export const args = {
1112 target : {
@@ -39,46 +40,19 @@ export async function build(argv: Argv) {
3940 ) ;
4041 }
4142
42- const options : Options = result ! . config ;
43+ const parsed = config ( result . config ) ;
4344
44- if ( ! options . targets ?. length ) {
45- 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- ) } '.`
61- ) ;
45+ if ( parsed instanceof type . errors ) {
46+ throw new Error ( `Invalid configuration: ${ parsed . summary } ` ) ;
6247 }
6348
64- const output = options . output ;
49+ const { source , output, targets , exclude } = parsed ;
6550
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 ) =>
51+ const commonjs = targets . some ( ( t ) =>
7852 Array . isArray ( t ) ? t [ 0 ] === 'commonjs' : t === 'commonjs'
7953 ) ;
8054
81- const module = options . targets ? .some ( ( t ) =>
55+ const module = targets . some ( ( t ) =>
8256 Array . isArray ( t ) ? t [ 0 ] === 'module' : t === 'module'
8357 ) ;
8458
@@ -94,66 +68,67 @@ export async function build(argv: Argv) {
9468 source,
9569 output,
9670 exclude,
97- options ,
71+ config : parsed ,
9872 variants,
9973 } ) ;
10074 } else {
101- for ( const target of options . targets ! ) {
75+ for ( const target of targets ) {
10276 buildTarget ( {
10377 root,
104- target,
78+ target : Array . isArray ( target ) ? target [ 0 ] : target ,
10579 source,
10680 output,
10781 exclude,
108- options ,
82+ config : parsed ,
10983 variants,
11084 } ) ;
11185 }
11286 }
11387}
11488
115- async function buildTarget ( {
89+ async function buildTarget < T extends Target > ( {
11690 root,
11791 target,
11892 source,
11993 output,
12094 exclude,
121- options ,
95+ config ,
12296 variants,
12397} : {
12498 root : string ;
125- target : Exclude < Options [ 'targets' ] , undefined > [ number ] ;
99+ target : T ;
126100 source : string ;
127101 output : string ;
128102 exclude : string ;
129- options : Options ;
103+ config : Config ;
130104 variants : {
131105 commonjs ?: boolean ;
132106 module ?: boolean ;
133107 } ;
134108} ) {
135- const targetName = Array . isArray ( target ) ? target [ 0 ] : target ;
136- const targetOptions = Array . isArray ( target ) ? target [ 1 ] : undefined ;
109+ const options = config . targets
110+ . map ( ( t ) => ( Array . isArray ( t ) ? t : ( [ t , undefined ] as const ) ) )
111+ . find ( ( t ) => t [ 0 ] === target ) ?. [ 1 ] ;
137112
138- const report = logger . grouped ( targetName ) ;
113+ const report = logger . grouped ( target ) ;
139114
140- switch ( targetName ) {
115+ switch ( target ) {
141116 case 'commonjs' :
142117 case 'module' :
143- await run ( targetName , {
118+ await run ( target , {
144119 root,
145120 source : path . resolve ( root , source ) ,
146- output : path . resolve ( root , output , targetName ) ,
121+ output : path . resolve ( root , output , target ) ,
147122 exclude,
148- options : targetOptions ,
123+ options : options as TargetOptions < 'commonjs' | 'module' > ,
149124 variants,
150125 report,
151126 } ) ;
152127 break ;
153128 case 'typescript' :
154129 {
155130 const esm =
156- options . targets ?. some ( ( t ) => {
131+ config . targets ?. some ( ( t ) => {
157132 if ( Array . isArray ( t ) ) {
158133 const [ name , options ] = t ;
159134
@@ -169,7 +144,7 @@ async function buildTarget({
169144 root,
170145 source : path . resolve ( root , source ) ,
171146 output : path . resolve ( root , output , 'typescript' ) ,
172- options : targetOptions ,
147+ options : options as TargetOptions < 'typescript' > ,
173148 esm,
174149 variants,
175150 report,
@@ -180,19 +155,18 @@ async function buildTarget({
180155 await run ( 'codegen' , {
181156 root,
182157 source : path . resolve ( root , source ) ,
183- output : path . resolve ( root , output , 'typescript' ) ,
184158 report,
185159 } ) ;
186160 break ;
187161 case 'custom' :
188162 await run ( 'custom' , {
189- options : targetOptions ,
163+ root ,
190164 source : path . resolve ( root , source ) ,
165+ options : options as TargetOptions < 'custom' > ,
191166 report,
192- root,
193167 } ) ;
194168 break ;
195169 default :
196- throw new Error ( `Invalid target ${ kleur . blue ( targetName ) } .` ) ;
170+ throw new Error ( `Invalid target ${ kleur . blue ( target ) } .` ) ;
197171 }
198172}
0 commit comments