confer
Advanced tools
Comparing version 0.0.1 to 0.1.0
65
index.js
var fs = require('fs'), | ||
path = require('path'), | ||
compress = require('json-compressor'), | ||
parents = require('parents'), | ||
exists = fs.existsSync || path.existsSync, | ||
readFile = fs.readFileSync, | ||
sep = path.sep || ('win32' === process.platform ? '\\' : '/'), | ||
confer; | ||
path = require('path'), | ||
compress = require('json-compressor'), | ||
parents = require('parents'), | ||
exists = fs.existsSync, | ||
readFile = fs.readFileSync, | ||
confer; | ||
module.exports = confer = function confer(file, dir) { | ||
if (file.indexOf(sep) === -1) { | ||
file = locateFile(file, dir); | ||
} | ||
else { | ||
file = path.resolve(file); | ||
if (file.indexOf(path.sep) === -1) { | ||
file = locateFile(file, dir); | ||
} | ||
else { | ||
file = path.resolve(file); | ||
if (!exists(file)) { | ||
file = false; | ||
} | ||
} | ||
if (!exists(file)) { | ||
file = false; | ||
} | ||
} | ||
return file && JSON.parse(compress(readFile(file, 'utf8'))); | ||
return file && JSON.parse(compress(readFile(file, 'utf8'))); | ||
}; | ||
var locateFile = confer.locateFile = function (file, dir) { | ||
var dirs = parents(path.resolve(dir || process.cwd())), | ||
home = path.join(process.env.HOME, file), | ||
found; | ||
var dirs = parents(path.resolve(dir || process.cwd())), | ||
home = path.join(process.env.HOME, file), | ||
found; | ||
while (!found && dirs.length) { | ||
found = path.join(dirs.shift(), file); | ||
while (!found && dirs.length) { | ||
found = path.join(dirs.shift(), file); | ||
if (!exists(found)) { | ||
found = null; | ||
} | ||
} | ||
if (!exists(found)) { | ||
found = null; | ||
} | ||
} | ||
if (found) { | ||
return found; | ||
} | ||
if (found) { | ||
return found; | ||
} | ||
if (exists(home)) { | ||
return home; | ||
} | ||
if (exists(home)) { | ||
return home; | ||
} | ||
return false; | ||
return false; | ||
}; |
{ | ||
"name": "confer", | ||
"description": "Locate and parse JSON files", | ||
"version": "0.0.1", | ||
"name": "confer", | ||
"description": "Locate and parse JSON files", | ||
"version": "0.1.0", | ||
"author": "Brett Stimmerman <brettstimmerman@gmail.com>", | ||
"homepage": "https://github.com/brettstimmerman/confer", | ||
"keywords": ["find", "locate", "parse", "JSON", "file"], | ||
"author": "Brett Stimmerman <brettstimmerman@gmail.com>", | ||
"homepage": "https://github.com/brettstimmerman/confer", | ||
"keywords": ["find", "locate", "parse", "JSON", "file"], | ||
"license": "MIT", | ||
"license": "MIT", | ||
"respository": { | ||
"type": "git", | ||
"url": "https://github.com/brettstimmerman/confer.git" | ||
}, | ||
"respository": { | ||
"type": "git", | ||
"url": "git://github.com/brettstimmerman/confer.git" | ||
}, | ||
"engines": { | ||
"node": ">= 0.6" | ||
}, | ||
"engines": { | ||
"node": ">= 0.8" | ||
}, | ||
"dependencies": { | ||
"json-compressor": "~0.0.1", | ||
"parents": "~0.0.1" | ||
}, | ||
"dependencies": { | ||
"json-compressor": "~0.0.1", | ||
"parents": "~0.0.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*" | ||
}, | ||
"main": "index", | ||
"main": "index", | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
} | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha -R spec" | ||
} | ||
} |
@@ -10,5 +10,7 @@ # confer | ||
[![Build Status](https://secure.travis-ci.org/brettstimmerman/confer.png?branch=master)](http://travis-ci.org/brettstimmerman/confer) | ||
## Installation | ||
``` bash | ||
``` shell | ||
npm install confer | ||
@@ -30,4 +32,4 @@ ``` | ||
* `file` {string} filename or path | ||
* `dir` {string} (optional) directory to begin from; default is `process.cwd()` | ||
* `file` {String} filename or path | ||
* `dir` {String} (optional) directory to begin from; default is `process.cwd()` | ||
@@ -42,9 +44,9 @@ If `file` is a path then confer will parse the given file directly, bypassing | ||
* `file` {string} filename | ||
* `dir` {string} (optional) directory to begin from; default is `process.cwd()` | ||
* `file` {String} filename | ||
* `dir` {String} (optional) directory to begin from; default is `process.cwd()` | ||
## Tests | ||
``` bash | ||
``` shell | ||
npm test | ||
``` |
var assert = require('assert'), | ||
fs = require('fs'), | ||
path = require('path'), | ||
confer = require('../'); | ||
fs = require('fs'), | ||
path = require('path'), | ||
confer = require('../'); | ||
describe('confer()', function () { | ||
it('should locate and parse a file given a full path', function (done) { | ||
var json = confer('./test/fixtures/config.json'); | ||
it('should locate and parse a file given a full path', function (done) { | ||
var json = confer('./test/fixtures/config.json'); | ||
assert.equal(json.foo, false); | ||
assert.equal(json.bar, true); | ||
done(); | ||
}); | ||
assert.equal(json.foo, false); | ||
assert.equal(json.bar, true); | ||
done(); | ||
}); | ||
it('should NOT locate a file given a non-existent full path', function (done) { | ||
var json = confer('./test/fixtures/baz/config.json'); | ||
it('should NOT locate a file given a non-existent full path', function (done) { | ||
var json = confer('./test/fixtures/baz/config.json'); | ||
assert.strictEqual(json, false); | ||
done(); | ||
}); | ||
assert.strictEqual(json, false); | ||
done(); | ||
}); | ||
it('should locate and parse a file given no directory', function (done) { | ||
var cwd = process.cwd(), | ||
file = path.join(cwd, 'config.json'), | ||
json; | ||
it('should locate and parse a file given no directory', function (done) { | ||
var cwd = process.cwd(), | ||
file = path.join(cwd, 'config.json'), | ||
json; | ||
fs.writeFile(file, '{"cwd":"' + cwd + '"}', 'utf8', function (err) { | ||
json = confer('config.json'); | ||
assert.equal(json.cwd, cwd); | ||
fs.unlink(file, done); | ||
}); | ||
}); | ||
fs.writeFile(file, '{"cwd":"' + cwd + '"}', 'utf8', function (err) { | ||
json = confer('config.json'); | ||
assert.equal(json.cwd, cwd); | ||
fs.unlink(file, done); | ||
}); | ||
}); | ||
it('should locate and parse a file in a directory', function (done) { | ||
var json = confer('config.json', './test/fixtures'); | ||
it('should locate and parse a file in a directory', function (done) { | ||
var json = confer('config.json', './test/fixtures'); | ||
assert.equal(json.foo, false); | ||
assert.equal(json.bar, true); | ||
done(); | ||
}); | ||
assert.equal(json.foo, false); | ||
assert.equal(json.bar, true); | ||
done(); | ||
}); | ||
it('should locate and parse a file in a subdirectory', function (done) { | ||
var json = confer('config.json', './test/fixtures/foo'); | ||
it('should locate and parse a file in a subdirectory', function (done) { | ||
var json = confer('config.json', './test/fixtures/foo'); | ||
assert.equal(json.foo, true); | ||
assert.equal(json.bar, false); | ||
done(); | ||
}); | ||
assert.equal(json.foo, true); | ||
assert.equal(json.bar, false); | ||
done(); | ||
}); | ||
it('should locate and parse a file in a parent directory', function (done) { | ||
var json = confer('config.json', './test/fixtures/bar'); | ||
it('should locate and parse a file in a parent directory', function (done) { | ||
var json = confer('config.json', './test/fixtures/bar'); | ||
assert.equal(json.foo, false); | ||
assert.equal(json.bar, true); | ||
done(); | ||
}); | ||
assert.equal(json.foo, false); | ||
assert.equal(json.bar, true); | ||
done(); | ||
}); | ||
it('should locate and parse a file in a non-existent sub-directory', function (done) { | ||
var json = confer('config.json', './test/fixtures/baz'); | ||
it('should locate and parse a file in a non-existent sub-directory', function (done) { | ||
var json = confer('config.json', './test/fixtures/baz'); | ||
assert.equal(json.foo, false); | ||
assert.equal(json.bar, true); | ||
done(); | ||
}); | ||
assert.equal(json.foo, false); | ||
assert.equal(json.bar, true); | ||
done(); | ||
}); | ||
it('should locate and parse a file in $HOME when not found in directory tree', function (done) { | ||
var home = process.env.HOME, | ||
file = path.join(home, 'config.json'), | ||
json; | ||
it('should locate and parse a file in $HOME when not found in directory tree', function (done) { | ||
var home = process.env.HOME, | ||
file = path.join(home, 'config.json'), | ||
json; | ||
fs.writeFile(file, '{"home":"' + home + '"}', 'utf8', function (err) { | ||
json = confer('config.json', '/tmp/nowayjose'); | ||
assert.equal(json.home, home); | ||
fs.unlink(file, done); | ||
}); | ||
}); | ||
fs.writeFile(file, '{"home":"' + home + '"}', 'utf8', function (err) { | ||
json = confer('config.json', '/tmp/nowayjose'); | ||
assert.equal(json.home, home); | ||
fs.unlink(file, done); | ||
}); | ||
}); | ||
it('should return false when no file is is found', function (done) { | ||
var json = confer('config.json', '/tmp/nowayjose'); | ||
it('should return false when no file is is found', function (done) { | ||
var json = confer('config.json', '/tmp/nowayjose'); | ||
assert.strictEqual(false, json); | ||
done(); | ||
}); | ||
assert.strictEqual(false, json); | ||
done(); | ||
}); | ||
it('should ignore search directory when given with a path', function (done) { | ||
var json = confer( | ||
'./test/fixtures/config.json', | ||
'./test/fixtures/foo/config.json' | ||
); | ||
it('should ignore search directory when given with a path', function (done) { | ||
var json = confer( | ||
'./test/fixtures/config.json', | ||
'./test/fixtures/foo/config.json' | ||
); | ||
assert.equal(json.foo, false); | ||
assert.equal(json.bar, true); | ||
done(); | ||
}); | ||
assert.equal(json.foo, false); | ||
assert.equal(json.bar, true); | ||
done(); | ||
}); | ||
it('should throw when invalid JSON is found', function (done) { | ||
assert.throws(function () { | ||
var json = confer.parseJSON('{[}'); | ||
}, | ||
function (err) { | ||
process.nextTick(done); | ||
return true; | ||
}); | ||
}); | ||
it('should throw when invalid JSON is found', function (done) { | ||
assert.throws(function () { | ||
var json = confer.parseJSON('{[}'); | ||
}, | ||
function (err) { | ||
process.nextTick(done); | ||
return true; | ||
}); | ||
}); | ||
}); | ||
describe('confer.locateFile()', function () { | ||
it('should locate a file given a filename and dir', function (done) { | ||
var file = confer.locateFile('config.json', 'test/fixtures'); | ||
it('should locate a file given a filename and dir', function (done) { | ||
var file = confer.locateFile('config.json', 'test/fixtures'); | ||
assert.equal(file, path.resolve('test/fixtures/config.json')); | ||
done(); | ||
}); | ||
assert.equal(file, path.resolve('test/fixtures/config.json')); | ||
done(); | ||
}); | ||
it('should locate a file given a full path', function (done) { | ||
var file = confer.locateFile('/test/fixtures/foo/config.json'); | ||
it('should locate a file given a full path', function (done) { | ||
var file = confer.locateFile('/test/fixtures/foo/config.json'); | ||
assert.equal(file, path.resolve('test/fixtures/foo/config.json')); | ||
done(); | ||
}); | ||
assert.equal(file, path.resolve('test/fixtures/foo/config.json')); | ||
done(); | ||
}); | ||
it('should NOT locate a file given a non-existent full path', function (done) { | ||
var file = confer.locateFile('test/fixtures/baz/config.json'); | ||
it('should NOT locate a file given a non-existent full path', function (done) { | ||
var file = confer.locateFile('test/fixtures/baz/config.json'); | ||
assert.equal(file, false); | ||
done(); | ||
}); | ||
assert.equal(file, false); | ||
done(); | ||
}); | ||
it('should NOT locate a file given a filename and non-existent dir', function (done) { | ||
var file = confer.locateFile('config.json', '/tmp/nowayjose'); | ||
it('should NOT locate a file given a filename and non-existent dir', function (done) { | ||
var file = confer.locateFile('config.json', '/tmp/nowayjose'); | ||
assert.equal(file, false); | ||
done(); | ||
}); | ||
assert.equal(file, false); | ||
done(); | ||
}); | ||
}); |
/* A test config file. */ | ||
{ | ||
// A test config option. | ||
"foo": false, | ||
"bar": true | ||
// A test config option. | ||
"foo": false, | ||
"bar": true | ||
} |
/* A test config file. */ | ||
{ | ||
// A test config option. | ||
"foo": true, | ||
"bar": false | ||
// A test config option. | ||
"foo": true, | ||
"bar": false | ||
} |
Sorry, the diff of this file is not supported yet
8492
9
50
155