forked from node-inspector/node-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
188 lines (169 loc) · 5.03 KB
/
config.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var fs = require('fs'),
rc = require('rc'),
path = require('path');
var conversions = {
checkIfNull: function(value) {
return value && value !== 'null' ? value : null;
},
keyToCamelKey: function(value) {
return value.replace(/-(.)/g, function(_, lower) {
return lower.toUpperCase();
});
},
keyToDashedKey: function(value) {
return value.replace(/([A-Z])/g, function(_, upper) {
return '-' + upper.toLowerCase();
});
},
rcToInnerConfig: function(rcConfig) {
var options = {};
Object.keys(rcConfig).forEach(function(key) {
var camelKey = conversions.keyToCamelKey(key),
fixedVal = rcConfig[key],
predefined;
predefined = !!definitions[key];
if (predefined) {
try {
fixedVal = definitions[key].convert(fixedVal);
}
catch (e) {
console.warn('Cannot convert config option %s: %s.', key, e.message || e);
}
}
options[camelKey] = fixedVal;
});
return options;
},
stringToArray: function(value) {
var hidden;
if (typeof value === 'string') {
try {
value = JSON.parse(value);
} catch (e) {
throw new Error('The value is not a valid JSON. ' + (e.message || e));
}
}
if (value.length >= 0) {/*not string, but has length - Array*/
hidden = value.map(function(s) { return new RegExp(s, 'i'); });
} else {
var msg = 'The value ' + JSON.stringify(value) + ' is not an array.';
throw new Error(msg);
}
return hidden;
},
stringToBoolean: function(value) {
return !!value;
},
stringToInt: function(value) {
return parseInt(value, 10);
}
};
var definitions = {
'help': {
desc: 'Show this help',
convert: conversions.stringToBoolean,
defaultValue: false
},
'version': {
desc: 'Print Node Inspector\'s version',
convert: conversions.stringToBoolean,
defaultValue: false
},
'web-port': {
desc: 'Port to host the inspector',
convert: conversions.stringToInt,
defaultValue: 8080
},
'web-host': {
desc: 'Host to listen on',
convert: conversions.checkIfNull,
defaultValue: ''
},
'debug-port': {
desc: 'Port to connect to the debugging app',
convert: conversions.stringToInt,
defaultValue: 5858
},
'save-live-edit': {
desc: 'Save live edit changes to disk (update the edited files)',
convert: conversions.stringToBoolean,
defaultValue: false
},
'no-preload': {
desc: 'Disables preloading *.js to speed up startup',
convert: conversions.stringToBoolean,
defaultValue: false
},
'hidden': {
desc: 'Array of files to hide from the UI (breakpoints in these files' +
' will be ignored)',
convert: conversions.stringToArray,
defaultValue: []
},
'stack-trace-limit': {
desc: 'Number of stack frames to show on a breakpoint',
convert: conversions.stringToInt,
defaultValue: 50
}
};
var defaults = loadDefaults();
var rcConfig = rc('node-inspector', defaults);
var config = conversions.rcToInnerConfig(rcConfig);
config.isScriptHidden = function(scriptPath) {
return config.hidden.some(function fnHiddenScriptMatchesPath(r) {
return r.test(scriptPath);
});
};
module.exports = config;
module.exports._describeOptions = function() {
return Object.keys(definitions)
.map(function constructMessagePart(key) {
var definition = definitions[key];
var defaultValue = definition.defaultValue;
var defaultString = JSON.stringify(definition.defaultValue);
var typeString = Object.prototype.toString.call(defaultValue);
var matchedType = /^\[object (.*)\]$/.exec(typeString)[1];
var optionKey = '\u001b[92m--' + key;
var optionTypeAndDefault =
matchedType !== 'Undefined' && matchedType !== 'Boolean' ?
'=\u001b[90m{' + matchedType + '}' +
' \u001b[96m(default: ' + defaultString + ')' :
'';
var optionDescription = '\u001b[0m' + definition.desc;
return ' ' + optionKey + optionTypeAndDefault +
'\n ' + optionDescription;
})
.join('\n\n');
};
function collectDefaultsFromDefinitions() {
var options = {};
Object.keys(definitions).forEach(function(key) {
var camelKey = conversions.keyToCamelKey(key);
options[camelKey] = definitions[key].defaultValue;
});
return options;
}
function collectDefaultsFromJSONConfig() {
var options = {},
camelKeyOptions,
pathToConfig = path.join(__dirname, '../config.json');
try {
camelKeyOptions = JSON.parse(fs.readFileSync(pathToConfig));
}
catch (e) {
camelKeyOptions = {};
}
Object.keys(camelKeyOptions).forEach(function(key) {
var dashedKey = conversions.keyToDashedKey(key);
options[dashedKey] = camelKeyOptions[key];
});
return options;
}
function loadDefaults() {
var defaults = collectDefaultsFromDefinitions(),
override = collectDefaultsFromJSONConfig(); /*Backward compatibility*/
Object.keys(override).forEach(function(dashedKey) {
defaults[dashedKey] = override[dashedKey];
});
return defaults;
}