-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
39 lines (33 loc) · 978 Bytes
/
util.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
const fs = require('fs');
const readline = require('readline');
function readAllLines(path) {
return new Promise((resolve, reject) => {
// Test file access directly, so that we can fail fast.
// Otherwise, an ENOENT is thrown in the global scope by the readline internals.
try {
fs.accessSync(path, fs.constants.R_OK);
} catch (err) {
reject(err);
}
let lines = [];
const reader = readline.createInterface({
input: fs.createReadStream(path),
crlfDelay: Infinity
});
reader
.on('line', (line) => lines.push(line))
.on('close', () => resolve(lines));
});
}
exports.configFromPath = async function configFromPath(path) {
const lines = await readAllLines(path);
return lines
.filter((line) => !/^\s*?#/.test(line))
.map((line) => line
.split('=')
.map((s) => s.trim()))
.reduce((config, [k, v]) => {
config[k] = v;
return config;
}, {});
};