Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
update to 0.1.5, support mkdir set mode
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Sep 22, 2012
1 parent 7e85916 commit b31d3c7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 46 deletions.
13 changes: 4 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
57 changes: 36 additions & 21 deletions lib/ndir.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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();
}
Expand All @@ -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) {
Expand All @@ -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);
});
Expand All @@ -150,36 +151,42 @@ 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);
});
}

/**
* 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.
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]> (http://github.com/fengmk2)",
Expand Down
30 changes: 15 additions & 15 deletions test/ndir.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
});

Expand All @@ -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');
Expand Down

0 comments on commit b31d3c7

Please sign in to comment.