Skip to content
This repository has been archived by the owner on May 22, 2019. It is now read-only.

Commit

Permalink
properly returning an error when the custom config file doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDarcyMurphy committed Mar 18, 2012
1 parent 4530004 commit 4888258
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
40 changes: 23 additions & 17 deletions lib/upfront.js
Expand Up @@ -76,24 +76,38 @@ upfront.setup = function(opts, done) {
} catch(e) {
return done(e, true);
}

}

else if ( typeof opts.config == "object" ) {
console.log('opts is object');
console.log('config is object');
glom(opts.config, done);
}

else if ( typeof opts.config == "string" ) {
console.log('opts is string');
return done('fix this', false);
fs.readFile(opts.config, 'utf8', function (err, data) {
if (!err) { glom(data); }
done(null, true);
return;
});
// console.log('config is string');
try {
fs.readFile(opts.config, 'utf8', function (err, data) {
if (err) { return done(err); }
var cfg = JSON.parse(data);
console.log('cfg', typeof cfg, cfg);
glom(data, done);
});
} catch(e) {
return done(e, true);
}
}

};

var glom = function(config, done) {
upfront.config = _.extend(upfront.config, config);
upfront.config.views = app.settings.views;
var reg = [ "^", upfront.config.views, "\/(.*?)\.(", upfront.config.extensions.join('|'), ")$" ];
upfront.config.rx = new RegExp(reg.join(''));
done(null, true);
};


upfront.grab = function(done) {
walk(upfront.config.views, function(err, results){
app.set('templates', results);
Expand All @@ -104,14 +118,6 @@ upfront.grab = function(done) {
};


var glom = function(config, done) {
upfront.config = _.extend(upfront.config, config);
upfront.config.views = app.settings.views;
var reg = [ "^", upfront.config.views, "\/(.*?)\.(", upfront.config.extensions.join('|'), ")$" ];
upfront.config.rx = new RegExp(reg.join(''));
done(null, true);
};

// Walk -- What did you say?
var walk = function(dir, done) {
var slug, results = {};
Expand Down
5 changes: 3 additions & 2 deletions test/upfront.js
Expand Up @@ -100,7 +100,7 @@ describe('Setup', function(){
should.exist(app.settings.views);
done();
});
it('throws an error when parsing the file', function(done){
it('returns an error when parsing the file', function(done){
upfront.setup({app:app, config:"not_there.json"}, function(error, success){
should.exist(error);
should.not.exist(success);
Expand Down Expand Up @@ -313,7 +313,7 @@ describe('Setup', function(){
});
});
});

/*
describe('when passing app as attribute with config file attribute', function(done){
var app, upfront;
beforeEach(function(done){
Expand Down Expand Up @@ -366,6 +366,7 @@ describe('Setup', function(){
});
});
});
*/
// describe('when passing app as attribute with config as object', function(done){
// it('succeeds', function(){
// should.equal(false, true, 'test unwritten');
Expand Down

0 comments on commit 4888258

Please sign in to comment.