-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (131 loc) · 3.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
const path = require('path');
const fs = require('fs');
const osHomedir = require('os-homedir');
const iniparser = require('iniparser');
const CONFIG_TYPE = {
LOCAL: 0,
HOME: 2
};
// Helpers
const repoPathConfig = cPath => path.resolve(cPath, '.git/config');
const homePathConfig = cPath => path.resolve(cPath, '.gitconfig');
const localConfig = repoPathConfig(process.cwd()); // Current working directory
const homeConfig = homePathConfig(osHomedir()); // Current user's home path
const getConfigPathLocal = (sync = false) => readResolve(localConfig, sync);
const getConfigPathHome = (sync = false) => readResolve(homeConfig, sync);
const getConfigPathRepo = (cPath, sync = false) => {
if (sync) {
try {
return readResolve(repoPathConfig(cPath), sync); // Catches in case of fail
} catch (err) {
return readResolve(homePathConfig(cPath), sync); // Throws in case of fail
}
}
return readResolve(repoPathConfig(cPath), sync) // Catches in case of fail
.catch(() => readResolve(homePathConfig(cPath), sync)); // Throws in case of fail
};
// Main api
const gitFig = type => getConfig(type, false);
gitFig.sync = type => getConfig(type, true);
Object.assign(gitFig, CONFIG_TYPE);
module.exports = gitFig;
/**
* Returns parsed ini file as object by config type
* @param [type]
* @param [sync]
* @return {Promise<object>|object}
* @throws if sync
*/
function getConfig(type, sync = false) {
const configPath = getConfigPath(type, sync);
return parseIni(configPath, sync);
}
/**
* Returns parsed ini file as object by path
* @param cPath
* @param [sync]
* @return {Promise<object>|object}
* @throws if sync
*/
function parseIni(cPath, sync = false) {
if (sync) {
return iniparser.parseSync(cPath) || {};
}
// Supports path as just value either as a promise
return new Promise((resolve, reject) => {
Promise.resolve(cPath)
.then(rPath => iniparser.parse(rPath, (err, data) => {
/* eslint-disable no-unused-expressions */
err ? reject(err) : resolve(data);
}))
.catch(err => reject(err));
});
}
/**
* Returns config path by config type
* @param [type]
* @param [sync]
* @return {Promise<string>|string}
* @throws if sync
*/
function getConfigPath(type, sync = false) {
if (arguments.length === 1 && typeof type === 'boolean') {
sync = type;
type = undefined;
}
if (typeof type === 'string') {
return getConfigPathRepo(type, sync);
}
if (type === CONFIG_TYPE.LOCAL) {
return getConfigPathLocal(sync);
}
if (type === CONFIG_TYPE.HOME) {
return getConfigPathHome(sync);
}
return getConfigPathCascade(sync);
}
/**
* Resolves local path if possible, then home path if possible and then custom path
* @param [sync]
* @return {Promise<string>|string}
* @throws if sync
*/
function getConfigPathCascade(sync = false) {
if (sync) {
try {
return getConfigPathLocal(sync); // Catches in case of fail
} catch (err) {
return getConfigPathHome(sync); // Throws in case of fail
}
}
return getConfigPathLocal(sync) // Catches in case of fail
.catch(() => getConfigPathHome(sync)); // Throws in case of fail
}
/**
* Returns file path if file is available for reading
* @param fName
* @param [sync]
* @return {Promise<string>|string}
* @throws if sync
*/
function readResolve(fName, sync = false) {
if (sync) {
tryRead(fName, sync);
return fName;
}
return tryRead(fName, sync).then(() => fName);
}
/**
* Check if file is available for reading
* @param fName
* @param [sync]
* @return {Promise|undefined}
* @throws if sync
*/
function tryRead(fName, sync = false) {
if (sync) {
return fs.accessSync(fName, fs.constants.R_OK);
}
return new Promise((resolve, reject) => fs.access(fName, fs.constants.R_OK, err => err ? reject(err) : resolve()));
}