Socket
Socket
Sign inDemoInstall

@danmasta/walk

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@danmasta/walk - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

tests/options.js

134

index.js

@@ -13,9 +13,9 @@ const path = require('path');

// walk directory asyncronously using promises
function walkDirAsync(opts, res = []) {
function walkDirAsync() {
opts = util.opts(opts);
let opts = util.opts(...arguments);
function walkdir(dir) {
return readdirAsync(dir).map(name => {
return util.concatMapAsync(readdirAsync(dir), name => {

@@ -37,3 +37,3 @@ let abs = path.resolve(dir, name);

if (opts.matcher(file.relative || file.base)) {
return res.push(file);
return file;
}

@@ -49,14 +49,48 @@

return walkdir(opts.root).return(res);
return walkdir(opts.root);
}
// walk async
function walk() {
let opts = util.opts(...arguments);
return statAsync(opts.root).then(stat => {
if (stat.isDirectory()) {
return walkDirAsync(opts);
} else {
let file = new File({ path: opts.root, stat: stat, cwd: opts.cwd, root: opts.root });
if (opts.matcher(file.relative || file.base)) {
return [file];
} else {
return [];
}
}
}).catch(err => {
opts.root = require.resolve(opts.root);
return walk(opts);
});
}
// walk directory syncronously
function walkDirSync(opts, res = []) {
function walkDirSync() {
opts = util.opts(opts);
let opts = util.opts(...arguments);
function walkdir(dir) {
return fs.readdirSync(dir).map(name => {
return util.concatMap(fs.readdirSync(dir), name => {

@@ -77,3 +111,3 @@ let abs = path.resolve(dir, name);

if (opts.matcher(file.relative || file.base)) {
return res.push(file);
return file;
}

@@ -87,42 +121,10 @@

return walkdir(opts.root) && res;
return walkdir(opts.root);
}
// walk async
function walk(opts, res = []) {
opts = util.opts(opts);
return statAsync(opts.root).then(stat => {
if (stat.isDirectory()) {
return walkDirAsync(opts, res);
} else {
let file = new File({ path: opts.root, stat: stat, cwd: opts.cwd, root: opts.root });
if (opts.matcher(file.relative || file.base)) {
return res.push(file);
}
}
}).return(res).catch(err => {
opts.root = require.resolve(opts.root);
return walk(opts, res);
});
}
// walk sync
function walkSync(opts, res = []) {
function walkSync() {
opts = util.opts(opts);
let opts = util.opts(...arguments);
let stat = null;

@@ -138,3 +140,3 @@

return walkSync(opts, res);
return walkSync(opts);

@@ -145,3 +147,3 @@ }

return walkDirSync(opts, res);
return walkDirSync(opts);

@@ -153,7 +155,7 @@ } else {

if (opts.matcher(file.relative || file.base)) {
res.push(file);
return [file];
} else {
return [];
}
return res;
}

@@ -164,7 +166,7 @@

// get file contents async
function contents(opts, res = []) {
function contents() {
opts = util.opts(opts);
let opts = util.opts(...arguments);
return walk(opts, res).map(file => {
return walk(opts).map(file => {

@@ -194,7 +196,7 @@ if (opts.require) {

// get file contents sync
function contentsSync(opts, res = []) {
function contentsSync() {
opts = util.opts(opts);
let opts = util.opts(...arguments);
return walkSync(opts, res).map(file => {
return walkSync(opts).map(file => {

@@ -220,12 +222,11 @@ if (opts.require) {

// run callback for each file async
function each(opts, cb) {
function each(...args) {
opts = util.opts(opts);
let cb = typeof args[args.length - 1] === 'function' ? args.pop() : file => {
return file;
};
let opts = util.opts(...args);
let fn = opts.read ? contents : walk;
cb = typeof cb === 'function' ? cb : function (file) {
return file;
};
return fn.call(null, opts).map(cb);

@@ -236,12 +237,11 @@

// run callback for each file sync
function eachSync(opts, cb) {
function eachSync(...args) {
opts = util.opts(opts);
let cb = typeof args[args.length - 1] === 'function' ? args.pop() : file => {
return file;
};
let opts = util.opts(...args);
let fn = opts.read ? contentsSync : walkSync;
cb = typeof cb === 'function' ? cb : function (file) {
return file;
};
return fn.call(null, opts).map(cb);

@@ -248,0 +248,0 @@

@@ -8,3 +8,3 @@ const path = require('path');

cwd: process.cwd(),
root: '/',
root: './',
exclude: ['.git', 'node_modules', 'bower_components'],

@@ -20,10 +20,8 @@ require: false,

constructor(opts = {}) {
constructor(opts) {
_.defaults(this, opts, defaults);
this.root = stripTrailingSep(path.join(this.cwd, this.root));
this.root = stripTrailingSep(path.resolve(this.cwd, this.root));
this.regex = new RegExp(_.join(_.concat(this.exclude), '|') || 'a^');
this.matcher = micromatch.matcher(this.src, { dot: this.dot });

@@ -35,2 +33,46 @@

function concatMap(collection, fn) {
let res = [];
_.map(collection, (item, key) => {
let x = fn(item, key);
if (_.isArray(x)) {
return res.push.apply(res, x);
}
if (x !== undefined) {
res.push(x);
}
});
return res;
}
function concatMapAsync(promise, fn) {
let res = [];
return promise.map(fn).map(x => {
if (_.isArray(x)) {
return res.push.apply(res, x);
}
if (x !== undefined) {
res.push(x);
}
}).then(() => {
return res;
});
}
function normalize(str) {

@@ -60,4 +102,24 @@ return str && path.normalize(str);

exports.opts = function (opts) {
return opts instanceof Options ? opts : new Options(opts);
exports.opts = function(str, opts) {
if(str instanceof Options){
return str;
}
if (str && _.isPlainObject(opts)) {
opts.root = str;
} else if (_.isString(str)) {
opts = { root: str };
} else {
opts = str;
}
return new Options(opts);
};

@@ -70,1 +132,3 @@

exports.isBuffer = isBuffer;
exports.concatMap = concatMap;
exports.concatMapAsync = concatMapAsync;
{
"name": "@danmasta/walk",
"version": "1.0.0",
"version": "1.1.0",
"author": "Daniel Smith <dannmasta@gmail.com>",

@@ -5,0 +5,0 @@ "description": "Directory and file walking utility for node apps",

@@ -32,7 +32,7 @@ # Walk

-----|----- | -----------
`cwd` | *`string`* | Base directory to start walk from. Default is `process.cwd()`
`root` | *`string`* | Directory or file path to walk. This gets normalized as `path.join(cwd, root)`. Default is `/`
`cwd` | *`string`* | Base directory to start walk from. Default is `process.cwd`
`root` | *`string`* | Directory or file path to walk. This gets normalized as `path.resolve(cwd, root)`. Default is `./`
`exclude` | *`array`* | Array of directory names to exclude from walk. Defaults to `['.git', 'node_modules', 'bower_components']`. Directory names are excluded `before` reading them, this helps it stay fast
`require` | *`boolean`* | Whether to `require` file contents instead of reading them. Default is `false`
`read` | *`boolean`* | Whether to `read\|require` file contents when using `each()`. Defaults to `true`
`read` | *`boolean`* | Whether to `read\|require` file contents when using `each`. Defaults to `true`
`src` | *`Array\|String\|RegExp`* | [Micromatch pattern](https://github.com/micromatch/micromatch#matcher) for result filtering. Can be a path string, glob pattern string, regular expression, or an array of strings. Defaults to `**/*`

@@ -44,8 +44,9 @@ `dot` | *`boolean`* | Whether or not to ignore dot files when matching. Default is `true`

-----|------------
`walk(opts)` | Get a list of files based on specified options. Returns a promise that resolves with an array of file objects
`walkSync(opts)` | Sync version of `walk()`
`contents(opts)` | Get the contents of files based on specified options. Returns a promise that resolves with an array of file objects
`contentsSync(opts)` | Sync version of `contents()`
`each(opts, cb)` | Runs a callback for each file based on specified options. Returns a promise that resolves with an array of file objects. Callback takes one argument [`file`](#file-objects)
`eachSync(opts, cb)` | Sync version of `each()`
`walk([path,][opts])` | Get a list of files based on specified options. Returns a promise that resolves with an array of file objects
`walkSync` | Sync version of `walk`
`contents([path,][opts])` | Get the contents of files based on specified options. Returns a promise that resolves with an array of file objects
`contentsSync` | Sync version of `contents`
`each([path,][opts,][iteratee])` | Runs an iteratee function for each file based on specified options. Returns a promise that resolves with an array of file objects. Iteratee takes one argument [`file`](#file-objects)
`eachSync` | Sync version of `each`
*Each method takes an optional `path` and `options` param as arguments. The `each` methods also accept an iteratee function as the last argument*

@@ -83,3 +84,3 @@ ## File Objects

```
Walk the current directory, exclude all `.json` files
Walk the current working directory, exclude all `.json` files
```js

@@ -92,6 +93,12 @@ walk({ src: '**/*.!(json)' }).then(res => {

```js
walk({ root: './config', src: '**/*.json' }).then(res => {
walk('./config', { src: '**/*.json' }).then(res => {
console.log('files:', res);
});
```
Walk a directory using an absolute path
```js
walk('/usr/local').then(res => {
console.log('files:', res);
});
```

@@ -104,3 +111,3 @@ ### Contents

```js
contents({ root: './views', src: '**/*.pug' }).then(res => {
contents('./views', { src: '**/*.pug' }).then(res => {
console.log('templates:', res);

@@ -116,3 +123,3 @@ });

```js
each({ root: './routes', src: '**/*.js', require: true }, route => {
each('./routes', { src: '**/*.js', require: true }, route => {
app.use(route());

@@ -131,3 +138,3 @@ }).then(res => {

```js
const templates = contents({ root: './views', src: '**/*.pug' });
const templates = contents('./views', { src: '**/*.pug' });
console.log('templates:', templates);

@@ -134,0 +141,0 @@ ```

@@ -0,5 +1,9 @@

const path = require('path');
const chai = require('chai');
const walk = require('../index');
const File = require('../lib/file');
const util = require('../lib/util');
beforeEach(function() {
global.path = path;
global.assert = chai.assert;

@@ -9,2 +13,4 @@ global.expect = chai.expect;

global.walk = walk;
global.File = File;
global.util = util;
});
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