@@ -154,6 +154,50 @@ function searchFile (dir, fileName, callback) {
154
154
} ) ;
155
155
}
156
156
157
+ // uses the folderScanSync to DFS and grab absolute paths of all files and folders, deleting folders (deepest first) and moving files to the target
158
+ // requires an absolute path for sourcePath and targetFolderPath.
159
+ function flattenFolderStructure ( sourcePath , targetFolderPath , callback ) {
160
+ return Q . resolve ( ) . then ( function ( ) {
161
+ var output = {
162
+ files : [ ] ,
163
+ folders : [ ]
164
+ }
165
+ folderScanSync ( output , sourcePath ) ;
166
+
167
+ // move/copy files
168
+ output . files . forEach ( file => {
169
+ fs . renameSync ( file , targetFolderPath + path . parse ( file ) . base , ( err ) => {
170
+ return callback ( err ) ;
171
+ } ) ;
172
+ } ) ;
173
+
174
+ // remove directories
175
+ output . folders . forEach ( folder => {
176
+ fs . rmdirSync ( folder , ( err ) => {
177
+ callback ( err ) ;
178
+ } ) ;
179
+ } ) ;
180
+
181
+ return Q . resolve ( ) ;
182
+ } ) . nodeify ( callback ) ;
183
+ }
184
+
185
+ // DFS crawl into the folder structure and grab all references.
186
+ // output is an object: { files: [], folders: [] }
187
+ function folderScanSync ( output , directory ) {
188
+ fs . readdirSync ( directory )
189
+ . forEach ( function ( fileEntry ) {
190
+ var fileEntryPath = path . join ( directory , fileEntry ) ;
191
+
192
+ if ( fs . statSync ( fileEntryPath ) . isDirectory ( ) ) {
193
+ folderScanSync ( output , fileEntryPath + "/" ) ;
194
+ output . folders . push ( fileEntryPath ) ;
195
+ } else {
196
+ output . files . push ( fileEntryPath ) ;
197
+ }
198
+ } ) ;
199
+ }
200
+
157
201
// Copies the 'source' file to 'target' if it's missing after creating the
158
202
// required directory structure.
159
203
function syncFile ( source , target , callback ) {
@@ -301,5 +345,7 @@ module.exports = {
301
345
createShortcut : createShortcut ,
302
346
replaceFileContent : replaceFileContent ,
303
347
searchFile : searchFile ,
304
- syncFiles : syncFiles
348
+ syncFiles : syncFiles ,
349
+ flattenFolderStructure : flattenFolderStructure ,
350
+ folderScanSync : folderScanSync
305
351
} ;
0 commit comments