Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read and parse gitconfig file with node.js?

[user]
        name = Alvin J. Alexander
        email = [omitted]
[merge]
        tool = vimdiff

This is what ~/.gitconfig file looks like. I've never encountered such data objects before. Does this format have a name like json files? Or is this a custom format?

My goal is to extract data from this file to fill out a package.json template. I want to research this format to better understand how to parse it. Do parsing functions already exist for this?


For Reference

This is a template for how to parse it:

(requires iniparser module to be installed)

var iniparser = require('iniparser');
  var fs = require('fs');
  var home_dir = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
  console.log (home_dir);
  var config_file = home_dir+'/.gitconfig';
  var exists = fs.existsSync(config_file);
  if (exists) {
    console.log("Getting some information from the git configuration...");
    var config = iniparser.parseSync(config_file);
    console.log(config);
    return config;
  }
  else {
    console.log("Git configuration file does not exist...");
    return {};
  };
like image 775
Logan Avatar asked Dec 21 '22 04:12

Logan


1 Answers

This file is a ini file. You can try this parser, but any node-ini parser should do the job :).

like image 51
Ven Avatar answered Dec 24 '22 01:12

Ven