index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. var fs = require('fs');
  2. var ini = require('ini');
  3. var path = require('path');
  4. module.exports = function(dir,cb){
  5. findGit(dir,function(config) {
  6. if(!config) return cb(new Error('no gitconfig to be found at '+dir))
  7. fs.readFile(config,function(err,data){
  8. if(err) return cb(err);
  9. try{
  10. var formatted = format(ini.parse(data.toString()));
  11. } catch (e){
  12. return cb(e);
  13. }
  14. cb(false,formatted);
  15. })
  16. })
  17. }
  18. function format(data){
  19. var out = {};
  20. Object.keys(data).forEach(function(k){
  21. if(k.indexOf('"')> -1) {
  22. var parts = k.split('"');
  23. var parentKey = parts.shift().trim();
  24. var childKey = parts.shift().trim();
  25. if(!out[parentKey]) out[parentKey] = {};
  26. out[parentKey][childKey] = data[k];
  27. } else {
  28. out[k] = data[k];
  29. }
  30. });
  31. return out;
  32. }
  33. function findGit(dir, cb) {
  34. var folder = path.join(dir, '.git/config')
  35. fs.exists(folder,function(exists) {
  36. if(exists) return cb(folder)
  37. if(dir === path.resolve(dir, '..')) return cb(false)
  38. findGit(path.resolve(dir, '..'), cb)
  39. })
  40. }