@@ -11,17 +11,24 @@ import {
11
11
} from 'path' ;
12
12
import * as ts from 'typescript' ;
13
13
14
+ import {
15
+ getPullRequestHead ,
16
+ statusReporterFactory ,
17
+ } from '../common/github' ;
18
+
19
+ const reportStatus = statusReporterFactory ( 'public-api-check' ) ;
20
+
14
21
const {
15
22
red,
16
23
cyan,
17
24
green,
25
+ grey,
18
26
} = chalk . default ;
19
27
20
28
const ROOT = resolve ( __dirname , '../..' ) ;
21
29
const BARREL = 'index.ts' ;
22
30
23
31
const resolveParentDir = ( path ) => dirname ( resolve ( ROOT , path ) ) ;
24
-
25
32
interface IExportPair {
26
33
exportLocation : string ;
27
34
exported : string ;
@@ -39,6 +46,12 @@ interface IDecoratedError extends IDecoratedClass {
39
46
40
47
let isErrorFlagged = false ;
41
48
49
+ const _handleUnexpectedError = ( err ?: string ) => {
50
+ if ( err ) { console . error ( err ) ; }
51
+
52
+ process . exit ( 1 ) ;
53
+ } ;
54
+
42
55
const buildError = ( { className, decoratorName, exportLocation, rootPath } : IDecoratedError , message : string ) => {
43
56
return [
44
57
red ( `ERROR:` ) ,
@@ -144,7 +157,7 @@ const resolveFileLocation = (exportLocation: string, path: string) => {
144
157
return resolve ( parentDir , fileName ) ;
145
158
} ;
146
159
147
- const checkForUnamedDecoratedExports = ( rootPath : string , path : string , exportList : IExportPair [ ] ) =>
160
+ const checkForUnnamedDecoratedExports = ( rootPath : string , path : string , exportList : IExportPair [ ] ) =>
148
161
exportList
149
162
. filter ( e => ! e . exported )
150
163
. forEach ( ( { exportLocation } ) => {
@@ -171,7 +184,7 @@ const checkForUnamedDecoratedExports = (rootPath: string, path: string, exportLi
171
184
} ) ,
172
185
) ;
173
186
} else {
174
- checkForUnamedDecoratedExports ( rootPath , fileLocation , exportDeclarations ) ;
187
+ checkForUnnamedDecoratedExports ( rootPath , fileLocation , exportDeclarations ) ;
175
188
}
176
189
} ) ;
177
190
@@ -229,22 +242,53 @@ const checkFile = (path: string) => {
229
242
const sourceFile = readFileNode ( path ) ;
230
243
const rootExports = getExportDeclarations ( sourceFile ) ;
231
244
232
- checkForUnamedDecoratedExports ( path , path , rootExports ) ;
245
+ console . log ( `${ grey ( 'Checking' ) } ${ cyan ( sourceFile . fileName ) } ${ grey ( 'for unnamed decorated exports...' ) } ` ) ;
246
+ checkForUnnamedDecoratedExports ( path , path , rootExports ) ;
247
+
248
+ console . log ( `${ grey ( 'Checking' ) } ${ cyan ( sourceFile . fileName ) } ${ grey ( 'decorated exports hidden behind barrels...' ) } ` ) ;
233
249
checkForBarreledDecoratedExports ( path , path , rootExports ) ;
234
250
} ;
235
251
236
- glob (
237
- './projects/**/public_api.ts' ,
238
- { root : ROOT } ,
239
- ( err , files ) => {
240
- if ( err ) { throw err ; }
252
+ const forEachApiFile = ( callback : ( string ) => void ) => {
253
+ return new Promise ( ( res , rej ) => {
254
+ const walker = new glob . Glob ( './projects/**/public_api.ts' , {
255
+ root : ROOT ,
256
+ } , ( err , files ) => {
257
+ if ( err ) {
258
+ console . error ( err ) ;
259
+ rej ( err ) ;
260
+ }
241
261
242
- files . forEach ( file => {
243
- checkFile ( file ) ;
262
+ files . forEach ( callback ) ;
244
263
} ) ;
245
264
246
- if ( isErrorFlagged ) {
247
- process . exit ( 1 ) ;
248
- }
249
- } ,
250
- ) ;
265
+ walker . on ( 'end' , res ) ;
266
+ } ) ;
267
+ } ;
268
+
269
+ const run = async ( ) => {
270
+ const head = await getPullRequestHead ( ) ;
271
+
272
+ await reportStatus ( 'pending' , head . sha , 'Checking Public API...' )
273
+ . catch ( _handleUnexpectedError ) ;
274
+
275
+ await forEachApiFile ( checkFile ) ;
276
+
277
+ if ( isErrorFlagged ) {
278
+ console . error ( red ( `⛔ Something doesn't check out` ) ) ;
279
+
280
+ await reportStatus ( 'error' , head . sha , 'npm run check:exports for more info...' )
281
+ . catch ( _handleUnexpectedError ) ;
282
+ } else {
283
+ console . log ( green ( `💯 Good to go!` ) ) ;
284
+
285
+ await reportStatus ( 'success' , head . sha , '✔ Good to go!' )
286
+ . catch ( _handleUnexpectedError ) ;
287
+ }
288
+ } ;
289
+
290
+ ( async ( ) => {
291
+ await run ( )
292
+ . catch ( _handleUnexpectedError ) ;
293
+ } ) ( ) ;
294
+
0 commit comments