@@ -11,7 +11,6 @@ var log = require('./log'),
11
11
utils = require ( './utils' ) ;
12
12
13
13
var stat = Q . nfbind ( fs . stat ) ;
14
- var PromiseQueue = require ( './promiseQueue' ) ;
15
14
16
15
function readFile ( source , options , callback ) {
17
16
return Q . nfcall ( fs . readFile , source , options )
@@ -159,15 +158,11 @@ function searchFile (dir, fileName, callback) {
159
158
// requires an absolute path for sourcePath and targetFolderPath.
160
159
function flattenFolderStructureSync ( sourcePath , targetFolderPath , callback ) {
161
160
return Q . resolve ( ) . then ( function ( ) {
162
- var output = {
163
- files : [ ] ,
164
- folders : [ ]
165
- }
166
- folderScanSync ( output , sourcePath ) ;
161
+ var output = folderScanSync ( sourcePath ) ;
167
162
168
163
// move/copy files
169
164
output . files . forEach ( file => {
170
- fs . renameSync ( file , targetFolderPath + path . parse ( file ) . base , ( err ) => {
165
+ fs . renameSync ( file , path . resolve ( targetFolderPath , path . parse ( file ) . base ) , ( err ) => {
171
166
return callback ( err ) ;
172
167
} ) ;
173
168
} ) ;
@@ -183,68 +178,32 @@ function flattenFolderStructureSync(sourcePath, targetFolderPath, callback) {
183
178
} ) . nodeify ( callback ) ;
184
179
}
185
180
186
- // 1. Scan Folders, create an object
187
- function flattenFolderStructure ( sourcePath , targetFolderPath , callback ) {
188
- return folderScan ( sourcePath ) . then ( output => {
189
- return Q . all ( output . files . map ( file => { return fs . promises . rename ( file , targetFolderPath + path . parse ( file ) . base ) } ) )
190
- . then ( ( ) => {
191
- return Q . all ( output . folders . map ( folder => {
192
- return fs . promises . rmdir ( folder )
193
- } ) ) ;
194
- } ) ;
195
- } ) . nodeify ( callback ) ;
196
- }
197
-
198
- // DFS crawl into the folder structure and grab all references.
199
- // output is an object: { files: [], folders: [] }
200
- function folderScanSync ( output , directory ) {
201
- fs . readdirSync ( directory )
202
- . forEach ( function ( fileEntry ) {
203
- var fileEntryPath = path . join ( directory , fileEntry ) ;
204
-
205
- if ( fs . statSync ( fileEntryPath ) . isDirectory ( ) ) {
206
- folderScanSync ( output , fileEntryPath + "/" ) ;
207
- output . folders . push ( fileEntryPath ) ;
208
- } else {
209
- output . files . push ( fileEntryPath ) ;
210
- }
211
- } ) ;
212
- }
213
-
214
- // utilizes a simple promise queue to
215
- function folderScan ( rootDirectory ) {
216
- return Q . Promise ( function ( scanResolve , scanReject ) {
217
- let queue = new PromiseQueue ( ) ;
218
- const folders = [ ] ;
219
- const files = [ ] ;
220
-
221
- queue . enqueue ( folderScanPromise ( queue , rootDirectory , folders , files ) )
222
- . then ( ( ) => {
223
- scanResolve ( {
224
- folders : folders . reverse ( ) ,
225
- files : files . reverse ( )
226
- } ) ;
227
- } ) ;
228
- } ) ;
229
- }
230
-
231
- function folderScanPromise ( promiseQueue , directory , folders , files ) {
232
- // readdir promise
233
- return fs . promises . readdir ( directory ) . then ( directoryEntries => {
234
- // Q.all on the files in the directory
235
- return Q . all ( directoryEntries . map ( entry => {
236
- // grab the stats, if directory, then put it in the queue and add it to folders, if a file, add it to files
237
- const filePath = path . join ( directory , entry ) ;
238
- return fs . promises . stat ( ) . then ( stats => {
239
- if ( stats . isDirectory ( ) ) {
240
- folders . push ( filePath ) ;
241
- promiseQueue . enqueue ( folderScanPromise ( promiseQueue , filePath , folders , files ) ) ;
181
+ // queues new entries into the unexplored paths, and iterates through it, execution stops when all reaches end.
182
+ // output is an object: { files: [], folders: [] }, the order of the files and object is reversed of traversal order
183
+ function folderScanSync ( directory ) {
184
+ const unexplored = [ directory ] ;
185
+ const folders = [ ] ;
186
+ const files = [ ] ;
187
+
188
+ for ( let i = 0 ; i < unexplored . length ; i ++ ) {
189
+ const currentPath = unexplored [ i ] ;
190
+ fs . readdirSync ( currentPath )
191
+ . forEach ( function ( fileEntry ) {
192
+ var fileEntryPath = path . join ( currentPath , fileEntry ) ;
193
+
194
+ if ( fs . statSync ( fileEntryPath ) . isDirectory ( ) ) {
195
+ unexplored . push ( fileEntryPath ) ;
196
+ folders . push ( fileEntryPath ) ;
242
197
} else {
243
- files . push ( filePath ) ;
198
+ files . push ( fileEntryPath ) ;
244
199
}
245
- } ) ;
246
- } ) ) ;
247
- } ) ;
200
+ } ) ;
201
+ }
202
+
203
+ return {
204
+ folders : folders . reverse ( ) ,
205
+ files : files . reverse ( )
206
+ } ;
248
207
}
249
208
250
209
// Copies the 'source' file to 'target' if it's missing after creating the
@@ -395,6 +354,6 @@ module.exports = {
395
354
replaceFileContent : replaceFileContent ,
396
355
searchFile : searchFile ,
397
356
syncFiles : syncFiles ,
398
- flattenFolderStructure : flattenFolderStructure ,
357
+ flattenFolderStructure : flattenFolderStructureSync ,
399
358
folderScanSync : folderScanSync
400
359
} ;
0 commit comments