UNPKG

803 BJavaScriptView Raw
1const os = require('os')
2const path = require('path')
3const ini = require('ini')
4const fs = require('fs-extra')
5
6let gitInfo = null
7
8/**
9 * @typedef {Object} GitInfo
10 * @property {string} name
11 * @property {string} username
12 * @property {string} email
13 * @return {GitInfo}
14 */
15module.exports = mock => {
16 if (gitInfo) return gitInfo
17
18 if (mock) {
19 return {
20 name: 'MOCK_NAME',
21 username: 'MOCK_USERNAME',
22 email: 'mock@example.com'
23 }
24 }
25
26 const filepath = path.join(os.homedir(), '.gitconfig')
27 if (!fs.existsSync(filepath)) {
28 return { name: '', username: '', email: '' }
29 }
30 const { user = {} } = ini.parse(fs.readFileSync(filepath, 'utf8'))
31 gitInfo = {
32 name: user.name || '',
33 username: user.username || '',
34 email: user.email || ''
35 }
36 return gitInfo
37}