@@ -2,15 +2,76 @@ import type {RootDoc, SourceFile} from "@webdoc/types";
22import globby from "globby" ;
33import { packages } from "./packages" ;
44
5+ // Create a filtering callback that returns whether a path passes all
6+ // patterns (if exclude = false) or if it fails all of them (if exclude = true).
7+ function makePatternFilter (
8+ patterns : string | string [ ] ,
9+ exclude : boolean = false ,
10+ ) : ( path : string ) => boolean {
11+ const normalizedFormat = Array . isArray ( patterns ) ?
12+ patterns :
13+ ( ( patterns && [ patterns ] ) || [ ] ) ;
14+
15+ if ( normalizedFormat . length === 0 ) {
16+ // If no patterns to match, then short circuit evaluation.
17+ return ( ) => true ;
18+ }
19+
20+ const regExps = normalizedFormat . map (
21+ ( pattern ) => {
22+ try {
23+ return new RegExp ( pattern , "g" ) ;
24+ } catch ( e ) {
25+ console . error ( "Invalid pattern: " + pattern +
26+ "\nPlease make sure the patterns in sources.includePattern or source.excludePattern " +
27+ "defined in your configuration file are valid\n" ,
28+ ) ;
29+ }
30+ } ,
31+ ) ;
32+
33+ return function ( path : string ) : boolean {
34+ for ( const regExp of regExps ) {
35+ if ( path . match ( regExp ) ) {
36+ // If include pattern matched, return true.
37+ // If exclude pattern matched, return false.
38+ return ! exclude ;
39+ }
40+ }
41+
42+ // If all include patterns failed, return false.
43+ // If all exclude patterns passed, return true.
44+ return exclude ;
45+ } ;
46+ }
47+
548export function sources ( config : Object , documentTree : RootDoc ) : SourceFile [ ] {
6- const includePattern = config . source . includePattern || config . source . include ;
49+ const {
50+ include ,
51+ includePattern ,
52+ exclude ,
53+ excludePattern ,
54+ } = config . source ;
755
8- if ( ! includePattern ) {
9- console . log ( "No source.include or source.includePattern found in config file" ) ;
56+ if ( ! include ) {
57+ console . error ( "No input files specified. You must " +
58+ "define source.include in your configuration file." ) ;
59+
60+ return [ ] ;
1061 }
1162
12- const paths = globby . sync ( includePattern ) ;
13- const sources : SourceFile [ ] = paths . map ( ( file ) => ( { path : file } ) ) ;
63+ const paths = globby . sync ( include ) ;
64+ const excludePaths = Array . isArray ( exclude ) ?
65+ exclude :
66+ ( ( exclude && [ exclude ] ) || [ ] ) ;
67+
68+ const sources : SourceFile [ ] = paths
69+ . filter ( makePatternFilter ( includePattern , false ) )
70+ . filter ( makePatternFilter ( excludePattern , true ) )
71+ . filter ( ( path ) => ! ( excludePaths . some (
72+ ( excludedPath ) => path === excludePaths ,
73+ ) ) )
74+ . map ( ( path ) => ( { path} ) ) ;
1475
1576 documentTree . packages = packages ( sources ) ;
1677
0 commit comments