This repository has been archived by the owner on Jan 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.js
executable file
·252 lines (201 loc) · 7.04 KB
/
build.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
#!/usr/bin/env node
/*
Copyright 2011 Yahoo! Inc. All rights reserved.
Licensed under the BSD License.
http://yuilibrary.com/license/
*/
var ejs = require('ejs'),
fs = require('fs'),
http = require('http'),
path = require('path'),
util = require('./lib/util'),
argv = process.argv.slice(2),
yuiVersion = argv.shift(),
// Things to exclude.
EXCLUDE_CLASSES = [],
EXCLUDE_CONSTANTS = [],
EXCLUDE_FUNCTIONS = ['()', 'DEFAULT_GETTER', 'DEFAULT_SETTER'],
EXCLUDE_VARIABLES = [],
// Syntax templates to render.
TEMPLATES = [
'textmate/JavaScript - YUI 3.tmLanguage.ejs',
'vim/after/syntax/javascript.vim.ejs'
],
// Words that need to be escaped in vim syntax output.
VIM_ESCAPE = [
'contained', 'containedin', 'contains', 'display', 'fold', 'nextgroup',
'oneline', 'skipempty', 'skipnl', 'skipwhite', 'transparent'
],
// YUI website API details.
YUI_API_HOST = 'stage.yuilibrary.com',
YUI_API_PORT = 80,
YUI_API_ROOT = '/api/v1',
getPath;
// First, make sure we have a version number.
if (!yuiVersion) {
process.stderr.write(
"Usage:\n" +
" build.js <yui3 version>\n"
);
process.exit(1);
}
getPath = YUI_API_ROOT + '/classes/?sort=sortName&version=' +
encodeURIComponent(yuiVersion);
// Hit the YUI website API to get the latest syntax data.
console.log('Fetching latest syntax data from the YUI website API');
console.log('http://' + YUI_API_HOST + getPath);
http.get({
host: YUI_API_HOST,
port: YUI_API_PORT,
path: getPath
}, function (res) {
var data = '';
if (res.statusCode < 200 || res.statusCode > 299) {
onFetchError(new Error('HTTP ' + res.statusCode));
return;
}
res.on('data', function (chunk) { data += chunk; });
res.on('error', onFetchError);
res.on('end', function () {
try {
data = JSON.parse(data);
if (!data.classes.length) {
onFetchError(new Error('No classes returned for version ' + yuiVersion));
return;
}
} catch (ex) {
onFetchError(ex);
return;
}
onFetchSuccess(data);
});
}).on('error', onFetchError);
function onFetchError(err) {
process.stderr.write('==> Error: ' + err.message + '\n');
process.exit(1);
}
function onFetchSuccess(data) {
var syntax = {
classes : [],
constants: [],
functions: [],
variables: [],
version : yuiVersion
};
console.log('Parsing syntax data');
data.classes.forEach(function (klass) {
var names = klass.name.split('.');
names.forEach(function (name) {
name = name.trim();
if (name && name.indexOf('~') === -1) {
syntax.classes.push(name);
}
});
if (klass.methods) {
util.each(klass.methods, function (method, name) {
var match, descr;
// catch a doc bug where description gets inserted in method name
if (name && name.indexOf('\n') > -1) {
descr = name.split('\n');
// clean method name
name = descr.shift().trim();
// append to existing description, if present
if (method.description) {
descr.unshift(method.description);
}
// augment methods object with clean copy of method config
klass.methods[name] = util.merge(method, {
description: descr.join('\n'),
name: name
});
}
match = name.match(/\.([^\.]+)$/);
name = match ? match[1].trim() : name.trim();
if (!name || method.protected || method.private) {
return;
}
if (EXCLUDE_FUNCTIONS.indexOf(name) === -1) {
syntax.functions.push(name);
}
});
}
if (klass.properties) {
util.each(klass.properties, function (prop, name) {
var match = name.match(/\.([^\.]+)$/);
name = match ? match[1].trim() : name.trim();
if (!name || prop.protected || prop.private) {
return;
}
if (prop.final) {
if (EXCLUDE_CONSTANTS.indexOf(name) === -1) {
syntax.constants.push(name);
}
} else {
if (EXCLUDE_VARIABLES.indexOf(name) === -1) {
syntax.variables.push(name);
}
}
});
}
});
['classes', 'constants', 'functions', 'variables'].forEach(function (key) {
var items = util.dedupe(syntax[key]);
items.sort();
syntax[key] = items;
syntax[key + '_joined'] = items.join('|');
});
generate(syntax);
}
function generate(syntax) {
var readStack = new util.Stack(),
writeStack = new util.Stack();
console.log('Generating syntax files');
TEMPLATES.forEach(function (filename) {
fs.readFile(filename, 'utf8', readStack.add(function (err, template) {
if (err) { return filename; }
return {
filename: filename,
template: template
};
}));
});
readStack.done(function (errors, results) {
if (errors) {
errors.forEach(function (err, index) {
process.stderr.write('==> Error [' + results[index] + ']: ' + err + '\n');
});
process.exit(1);
return;
}
results.forEach(function (input) {
var extname = path.extname(input.filename),
locals = util.merge(syntax),
outname = input.filename.substring(0,
input.filename.lastIndexOf(extname)),
output;
extname = path.extname(outname);
if (extname === '.vim') {
['classes', 'constants', 'functions', 'variables'].forEach(function (key) {
locals[key] = locals[key].map(function (name) {
return VIM_ESCAPE.indexOf(name) === -1 ? name : '\\' + name;
});
});
}
output = ejs.render(input.template, {locals: locals});
fs.writeFile(outname, output, 'utf8', writeStack.add(function (err) {
if (err) { return outname; }
console.log('==> ' + outname);
}));
});
writeStack.done(function (errors, results) {
if (errors) {
errors.forEach(function (err, index) {
process.stderr.write('==> Error [' + results[index] + ']: ' + err + '\n');
});
process.exit(1);
return;
}
console.log('Done!');
});
});
}