-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
252 lines (219 loc) · 9.09 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*eslint-env node*/
'use strict';
var util = require('util'),
path = require('path');
var chalk = require('chalk');
const createNoteDownInstance = function () {
var noteDown = {};
var getLine = function () {
var errStr = '';
try {
throw new Error('dummy error');
} catch (e) {
errStr = e.stack.toString();
// TODO: This can be optimized. When optimizing, ensure that this works correctly for test cases.
errStr = errStr.split('note-down/index.js').pop();
const ignoreLogsFor = noteDown.getComputedOption('ignoreLogsFor');
if (Array.isArray(ignoreLogsFor)) {
for (const entryToIgnore of ignoreLogsFor) {
errStr = errStr.split(entryToIgnore).pop();
}
}
errStr = errStr.substr(errStr.indexOf('\n at ') + 1);
errStr = errStr.substr(0, errStr.indexOf('\n')).replace(' at ', '');
if (errStr.indexOf('(') >= 0) {
errStr = errStr.substr(errStr.indexOf('(') + 1);
errStr = errStr.substr(0, errStr.indexOf(')'));
}
}
var line = errStr;
line = line.replace(/^file:\/\/\//, '/'); // Seemingly, useful for cases like TypeScript
if (noteDown.getComputedOption('basePath')) {
// TODO: Check if it works fine with Windows paths
var filePathAndLine = line;
var match = filePathAndLine.match(/:/g);
var extractedPath = filePathAndLine;
if (match && match.length >= 2) {
extractedPath = extractedPath.substr(0, extractedPath.lastIndexOf(':'));
extractedPath = extractedPath.substr(0, extractedPath.lastIndexOf(':'));
}
var relativePath = path.relative(noteDown.getComputedOption('basePath'), extractedPath);
line = relativePath + filePathAndLine.replace(extractedPath, '');
}
return line;
};
const deviceNameIfAvailableInGray = (function () {
// $ export NOTE_DOWN_DEVICE_NAME="example.com"
const deviceName = process.env.NOTE_DOWN_DEVICE_NAME;
if (deviceName) {
return chalk.gray.dim('[' + process.env.NOTE_DOWN_DEVICE_NAME + '] ');
} else {
return '';
}
}());
var log = function (msg) {
if (noteDown.getComputedOption('disabled')) {
// do nothing (because logging is disabled)
} else {
if (noteDown.getComputedOption('showLogLine')) {
console.log(deviceNameIfAvailableInGray + msg + chalk.gray.dim(' @ ' + getLine()));
} else {
console.log(deviceNameIfAvailableInGray + msg);
}
}
};
var idempotent = function (param) {
return param;
};
var logIt = function (passedArguments, processFn) {
var i;
for (i = 0; i < passedArguments.length; i++) {
var passedArgument = passedArguments[i];
log(processFn(passedArgument));
}
};
// debugCategoryList would be used to decide whether to log the .debug(message, category) calls or not
var debugCategoryList = {};
// Examples:
// {} - All noteDown.debug() messages would be printed
// { '*': 'enabled' } - All noteDown.debug() messages would be printed
// { 'TEST': 'disabled' } - All noteDown.debug() messages, except noteDown.debug(<message>, 'TEST') would be printed
// { '*': 'disabled' } - No noteDown.debug() messages would be printed
// { '*': 'disabled', 'TEST': 'enabled' } - Only noteDown.debug(<message>, 'TEST') messages would be printed
/*
operation: enable/disable/delete/get/getAll
category: any string
*/
noteDown.debugCategoryOperation = function (operation, category) {
if (['enable', 'disable', 'delete', 'get'].indexOf(operation) >= 0) {
if (category && typeof category === 'string') {
if (operation === 'enable') {
debugCategoryList[category] = 'enabled';
} else if (operation === 'disable') {
debugCategoryList[category] = 'disabled';
} else if (operation === 'delete') {
delete debugCategoryList[category];
} else if (operation === 'get') {
return debugCategoryList[category];
} else if (operation === 'getAll') {
return debugCategoryList;
} else {
console.log('Warning: The code should never reach this line. Please report a bug in this package.');
}
} else {
console.log('Warning: Unexpected category for debugCategoryOperation');
}
} else if (operation === 'getAll') {
return debugCategoryList;
} else {
console.log('Warning: Unexpected operation for debugCategoryOperation');
}
// Make this function call chainable for the cases where it has not returned some data
return noteDown;
};
var fnMap = {
error: function () { logIt(arguments, chalk.red); },
errorHeading: function () { logIt(arguments, chalk.white.bgRed); },
fatal: function () { logIt(arguments, chalk.white.bgRed); },
fixme: function () { logIt(arguments, chalk.yellow); },
help: function () { logIt(arguments, chalk.black.bgWhite); },
info: function () { logIt(arguments, chalk.cyan); },
log: function () { logIt(arguments, idempotent); },
success: function () { logIt(arguments, chalk.green); },
todo: function () { logIt(arguments, chalk.yellow); },
trace: function () { logIt(arguments, chalk.yellow); },
warn: function () { logIt(arguments, chalk.yellow); },
warnHeading: function () { logIt(arguments, chalk.black.bgYellow); },
data: function () {
logIt(arguments, function (msg) {
return chalk.magenta(util.inspect(msg, {
showHidden: false,
depth: null,
colors: true
}));
});
},
debug: function (msg, category) {
var showMessage = true;
if (debugCategoryList['*'] === 'disabled' || debugCategoryList[category] === 'disabled') {
showMessage = false;
}
if (debugCategoryList[category] === 'enabled') {
showMessage = true;
}
if (showMessage) {
logIt([category + ': ' + msg], chalk.cyan);
}
},
json: function () {
logIt(arguments, function (msg) {
return chalk.magenta(util.inspect(msg, {
showHidden: false,
depth: null,
colors: true
}));
});
},
verbose: function () {
logIt(arguments, function (msg) {
if (typeof msg !== 'string') {
msg = util.inspect(msg, {
showHidden: false,
depth: null
});
}
return chalk.dim(msg);
});
}
};
Object.keys(fnMap).forEach(function (key) {
noteDown[key] = function () {
fnMap[key].apply(this, arguments);
};
});
var globalPrefix = '_noteDown_';
// Get/Set an option
noteDown.option = function (name, value, globalSetting) {
if (value === undefined) {
if (globalSetting) {
return global[globalPrefix + name];
} else {
return options[name];
}
} else {
if (globalSetting) {
global[globalPrefix + name] = value;
} else {
options[name] = value;
}
return noteDown;
}
};
// Remove an option
noteDown.removeOption = function (name, globalSetting) {
if (globalSetting) {
delete global[globalPrefix + name];
} else {
delete options[name];
}
};
// Get the computed value for the option (value set for the option and fallback to the global value)
noteDown.getComputedOption = function (name) { return noteDown.option(name) || noteDown.option(name, undefined, true); };
noteDown.off = noteDown.disable = function () { noteDown.option('disabled', true); };
noteDown.on = noteDown.enable = function () { noteDown.option('disabled', false); };
noteDown.chalk = chalk;
// Options object
var options = {
disabled: noteDown.option('disabled', undefined, true) || undefined,
basePath: noteDown.option('basePath', undefined, true) || process.cwd() || undefined,
showLogLine: noteDown.option('showLogLine', undefined, true) || true
};
return noteDown;
};
const noteDown = createNoteDownInstance();
const logger = noteDown;
module.exports = {
createNoteDownInstance,
noteDown,
logger
};