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.

62 lines
1.1 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');
10 years ago
/**
* Expose `config`
*/
module.exports = git;
10 years ago
10 years ago
function git(cwd, cb) {
10 years ago
if (typeof cwd === 'function') {
cb = cwd; cwd = null;
}
10 years ago
if (typeof cb !== 'function') {
throw new TypeError('parse-git-config async expects a callback function.');
}
read(resolve(cwd), function (err, buffer) {
10 years ago
if (err) {
cb(err);
return;
}
10 years ago
cb(null, ini.parse(buffer.toString()));
10 years ago
});
}
10 years ago
git.sync = function configSync(cwd) {
var fp = resolve(cwd);
10 years ago
if (!fs.existsSync(fp)) {
return null;
10 years ago
}
return ini.parse(fs.readFileSync(fp, 'utf8'));
};
function read(fp, cb) {
try {
10 years ago
fs.readFile(fp, function (err, config) {
if (err) {
return cb(err);
}
cb(null, config);
10 years ago
});
} catch (err) {
cb(err);
}
}
10 years ago
function resolve(cwd) {
return path.join(cwd || process.cwd(), '.git/config');
}