Skip to content

Commit

Permalink
Merge pull request #49 from aredridel/use-position-tracking-json-parser
Browse files Browse the repository at this point in the history
Use load-jsonic-sync to load json with comments and report position and filename in error messages
  • Loading branch information
grawk authored Jan 17, 2017
2 parents c5d6985 + 3063534 commit c439495
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*.pid
*.gz
*.orig
*.swp

dist
work
Expand All @@ -19,6 +20,7 @@ html-report
xunit.xml
node_modules
npm-debug.log
.nyc_output

.project
.idea
Expand Down
12 changes: 6 additions & 6 deletions lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
│ limitations under the License. │
\*───────────────────────────────────────────────────────────────────────────*/
import Path from 'path';
import shush from 'shush';
import loadJsonicSync from 'load-jsonic-sync';
import debuglog from 'debuglog';
import Thing from 'core-util-is';
import Config from './config';
Expand All @@ -34,13 +34,13 @@ export default class Factory {
.then(store => Common.merge(Provider.convenience(), store))
.then(Factory.conditional(store => {
let file = Path.join(this.basedir, defaults);
return Handlers.resolveImport(shush(file), this.basedir)
return Handlers.resolveImport(loadJsonicSync(file), this.basedir)
.then(data => Common.merge(data, store));
}))
.then(Factory.conditional(store => {
let file = Path.join(this.basedir, `${store.env.env}.json`);
return Handlers.resolveImport(shush(file), this.basedir)
.then(data => Common.merge(shush(file), store));
return Handlers.resolveImport(loadJsonicSync(file), this.basedir)
.then(data => Common.merge(loadJsonicSync(file), store));
}))
.then(store => Common.merge(Provider.env(), store))
.then(store => Common.merge(Provider.argv(), store));
Expand Down Expand Up @@ -73,7 +73,7 @@ export default class Factory {
_resolveFile(path) {
if (Thing.isString(path)) {
let file = Common.isAbsolute(path) ? path : Path.join(this.basedir, path);
return shush(file);
return loadJsonicSync(file);
}
return path;
}
Expand All @@ -83,7 +83,7 @@ export default class Factory {
try {
return fn(store);
} catch (err) {
if (err.code && err.code === 'MODULE_NOT_FOUND') {
if (err.cause() && err.cause().code === 'ENOENT') {
debug(`WARNING: ${err.message}`);
return store;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
│ See the License for the specific language governing permissions and │
│ limitations under the License. │
\*───────────────────────────────────────────────────────────────────────────*/
import shush from 'shush';
import loadJsonicSync from 'load-jsonic-sync';
import async from 'async';
import Shortstop from 'shortstop'
import { path as createPath } from 'shortstop-handlers';
Expand Down Expand Up @@ -79,7 +79,7 @@ export default class Handlers {
shorty.use('import', function (file, cb) {
try {
file = path(file);
return shorty.resolve(shush(file), cb);
return shorty.resolve(loadJsonicSync(file), cb);
} catch (err) {
cb(err);
}
Expand Down
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
"description": "Environment-aware configuration.",
"main": "dist/index.js",
"directories": {
"test": "test",
"dist": "dist"
"dist": "dist",
"test": "test"
},
"scripts": {
"compile": "babel --optional runtime --modules common --out-dir dist index.js lib/**.js",
"cover": "npm run compile && nyc tap test/**/*-test.js && nyc report -r html",
"prepublish": "npm run compile",
"test": "npm run compile && babel-node test/harness.js test/**/*-test.js",
"cover": "npm run compile && babel-node node_modules/.bin/istanbul cover test/harness.js test/**/*-test.js"
"test": "npm run compile && tap test/**/*-test.js"
},
"repository": {
"type": "git",
"url": "git://github.com/krakenjs/confit.git"
},
"keywords": [
"application",
"configuration",
"config"
"config",
"configuration"
],
"author": "Erik Toth <[email protected]>",
"licenses": [
Expand All @@ -35,17 +35,18 @@
"glob": "^4.3.5",
"istanbul": "^0.3.5",
"jshint": "^2.6.0",
"tape": "^3.5.0"
"nyc": "^3.0.1",
"tap": "^1.3.1"
},
"dependencies": {
"async": "^0.9.0",
"babel-runtime": "4.7.3",
"caller": "^1.0.0",
"core-util-is": "^1.0.1",
"debuglog": "^1.0.1",
"load-jsonic-sync": "^1.0.0",
"minimist": "^1.1.0",
"shortstop": "^1.0.1",
"shortstop-handlers": "^1.0.0",
"shush": "^1.0.0"
"shortstop-handlers": "^1.0.0"
}
}
5 changes: 3 additions & 2 deletions test/common-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var test = require('tape');
require('babel/register');
var test = require('tap').test;
var common = require('../lib/common');


Expand Down Expand Up @@ -76,4 +77,4 @@ test('merge with existing props', function (t) {
});

t.end();
});
});
12 changes: 9 additions & 3 deletions test/confit-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var path = require('path');
var test = require('tape');
var test = require('tap').test;
var confit = require('../');

var env = process.env.NODE_ENV;
Expand Down Expand Up @@ -201,7 +201,7 @@ test('confit', function (t) {
.create(function (err, config) {
t.ok(err);
t.notOk(config);
t.equal(err.code, 'MODULE_NOT_FOUND');
t.equal(err.cause().code, 'ENOENT');
t.end();
});
});
Expand Down Expand Up @@ -446,7 +446,13 @@ test('confit', function (t) {
var basedir = path.join(__dirname, 'fixtures', 'malformed');
confit(basedir).create(function (err, config) {
t.ok(err);
t.notOk(config)
t.matches(err.message, /loading jsonic file.*fixtures/);
t.matches(err.cause().message, /pair rule failed at: \d/);
t.matches(err.cause(), {
line: 1,
column: 3
});
t.notOk(config);
t.end();
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/harness.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var tape = require('tape');
var tape = require('tap').test;
var glob = require('glob');
var Path = require('path');

Expand Down
8 changes: 6 additions & 2 deletions test/provider-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var test = require('tape');
require('babel/register');
var test = require('tap').test;
var provider = require('../lib/provider');


test('env', function (t) {
var env = process.env;

Expand Down Expand Up @@ -49,6 +49,8 @@ test('argv', function (t) {
t.equal(val.h, null);
t.end();
});

t.end();
});


Expand Down Expand Up @@ -148,4 +150,6 @@ test('convenience', function (t) {
t.notOk(val.env.production);
t.end();
});

t.end();
});

0 comments on commit c439495

Please sign in to comment.