-
Notifications
You must be signed in to change notification settings - Fork 0
/
logremover.js
227 lines (206 loc) · 6.01 KB
/
logremover.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
/* Copyright 2012 Huygens ING
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
var falafel = require('falafel');
var fs = require('fs');
var util = require('util');
var CONSOLE_METHODS = [
'assert',
'clear',
'count',
'debug',
'dir',
'dirxml',
'error',
'exception',
'error',
'group',
'groupEnd',
'groupCollapsed',
'info',
'log',
'memoryProfile',
'memoryProfileEnd',
'profile',
'profileEnd',
'table',
'time',
'timeEnd',
'timeStamp',
'trace',
'warn'
];
function isConsoleLog(node, logOnly) {
function isConsole(n) {
return (n.name == 'console') || (n.object && n.object.name == 'window' && n.property.name == 'console');
}
function shouldMessWithMethod(method) {
return method && (method.name == 'log' ||
(!logOnly && CONSOLE_METHODS.indexOf(method.name) != -1));
}
return node.type == 'CallExpression' && node.callee && node.callee.object && isConsole(node.callee.object) && shouldMessWithMethod(node.callee.property);
}
function cleanArg(arg) {
var someNode = null;
var n = falafel(arg, function(node) {
someNode = node;
if (!node.__hasSideEffects) {
node.__hasSideEffects = node.type == 'CallExpression' || node.type == 'UpdateExpression' ||
node.type == 'AssignmentExpression';
}
if (node.parent && node.__hasSideEffects) {
node.parent.__hasSideEffects = true;
}
});
var topNode = someNode;
while (topNode.parent) {
topNode = topNode.parent;
}
function cleanNodeWithPossibleKids(n) {
if (n.__hasSideEffects) {
return ['(' + n.source() + ')'];
}
// Otherwise, nothin':
return [];
}
return cleanNodeWithPossibleKids(topNode);
}
function updateNode(node, keepSideEffects) {
var nodesToLeave;
if (keepSideEffects) {
nodesToLeave = callSideEffectsButDoNothing(node['arguments'], function(n) {
return cleanArg(n.source(), keepSideEffects);
});
} else {
nodesToLeave = [];
}
if (!nodesToLeave.length) {
if (node.parent.type == "ExpressionStatement") {
node.parent.__shouldRemoveEntirely = true;
} else {
node.update('0');
}
} else {
node.update('(' + nodesToLeave.join(' && 0) || (') + ' && 0)');
}
}
function callSideEffectsButDoNothing(ary, fn) {
var rv = [];
for (var i = 0; i < ary.length; i++) {
var newRv = fn(ary[i]);
if (newRv.length) {
rv = rv.concat(newRv);
}
}
return rv;
}
var queuedFiles = 0, totalFileCount = 0;
var emitter = require('events').EventEmitter;
function LogRemover() {
emitter.call(this);
}
util.inherits(LogRemover, emitter);
LogRemover.prototype.remove =
function remLogs(inDesignator, outDesignator, logOnly, batchMode, nonExplicit, keepSideEffects) {
var self = this;
function onInputError(ex, manualError) {
if (!manualError) {
queuedFiles--;
totalFileCount--;
}
if (ex && ex.code == 'EISDIR') {
self.emit('dirs', inDesignator);
return;
}
var resultOfError = batchMode ? "skipping." : "exiting without doing anything.";
console.error("Error: could not read from:", inDesignator, ";", resultOfError);
if (!batchMode) {
process.exit(1);
}
}
var instream;
if (inDesignator == 'stdin') {
instream = process.stdin;
instream.resume();
} else if (batchMode && nonExplicit && inDesignator.indexOf('.js') == -1) {
fs.stat(inDesignator, function(err, statInfo) {
if (!err) {
if (statInfo.isDirectory()) {
self.emit('dirs', inDesignator);
} else {
console.error("Ignoring '" + inDesignator + "'.");
}
} else {
console.error("Error trying to determine if '" + inDesignator + "' is a directory or not.");
}
});
return;
} else {
try {
instream = fs.createReadStream(inDesignator);
instream.on('error', onInputError);
} catch (ex) {
onInputError(ex, true);
return;
}
}
processStream(instream, inDesignator, outDesignator, logOnly, batchMode, keepSideEffects, self);
};
function processStream(instream, inDesignator, outDesignator, logOnly, batchMode, keepSideEffects, self) {
var isStdOut = outDesignator == 'stdout';
instream.setEncoding('utf8');
var scriptdata = '';
queuedFiles++;
totalFileCount++;
instream.on('data', function(str) { scriptdata += str; });
instream.on('end', function() {
var outputStr;
try {
outputStr = falafel(scriptdata, function(node) {
if (isConsoleLog(node, logOnly)) {
updateNode(node, keepSideEffects);
} else if (node.__shouldRemoveEntirely) {
node.update('');
}
});
} catch (ex) {
queuedFiles--;
totalFileCount--;
console.error("Error trying to process '" + inDesignator + "':", ex);
return;
}
if (isStdOut) {
process.stdout.write(outputStr.toString());
if (!batchMode); {
process.exit(0);
}
} else {
process.nextTick(function() {
fs.writeFile(outDesignator, outputStr.toString(), 'utf8', function() {
if (!batchMode) {
console.log('Saved output as "' + outDesignator + '".');
} else {
console.log('Processed "' + outDesignator + '".');
}
queuedFiles--;
if (batchMode && queuedFiles === 0) {
self.emit('finished', totalFileCount);
}
});
});
}
});
}
module.exports = LogRemover;