resolve global path

master
jonschlinkert 9 years ago
parent 3fb683c02d
commit 936b3f17bc

@ -42,7 +42,6 @@ function parse(options, cb) {
options = options || {}; options = options || {};
var filepath = parse.resolve(options); var filepath = parse.resolve(options);
if (gitCache[filepath]) { if (gitCache[filepath]) {
cb(null, gitCache[filepath]); cb(null, gitCache[filepath]);
return; return;
@ -77,7 +76,6 @@ function parse(options, cb) {
parse.sync = function parseSync(options) { parse.sync = function parseSync(options) {
options = options || {}; options = options || {};
var filepath = parse.resolve(options); var filepath = parse.resolve(options);
if (gitCache[filepath]) { if (gitCache[filepath]) {
return gitCache[filepath]; return gitCache[filepath];
} }
@ -93,10 +91,16 @@ parse.sync = function parseSync(options) {
*/ */
parse.resolve = function resolve(options) { parse.resolve = function resolve(options) {
var isGlobal = false;
if (options === 'global') {
isGlobal = true;
options = {};
}
if (typeof options === 'string') { if (typeof options === 'string') {
options = { path: options }; options = { path: options };
} }
var opts = extend({path: configPath()}, options); var opts = extend({path: configPath(isGlobal ? 'global' : null)}, options);
if (opts.cwd) { if (opts.cwd) {
opts.path = path.resolve(opts.cwd, opts.path); opts.path = path.resolve(opts.cwd, opts.path);
} }

@ -1,7 +1,7 @@
{ {
"name": "parse-git-config", "name": "parse-git-config",
"description": "Parse `.git/config` into a JavaScript object. sync or async.", "description": "Parse `.git/config` into a JavaScript object. sync or async.",
"version": "0.4.1", "version": "0.4.2",
"homepage": "https://github.com/jonschlinkert/parse-git-config", "homepage": "https://github.com/jonschlinkert/parse-git-config",
"author": "Jon Schlinkert (https://github.com/jonschlinkert)", "author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"repository": "jonschlinkert/parse-git-config", "repository": "jonschlinkert/parse-git-config",
@ -26,7 +26,8 @@
}, },
"devDependencies": { "devDependencies": {
"gulp-format-md": "^0.1.7", "gulp-format-md": "^0.1.7",
"mocha": "^2.4.5" "mocha": "^2.4.5",
"os-homedir": "^1.0.1"
}, },
"keywords": [ "keywords": [
"config", "config",
@ -60,4 +61,4 @@
"reflinks": true "reflinks": true
} }
} }
} }

@ -10,6 +10,7 @@
require('mocha'); require('mocha');
var assert = require('assert'); var assert = require('assert');
var path = require('path'); var path = require('path');
var home = require('os-homedir');
var parse = require('./'); var parse = require('./');
describe('sync:', function() { describe('sync:', function() {
@ -52,7 +53,7 @@ describe('resolve:', function() {
}); });
it('should allow override path', function() { it('should allow override path', function() {
var fp = path.resolve(process.env.HOME, '.gitconfig'); var fp = path.resolve(home(), '.gitconfig');
assert.equal(parse.resolve({path: fp}), fp); assert.equal(parse.resolve({path: fp}), fp);
}); });
@ -60,6 +61,10 @@ describe('resolve:', function() {
assert.equal(parse.resolve({path: '.config'}), path.resolve(process.cwd(), '.config')); assert.equal(parse.resolve({path: '.config'}), path.resolve(process.cwd(), '.config'));
}); });
it('should resolve relative path to the global git config when `global` is passed', function() {
assert.equal(parse.resolve('global'), path.resolve(home(), '.gitconfig'));
});
it('should allow override of cwd', function() { it('should allow override of cwd', function() {
var actual = parse.resolve({path: '.config', cwd: '/opt/config'}); var actual = parse.resolve({path: '.config', cwd: '/opt/config'});
assert.equal(actual, path.resolve('/opt/config/.config')); assert.equal(actual, path.resolve('/opt/config/.config'));

Loading…
Cancel
Save