Socket
Socket
Sign inDemoInstall

more-node-fs

Package Overview
Dependencies
0
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.3 to 1.2.0

17

dist/index.d.ts

@@ -40,2 +40,3 @@ /// <reference types="node" />

* Asyncronously reads and returns all file and folder paths inside of the path parameter.
* Internally this uses forEachPath.
* @param path The path to read inside of.

@@ -48,3 +49,4 @@ * @param options Path options.

/**
* Syncronously reads and returns all file and folder paths inside of the path parameter.
* Syncronously reads and returns all file and folder paths inside of the path parameter. Internally
* this uses walkdir.
* @param path The path to read inside of.

@@ -68,9 +70,16 @@ * @param options Path options.

export declare function deleteDeepSync(path: string): void;
interface WalkdirOptions extends PathOptions {
/**
* The type of search method to use. Either depth first search (DFS) or breadth first
* search (BFS). BFS is default.
*/
search?: 'dfs' | 'bfs';
}
/**
* Iterator for recursively searching through a directory. Unlike forEachPath, this uses breadth
* first search.
* Iterator for searching through a directory and any sub directories. Can to use either depth first
* search of breadth first search. BFS is default.
* @param path The starting path.
* @param options Path options.
*/
export declare function walkdir(path: string, options?: PathOptions): Generator<{
export declare function walkdir(path: string, options?: WalkdirOptions): Generator<{
stats: fs.Stats;

@@ -77,0 +86,0 @@ path: string;

@@ -81,2 +81,3 @@ "use strict";

* Asyncronously reads and returns all file and folder paths inside of the path parameter.
* Internally this uses forEachPath.
* @param path The path to read inside of.

@@ -103,3 +104,4 @@ * @param options Path options.

/**
* Syncronously reads and returns all file and folder paths inside of the path parameter.
* Syncronously reads and returns all file and folder paths inside of the path parameter. Internally
* this uses walkdir.
* @param path The path to read inside of.

@@ -112,3 +114,3 @@ * @param options Path options.

const result = new ReaddirResult();
forEachPathSync(path, (pathInFolder, stats) => {
for (const { stats, path: pathInFolder } of walkdir(path, options)) {
if (stats.isFile())

@@ -120,3 +122,3 @@ result.files.push(pathInFolder);

result.others.push(pathInFolder);
}, options);
}
return result;

@@ -132,3 +134,3 @@ }

return __awaiter(this, void 0, void 0, function* () {
yield forEachPath(path, (pathInFolder, stats) => __awaiter(this, void 0, void 0, function* () {
for (const { stats, path: pathInFolder } of walkdir(path, { search: 'dfs' })) {
if (stats.isDirectory())

@@ -138,3 +140,3 @@ yield rmdir(pathInFolder);

yield unlink(pathInFolder);
}));
}
});

@@ -149,3 +151,3 @@ }

function deleteDeepSync(path) {
forEachPathSync(path, (pathInFolder, stats) => {
for (const { stats, path: pathInFolder } of walkdir(path, { search: 'dfs' })) {
if (stats.isDirectory())

@@ -155,3 +157,3 @@ fs_1.default.rmdirSync(pathInFolder);

fs_1.default.unlinkSync(pathInFolder);
});
}
}

@@ -163,11 +165,3 @@ exports.deleteDeepSync = deleteDeepSync;

}
/**
* Iterator for recursively searching through a directory. Unlike forEachPath, this uses breadth
* first search.
* @param path The starting path.
* @param options Path options.
*/
function* walkdir(path, options = {}) {
if (!fs_1.default.existsSync(path) || !passesRegex(path, options))
return;
function* iterativeBFS(path, options = {}) {
let stats = fs_1.default.statSync(path);

@@ -195,3 +189,51 @@ yield { stats, path };

}
function* iterativeDFS(path, options = {}) {
let stats = fs_1.default.statSync(path);
if (!stats.isDirectory()) {
yield { stats, path };
return;
}
const stack = [{ stats, path }]; // A stack for everything
const dirStack = [{ stats, path }]; // A stack for all unvisited directories
while (dirStack.length) {
const dir = dirStack.pop();
const dirStackLength = dirStack.length;
const pathsInDir = fs_1.default.readdirSync(dir.path);
if (options.sort instanceof Function)
pathsInDir.sort(options.sort);
for (path of pathsInDir) {
path = path_1.join(dir.path, path);
if (passesRegex(path, options)) {
stats = fs_1.default.statSync(path);
stack.push({ stats, path });
if (stats.isDirectory()) {
dirStack.push({ stats, path });
}
}
}
// If dir had no other directories inside of it:
if (dirStack.length && dirStack.length === dirStackLength) {
while (stack.length && stack[stack.length - 1].path !== dirStack[dirStack.length - 1].path)
yield stack.pop();
}
}
// Finish emptying the rest of the stack:
while (stack.length)
yield stack.pop();
}
/**
* Iterator for searching through a directory and any sub directories. Can to use either depth first
* search of breadth first search. BFS is default.
* @param path The starting path.
* @param options Path options.
*/
function walkdir(path, options = { search: 'bfs' }) {
if (!fs_1.default.existsSync(path) || !passesRegex(path, options))
return;
if (options.search === 'dfs')
return iterativeDFS(path, options);
else
return iterativeBFS(path, options);
}
exports.walkdir = walkdir;
//# sourceMappingURL=index.js.map
{
"name": "more-node-fs",
"version": "1.1.3",
"version": "1.2.0",
"description": "A utility which adds some more File System functions for Node.",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -9,8 +9,8 @@

- forEachPath & forEachPathSync:
Recursively loop through all files and directories within or at the specified path and calls the callback given for each of them. Uses depth first search.
Recursively loop through all files and directories within or at the specified path and calls the callback given for each of them. Uses a recursive depth first search.
- readdirDeep & readdirDeepSync:
Recursively finds all files, directories and other files and stores them into separate properties.
Recursively finds all files, directories and other files and stores them into separate properties. Internally, readdirDeep uses forEachPath and readdirDeepSync uses walkdir.
- deleteDeep & deleteDeepSync:
Deletes files and directories. If a directory is specified then it will recursively delete everything inside of it as well.
- walkdir: Generator function that iterates through a directory and any sub directories. Unlike the other functions, this uses breadth first search.
- walkdir: Generator function that iterates through a directory and any sub directories. It can use either depth or breadth first search via an option property called 'search'. It is much quicker than the forEachPath variants but it's synchronous. Neither search option is faster than the other, but 'bfs' is somewhat more memory efficient.
- Extra options available to forEachPath, readdirDeep and walkdir:

@@ -35,3 +35,3 @@ - ignore: If specified, then will not look at paths that match this regex. This and the match option can significantly speed up path searches.

const images2 = [];
for (const { stats, path } of walkdir('./path/to/somewhere')) {
for (const { stats, path } of walkdir('./path/to/somewhere'), { search: 'dfs' }) {
if (stats.isFile() && /\.png$/i.test(path))

@@ -44,5 +44,8 @@ images2.push(path);

`forEachPathSync x 394 ops/sec, ±3 ops/sec or ±0.85% (90 runs sampled)`
`readdirDeepSync x 401 ops/sec, ±1 ops/sec or ±0.37% (92 runs sampled)`
`walkdir x 897 ops/sec, ±5 ops/sec or ±0.58% (91 runs sampled)`
```
forEachPathSync x 410 ops/sec, ±2 ops/sec or ±0.52% (92 runs sampled)
readdirDeepSync x 925 ops/sec, ±3 ops/sec or ±0.36% (95 runs sampled)
walkdir x 930 ops/sec, ±3 ops/sec or ±0.30% (95 runs sampled)
Fastest is walkdir
```

@@ -49,0 +52,0 @@ ## Authors or Acknowledgments

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc