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

Commit af31823

Browse files
authored
Merge pull request #61 from pwa-builder/flatten_directory
functions to flatten the directory structure
2 parents 22879ba + 2d61e7a commit af31823

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

lib/fileTools.js

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

157+
// uses the folderScanSync to 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 flattenFolderStructureSync(sourcePath, targetFolderPath, callback) {
160+
return Q.resolve().then(function () {
161+
var output = folderScanSync(sourcePath);
162+
163+
// move/copy files
164+
output.files.forEach(file => {
165+
fs.renameSync(file, path.resolve(targetFolderPath, path.parse(file).base), (err) => {
166+
return callback(err);
167+
});
168+
});
169+
170+
// remove directories
171+
output.folders.forEach(folder => {
172+
fs.rmdirSync(folder, (err) => {
173+
callback(err);
174+
});
175+
});
176+
177+
return Q.resolve();
178+
}).nodeify(callback);
179+
}
180+
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);
197+
} else {
198+
files.push(fileEntryPath);
199+
}
200+
});
201+
}
202+
203+
return {
204+
folders: folders.reverse(),
205+
files: files.reverse()
206+
};
207+
}
208+
157209
// Copies the 'source' file to 'target' if it's missing after creating the
158210
// required directory structure.
159211
function syncFile (source, target, callback) {
@@ -301,5 +353,7 @@ module.exports = {
301353
createShortcut: createShortcut,
302354
replaceFileContent: replaceFileContent,
303355
searchFile: searchFile,
304-
syncFiles: syncFiles
356+
syncFiles: syncFiles,
357+
flattenFolderStructure: flattenFolderStructureSync,
358+
folderScanSync: folderScanSync
305359
};

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pwabuilder-lib",
3-
"version": "2.0.5",
3+
"version": "2.0.6",
44
"description": "PWA Builder Core Library",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)