前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >NodeJs 获取 Git 用户和邮箱信息

NodeJs 获取 Git 用户和邮箱信息

作者头像
上山打老虎了
发布2022-06-15 08:51:54
1.5K0
发布2022-06-15 08:51:54
举报
文章被收录于专栏:Article

获取 git config 的路径,一种是项目单独配置的,一种是全局配置的

代码语言:javascript
复制
'use strict';

const fs = require('fs');
const os = require('os');
const path = require('path');

module.exports = function (type) {
    let configPath = '';
    const workDir = process.cwd();

    if (type === 'global') {
        configPath = path.join(os.homedir(), '.gitconfig');
    } else {
        configPath = path.resolve(workDir, '.git/config');
    }

    if (!fs.existsSync(configPath)) {
        configPath = path.join(os.homedir(), '.config/git/config');
    }

    return fs.existsSync(configPath) ? configPath : null;
}

读取配置文件,使用 ini 库解析成对象模式

代码语言:javascript
复制
'use strict'

const fs = require('fs');
const filePath = require('./path');
const ini = require('ini');

module.exports = function (type) {
    const path = filePath(type)
    if (path) {
        const file = fs.readFileSync(path, 'utf8');
        return ini.parse(file);
    }
    return '';
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年04月30日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档