sb-scandir
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -0,3 +1,8 @@ | ||
#### 2.0.0 | ||
- Change CJS export to ES Export | ||
- Return directories along with files | ||
#### 1.0.0 | ||
- Initial release |
'use strict'; | ||
var scanDirectory = function () { | ||
var _ref = _asyncToGenerator(function* (path, recursive, validate) { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var scanDirectoryInternal = function () { | ||
var _ref = _asyncToGenerator(function* (path, recursive, validate, result) { | ||
var itemStat = yield stat(path); | ||
if (itemStat.isFile()) { | ||
return [path]; | ||
result.files.push(path); | ||
} else if (itemStat.isDirectory() && recursive !== 0) { | ||
result.directories.push(path); | ||
} | ||
if (!itemStat.isDirectory() || recursive === 0) { | ||
return []; | ||
return; | ||
} | ||
var contents = yield readdir(path); | ||
var results = yield Promise.all(contents.map(function (item) { | ||
var itemPath = _path2.default.join(path, item); | ||
if (validate(itemPath)) { | ||
return scanDirectory(itemPath, recursive === 1 ? 0 : 2, validate); | ||
} | ||
return []; | ||
})); | ||
return results.reduce(function (toReturn, current) { | ||
return toReturn.concat(current); | ||
}, []); | ||
yield (0, _pMap2.default)(contents, function () { | ||
var _ref2 = _asyncToGenerator(function* (item) { | ||
var itemPath = _path2.default.join(path, item); | ||
if (validate(itemPath)) { | ||
yield scanDirectoryInternal(itemPath, recursive === 1 ? 0 : 2, validate, result); | ||
} | ||
}); | ||
return function (_x5) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}()); | ||
}); | ||
return function scanDirectory(_x, _x2, _x3) { | ||
return function scanDirectoryInternal(_x, _x2, _x3, _x4) { | ||
return _ref.apply(this, arguments); | ||
@@ -30,2 +38,25 @@ }; | ||
var scanDirectory = function () { | ||
var _ref3 = _asyncToGenerator(function* (path) { | ||
var givenRecursive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
var givenValidate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; | ||
(0, _assert2.default)(path && typeof path === 'string', 'path must be a valid string'); | ||
(0, _assert2.default)(!givenValidate || typeof givenValidate === 'function', 'validate must be a valid function'); | ||
var recursive = !!givenRecursive; | ||
var validate = givenValidate || function (itemPath) { | ||
return _path2.default.basename(itemPath).substr(0, 1) !== '.'; | ||
}; | ||
var result = { files: [], directories: [] }; | ||
yield scanDirectoryInternal(path, recursive ? 2 : 1, validate, result); | ||
return result; | ||
}); | ||
return function scanDirectory(_x6) { | ||
return _ref3.apply(this, arguments); | ||
}; | ||
}(); | ||
var _fs = require('fs'); | ||
@@ -39,2 +70,6 @@ | ||
var _pMap = require('p-map'); | ||
var _pMap2 = _interopRequireDefault(_pMap); | ||
var _assert = require('assert'); | ||
@@ -50,3 +85,3 @@ | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; } | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } | ||
@@ -56,21 +91,2 @@ var stat = (0, _sbPromisify2.default)(_fs2.default.stat); | ||
module.exports = function () { | ||
var _ref2 = _asyncToGenerator(function* (path) { | ||
var givenRecursive = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1]; | ||
var givenValidate = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2]; | ||
(0, _assert2.default)(path && typeof path === 'string', 'path must be a valid string'); | ||
(0, _assert2.default)(!givenValidate || typeof givenValidate === 'function', 'validate must be a valid function'); | ||
var recursive = !!givenRecursive; | ||
var validate = givenValidate || function (itemPath) { | ||
return _path2.default.basename(itemPath).substr(0, 1) !== '.'; | ||
}; | ||
return yield scanDirectory(path, recursive ? 2 : 1, validate); | ||
}); | ||
return function (_x4, _x5, _x6) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}(); | ||
exports.default = scanDirectory; |
{ | ||
"name": "sb-scandir", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "File scanning module for Node.js", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "(apm test) && (flow check | grep -q '0 errors') && (eslint . )", | ||
"test": "(apm test) && (flow check) && (eslint . )", | ||
"clean": "rm -rf lib", | ||
@@ -14,9 +14,10 @@ "build": "npm run clean; babel src --out-dir lib", | ||
"babel-cli": "^6.11.4", | ||
"babel-preset-steelbrain": "^4.0.2", | ||
"eslint-config-steelbrain": "^1.0.4", | ||
"flow-bin": "^0.30.0", | ||
"babel-preset-steelbrain": "^5.0.0", | ||
"eslint-config-steelbrain": "^3.0.0", | ||
"flow-bin": "^0.54.1", | ||
"jasmine-fix": "^1.0.1" | ||
}, | ||
"dependencies": { | ||
"sb-promisify": "^1.3.0" | ||
"p-map": "^1.2.0", | ||
"sb-promisify": "^2.0.1" | ||
}, | ||
@@ -23,0 +24,0 @@ "repository": { |
# ScanDir | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/steelbrain/scandir.svg)](https://greenkeeper.io/) | ||
`sb-scandir` is a node module that supports simple file scanning with some sugar features. | ||
@@ -14,3 +16,3 @@ | ||
```js | ||
export default function scandir(path: string, recursive: boolean, validate: ((file: string) => boolean)): Promise<Array<string>> | ||
export default function scandir(path: string, recursive: boolean, validate: ((file: string) => boolean)): Promise<{ files: Array<string>, directories: Array<string> }> | ||
``` | ||
@@ -25,4 +27,5 @@ | ||
// Scan all files except the dot ones | ||
scandir(__dirname).then(function(files) { | ||
console.log('files', files) | ||
scandir(__dirname).then(function(result) { | ||
console.log('files', result.files) | ||
console.log('directories', result.directories) | ||
}) | ||
@@ -32,3 +35,4 @@ | ||
scandir(__dirname, false).then(function(files) { | ||
console.log('files', files) | ||
console.log('files', result.files) | ||
console.log('directories', result.directories) | ||
}) | ||
@@ -40,3 +44,4 @@ | ||
}).then(function(files) { | ||
console.log('files', files) | ||
console.log('files', result.files) | ||
console.log('directories', result.directories) | ||
}) | ||
@@ -49,3 +54,4 @@ | ||
}).then(function(files) { | ||
console.log('files', files) | ||
console.log('files', result.files) | ||
console.log('directories', result.directories) | ||
}) | ||
@@ -52,0 +58,0 @@ ``` |
7252
71
58
2
9
+ Addedp-map@^1.2.0
+ Addedp-map@1.2.0(transitive)
+ Addedsb-promisify@2.0.2(transitive)
- Removedsb-promisify@1.3.0(transitive)
Updatedsb-promisify@^2.0.1