diff --git a/Makefile b/Makefile index fd906f0..2b51c45 100644 --- a/Makefile +++ b/Makefile @@ -7,15 +7,10 @@ test: @NODE_ENV=test ./node_modules/.bin/mocha \ --reporter $(REPORTER) --timeout $(TESTTIMEOUT) $(TESTS) -test-cov: lib-cov +test-cov: + @rm -rf lib-cov + @jscoverage lib lib-cov + @NDIR_COV=1 $(MAKE) test REPORTER=dot @NDIR_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html -lib-cov: - @rm -rf ./$@ - @jscoverage lib $@ - -clean: - rm -rf lib-cov - rm -f coverage.html - .PHONY: test test-cov diff --git a/lib/ndir.js b/lib/ndir.js index 0ef813c..81f4d5f 100644 --- a/lib/ndir.js +++ b/lib/ndir.js @@ -12,7 +12,7 @@ var fs = require('fs'); var path = require('path'); var util = require('util'); var EventEmitter = require('events').EventEmitter; - +fs.exists = fs.exists || path.exists; /** * dir Walker Class. @@ -94,6 +94,7 @@ Walk.prototype._dir = function (dir) { files.forEach(function (file) { var p = path.join(dir, file); fs.lstat(p, function (err, stats) { + counter++; if (err) { self.emit('error', err, p); } else { @@ -102,7 +103,7 @@ Walk.prototype._dir = function (dir) { self.dirs.push(p); } } - if (++counter === files.length) { + if (counter === files.length) { self.emit('dir', dir, infos); self.next(); } @@ -114,9 +115,9 @@ Walk.prototype._dir = function (dir) { /** * Copy file, auto create tofile dir if dir not exists. * - * @param {String} fromfile Source file path. - * @param {String} tofile Target file path. - * @param {Function(err)} callback + * @param {String} fromfile, Source file path. + * @param {String} tofile, Target file path. + * @param {Function(err)} callback * @public */ exports.copyfile = function copyfile(fromfile, tofile, callback) { @@ -133,15 +134,15 @@ exports.copyfile = function copyfile(fromfile, tofile, callback) { var ws = fs.createWriteStream(tofile); var rs = fs.createReadStream(fromfile); var onerr = function (err) { - var cb = callback; + callback && callback(err); callback = null; - cb(err); }; - ws.on('error', onerr); // if file not open, these is only error event will be emit. - rs.on('error', onerr); + ws.once('error', onerr); // if file not open, these is only error event will be emit. + rs.once('error', onerr); ws.on('close', function () { // after file open, error event could be fire close event before. callback && callback(); + callback = null; }); rs.pipe(ws); }); @@ -150,12 +151,12 @@ exports.copyfile = function copyfile(fromfile, tofile, callback) { /** * @private */ -function _mkdir(dir, callback) { - path.exists(dir, function (exists) { +function _mkdir(dir, mode, callback) { + fs.exists(dir, function (exists) { if (exists) { return callback(); } - fs.mkdir(dir, '0777', callback); + fs.mkdir(dir, mode, callback); }); } @@ -163,23 +164,29 @@ function _mkdir(dir, callback) { * mkdir if dir not exists, equal mkdir -p /path/foo/bar * * @param {String} dir + * @param {Number} [mode] file mode, default is 0777. * @param {Function(err)} callback * @public */ -exports.mkdir = function mkdir(dir, callback) { +exports.mkdir = function mkdir(dir, mode, callback) { + if (typeof mode === 'function') { + callback = mode; + mode = 0777 & (~process.umask()); + } var parent = path.dirname(dir); - path.exists(parent, function (exists) { + fs.exists(parent, function (exists) { if (exists) { - return _mkdir(dir, callback); + return _mkdir(dir, mode, callback); } - exports.mkdir(parent, function (err) { + exports.mkdir(parent, mode, function (err) { if (err) { return callback(err); } - _mkdir(dir, callback); + _mkdir(dir, mode, callback); }); }); }; +exports.mkdirp = exports.mkdir; /** * Read stream data line by line. @@ -256,12 +263,20 @@ LineReader.prototype.ondata = function (data) { * Line data reader * * @example + * ``` * var ndir = require('ndir'); * ndir.createLineReader('/tmp/access.log') - * .on('line', function(line) { console.log(line.toString()); }) - * .on('end', function() {}) - * .on('error', function(err) { console.error(err); }); - * + * .on('line', function (line) { + * console.log(line.toString()); + * }) + * .on('end', function () { + * console.log('end'); + * }) + * .on('error', function (err) { + * console.error(err); + * }); + * ``` + * * @param {String|ReadStream} file, file path or a `ReadStream` object. */ exports.createLineReader = function (file) { diff --git a/package.json b/package.json index 743bd7e..ac4639b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ndir", - "version": "0.1.4", + "version": "0.1.5", "description": "The lost dir util tools for Nodejs. Handle dir and file in Event", "keywords": ["dir", "mkdir", "walk", "file", "rm", "filesystem", "event", "readfile", "linereader"], "author": "fengmk2 (http://github.com/fengmk2)", diff --git a/test/ndir.test.js b/test/ndir.test.js index 6725897..87ac264 100644 --- a/test/ndir.test.js +++ b/test/ndir.test.js @@ -7,20 +7,20 @@ var should = require('../node_modules/should'); var path = require('path'); var fs = require('fs'); var exec = require('child_process').exec; -var existsSync = fs.existsSync || path.existsSync; +fs.existsSync = fs.existsSync || path.existsSync; var root = path.resolve('.'); describe('ndir', function () { - describe('#walk()', function () { + describe('walk()', function () { var emptydir = path.join(root, 'test/emptydir'); before(function () { - if (!existsSync(emptydir)) { + if (!fs.existsSync(emptydir)) { fs.mkdirSync(emptydir, '0777'); } }); after(function () { - if (existsSync(emptydir)) { + if (fs.existsSync(emptydir)) { fs.rmdirSync(emptydir); } }); @@ -94,7 +94,7 @@ describe('ndir', function () { }); }); - if (existsSync('/.fseventsd')) { + if (fs.existsSync('/.fseventsd')) { it('should error when walk noPermission dir', function (done) { dir.walk('/.fseventsd', check, done, function (err) { err.should.be.an.instanceof(Error); @@ -105,13 +105,13 @@ describe('ndir', function () { }); - describe('#copyfile()', function () { + describe('copyfile()', function () { var from = 'test/dir.test.foo.txt'; var to = 'test/dir.test.bar.txt'; var toParentNotExists = '/tmp/' + new Date().getTime() + '/dir.test.bar.txt'; before(function () { - existsSync(to) && fs.unlinkSync(to); + fs.existsSync(to) && fs.unlinkSync(to); }); it('should worked', function (done) { @@ -141,12 +141,12 @@ describe('ndir', function () { }); - describe('#mkdir()', function () { + describe('mkdir()', function () { var existsDir = '/tmp/dir.test.exists.dir'; - var notExistsDir = '/tmp/dir.test/not.exists.dir'; + var notExistsDir = '/tmp/dir.test/not.exists.dir/haha/1/2/3/4/2/3/1/2/3'; before(function (done) { - !existsSync(existsDir) && fs.mkdirSync(existsDir, '0777'); + !fs.existsSync(existsDir) && fs.mkdirSync(existsDir, '0777'); exec('rm -rf /tmp/dir.test', done); }); @@ -155,24 +155,24 @@ describe('ndir', function () { }); it('should make exists dir success', function (done) { - existsSync(existsDir).should.be.true; + fs.existsSync(existsDir).should.be.true; dir.mkdir(existsDir, function (err) { - existsSync(existsDir).should.be.true; + fs.existsSync(existsDir).should.be.true; done(err); }); }); it('should make not exists dir success', function (done) { - existsSync(notExistsDir).should.be.false; + fs.existsSync(notExistsDir).should.be.false; dir.mkdir(notExistsDir, function (err) { - existsSync(notExistsDir).should.be.true; + fs.existsSync(notExistsDir).should.be.true; done(err); }); }); }); - describe('#createLineReader()', function () { + describe('createLineReader()', function () { it('should read line by line', function (done) { var logfile = __dirname + '/access.log'; var lines = fs.readFileSync(logfile, 'utf8').split('\n');