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

Commit 5ce1862

Browse files
committed
change rm promise code, edits to make the synchronouse version use a loop rather than recursion
1 parent 59bea1a commit 5ce1862

File tree

1 file changed

+27
-68
lines changed

1 file changed

+27
-68
lines changed

lib/fileTools.js

Lines changed: 27 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ var log = require('./log'),
1111
utils = require('./utils');
1212

1313
var stat = Q.nfbind(fs.stat);
14-
var PromiseQueue = require('./promiseQueue');
1514

1615
function readFile (source, options, callback) {
1716
return Q.nfcall(fs.readFile, source, options)
@@ -159,15 +158,11 @@ function searchFile (dir, fileName, callback) {
159158
// requires an absolute path for sourcePath and targetFolderPath.
160159
function flattenFolderStructureSync(sourcePath, targetFolderPath, callback) {
161160
return Q.resolve().then(function () {
162-
var output = {
163-
files: [],
164-
folders: []
165-
}
166-
folderScanSync(output, sourcePath);
161+
var output = folderScanSync(sourcePath);
167162

168163
// move/copy files
169164
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) => {
171166
return callback(err);
172167
});
173168
});
@@ -183,68 +178,32 @@ function flattenFolderStructureSync(sourcePath, targetFolderPath, callback) {
183178
}).nodeify(callback);
184179
}
185180

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);
242197
} else {
243-
files.push(filePath);
198+
files.push(fileEntryPath);
244199
}
245-
});
246-
}));
247-
});
200+
});
201+
}
202+
203+
return {
204+
folders: folders.reverse(),
205+
files: files.reverse()
206+
};
248207
}
249208

250209
// Copies the 'source' file to 'target' if it's missing after creating the
@@ -395,6 +354,6 @@ module.exports = {
395354
replaceFileContent: replaceFileContent,
396355
searchFile: searchFile,
397356
syncFiles: syncFiles,
398-
flattenFolderStructure: flattenFolderStructure,
357+
flattenFolderStructure: flattenFolderStructureSync,
399358
folderScanSync: folderScanSync
400359
};

0 commit comments

Comments
 (0)