Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80657 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
var inhertis = require('util').inherits;
17
var cli = require('./cli');
18
19
/**
20
* @class Base message class, simply renders the text
21
* @param {String} file path to the broken file
22
* @param {String} type project related type, like sprites or haste map
23
* @param {String} text message itself
24
*/
25
function Message(file, type, text) {
26
this.file = file;
27
this.type = type;
28
this.text = text;
29
}
30
31
/**
32
* @return {String}
33
*/
34
Message.prototype.render = function() {
35
return this.text.replace(/\n/g, ' ');
36
};
37
38
39
/**
40
* @class Warning is bad but we can live with it
41
* @extends {Message}
42
*/
43
function Warning(file, type, text) {
44
Message.call(this, file, type, text);
45
}
46
inhertis(Warning, Message);
47
48
Warning.prototype.render = function() {
49
return cli.bold('Warning') + ': [' + this.type + '] ' +
50
Message.prototype.render.call(this);
51
};
52
53
54
/**
55
* @class Somthing we should fix but the site might still load
56
* @extends {Message}
57
*/
58
function Error(file, type, text) {
59
Message.call(this, file, type, text);
60
}
61
inhertis(Error, Message);
62
63
Error.prototype.render = function() {
64
return cli.awesome('Error') + ': [' + this.type + '] ' +
65
Message.prototype.render.call(this);
66
};
67
68
/**
69
* @class Everything is broken. Fix now. Nothing will work until you fix
70
* @extends {Message}
71
*/
72
function ClowntownError(file, type, text) {
73
Message.call(this, file, type, text);
74
}
75
inhertis(ClowntownError, Message);
76
77
var clowntown;
78
ClowntownError.prototype.render = function() {
79
clowntown = clowntown || cli.bold(
80
cli.color('yellow', 'C') +
81
cli.color('magenta', 'L') +
82
cli.color('cyan', 'O') +
83
cli.color('yellow', 'W') +
84
cli.color('magenta', 'N') +
85
cli.color('cyan', 'T') +
86
cli.color('yellow', 'O') +
87
cli.color('magenta', 'W') +
88
cli.color('cyan', 'N')
89
);
90
return clowntown + ' ' + cli.awesome('Error') + ': [' + this.type + '] ' +
91
Message.prototype.render.call(this);
92
};
93
94
95
/**
96
* @class A list of messages obviously. Can be merged into the other list
97
* Uses a pool of objects so that we can reuse existing message lists when
98
* parsing stuff instead of creating tons of them over and over again.
99
*/
100
function MessageList() {
101
this.messages = [];
102
this.length = 0;
103
}
104
105
MessageList._cache = [];
106
107
MessageList.create = function() {
108
if (this._cache.length) {
109
return this._cache.pop();
110
}
111
return new MessageList();
112
};
113
114
MessageList.clearCache = function() {
115
this._cache.length = 0;
116
};
117
118
/**
119
* Merges other list object into this. You should consider recycling the merged
120
* MessageList with .recycle() so it can be reused later on
121
* @param {MessageList} list
122
* @return {MessageList} this
123
*/
124
MessageList.prototype.merge = function(list) {
125
list.messages.forEach(this.add, this);
126
return this;
127
};
128
129
MessageList.prototype.mergeAndRecycle = function(list) {
130
this.merge(list);
131
list.recycle();
132
return this;
133
};
134
135
MessageList.prototype.render = function() {
136
var fileMap = {};
137
var groups = [];
138
this.messages.forEach(function(message) {
139
if (!fileMap[message.file]) {
140
groups.push(fileMap[message.file] = []);
141
}
142
fileMap[message.file].push(message);
143
});
144
145
var result = '';
146
groups.forEach(function(group) {
147
result += ' ' + cli.bold('File:') + ' ' + group[0].file + '. ';
148
if (group.length === 1) {
149
result += group[0].render() + '\n';
150
} else {
151
result += cli.bold('Messages') + ':\n';
152
group.forEach(function(message) {
153
result += ' ' + message.render() + '\n';
154
});
155
}
156
});
157
158
return result;
159
};
160
161
MessageList.prototype.add = function(message) {
162
this.messages.push(message);
163
this.length = this.messages.length;
164
return this;
165
};
166
167
MessageList.prototype.addMessage = function(file, type, text) {
168
return this.add(new Message(file, type, text));
169
};
170
171
MessageList.prototype.addWarning = function(file, type, text) {
172
return this.add(new Warning(file, type, text));
173
};
174
175
MessageList.prototype.addError = function(file, type, text) {
176
return this.add(new Error(file, type, text));
177
};
178
179
MessageList.prototype.addClowntownError = function(file, type, text) {
180
return this.add(new ClowntownError(file, type, text));
181
};
182
183
MessageList.prototype.recycle = function(first_argument) {
184
this.messages.length = 0;
185
this.length = 0;
186
MessageList._cache.push(this);
187
return this;
188
};
189
190
/**
191
* Next 2 methods are ugly (voloko). But it's the minimal thing that works.
192
* So let them live.
193
*/
194
MessageList.prototype.toObject = function() {
195
return this.messages.map(function(m) {
196
var type = m instanceof Warning ? 'Warning' :
197
m instanceof Error ? 'Error' :
198
m instanceof ClowntownError ? 'ClowntownError':
199
'Message';
200
return [type, [m.file, m.type, m.text]];
201
});
202
};
203
204
MessageList.fromObject = function(object) {
205
var list = MessageList.create();
206
object.forEach(function(m) {
207
var f = m[0] === 'Warning' ? list.addWarning :
208
m[0] === 'Error' ? list.addError :
209
m[0] === 'ClowntownError' ? list.addClowntownError :
210
list.addMessage;
211
f.apply(list, m[1]);
212
});
213
return list;
214
};
215
216
module.exports = MessageList;
217
218