@@ -5,8 +5,26 @@ import { Ignore } from 'ignore';
55let allFiles : Set < string > ;
66let allFolders : Set < string > ;
77
8+ interface WalkInput {
9+ /** Whether this is walking the tree from the root */
10+ init : boolean ,
11+ /** The common root absolute path of all folders being checked */
12+ commonRoot : string ,
13+ /** The absolute path that each folder is relative to */
14+ folderRoots : string [ ] ,
15+ /** The absolute path of folders being checked */
16+ folders : string [ ] ,
17+ gitignores : Ignore ,
18+ regexIgnores : RegExp [ ] ,
19+ } ;
20+ interface WalkOutput {
21+ files : string [ ] ,
22+ folders : string [ ] ,
23+ } ;
24+
825/** Generate list of files in a directory. */
9- export default function walk ( init : boolean , root : string , folders : string [ ] , gitignores : Ignore , regexIgnores : RegExp [ ] ) : { files : string [ ] , folders : string [ ] } {
26+ export default function walk ( data : WalkInput ) : WalkOutput {
27+ const { init, commonRoot, folderRoots, folders, gitignores, regexIgnores } = data ;
1028
1129 // Initialise files and folders lists
1230 if ( init ) {
@@ -17,46 +35,48 @@ export default function walk(init: boolean, root: string, folders: string[], git
1735 // Walk tree of a folder
1836 if ( folders . length === 1 ) {
1937 const folder = folders [ 0 ] ;
38+ const localRoot = folderRoots [ 0 ] . replace ( commonRoot , '' ) . replace ( / ^ \/ / , '' ) ;
2039 // Get list of files and folders inside this folder
2140 const files = fs . readdirSync ( folder ) . map ( file => {
2241 // Create path relative to root
23- const base = paths . resolve ( folder , file ) . replace ( / \\ / g, '/' ) . replace ( root , '.' ) ;
42+ const base = paths . resolve ( folder , file ) . replace ( / \\ / g, '/' ) . replace ( commonRoot , '.' ) ;
2443 // Add trailing slash to mark directories
25- const isDir = fs . lstatSync ( paths . resolve ( root , base ) ) . isDirectory ( ) ;
44+ const isDir = fs . lstatSync ( paths . resolve ( commonRoot , base ) ) . isDirectory ( ) ;
2645 return isDir ? `${ base } /` : base ;
2746 } ) ;
2847 // Loop through files and folders
2948 for ( const file of files ) {
3049 // Create absolute path for disc operations
31- const path = paths . resolve ( root , file ) . replace ( / \\ / g, '/' ) ;
50+ const path = paths . resolve ( commonRoot , file ) . replace ( / \\ / g, '/' ) ;
51+ const localPath = localRoot ? file . replace ( `./${ localRoot } /` , '' ) : file . replace ( './' , '' ) ;
3252 // Skip if nonexistant or ignored
3353 const nonExistant = ! fs . existsSync ( path ) ;
34- const isGitIgnored = gitignores . test ( file . replace ( './' , '' ) ) . ignored ;
35- const isRegexIgnored = regexIgnores . find ( match => file . replace ( './' , '' ) . match ( match ) ) ;
54+ const isGitIgnored = gitignores . test ( localPath ) . ignored ;
55+ const isRegexIgnored = regexIgnores . find ( match => localPath . match ( match ) ) ;
3656 if ( nonExistant || isGitIgnored || isRegexIgnored ) continue ;
3757 // Add absolute folder path to list
3858 allFolders . add ( paths . resolve ( folder ) . replace ( / \\ / g, '/' ) ) ;
3959 // Check if this is a folder or file
4060 if ( file . endsWith ( '/' ) ) {
4161 // Recurse into subfolders
4262 allFolders . add ( path ) ;
43- walk ( false , root , [ path ] , gitignores , regexIgnores ) ;
63+ walk ( { init : false , commonRoot : commonRoot , folderRoots , folders : [ path ] , gitignores, regexIgnores } ) ;
4464 }
4565 else {
46- // Add relative file path to list
66+ // Add file path to list
4767 allFiles . add ( path ) ;
4868 }
4969 }
5070 }
5171 // Recurse into all folders
5272 else {
53- for ( const path of folders ) {
54- walk ( false , root , [ path ] , gitignores , regexIgnores ) ;
73+ for ( const i in folders ) {
74+ walk ( { init : false , commonRoot : commonRoot , folderRoots : [ folderRoots [ i ] ] , folders : [ folders [ i ] ] , gitignores, regexIgnores } ) ;
5575 }
5676 }
5777 // Return absolute files and folders lists
5878 return {
59- files : [ ...allFiles ] . map ( file => file . replace ( / ^ \. / , root ) ) ,
79+ files : [ ...allFiles ] . map ( file => file . replace ( / ^ \. / , commonRoot ) ) ,
6080 folders : [ ...allFolders ] ,
6181 } ;
6282}
0 commit comments