Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

more-node-fs

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

more-node-fs - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

84

dist/index.d.ts
/// <reference types="node" />
import fs from 'fs';
interface PathOptions {
/** If specified, then will not look at paths that match this regex. */
ignore?: RegExp;
/** If specified, then will only look at paths that match this regex. */
match?: RegExp;
/** Specify a comparison function that is used when sorting each found directory.
* Note: the paths inside of a directory will still come before the directory itself, no matter
* what the comparison function does. For example:
* ['somedir/fileA', 'somedir/fileB', 'somedir/fileC', 'somedir/']
*/
sort?: (a: string, b: string) => number;
}
declare class ReaddirResult {
files: string[];
dirs: string[];
others: string[];
constructor();
}
/**

@@ -8,10 +26,4 @@ * Asyncronously loop through all paths found in or at the path parameter. This function is

* @param callback Called for each path found inside the path parameter. Can be an async function.
* @param options Optional parameters:
* - ignore: If specified, then will not look at paths that match this regex.
* @note This is about ~30% slower compared to the synchronous version of this function. So use
* that instead of this unless it's necessary.
*/
export declare function forEachPath(path: string, callback: (path: string, stats: fs.Stats) => void, options?: {
ignore?: RegExp;
}): Promise<void>;
export declare function forEachPath(path: string, callback: (path: string, stats: fs.Stats) => void, options?: PathOptions): Promise<void>;
/**

@@ -22,25 +34,26 @@ * Synchronously loop through all paths found in or at the path parameter. This function is

* @param callback Called for each path found inside the path parameter.
* @param options Optional parameters:
* - ignore: If specified, then will not look at paths that match this regex.
*/
export declare function forEachPathSync(path: string, callback: (path: string, stats: fs.Stats) => void, options?: {
ignore?: RegExp;
}): void;
export declare function forEachPathSync(path: string, callback: (path: string, stats: fs.Stats) => void, options?: PathOptions): void;
/**
* Asyncronously loop through all paths found in or at the path parameter and return each
* element that is returned inside the callback parameter. This function is called recursively.
* @param path The starting path.
* @param callback Called for each path found inside the path parameter. Can be
* an async function.
*/
export declare function mapPath(path: string, callback: (path: string, stats: fs.Stats) => void, options?: PathOptions): Promise<any[]>;
/**
* Syncronously loop through all paths found in or at the path parameter and return each
* element that is returned inside the callback parameter. This function is called recursively.
* @param path The starting path.
* @param callback Called for each path found inside the path parameter.
*/
export declare function mapPathSync(path: string, callback: (path: string, stats: fs.Stats) => void, options?: PathOptions): any[];
/**
* Asyncronously reads and returns all file and folder paths inside of the path parameter.
* @param path The path to read inside of.
* @param options Optional parameters:
* - ignore: If specified, then will not look at paths that match this regex.
* @returns Every path inside of the path parameter seperated into files, directories, and anything
* else is put in the others property.
* @note This is about ~30% slower compared to the synchronous version of this function. So use
* that instead of this unless it's necessary.
*/
export declare function readdirDeep(path: string, options?: {
ignore?: RegExp;
}): Promise<{
files: string[];
dirs: string[];
others: string[];
}>;
export declare function readdirDeep(path: string, options?: PathOptions): Promise<ReaddirResult>;
/**

@@ -51,12 +64,23 @@ * Syncronously reads and returns all file and folder paths inside of the path parameter.

* - ignore: If specified, then will not look at paths that match this regex.
* - match: If specified, then will only look at paths that match this regex.
* - sort: Specify a comparison function that is used when sorting each found directory.
* Note: the paths inside of a directory will still come before the directory itself. For example:
* ['somedir/file1', 'somedir/file2', 'somedir/file3', 'somedir/']
* @returns Every path inside of the path parameter seperated into files, directories, and anything
* else is put in the others property.
*/
export declare function readdirDeepSync(path: string, options?: {
ignore?: RegExp;
}): {
files: string[];
dirs: string[];
others: string[];
};
export declare function readdirDeepSync(path: string, options?: PathOptions): ReaddirResult;
/**
* Asynchronously deletes files and folders/directories. If path is to a directory then it will
* recursively delete all files within it first and then delete the directory itself.
* @param path Path to the file or directory.
*/
export declare function deleteDeep(path: string): Promise<void>;
/**
* Synchronously deletes files and folders/directories. If path is to a directory then it will
* recursively delete all files within it first and then delete the directory itself.
* @param path Path to the file or directory.
*/
export declare function deleteDeepSync(path: string): void;
export {};
//# sourceMappingURL=index.d.ts.map

@@ -15,3 +15,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.readdirDeepSync = exports.readdirDeep = exports.forEachPathSync = exports.forEachPath = void 0;
exports.deleteDeepSync = exports.deleteDeep = exports.readdirDeepSync = exports.readdirDeep = exports.mapPathSync = exports.mapPath = exports.forEachPathSync = exports.forEachPath = void 0;
const fs_1 = __importDefault(require("fs"));

@@ -24,2 +24,10 @@ const path_1 = require("path");

const unlink = util_1.promisify(fs_1.default.unlink);
const rmdir = util_1.promisify(fs_1.default.rmdir);
class ReaddirResult {
constructor() {
this.files = [];
this.dirs = [];
this.others = [];
}
}
/**

@@ -30,15 +38,17 @@ * Asyncronously loop through all paths found in or at the path parameter. This function is

* @param callback Called for each path found inside the path parameter. Can be an async function.
* @param options Optional parameters:
* - ignore: If specified, then will not look at paths that match this regex.
* @note This is about ~30% slower compared to the synchronous version of this function. So use
* that instead of this unless it's necessary.
*/
function forEachPath(path, callback, options = {}) {
return __awaiter(this, void 0, void 0, function* () {
if (!fs_1.default.existsSync(path) || (options.ignore instanceof RegExp && options.ignore.test(path)))
if (!fs_1.default.existsSync(path) ||
(options.ignore instanceof RegExp && options.ignore.test(path)) ||
(options.match instanceof RegExp && !options.match.test(path)))
return;
const stats = yield stat(path);
if (stats.isDirectory())
for (const pathInFolder of (yield readdir(path)))
yield forEachPath(path_1.join(path, pathInFolder.toString()), callback, options);
if (stats.isDirectory()) {
const dir = yield readdir(path);
if (options.sort instanceof Function)
dir.sort(options.sort);
for (const pathInDir of dir)
yield forEachPath(path_1.join(path, pathInDir.toString()), callback, options);
}
yield callback(path, stats);

@@ -53,12 +63,16 @@ });

* @param callback Called for each path found inside the path parameter.
* @param options Optional parameters:
* - ignore: If specified, then will not look at paths that match this regex.
*/
function forEachPathSync(path, callback, options = {}) {
if (!fs_1.default.existsSync(path) || (options.ignore instanceof RegExp && options.ignore.test(path)))
if (!fs_1.default.existsSync(path) ||
(options.ignore instanceof RegExp && options.ignore.test(path)) ||
(options.match instanceof RegExp && !options.match.test(path)))
return;
const stats = fs_1.default.lstatSync(path);
if (stats.isDirectory())
for (const pathInFolder of fs_1.default.readdirSync(path))
if (stats.isDirectory()) {
const dir = fs_1.default.readdirSync(path);
if (options.sort instanceof Function)
dir.sort(options.sort);
for (const pathInFolder of dir)
forEachPathSync(path_1.join(path, pathInFolder), callback, options);
}
callback(path, stats);

@@ -68,16 +82,42 @@ }

/**
* Asyncronously loop through all paths found in or at the path parameter and return each
* element that is returned inside the callback parameter. This function is called recursively.
* @param path The starting path.
* @param callback Called for each path found inside the path parameter. Can be
* an async function.
*/
function mapPath(path, callback, options = {}) {
return __awaiter(this, void 0, void 0, function* () {
const result = [];
yield forEachPath(path, (pathInFolder, stats) => __awaiter(this, void 0, void 0, function* () {
result.push((yield callback(pathInFolder, stats)));
}), options);
return result;
});
}
exports.mapPath = mapPath;
/**
* Syncronously loop through all paths found in or at the path parameter and return each
* element that is returned inside the callback parameter. This function is called recursively.
* @param path The starting path.
* @param callback Called for each path found inside the path parameter.
*/
function mapPathSync(path, callback, options = {}) {
const result = [];
forEachPathSync(path, (pathInFolder, stats) => {
result.push(callback(pathInFolder, stats));
}, options);
return result;
}
exports.mapPathSync = mapPathSync;
/**
* Asyncronously reads and returns all file and folder paths inside of the path parameter.
* @param path The path to read inside of.
* @param options Optional parameters:
* - ignore: If specified, then will not look at paths that match this regex.
* @returns Every path inside of the path parameter seperated into files, directories, and anything
* else is put in the others property.
* @note This is about ~30% slower compared to the synchronous version of this function. So use
* that instead of this unless it's necessary.
*/
function readdirDeep(path, options = {}) {
return __awaiter(this, void 0, void 0, function* () {
const result = { files: [], dirs: [], others: [] };
yield forEachPath(path, (pathInFolder) => __awaiter(this, void 0, void 0, function* () {
const stats = yield stat(pathInFolder);
const result = new ReaddirResult();
yield forEachPath(path, (pathInFolder, stats) => __awaiter(this, void 0, void 0, function* () {
if (stats.isFile())

@@ -99,2 +139,6 @@ result.files.push(pathInFolder);

* - ignore: If specified, then will not look at paths that match this regex.
* - match: If specified, then will only look at paths that match this regex.
* - sort: Specify a comparison function that is used when sorting each found directory.
* Note: the paths inside of a directory will still come before the directory itself. For example:
* ['somedir/file1', 'somedir/file2', 'somedir/file3', 'somedir/']
* @returns Every path inside of the path parameter seperated into files, directories, and anything

@@ -104,5 +148,4 @@ * else is put in the others property.

function readdirDeepSync(path, options = {}) {
const result = { files: [], dirs: [], others: [] };
forEachPathSync(path, pathInFolder => {
const stats = fs_1.default.statSync(pathInFolder);
const result = new ReaddirResult();
forEachPathSync(path, (pathInFolder, stats) => {
if (stats.isFile())

@@ -118,2 +161,32 @@ result.files.push(pathInFolder);

exports.readdirDeepSync = readdirDeepSync;
/**
* Asynchronously deletes files and folders/directories. If path is to a directory then it will
* recursively delete all files within it first and then delete the directory itself.
* @param path Path to the file or directory.
*/
function deleteDeep(path) {
return __awaiter(this, void 0, void 0, function* () {
yield forEachPath(path, (pathInFolder, stats) => __awaiter(this, void 0, void 0, function* () {
if (stats.isDirectory())
yield rmdir(pathInFolder);
else
yield unlink(pathInFolder);
}));
});
}
exports.deleteDeep = deleteDeep;
/**
* Synchronously deletes files and folders/directories. If path is to a directory then it will
* recursively delete all files within it first and then delete the directory itself.
* @param path Path to the file or directory.
*/
function deleteDeepSync(path) {
forEachPathSync(path, (pathInFolder, stats) => {
if (stats.isDirectory())
fs_1.default.rmdirSync(pathInFolder);
else
fs_1.default.unlinkSync(pathInFolder);
});
}
exports.deleteDeepSync = deleteDeepSync;
//# sourceMappingURL=index.js.map
{
"name": "more-node-fs",
"version": "0.2.0",
"version": "0.3.0",
"description": "A utility which adds some more File System functions for Node.",

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

# More-Node-FS
A utility which adds some more File System functions for Node.
A utility which adds some more File System functions for NodeJS. It's written in typescript and is well documented with types and descriptions.
There are synchronous and asynchronous versions of each function. Note the synchronous versions are faster but the asynchronous versions are still provided for cases like running in a server, etc.
## Documentation
### forEachPath & forEachPathSync
This recursively loops through all files and directories within or at the specified path and calls the callback given for each of them.
```js
(async () => {
const { forEachPath, forEachPathSync } = require('more-node-fs');
// Will log all png files within the somewhere directory:
await forEachPath('./path/to/somewhere', (path, stats) => {
if (stats.isFile() && /\.png$/i.test(path))
console.log(path);
});
// Will log all pdf files withing the else directory:
forEachPathSync('./path/to/somewhere/else', (path, stats) => {
if (stats.isFile() && /\.pdf$/i.test(path))
console.log(path);
});
})();
```
### mapPath & mapPathSync
```js
(async () => {
const { mapPath, mapPathSync } = require('more-node-fs');
const { readFileSync } = require('fs');
// Will find all javascript files within the somewhere directory:
const jsFiles = await mapPath('./path/to/somewhere', (path, stats) => {
return stats.isFile() && /\.js$/i.test(path)
? path
: null;
}).filter(jsFilePath => jsFilePath);
// Will read and store all png files to images variable:
const images = mapPathSync('./path/to/somewhere/else', (path, stats) => {
return stats.isFile() && /\.png$/i.test(path))
? readFileSync(path)
: null;
}).filter(image => image);
})();
```
### readdirDeep & readdirDeepSync
Recursively finds all files, directories and other files and stores them into seperate properties.
```js
(async () => {
const { readdir, readdirSync } = require('more-node-fs');
const { files, dirs, others } = await readdir('path/to/somewhere');
// or:
const { files, dirs, others } = readdirSync('some/other/path');
})();
```
### deleteDeep & deleteDeepSync
Deletes files and directories. If a directory is specified then it will recursively delete everything inside of it as well.
```js
(async () => {
const { deleteDeep, deleteDeepSync } = require('more-node-fs');
await deleteDeep('path/to/somewhere');
// or:
deleteDeepSync('some/other/path');
})();
```

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc