Socket
Socket
Sign inDemoInstall

node-dir

Package Overview
Dependencies
4
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.16 to 0.1.17

4

index.js
var dirpaths = require('./lib/paths');
exports.files = dirpaths.files;
exports.paths = dirpaths.paths;
exports.subdirs = dirpaths.subdirs;
Object.assign(exports, dirpaths)
exports.readFiles = require('./lib/readfiles');
exports.readFilesStream = require('./lib/readfilesstream');
var fs = require('fs'),
path = require('path');
exports.promiseFiles = function promiseFiles(dir, type, options){
type = type || 'file'
var processor = function(res,rej){
var cb = function(err,data){
if(err)return rej(err)
res(data)
}
exports.files(dir,type,cb,options)
}
return new Promise(processor)
}
/**

@@ -16,71 +29,139 @@ * find all files or subdirs (recursive) and pass to callback fn

*/
exports.files = function files(dir, type, callback, /* used internally */ ignoreType) {
exports.files = function files(dir, type, callback, options) {
var ofType = typeof type
if(ofType == 'object'){
options = options || type
type = 'file'
callback = function(){}
}else if (ofType !== 'string') {
//ignoreType = callback;
callback = type;
type = 'file';
}
options = options || {}
var pending,
results = {
files: [],
dirs: []
};
var done = function() {
if (ignoreType || type === 'all') {
callback(null, results);
} else {
callback(null, results[type + 's']);
var pending,
results = {
files: [],
dirs: []
};
var done = function() {
if(type==='combine'){
results = results.files.concat(results.dirs)
} else if (!type || options.ignoreType || ['all','combine'].indexOf(type)>=0) {
results = results
} else {
results = results[type + 's']
}
if(options.sync)return;
callback(null, results);
};
var getStatHandler = function(statPath, name, lstatCalled) {
return function(err, stat) {
if (err) {
if (!lstatCalled) {
return fs.lstat(statPath, getStatHandler(statPath, name, true));
}
};
return callback(err);
}
var getStatHandler = function(statPath, lstatCalled) {
return function(err, stat) {
if (err) {
if (!lstatCalled) {
return fs.lstat(statPath, getStatHandler(statPath, true));
}
return callback(err);
var pushVal = options.shortName ? name : statPath
if (stat && stat.isDirectory() && stat.mode !== 17115) {
if (type !== 'file') {
results.dirs.push(pushVal);
}
if (options.recursive==null || options.recursive) {
var subloop = function(err, res) {
if (err){
return callback(err)
}
if (stat && stat.isDirectory() && stat.mode !== 17115) {
if (type !== 'file') {
results.dirs.push(statPath);
}
files(statPath, type, function(err, res) {
if (err) return callback(err);
if (type === 'all') {
results.files = results.files.concat(res.files);
results.dirs = results.dirs.concat(res.dirs);
} else if (type === 'file') {
results.files = results.files.concat(res.files);
} else {
results.dirs = results.dirs.concat(res.dirs);
}
if (!--pending) done();
}, true);
if(type === 'combine'){
results.files = results.files.concat(res);
}else if (type === 'all') {
results.files = results.files.concat(res.files);
results.dirs = results.dirs.concat(res.dirs);
} else if (type === 'file') {
results.files = results.files.concat(res.files);
} else {
if (type !== 'dir') {
results.files.push(statPath);
}
// should be the last statement in statHandler
if (!--pending) done();
results.dirs = results.dirs.concat(res.dirs);
}
};
};
if (typeof type !== 'string') {
ignoreType = callback;
callback = type;
type = 'file';
if (!--pending){
done();
}
}
var newOptions = Object.assign({}, options)
newOptions.ignoreType = true
var moreResults = files(statPath, type, subloop, newOptions);
if(options.sync){
subloop(null, moreResults)
}
}else if (!--pending){
done()
}
} else {
if (type !== 'dir') {
results.files.push(pushVal);
}
// should be the last statement in statHandler
if (!--pending){
done()
}
}
}
}
fs.stat(dir, function(err, stat) {
if (err) return callback(err);
if(stat && stat.mode === 17115) return done();
var bufdir = Buffer.from(dir);
fs.readdir(dir, function(err, list) {
if (err) return callback(err);
pending = list.length;
if (!pending) return done();
for (var file, i = 0, l = list.length; i < l; i++) {
file = path.join(dir, list[i]);
fs.stat(file, getStatHandler(file));
}
});
});
const onDirRead = function(err, list) {
if (err) return callback(err);
pending = list.length;
if (!pending) return done();
for (var file, i = 0, l = list.length; i < l; i++) {
var fname = list[i].toString();
file = path.join(dir, fname);
var buffile = Buffer.concat([bufdir, Buffer.from(path.sep), list[i]]);
if(options.sync){
var res = fs.statSync(buffile);
getStatHandler(file,fname)(null, res)
}else{
fs.stat(buffile, getStatHandler(file,fname));
}
}
return results
}
const onStat = function(err, stat) {
if (err) return callback(err);
if (stat && stat.mode === 17115) return done();
if(options.sync){
const list = fs.readdirSync(bufdir, {encoding: 'buffer'})
return onDirRead(null, list)
}else{
fs.readdir(bufdir, {encoding: 'buffer'}, onDirRead)
}
}
if(options.sync){
const stat = fs.statSync(bufdir);
return onStat(null, stat)
}else{
fs.stat(bufdir, onStat);
}
};

@@ -140,7 +221,22 @@

*/
exports.subdirs = function subdirs(dir, callback) {
exports.files(dir, 'dir', function(err, subdirs) {
if (err) return callback(err);
callback(null, subdirs);
});
exports.subdirs = function subdirs(dir, callback, type, options) {
options = options || {}
const iCallback = function(err, subdirs) {
if (err) return callback(err);
if(type=='combine'){
subdirs = subdirs.files.concat(subdirs.dirs)
}
if(options.sync)return subdirs
callback(null, subdirs);
}
const res = exports.files(dir, 'dir', iCallback, options)
if(options && options.sync){
return iCallback(null,res)
}
};
{
"name": "node-dir",
"version": "0.1.16",
"version": "0.1.17",
"description": "asynchronous file and directory operations for Node.js",

@@ -5,0 +5,0 @@ "main": "index",

@@ -6,2 +6,19 @@ [![Build Status](https://secure.travis-ci.org/fshost/node-dir.svg)](http://travis-ci.org/fshost/node-dir)

### Table of Contents
- [installation](#installation)
- [usage](#usage)
- [methods](#methods)
- [readFiles( dir, options, fileCallback, finishedCallback)](#readfiles-dir-options-filecallback-finishedcallback)
- [readFilesStream( dir, options, streamCallback, finishedCallback)](#readfilesstream-dir-options-streamcallback-finishedcallback)
- [readFilesStream examples](#readfilesstream-examples)
- [files( dir, callback )](#files-dir-callback)
- [files( dir, {sync:true} )](#files-dir-synctrue)
- [promiseFiles( dir, callback )](#promisefiles-dir-callback)
- [subdirs( dir, callback )](#subdirs-dir-callback)
- [paths(dir, [combine], callback )](#pathsdir-combine-callback)
- [API Docs](#api-docs)
- [files(dir, type, callback, options)](#filesdir-type-callback-options)
- [License](#license)
#### installation

@@ -11,2 +28,4 @@

### usage
#### methods

@@ -37,3 +56,3 @@ For the sake of brevity, assume that the following line of code precedes all of the examples.

examples
#### readFilesStream examples

@@ -117,3 +136,22 @@ ```javascript

```
#### files( dir, {sync:true} )
Synchronously iterate the files of a directory and its subdirectories and pass an array of file paths to a callback.
```javascript
var files = dir.files(__dirname, {sync:true});
console.log(files);
```
#### promiseFiles( dir, callback )
Asynchronously iterate the files of a directory and its subdirectories and pass an array of file paths to a callback.
```javascript
dir.promiseFiles(__dirname)
.then((files)=>{
console.log(files);
})
.catch(e=>console.error(e))
```
Note that for the files and subdirs the object returned is an array, and thus all of the standard array methods are available for use in your callback for operations like filters or sorting. Some quick examples:

@@ -174,4 +212,19 @@

## API Docs
### files(dir, type, callback, options)
- **dir** - directory path to read
- **type**='file'
- 'file' returns only file listings
- 'dir' returns only directory listings
- 'all' returns {dirs:[], files:[]}
- 'combine' returns []
- **callback** -
- **options**
- **sync**=false - results are returned inline and no callbacks are used
- **shortName**=false - instead of fullpath file names, just get the names
- **recursive**=true - traverse through all children of given path
## License
MIT licensed (See LICENSE.txt)
SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc