fs-extra
Advanced tools
Comparing version 0.24.0 to 0.25.0
@@ -0,1 +1,5 @@ | ||
0.25.0 / 2015-10-24 | ||
------------------- | ||
- now has a file walker `walk()` | ||
0.24.0 / 2015-08-28 | ||
@@ -2,0 +6,0 @@ ------------------- |
@@ -20,2 +20,3 @@ var path = require('path') | ||
} | ||
var dir = path.dirname(dstpath) | ||
@@ -34,3 +35,2 @@ fs.exists(dir, function (dirExists) { | ||
function createLinkSync (srcpath, dstpath, callback) { | ||
var destinationExists = fs.existsSync(dstpath) | ||
@@ -37,0 +37,0 @@ if (destinationExists) return undefined |
@@ -18,3 +18,3 @@ var fs = require('graceful-fs') | ||
var stats = fs.lstatSync(srcpath) | ||
} catch(e) { | ||
} catch (e) { | ||
return 'file' | ||
@@ -21,0 +21,0 @@ } |
@@ -23,2 +23,3 @@ var assign = require('./util/assign') | ||
assign(fs, require('./output')) | ||
assign(fs, require('./walk')) | ||
@@ -25,0 +26,0 @@ module.exports = fs |
var Walker = require('./walker') | ||
function walk (path, filter) { | ||
return new Walker(path, filter)// .start() | ||
function walk (path) { | ||
return new Walker(path)// .start() | ||
} | ||
module.exports = walk | ||
module.exports = { | ||
walk: walk | ||
} |
@@ -7,2 +7,3 @@ var fs = require('fs') | ||
// DO NOT USE OR DEPEND UPON FILTER. It will almost certainly be removed. | ||
function Walker (dir, filter, streamOptions) { | ||
@@ -26,12 +27,12 @@ Readable.call(this, assign({ objectMode: true }, streamOptions)) | ||
fs.lstat(item, function (err, stat) { | ||
fs.lstat(item, function (err, stats) { | ||
if (err) { | ||
self.emit('error', err, {path: item, stat: stat}) | ||
self.emit('error', err, {path: item, stats: stats}) | ||
return self.finishItem() | ||
} | ||
if (self.filter && !self.filter({path: item, stat: stat})) return self.finishItem() | ||
if (self.filter && !self.filter({path: item, stats: stats})) return self.finishItem() | ||
if (!stat.isDirectory()) { | ||
self.push({ path: item, stat: stat }) | ||
if (!stats.isDirectory()) { | ||
self.push({ path: item, stats: stats }) | ||
return self.finishItem() | ||
@@ -42,7 +43,7 @@ } | ||
if (err) { | ||
self.emit('error', err, {path: item, stat: stat}) | ||
self.emit('error', err, {path: item, stats: stats}) | ||
return self.finishItem() | ||
} | ||
self.push({path: item, stat: stat}) | ||
self.push({path: item, stats: stats}) | ||
items.forEach(function (part) { | ||
@@ -49,0 +50,0 @@ self.visit(path.join(item, part)) |
{ | ||
"name": "fs-extra", | ||
"version": "0.24.0", | ||
"version": "0.25.0", | ||
"description": "fs-extra contains methods that aren't included in the vanilla Node.js fs package. Such as mkdir -p, cp -r, and rm -rf.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/jprichardson/node-fs-extra", |
Node.js: fs-extra | ||
================= | ||
[![build status](https://secure.travis-ci.org/jprichardson/node-fs-extra.svg)](http://travis-ci.org/jprichardson/node-fs-extra) | ||
[![build status](https://api.travis-ci.org/jprichardson/node-fs-extra.svg)](http://travis-ci.org/jprichardson/node-fs-extra) | ||
[![windows Build status](https://img.shields.io/appveyor/ci/jprichardson/node-fs-extra/master.svg?label=windows%20build)](https://ci.appveyor.com/project/jprichardson/node-fs-extra/branch/master) | ||
@@ -87,2 +87,3 @@ [![downloads per month](http://img.shields.io/npm/dm/fs-extra.svg)](https://www.npmjs.org/package/fs-extra) | ||
- [removeSync](#removedir-callback) | ||
- [walk](#walk) | ||
- [writeJson](#writejsonfile-object-options-callback) | ||
@@ -103,4 +104,5 @@ - [writeJsonSync](#writejsonfile-object-options-callback) | ||
Options: | ||
clobber (boolean): overwrite existing file or directory | ||
preserveTimestamps (boolean): will set last modification and access times to the ones of the original source files, default is `false`. | ||
clobber (boolean): overwrite existing file or directory | ||
preserveTimestamps (boolean): will set last modification and access times to the ones of the original source files, default is `false`. | ||
filter: Function or RegExp to filter copied files. If function, return true to include, false to exclude. If RegExp, same as function, where `filter` is `filter.test`. | ||
@@ -398,4 +400,79 @@ Sync: `copySync()` | ||
### walk() | ||
**walk(dir)** | ||
Returns a [Readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable) that iterates | ||
through every file and directory starting with `dir` as the root. Every `read()` or `data` event | ||
returns an object with two properties: `path` and `stats`. `path` is the full path of the file and | ||
`stats` is an instance of [fs.Stats](https://nodejs.org/api/fs.html#fs_class_fs_stats). | ||
Streams 1 (push) example: | ||
```js | ||
var items = [] // files, directories, symlinks, etc | ||
fse.walk(TEST_DIR) | ||
.on('data', function (item) { | ||
items.push(item.path) | ||
}) | ||
.on('end', function () { | ||
console.dir(items) // => [ ... array of files] | ||
}) | ||
``` | ||
Streams 2 & 3 (pull) example: | ||
```js | ||
var items = [] // files, directories, symlinks, etc | ||
fse.walk(TEST_DIR) | ||
.on('readable', function () { | ||
var item | ||
while ((item = this.read())) { | ||
items.push(item.path) | ||
} | ||
}) | ||
.on('end', function () { | ||
console.dir(items) // => [ ... array of files] | ||
}) | ||
``` | ||
If you're not sure of the differences on Node.js streams 1, 2, 3 then I'd | ||
recommend this resource as a good starting point: https://strongloop.com/strongblog/whats-new-io-js-beta-streams3/. | ||
#### Filtering the file walker stream | ||
On many occasions you may want to filter files based upon size, extension, etc. | ||
You should use the module [`through2`](https://www.npmjs.com/package/through2) to easily | ||
accomplish this. | ||
Example (skipping directories): | ||
first: | ||
npm i --save through2 | ||
```js | ||
var fs = require('fs-extra') | ||
var through2 = require('through2') | ||
var excludeDirFilter = through2.obj(function (item, enc, next) { | ||
if (!item.stat.isDirectory()) this.push(item) | ||
next() | ||
}) | ||
var items = [] // files, directories, symlinks, etc | ||
fse.walk(TEST_DIR) | ||
.pipe(excludeDirFilter) | ||
.on('data', function (item) { | ||
items.push(item.path) | ||
}) | ||
.on('end', function () { | ||
console.dir(items) // => [ ... array of files without directories] | ||
}) | ||
``` | ||
### writeJson(file, object, [options], callback) | ||
@@ -469,4 +546,2 @@ | ||
`copy`, `remove`, or `mkdirs` should be built on top of it. | ||
- After the aforementioned functions are built on the directory walker, `fs-extra` should then explicitly | ||
support wildcards. | ||
@@ -473,0 +548,0 @@ Note: If you make any big changes, **you should definitely post an issue for discussion first.** |
63797
1159
592