-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.js
More file actions
66 lines (56 loc) · 1.38 KB
/
Copy pathutils.js
File metadata and controls
66 lines (56 loc) · 1.38 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
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
'use strict';
const fs = require('fs'),
path = require('path'),
os = require('os');
module.exports = {
loadFile,
loadJSON,
resolvePath,
shallowCopy
};
function loadFile(inputFile, failureIsFatal = true)
{
try {
return fs.readFileSync(inputFile, { 'encoding': 'utf8' });
}
catch(exc) {
if(failureIsFatal) {
console.error('Error: Unable to load file "' + inputFile + '": ' + exc);
process.exit(1);
}
throw exc;
}
}
function loadJSON(inputFile, runtimeSettings, config, failureIsFatal = true)
{
let input = loadFile(inputFile, failureIsFatal),
data;
try {
data = runtimeSettings.inputParser(config, input);
}
catch(exc) {
/* istanbul ignore else */
if(failureIsFatal) {
console.error('Error parsing input: ' + exc + '.\nInput:\n' + input);
process.exit(1);
}
else {
throw exc;
}
}
if(runtimeSettings.unwrapper)
data = runtimeSettings.unwrapper(config, data);
return data;
}
function resolvePath(p)
{
switch(p.charAt(0)) {
case '~': return path.join(os.homedir(), p.substr(1));
case '/': return p;
default: return path.join(process.cwd(), p);
}
}
function shallowCopy(source, dest)
{
Object.keys(source).forEach(key => dest[key] = source[key]);
}