Skip to content
This repository was archived by the owner on Jan 14, 2022. It is now read-only.

Commit e12677e

Browse files
committed
the code to scan and move files and delete directories, not sure how to what I need to have done to plug it into the promise structure
1 parent 22879ba commit e12677e

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

lib/fileTools.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,50 @@ function searchFile (dir, fileName, callback) {
154154
});
155155
}
156156

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+
157201
// Copies the 'source' file to 'target' if it's missing after creating the
158202
// required directory structure.
159203
function syncFile (source, target, callback) {
@@ -301,5 +345,7 @@ module.exports = {
301345
createShortcut: createShortcut,
302346
replaceFileContent: replaceFileContent,
303347
searchFile: searchFile,
304-
syncFiles: syncFiles
348+
syncFiles: syncFiles,
349+
flattenFolderStructure: flattenFolderStructure,
350+
folderScanSync: folderScanSync
305351
};

0 commit comments

Comments
 (0)