Comparing version 0.1.1 to 0.1.2
49
index.js
module.exports = RcLoader; | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var _ = require('lodash'); | ||
@@ -15,23 +14,33 @@ var RcFinder = require('rcfinder'); | ||
var config; | ||
finderConfig = _.isObject(finderConfig) ? finderConfig : {}; | ||
var config = {}; | ||
var configPending = false; | ||
var lookup = userConfig && userConfig.lookup !== void 0 ? !!userConfig.lookup : true; | ||
var finder = new RcFinder(name, finderConfig); | ||
if (typeof userConfig === 'string') { | ||
config = { | ||
defaultFile: userConfig, | ||
lookup: false | ||
}; | ||
lookup = false; | ||
config.defaultFile = userConfig; | ||
} else { | ||
config = _.defaults({}, userConfig || {}, { | ||
lookup: true | ||
}); | ||
_.assign(config, userConfig || {}); | ||
} | ||
if (config.defaultFile) { | ||
_.defaults(config, JSON.parse(fs.readFileSync(config.defaultFile))); | ||
} | ||
if (finder.canLoadSync) { | ||
_.assign(config, finder.get(config.defaultFile)); | ||
} else { | ||
// push callbacks here that need to wait for config to load | ||
configPending = []; | ||
// force the async loader | ||
finder.get(config.defaultFile, function (err, defaults) { | ||
if (err) throw err; | ||
_.assign(config, defaults); | ||
// setup the finder if we need it | ||
var finder; | ||
if (config.lookup) { | ||
finder = new RcFinder(name, finderConfig); | ||
// clear the configPending queue | ||
var cbs = configPending; | ||
configPending = null; | ||
cbs.forEach(function (cb) { cb(); }); | ||
}); | ||
} | ||
} | ||
@@ -49,2 +58,10 @@ | ||
if (err && !sync) return cb(err); | ||
// the config has not loaded yet, delay our response | ||
// until it is | ||
if (!sync && configPending) { | ||
return configPending.push(function () { | ||
respond(err, configFile); | ||
}); | ||
} | ||
configFile = _.assign(configFile || {}, config); | ||
@@ -56,3 +73,3 @@ | ||
if (!finder) { | ||
if (!lookup) { | ||
if (sync) return respond(); | ||
@@ -59,0 +76,0 @@ return process.nextTick(respond); |
{ | ||
"name": "rcloader", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "For build system plugins that need to fetch relative config files (like .jshintrc).", | ||
@@ -28,3 +28,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"rcfinder": "~0.1.3", | ||
"rcfinder": "~0.1.6", | ||
"lodash": "~2.4.1" | ||
@@ -31,0 +31,0 @@ }, |
var RcLoader = require('../'); | ||
var fs = require('fs'); | ||
var _ = require('lodash'); | ||
require('should'); | ||
var should = require('should'); | ||
@@ -10,3 +10,3 @@ var fixtures = { | ||
text: __dirname + '/fixtures/foo/foo/.baz', | ||
jshintrc: JSON.parse(fs.readFileSync(__dirname + '/.jshintrc')), | ||
rc: __dirname + '/.jshintrc', | ||
barJson: { | ||
@@ -16,2 +16,3 @@ baz: 'bog' | ||
}; | ||
fixtures.jshintrc = JSON.parse(fs.readFileSync(fixtures.rc)); | ||
@@ -71,2 +72,50 @@ describe('RcLoader', function () { | ||
}); | ||
it('waits for the config to load before responding', function (done) { | ||
// write the time that different paths are looked-up and complete | ||
var start = {}; | ||
var stop = {}; | ||
var now = function () { | ||
var t = process.hrtime(); | ||
return t[0] * 1000 + t[1] / 1000; | ||
}; | ||
// loader with a defaults file which also looks up relative files | ||
var loader = new RcLoader('.jshintrc', { | ||
lookup: true, | ||
defaultFile: fixtures.json | ||
}, { | ||
loader: function (path, _cb) { | ||
start[path] = now(); | ||
var done = function (err, contents) { | ||
stop[path] = now(); | ||
_cb(err, JSON.parse('' + contents)); | ||
}; | ||
// wrap done in a call to setTimeout | ||
if (path === fixtures.json) done = _.partial(setTimeout, done, 30); | ||
fs.readFile(path, done); | ||
} | ||
}); | ||
loader.for(fixtures.json, function (err, config) { | ||
should.not.exist(err); | ||
// bar.json file should have loaded successfully | ||
should.exist(start[fixtures.json]); | ||
should.exist(stop[fixtures.json]); | ||
// .jshintrc file should have loaded successfully | ||
should.exist(start[fixtures.rc]); | ||
should.exist(stop[fixtures.rc]); | ||
// .jshintrc file should have finished loading before the config | ||
stop[fixtures.rc].should.be.lessThan(stop[fixtures.json]); | ||
// but config should still include the defaultFile | ||
config.should.include(fixtures.barJson); | ||
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
9044
166
1
Updatedrcfinder@~0.1.6