@@ -12,7 +12,6 @@ var globParent = require('glob-parent');
1212var normalizePath = require ( 'normalize-path' ) ;
1313var isNegatedGlob = require ( 'is-negated-glob' ) ;
1414var toAbsoluteGlob = require ( '@gulpjs/to-absolute-glob' ) ;
15- var mapSeries = require ( 'now-and-later' ) . mapSeries ;
1615
1716var globErrMessage1 = 'File not found with singular glob: ' ;
1817var globErrMessage2 = ' (if this was purposeful, use `allowEmpty` option)' ;
@@ -24,29 +23,14 @@ function isFound(glob) {
2423 return isGlob ( glob ) ;
2524}
2625
27- function getSymlinkInfo ( filepath , cb ) {
28- fs . realpath ( filepath , function ( err , realPath ) {
29- if ( err ) return cb ( err ) ;
30-
31- fs . lstat ( realPath , function ( err , lstat ) {
32- if ( err ) return cb ( err ) ;
33-
34- cb ( null , {
35- destinationPath : realPath ,
36- destinationStat : lstat ,
37- } ) ;
38- } ) ;
39- } ) ;
40- }
41-
4226function walkdir ( ) {
4327 var readdirOpts = {
4428 withFileTypes : true ,
4529 } ;
4630
4731 var ee = new EventEmitter ( ) ;
4832
49- var queue = fastq ( process , 1 ) ;
33+ var queue = fastq ( onAction , 1 ) ;
5034 queue . drain = function ( ) {
5135 ee . emit ( 'end' ) ;
5236 } ;
@@ -69,10 +53,7 @@ function walkdir() {
6953 } ;
7054 ee . walk = walk ;
7155 ee . exists = exists ;
72-
73- function isDefined ( value ) {
74- return typeof value !== 'undefined' ;
75- }
56+ ee . resolve = resolve ;
7657
7758 function walk ( path ) {
7859 queue . push ( { action : 'walk' , path : path } ) ;
@@ -82,11 +63,41 @@ function walkdir() {
8263 queue . push ( { action : 'exists' , path : path } ) ;
8364 }
8465
85- function process ( data , cb ) {
66+ function resolve ( path ) {
67+ queue . push ( { action : 'resolve' , path : path } ) ;
68+ }
69+
70+ function resolveSymlink ( symlinkPath , cb ) {
71+ fs . realpath ( symlinkPath , function ( err , realpath ) {
72+ if ( err ) {
73+ return cb ( err ) ;
74+ }
75+
76+ fs . lstat ( realpath , function ( err , stat ) {
77+ if ( err ) {
78+ return cb ( err ) ;
79+ }
80+
81+ if ( stat . isDirectory ( ) && ! symlinkPath . startsWith ( realpath + path . sep ) ) {
82+ walk ( symlinkPath ) ;
83+ }
84+
85+ cb ( ) ;
86+ } )
87+ } ) ;
88+ }
89+
90+ function onAction ( data , cb ) {
8691 if ( data . action === 'walk' ) {
87- fs . readdir ( data . path , readdirOpts , onReaddir ) ;
88- } else {
89- fs . stat ( data . path , onStat ) ;
92+ return fs . readdir ( data . path , readdirOpts , onReaddir ) ;
93+ }
94+
95+ if ( data . action === 'exists' ) {
96+ return fs . stat ( data . path , onStat ) ;
97+ }
98+
99+ if ( data . action === 'resolve' ) {
100+ return resolveSymlink ( data . path , cb ) ;
90101 }
91102
92103 function onStat ( err , stat ) {
@@ -106,48 +117,22 @@ function walkdir() {
106117 return cb ( err ) ;
107118 }
108119
109- mapSeries ( dirents , processDirents , function ( err , dirs ) {
110- if ( err ) {
111- return cb ( err ) ;
112- }
120+ dirents . forEach ( processDirent ) ;
113121
114- dirs . filter ( isDefined ) . forEach ( walk ) ;
115-
116- cb ( ) ;
117- } ) ;
122+ cb ( ) ;
118123 }
119124
120- function processDirents ( dirent , key , cb ) {
125+ function processDirent ( dirent ) {
121126 var nextpath = path . join ( data . path , dirent . name ) ;
122127 ee . emit ( 'path' , nextpath , dirent ) ;
123128
124129 if ( dirent . isDirectory ( ) ) {
125- cb ( null , nextpath ) ;
126-
127- return ;
130+ return walk ( nextpath ) ;
128131 }
129132
130133 if ( dirent . isSymbolicLink ( ) ) {
131- // If it's a symlink, check if the symlink points to a directory
132- getSymlinkInfo ( nextpath , function ( err , info ) {
133- if ( err ) {
134- return cb ( err ) ;
135- }
136-
137- if (
138- info . destinationStat . isDirectory ( ) &&
139- ! nextpath . startsWith ( info . destinationPath + path . sep ) // don't follow circular symlinks
140- ) {
141- cb ( null , nextpath ) ;
142- } else {
143- cb ( ) ;
144- }
145- } ) ;
146-
147- return ;
134+ return resolve ( nextpath ) ;
148135 }
149-
150- cb ( ) ;
151136 }
152137 }
153138
0 commit comments