You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.3 KiB
JavaScript

10 years ago
/*!
* parse-git-config <https://github.com/jonschlinkert/parse-git-config>
*
* Copyright (c) 2015 Jon Schlinkert.
* Licensed under the MIT license.
*/
'use strict';
var fs = require('fs');
var path = require('path');
var ini = require('ini');
9 years ago
function parseGitConfig(options, cb) {
if (typeof options === 'function') {
9 years ago
cb = options;
options = null;
10 years ago
}
var fp = resolve(options);
10 years ago
if (typeof cb !== 'function') {
throw new TypeError('parse-git-config async expects a callback function.');
}
9 years ago
read(fp, function(err, buffer) {
10 years ago
if (err) {
cb(err);
return;
}
10 years ago
cb(null, ini.parse(buffer.toString()));
10 years ago
});
}
9 years ago
parseGitConfig.sync = function parseGitConfigSync(options) {
var fp = resolve(options);
10 years ago
if (!fs.existsSync(fp)) {
return null;
10 years ago
}
return ini.parse(fs.readFileSync(fp, 'utf8'));
};
function read(fp, cb) {
try {
9 years ago
fs.readFile(fp, function(err, config) {
10 years ago
if (err) {
return cb(err);
}
cb(null, config);
10 years ago
});
} catch (err) {
cb(err);
}
}
10 years ago
function resolve(options) {
options = options || {};
var cwd = options.cwd ? path.resolve(options.cwd) : process.cwd();
return path.resolve(cwd, options.path || '.git/config');
10 years ago
}
10 years ago
/**
* Expose `resolve`
*/
9 years ago
parseGitConfig.resolve = resolve;
/**
* Expose `parseGitConfig`
*/
module.exports = parseGitConfig;