@@ -11,7 +11,7 @@ const DEFAULT_FILTER = () => false;
1111const isNegative = pattern => pattern [ 0 ] === '!' ;
1212
1313const assertPatternsInput = patterns => {
14- if ( ! patterns . every ( x => typeof x === 'string' ) ) {
14+ if ( ! patterns . every ( pattern => typeof pattern === 'string' ) ) {
1515 throw new TypeError ( 'Patterns must be a string or an array of strings' ) ;
1616 }
1717} ;
@@ -31,27 +31,29 @@ const generateGlobTasks = (patterns, taskOptions) => {
3131
3232 const globTasks = [ ] ;
3333
34- taskOptions = Object . assign ( {
34+ taskOptions = {
3535 ignore : [ ] ,
36- expandDirectories : true
37- } , taskOptions ) ;
36+ expandDirectories : true ,
37+ ...taskOptions
38+ } ;
3839
39- patterns . forEach ( ( pattern , i ) => {
40+ for ( const [ index , pattern ] of patterns . entries ( ) ) {
4041 if ( isNegative ( pattern ) ) {
41- return ;
42+ continue ;
4243 }
4344
4445 const ignore = patterns
45- . slice ( i )
46+ . slice ( index )
4647 . filter ( isNegative )
4748 . map ( pattern => pattern . slice ( 1 ) ) ;
4849
49- const options = Object . assign ( { } , taskOptions , {
50+ const options = {
51+ ...taskOptions ,
5052 ignore : taskOptions . ignore . concat ( ignore )
51- } ) ;
53+ } ;
5254
5355 globTasks . push ( { pattern, options} ) ;
54- } ) ;
56+ }
5557
5658 return globTasks ;
5759} ;
@@ -63,9 +65,15 @@ const globDirs = (task, fn) => {
6365 }
6466
6567 if ( Array . isArray ( task . options . expandDirectories ) ) {
66- options = Object . assign ( options , { files : task . options . expandDirectories } ) ;
68+ options = {
69+ ...options ,
70+ files : task . options . expandDirectories
71+ } ;
6772 } else if ( typeof task . options . expandDirectories === 'object' ) {
68- options = Object . assign ( options , task . options . expandDirectories ) ;
73+ options = {
74+ ...options ,
75+ ...task . options . expandDirectories
76+ } ;
6977 }
7078
7179 return fn ( task . pattern , options ) ;
@@ -85,40 +93,29 @@ const globToTask = task => glob => {
8593 } ;
8694} ;
8795
88- const globby = ( patterns , options ) => {
89- let globTasks ;
96+ module . exports = async ( patterns , options ) => {
97+ const globTasks = generateGlobTasks ( patterns , options ) ;
9098
91- try {
92- globTasks = generateGlobTasks ( patterns , options ) ;
93- } catch ( error ) {
94- return Promise . reject ( error ) ;
95- }
99+ const getFilter = async ( ) => {
100+ return options && options . gitignore ?
101+ gitignore ( { cwd : options . cwd , ignore : options . ignore } ) :
102+ DEFAULT_FILTER ;
103+ } ;
96104
97- const getTasks = Promise . all ( globTasks . map ( task => Promise . resolve ( getPattern ( task , dirGlob ) )
98- . then ( globs => Promise . all ( globs . map ( globToTask ( task ) ) ) )
99- ) )
100- . then ( tasks => arrayUnion ( ...tasks ) ) ;
105+ const getTasks = async ( ) => {
106+ const tasks = await Promise . all ( globTasks . map ( async task => {
107+ const globs = await getPattern ( task , dirGlob ) ;
108+ return Promise . all ( globs . map ( globToTask ( task ) ) ) ;
109+ } ) ) ;
101110
102- const getFilter = ( ) => {
103- return Promise . resolve (
104- options && options . gitignore ?
105- gitignore ( { cwd : options . cwd , ignore : options . ignore } ) :
106- DEFAULT_FILTER
107- ) ;
111+ return arrayUnion ( ...tasks ) ;
108112 } ;
109113
110- return getFilter ( )
111- . then ( filter => {
112- return getTasks
113- . then ( tasks => Promise . all ( tasks . map ( task => fastGlob ( task . pattern , task . options ) ) ) )
114- . then ( paths => arrayUnion ( ...paths ) )
115- . then ( paths => paths . filter ( p => ! filter ( getPathString ( p ) ) ) ) ;
116- } ) ;
117- } ;
114+ const [ filter , tasks ] = await Promise . all ( [ getFilter ( ) , getTasks ( ) ] ) ;
115+ const paths = await Promise . all ( tasks . map ( task => fastGlob ( task . pattern , task . options ) ) ) ;
118116
119- module . exports = globby ;
120- // TODO: Remove this for the next major release
121- module . exports . default = globby ;
117+ return arrayUnion ( ...paths ) . filter ( path_ => ! filter ( getPathString ( path_ ) ) ) ;
118+ } ;
122119
123120module . exports . sync = ( patterns , options ) => {
124121 const globTasks = generateGlobTasks ( patterns , options ) ;
@@ -138,7 +135,7 @@ module.exports.sync = (patterns, options) => {
138135 return tasks . reduce (
139136 ( matches , task ) => arrayUnion ( matches , fastGlob . sync ( task . pattern , task . options ) ) ,
140137 [ ]
141- ) . filter ( p => ! filter ( p ) ) ;
138+ ) . filter ( path_ => ! filter ( path_ ) ) ;
142139} ;
143140
144141module . exports . generateGlobTasks = generateGlobTasks ;
0 commit comments