findup
Advanced tools
Comparing version 0.1.1 to 0.1.2
#!/usr/bin/env node | ||
var findup = require('..'), | ||
Path = require('path'); | ||
Path = require('path'), | ||
nopt = require('nopt'), | ||
knownOpts = { | ||
name : String, | ||
dir : Path, | ||
help : Boolean, | ||
verbose : Boolean | ||
}, | ||
description = { | ||
name : "The name of the file to found", | ||
dir : "The directoy where we will start walking up", | ||
help : "show usage", | ||
verbose : "print log" | ||
if(process.argv.length < 3) { | ||
}, | ||
defaults = { | ||
dir : process.cwd(), | ||
help : false, | ||
verbose : false | ||
}, | ||
shortHands = { | ||
d : "--dir", | ||
n : "--name", | ||
h : "--help", | ||
v : "--verbose" | ||
}, | ||
options = nopt(knownOpts, shortHands, process.argv, 2), | ||
argvRemain = options.argv.remain; | ||
// defaults value | ||
Object.keys(defaults).forEach(function(key){ | ||
var value = defaults[key]; | ||
options[key] = options[key] || value; | ||
}); | ||
if(argvRemain && argvRemain.length >=1 ) options.name = argvRemain[0]; | ||
if(!options.name || options.help) { | ||
console.error('Usage: findup [FILE]'); | ||
console.error(''); | ||
console.error(nopt.usage(knownOpts, shortHands, description, defaults)); | ||
process.exit(-1); | ||
} | ||
var file = process.argv[2]; | ||
findup(process.cwd(), file, function(err, dir){ | ||
var file = options.name; | ||
findup(process.cwd(), file, options, function(err, dir){ | ||
if(err) return console.error(err.message ? err.message : err); | ||
console.log(Path.join(dir, file)) | ||
console.log(Path.join(dir, file)); | ||
}); |
85
index.js
@@ -1,27 +0,84 @@ | ||
var fs = require('fs'), | ||
Path = require('path'); | ||
var fs = require('fs'), | ||
Path = require('path'), | ||
util = require('util'), | ||
colors = require('colors'), | ||
EE = require('events').EventEmitter; | ||
module.exports = function findup(dir, iterator, cb){ | ||
module.exports = function(dir, iterator, options, callback){ | ||
return FindUp(dir, iterator, options, callback); | ||
}; | ||
function FindUp(dir, iterator, options, callback){ | ||
if (!(this instanceof FindUp)) { | ||
return new FindUp(dir, iterator, options, callback); | ||
} | ||
if(typeof options === 'function'){ | ||
callback = options; | ||
options = {}; | ||
} | ||
options = options || {}; | ||
EE.call(this); | ||
this.found = false; | ||
this.stopPlease = false; | ||
var self = this; | ||
if(typeof iterator === 'string'){ | ||
var file = iterator; | ||
iterator = function(dir, cb){ | ||
return Path.exists(Path.join(dir, file), cb); | ||
} | ||
return fs.exists(Path.join(dir, file), cb); | ||
}; | ||
} | ||
cb = cb || function(){}; | ||
if (dir === '/') return cb(new Error('not found')); | ||
if(dir.indexOf('../../') !== -1 ) return cb(new Error(dir + ' is not correct.')); | ||
if(callback) { | ||
this.on('found', function(dir){ | ||
if(options.verbose) console.log(('found '+ dir ).green); | ||
callback(null, dir); | ||
self.stop(); | ||
}); | ||
this.on('end', function(){ | ||
if(options.verbose) console.log('end'.grey); | ||
if(!self.found) callback(new Error('not found')); | ||
}); | ||
this.on('error', function(err){ | ||
if(options.verbose) console.log('error'.red, err); | ||
callback(err); | ||
}); | ||
} | ||
this._find(dir, iterator, options, callback); | ||
} | ||
util.inherits(FindUp, EE); | ||
FindUp.prototype._find = function(dir, iterator, options, callback){ | ||
var self = this; | ||
iterator(dir, function(exists){ | ||
if(exists) return cb(null, dir); | ||
findup(Path.join(dir, '..'), iterator, cb); | ||
if(options.verbose) console.log(('traverse '+ dir).grey); | ||
if(exists) { | ||
self.found = true; | ||
self.emit('found', dir); | ||
} | ||
if (self.stopPlease) return self.emit('end'); | ||
if (dir === '/') return self.emit('end'); | ||
if(dir.indexOf('../../') !== -1 ) return self.emit('error', new Error(dir + ' is not correct.')); | ||
self._find(Path.join(dir, '..'), iterator, options, callback); | ||
}); | ||
} | ||
}; | ||
FindUp.prototype.stop = function(){ | ||
this.stopPlease = true; | ||
}; | ||
module.exports.FindUp = FindUp; | ||
module.exports.sync = function(dir, iteratorSync){ | ||
if(typeof iteratorSync === 'string'){ | ||
var file = iteratorSync; | ||
iteratorSync = function(dir, cb){ | ||
return Path.existsSync(Path.join(dir, file), cb); | ||
} | ||
iteratorSync = function(dir){ | ||
return fs.existsSync(Path.join(dir, file)); | ||
}; | ||
} | ||
@@ -28,0 +85,0 @@ var initialDir = dir; |
@@ -5,3 +5,3 @@ { | ||
"description": "Walk up ancester's dir up to root", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"repository": { | ||
@@ -15,19 +15,22 @@ "type": "git", | ||
"scripts": { | ||
"test": "vows test/*.js --spec" | ||
"test": "mocha ./test/*.js" | ||
}, | ||
"dependencies": { | ||
"nopt": "http://nodeload.github.com/Filirom1/nopt/tarball/master#pull-request-in-progress", | ||
"colors": "~0.6.0-1" | ||
}, | ||
"devDependencies": { | ||
"vows": "~0.6.2" | ||
"mocha": "~1.3.0", | ||
"chai": "~1.1.1" | ||
}, | ||
"optionalDependencies": {}, | ||
"engines": { | ||
"node": "*" | ||
"node": ">0.8.x" | ||
}, | ||
"licenses": [ | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://www.opensource.org/licenses/MIT" | ||
"url": "http : //www.opensource.org/licenses/MIT" | ||
} | ||
] | ||
} |
@@ -25,6 +25,7 @@ Find-up | ||
findup(dir, fileName, callback) | ||
findup(dir, iterator, callback) with `iterator(dir, cb)` where cb only accept `true` or `false` | ||
var findup = require('findup'); | ||
// Usage: findup(dir, fileName, callback) | ||
// Or findup(dir, iterator, callback) with `iterator(dir, cb)` where cb only accept `true` or `false` | ||
@@ -38,4 +39,4 @@ findup(__dirname + '/f/e/d/c/b/a', 'config.json', function(err, dir){ | ||
findup(__dirname + '/f/e/d/c/b/a', function(cb){ | ||
require('path').exists('config.json', cb); | ||
findup(__dirname + '/f/e/d/c/b/a', function(dir, cb){ | ||
require('path').exists(dir + '/config.json', cb); | ||
}, function(err, dir){ | ||
@@ -47,9 +48,47 @@ // if(e) e === new Error('not found') | ||
#### EventEmitter | ||
findup(dir, fileName) | ||
var findup = require('findup'); | ||
var fup = findup(__dirname + '/f/e/d/c/b/a', 'config.json'); | ||
findup(dir, iterator) with `iterator(dir, cb)` where cb only accept `true` or `false` | ||
var findup = require('findup'); | ||
var fup = findup(__dirname + '/f/e/d/c/b/a', function(dir, cb){ | ||
require('path').exists(dir + '/config.json', cb); | ||
}); | ||
findup return an EventEmitter. 3 events are emitted: `found`, `error`, `end` | ||
`found` event is emitted each time a file is found. | ||
You can stop the traversing by calling `stop` manually. | ||
fup.on('found', function(dir){ | ||
// dir === '/f/e/d/c' | ||
fup.stop(); | ||
}); | ||
`error` event is emitted when error happens | ||
fup.on('error', function(e){ | ||
// if(e) e === new Error('not found') | ||
}); | ||
`end` event is emitted at the end of the traversing or after `stop()` is | ||
called. | ||
fup.on('end', function(){ | ||
// happy end | ||
}); | ||
#### Sync | ||
findup(dir, fileName) | ||
findup(dir, iteratorSync) with `iteratorSync` return `true` or `false` | ||
var findup = require('findup'); | ||
// Usage: findup(dir, fileName) | ||
// Or findup(dir, iteratorSync) with `iteratorSync` return `true` or `false` | ||
try{ | ||
@@ -69,4 +108,15 @@ var dir = findup.sync(__dirname + '/f/e/d/c/b/a', 'config.json'); // dir === '/f/e/d/c' | ||
Usage | ||
$ findup -h | ||
Usage: findup [FILE] | ||
--name, -n The name of the file to found | ||
--dir, -d The directoy where we will start walking up $PWD | ||
--help, -h show usage false | ||
--verbose, -v print log false | ||
### LICENSE MIT | ||
### Read the tests :) |
@@ -1,3 +0,2 @@ | ||
var vows = require('vows'), | ||
assert = require('assert'), | ||
var assert = require('chai').assert, | ||
Path = require('path'), | ||
@@ -7,110 +6,105 @@ fs = require('fs'), | ||
vows.describe('Test find-up') | ||
.addBatch({ | ||
'Given a great file structure using a function': { | ||
topic: Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c', 'b', 'a'), | ||
'When findup a file in ancestors dir': { | ||
topic: function(fixtureDir){ | ||
findup(fixtureDir, function(dir, cb){ | ||
return Path.exists(Path.join(dir, 'config.json'), cb); | ||
}, this.callback); | ||
}, | ||
describe('find-up', function(){ | ||
var fixtureDir = Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c', 'b', 'a'); | ||
it('accept a function', function(done){ | ||
findup(fixtureDir, function(dir, cb){ | ||
return fs.exists(Path.join(dir, 'config.json'), cb); | ||
}, function(err, file){ | ||
assert.ifError(err); | ||
assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c')); | ||
done(); | ||
}); | ||
}); | ||
'Then the dir containing the file is returned': function(err, file){ | ||
assert.ifError(err); | ||
assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c')); | ||
} | ||
}, | ||
it('accept a string', function(done){ | ||
findup(fixtureDir, 'config.json', function(err, file){ | ||
assert.ifError(err); | ||
assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c')); | ||
done(); | ||
}); | ||
}); | ||
'When findup a file in ancestors dir using a string': { | ||
topic: function(fixtureDir){ | ||
findup(fixtureDir, 'config.json', this.callback); | ||
}, | ||
it('is usable with the Object syntax', function(done) { | ||
new findup.FindUp(fixtureDir, 'config.json', function(err, file){ | ||
assert.ifError(err); | ||
assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c')); | ||
done(); | ||
}); | ||
}); | ||
'Then the dir containing the file is returned': function(err, file){ | ||
assert.ifError(err); | ||
assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c')); | ||
} | ||
}, | ||
it('find several files when using with the EventEmitter syntax', function(done){ | ||
var ee = new findup.FindUp(fixtureDir, 'config.json'); | ||
ee.once('found', function(file){ | ||
assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c')); | ||
'When findup a file in top dir': { | ||
topic: function(fixtureDir){ | ||
findup(fixtureDir, 'top.json', this.callback); | ||
}, | ||
ee.once('found', function(file){ | ||
assert.equal(file, Path.join(__dirname, 'fixture')); | ||
'Then the dir containing the file is returned': function(err, file){ | ||
assert.ifError(err); | ||
assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c', 'b', 'a')); | ||
} | ||
}, | ||
ee.once('end', function(){ | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
'When findup a non existing file': { | ||
topic: function(fixtureDir){ | ||
findup(fixtureDir, 'toto.json', this.callback); | ||
}, | ||
it('return files in top dir', function(done){ | ||
findup(fixtureDir, 'top.json', function(err, file){ | ||
assert.ifError(err); | ||
assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c', 'b', 'a')); | ||
done(); | ||
}); | ||
}); | ||
'Then an error is returned': function(err, file){ | ||
assert.isNotNull(err); | ||
} | ||
}, | ||
it('return files in root dir', function(done){ | ||
findup(fixtureDir, 'dev', function(err, file){ | ||
assert.ifError(err); | ||
assert.equal(file, '/'); | ||
done(); | ||
}); | ||
}); | ||
'When findup in a bullshit dir': { | ||
topic: function(fixtureDir){ | ||
findup('dsqkjfnqsdkjghq', 'toto.json', this.callback); | ||
}, | ||
it('return an error when looking for non existiong files', function(done){ | ||
findup(fixtureDir, 'toto.json', function(err, file){ | ||
assert.isNotNull(err); | ||
done(); | ||
}); | ||
}); | ||
'Then an error is returned': function(err, file){ | ||
assert.isNotNull(err); | ||
} | ||
}, | ||
it('return an error when looking in a non existing directory', function(done){ | ||
findup('dsqkjfnqsdkjghq', 'toto.json', function(err, file){ | ||
assert.isNotNull(err); | ||
done(); | ||
}); | ||
}); | ||
'When findup a file in ancestors dir in synchronous mode': { | ||
topic: function(fixtureDir){ | ||
return findup.sync(fixtureDir, 'config.json'); | ||
}, | ||
it('trigger an error event when looking in a non existing directory', function(done){ | ||
findup('dsqkjfnqsdkjghq', 'toto.json').on('error', function(err, files){ | ||
assert.isNotNull(err); | ||
done(); | ||
}); | ||
}); | ||
'Then the dir containing the file is returned': function(file){ | ||
assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c')); | ||
} | ||
}, | ||
describe('Sync API', function(){ | ||
it('accept a string', function(){ | ||
var file = findup.sync(fixtureDir, 'config.json'); | ||
assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c')); | ||
}); | ||
'When findup a file in top dir in synchronous mode': { | ||
topic: function(fixtureDir){ | ||
return findup.sync(fixtureDir, 'top.json'); | ||
}, | ||
it('return a file in top dir', function(){ | ||
var file = findup.sync(fixtureDir, 'top.json'); | ||
assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c', 'b', 'a')); | ||
}); | ||
'Then the dir containing the file is returned': function(file){ | ||
assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c', 'b', 'a')); | ||
} | ||
}, | ||
it('throw error when looking for a non existing file', function(){ | ||
assert.throw(function(){ | ||
findup.sync(fixtureDir, 'toto.json'); | ||
}); | ||
}); | ||
'When findup a non existing file in synchronous mode': { | ||
topic: function(fixtureDir){ | ||
try{ | ||
return findup.sync(fixtureDir, 'toto.json'); | ||
}catch(e){ | ||
return e; | ||
} | ||
}, | ||
'Then an error is returned': function(err){ | ||
assert.isNotNull(err); | ||
} | ||
}, | ||
'When findup in a bullshit dir in synchronous mode': { | ||
topic: function(fixtureDir){ | ||
try{ | ||
return findup.sync('uhjhbjkg,nfg', 'toto.json'); | ||
}catch(e){ | ||
return e; | ||
} | ||
}, | ||
'Then an error is returned': function(err){ | ||
assert.isNotNull(err); | ||
} | ||
} | ||
} | ||
}).exportTo(module); | ||
it('throw error when looking for in a non existing directory', function(){ | ||
assert.throw(function(){ | ||
findup.sync('uhjhbjkg,nfg', 'toto.json'); | ||
}); | ||
}); | ||
}); | ||
}); |
HTTP dependency
Supply chain riskContains a dependency which resolves to a remote HTTP URL which could be used to inject untrusted code and reduce overall package reliability.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
10507
10
218
119
2
2
1
1
1
+ Addedcolors@~0.6.0-1
+ Addednopt@http://nodeload.github.com/Filirom1/nopt/tarball/master#pull-request-in-progress
+ Addedcolors@0.6.2(transitive)