1+ import glob from 'glob' ;
2+ import { existsSync } from 'fs' ;
3+ import { resolve } from 'path' ;
4+ import { projectRoot as root } from '../utils' ;
5+
6+ const TOP_LEVEL_FIELDS = [
7+ 'main' ,
8+ 'browser' ,
9+ 'module' ,
10+ 'typings' ,
11+ 'react-native' ,
12+ 'cordova' ,
13+ 'esm5' ,
14+ 'webworker' ,
15+ 'main-esm'
16+ ] ;
17+
18+ interface Result {
19+ packageName : string ;
20+ found : boolean ;
21+ filePath : string ;
22+ fieldPath : string ;
23+ }
24+ const results : Result [ ] = [ ] ;
25+
26+ function getPaths ( ) : Promise < string [ ] > {
27+ return new Promise ( ( resolve , reject ) => {
28+ glob ( 'packages/*' , ( err , paths ) => {
29+ if ( err ) reject ( err ) ;
30+ resolve ( paths ) ;
31+ } ) ;
32+ } )
33+ }
34+
35+ function checkExports ( pkgName : string , pkgRoot : string , path : string = '' , exports : Record < string , any > ) {
36+ for ( const key in exports ) {
37+ if ( typeof exports [ key ] === 'string' ) {
38+ const filePath = resolve ( pkgRoot , exports [ key ] ) ;
39+ const result = {
40+ packageName : pkgName ,
41+ found : false ,
42+ filePath,
43+ fieldPath : `exports${ path } [${ key } ]`
44+ } ;
45+ if ( existsSync ( filePath ) ) {
46+ result . found = true ;
47+ }
48+ results . push ( result ) ;
49+ } else {
50+ checkExports ( pkgName , pkgRoot , path ? `${ path } [${ key } ]` : `[${ key } ]` , exports [ key ] ) ;
51+ }
52+ }
53+ }
54+
55+ async function main ( ) {
56+ const paths = await getPaths ( ) ;
57+ for ( const path of paths ) {
58+ const pkgRoot = `${ root } /${ path } ` ;
59+ if ( existsSync ( `${ pkgRoot } /package.json` ) ) {
60+ const pkg = require ( `${ pkgRoot } /package.json` ) ;
61+ for ( const field of TOP_LEVEL_FIELDS ) {
62+ if ( pkg [ field ] ) {
63+ const filePath = resolve ( pkgRoot , pkg [ field ] ) ;
64+ const result = {
65+ packageName : pkg . name ,
66+ found : false ,
67+ filePath,
68+ fieldPath : field
69+ } ;
70+ if ( existsSync ( filePath ) ) {
71+ result . found = true ;
72+ }
73+ results . push ( result ) ;
74+ }
75+ }
76+ if ( pkg . exports ) {
77+ checkExports ( pkg . name , pkgRoot , '' , pkg . exports ) ;
78+ }
79+ }
80+ }
81+
82+ let missingPaths : boolean = false ;
83+
84+ for ( const result of results ) {
85+ if ( ! result . found ) {
86+ missingPaths = true ;
87+ console . log ( `${ result . packageName } : Field "${ result . fieldPath } " ` +
88+ `points to ${ result . filePath } which is not found.` ) ;
89+ }
90+ }
91+
92+ if ( missingPaths ) {
93+ process . exit ( 1 ) ;
94+ }
95+ }
96+
97+ main ( ) ;
0 commit comments