-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
40 lines (34 loc) · 1.09 KB
/
util.js
File metadata and controls
40 lines (34 loc) · 1.09 KB
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
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*$|^\s*?#/.test(line)) // ignore comments and whitespace-only lines
.map((line) => line
.split(/=(.*)/) // the (.*) group causes split only on the first occurrence since values can contain equals signs
.map((s) => s.trim())
)
.reduce((config, [k, v]) => {
config[k] = v;
return config;
}, {});
};