@@ -15,38 +15,49 @@ interface Export {
1515 default : string ;
1616}
1717
18- const packageJsonSchema = yup . object ( {
19- name : yup . string ( ) . required ( ) ,
20- exports : yup . lazy ( ( value ) =>
21- yup
22- . object (
23- typeof value === ' object'
24- ? Object . entries ( value ) . reduce ( ( acc , [ key , keyValue ] ) => {
25- if ( typeof keyValue === 'object' ) {
26- acc [ key ] = yup
27- . object ( {
18+ const createPackageJsonSchema = ( logger : Logger ) =>
19+ yup . object ( {
20+ name : yup . string ( ) . required ( ) ,
21+ exports : yup . lazy ( ( value ) =>
22+ yup
23+ . object (
24+ typeof value === 'object'
25+ ? Object . entries ( value ) . reduce ( ( acc , [ key , keyValue ] ) => {
26+ if ( typeof keyValue === 'object' ) {
27+ acc [ key ] = yup . object ( {
2828 types : yup . string ( ) . optional ( ) ,
2929 source : yup . string ( ) . required ( ) ,
3030 module : yup . string ( ) . optional ( ) ,
3131 import : yup . string ( ) . required ( ) ,
3232 require : yup . string ( ) . required ( ) ,
3333 default : yup . string ( ) . required ( ) ,
34- } )
35- . noUnknown ( true ) ;
36- } else {
37- acc [ key ] = yup
38- . string ( )
39- . matches ( / ^ \. \/ .* \. ( j s o n | d \. t s ) $ / )
40- . required ( ) ;
41- }
42-
43- return acc ;
44- } , { } as Record < string , yup . SchemaOf < string > | yup . SchemaOf < Export > > )
45- : undefined
46- )
47- . optional ( )
48- ) ,
49- } ) ;
34+ } ) ;
35+ } else {
36+ acc [ key ] = yup
37+ . string ( )
38+ . test (
39+ 'warn-if-not-matching' ,
40+ 'Warning: The file does not match the expected pattern (./*.json or ./*.d.ts)' ,
41+ ( innervalue ) => {
42+ const isValid = / ^ \. \/ .* \. ( j s o n | d \. t s ) $ / . test ( innervalue || '' ) ;
43+ if ( ! isValid ) {
44+ logger . warn (
45+ `Warning: '${ key } ' in 'exports' does not match the expected pattern.`
46+ ) ;
47+ }
48+ return true ; // Always return true to pass validation
49+ }
50+ )
51+ . required ( ) ;
52+ }
53+
54+ return acc ;
55+ } , { } as Record < string , yup . SchemaOf < string > | yup . SchemaOf < Export > > )
56+ : undefined
57+ )
58+ . optional ( )
59+ ) ,
60+ } ) ;
5061
5162/**
5263 * @description being a task to load the package.json starting from the current working directory
@@ -69,13 +80,21 @@ const loadPkg = async ({ cwd, logger }: { cwd: string; logger: Logger }): Promis
6980 return pkg ;
7081} ;
7182
72- type PackageJson = yup . Asserts < typeof packageJsonSchema > ;
83+ type PackageJson = yup . Asserts < ReturnType < typeof createPackageJsonSchema > > ;
7384
7485/**
7586 * @description validate the package.json against a standardised schema using `yup`.
7687 * If the validation fails, the process will throw with an appropriate error message.
7788 */
78- const validatePkg = async ( { pkg } : { pkg : object } ) : Promise < PackageJson > => {
89+ const validatePkg = async ( {
90+ pkg,
91+ logger,
92+ } : {
93+ pkg : object ;
94+ logger : Logger ;
95+ } ) : Promise < PackageJson > => {
96+ const packageJsonSchema = createPackageJsonSchema ( logger ) ;
97+
7998 try {
8099 const validatedPkg = await packageJsonSchema . validate ( pkg , {
81100 strict : true ,
@@ -114,7 +133,7 @@ const validatePkg = async ({ pkg }: { pkg: object }): Promise<PackageJson> => {
114133 throw new Error (
115134 `'${ err . path } ' in 'package.json' must be of type '${ chalk . magenta (
116135 err . params . type
117- ) } ' (recieved '${ chalk . magenta ( typeof err . params . value ) } ')`
136+ ) } ' (received '${ chalk . magenta ( typeof err . params . value ) } ')`
118137 ) ;
119138 }
120139 }
0 commit comments