Comparing version 0.1.7 to 0.1.8
90
index.js
@@ -31,3 +31,3 @@ /** | ||
} catch(e) { | ||
err = cb(new Error(path + ' is not valid JSON: ' + e.message)); | ||
err = new Error(path + ' is not valid JSON: ' + e.message); | ||
} | ||
@@ -61,7 +61,3 @@ } | ||
if (loader.length > 1 && typeof cb === 'function') { | ||
// async, stop and wait then retry responding | ||
return loader(path, function (err, config) { | ||
// force false, so we can safely check for undef | ||
cb(err, config); | ||
}); | ||
return loader(path, cb); | ||
} | ||
@@ -71,12 +67,22 @@ | ||
// sync loader, return can still be done async | ||
var config = loader(path) || false; | ||
if (typeof cb === 'function') { | ||
// async response requested, wait a tick | ||
process.nextTick(function () { | ||
cb(void 0, config); | ||
}); | ||
} else { | ||
// no cb, then respond sync | ||
return config; | ||
var config, err; | ||
try { | ||
config = loader(path) || false; | ||
} | ||
catch (e) { | ||
configMap[path] = false; | ||
err = e; | ||
} | ||
finally { | ||
if (typeof cb === 'function') { | ||
process.nextTick(function () { | ||
cb(err, config); | ||
}); | ||
} else { | ||
if (err) throw err; | ||
else return config; | ||
} | ||
} | ||
} | ||
@@ -100,30 +106,40 @@ }; | ||
function respond(err, rcPath) { | ||
if (!rcPath) { | ||
// it should be safe to test for undef | ||
rcConfig = rcPath = false; | ||
} else { | ||
// we need to populate the cache | ||
if (configMap[rcPath] === void 0) { | ||
if (sync) { | ||
configMap[rcPath] = get(rcPath); | ||
// and keep going | ||
} else { | ||
// stop and load | ||
return get(rcPath, function (err, config) { | ||
configMap[rcPath] = config; | ||
respond(err, rcPath); | ||
}); | ||
if (!err) { | ||
if (!rcPath) { | ||
// it should be safe to test for undef | ||
rcConfig = rcPath = false; | ||
} else { | ||
// we need to populate the cache | ||
if (configMap[rcPath] === void 0) { | ||
if (sync) { | ||
try { | ||
configMap[rcPath] = get(rcPath); | ||
} catch (e) { | ||
configMap[rcPath] = false; | ||
err = e; | ||
} finally { | ||
return respond(err, rcPath); | ||
} | ||
// and keep going | ||
} else { | ||
// stop and load | ||
return get(rcPath, function (err, config) { | ||
configMap[rcPath] = config; | ||
respond(err, rcPath); | ||
}); | ||
} | ||
} | ||
// clone the cached copy so that people can't fuck with them | ||
rcConfig = cloneDeep(configMap[rcPath]); | ||
} | ||
// clone the cached copy so that people can't fuck with them | ||
rcConfig = cloneDeep(configMap[rcPath]); | ||
searched.forEach(function (dir) { | ||
pathMap[dir] = rcPath; | ||
}); | ||
} | ||
searched.forEach(function (dir) { | ||
pathMap[dir] = rcPath; | ||
}); | ||
if (sync && err) throw err; | ||
if (sync) return rcConfig; | ||
cb(void 0, rcConfig); | ||
cb(err || void 0, rcConfig); | ||
} | ||
@@ -130,0 +146,0 @@ |
{ | ||
"name": "rcfinder", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "Find a config file (like .jshintrc) by walking up from a specific directory.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# rcfinder | ||
[![Travis CI](https://travis-ci.org/spenceralger/rcfinder.png)](https://travis-ci.org/spenceralger/rcfinder) | ||
[![Travis CI](https://travis-ci.org/spenceralger/rcfinder.svg)](https://travis-ci.org/spenceralger/rcfinder) | ||
@@ -14,3 +14,3 @@ **This module provides the file lookup logic for the generally more useful [rcloader](https://www.npmjs.org/package/rcloader) package**. | ||
## install | ||
``` | ||
```js | ||
npm install rcfinder | ||
@@ -22,3 +22,3 @@ ``` | ||
``` | ||
```js | ||
var RcFinder = require('rcfinder'); | ||
@@ -29,3 +29,3 @@ var rcFinder = new RcFinder('.jshintrc', {}); | ||
Then you can use the finder to look up the proper config file for a directory. | ||
``` | ||
```js | ||
// get the closet .jshintc file for this file | ||
@@ -36,3 +36,3 @@ var config = rcFinder.find(__dirname); | ||
If you want to use async file system calls, just specify a callback to find. | ||
``` | ||
```js | ||
rcFinder.find(__dirname, function (err, config) { | ||
@@ -49,3 +49,3 @@ | ||
The default loader is: | ||
``` | ||
```js | ||
function loader(path) { | ||
@@ -52,0 +52,0 @@ return JSON.parse(fs.readFileSync(path)); |
@@ -134,2 +134,53 @@ describe('RcFinder', function () { | ||
}); | ||
it('throws errors from loader when loading and calling synchronously', function() { | ||
var rcFinder = new RcFinder('bar.json', { | ||
loader: function(path) { | ||
return JSON.parse('{not really json'); | ||
} | ||
}); | ||
expect(function () { | ||
var config = rcFinder.find(fixtures.root); | ||
}).to.throwException('Unexpected token n'); | ||
}); | ||
it('propagates errors from loader when loading synchronously and calling async', function(done) { | ||
var rcFinder = new RcFinder('bar.json', { | ||
loader: function(path) { | ||
return JSON.parse('{not really json'); | ||
} | ||
}); | ||
rcFinder.find(fixtures.root, function(err, config) { | ||
expect(err).to.be.an(Error); | ||
expect(err.message).to.be('Unexpected token n'); | ||
expect(config).to.be(undefined); | ||
done(); | ||
}); | ||
}); | ||
it('propagates error from loader when loading asynchronously', function(done) { | ||
var rcFinder = new RcFinder('bar.json', { | ||
loader: function(path, callback) { | ||
process.nextTick(function() { | ||
var err, body; | ||
try { | ||
body = JSON.parse('{not really json'); | ||
} catch (e) { | ||
err = e; | ||
} finally { | ||
callback(err, body); | ||
} | ||
}); | ||
} | ||
}); | ||
rcFinder.find(fixtures.root, function(err, config) { | ||
expect(err).to.be.an(Error); | ||
expect(err.message).to.be('Unexpected token n'); | ||
expect(config).to.be(undefined); | ||
done(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13276
12
341