This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
forked from joewalker/gcli
-
Notifications
You must be signed in to change notification settings - Fork 23
/
node-main.js
447 lines (410 loc) · 14.2 KB
/
node-main.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* Copyright 2009-2011 Mozilla Foundation and contributors
* Licensed under the New BSD license. See LICENSE.txt or:
* http://opensource.org/licenses/BSD-3-Clause
*/
var copy = require('dryice').copy;
var path = require('path');
var fs = require('fs');
var gcliHome = __dirname;
/**
* The main() function is called at the bottom of this file to ensure all the
* globals are setup properly.
*/
function main() {
var args = process.argv;
if (args[2] === 'standard') {
buildStandard();
}
else if (args[2] === 'firefox') {
buildFirefox(args[3]);
}
else if (args[2] === 'test') {
test();
}
else if (args[2] === 'serve') {
serve();
}
else {
console.log('Targets:');
console.log('> node node-main.js standard');
console.log(' # Builds GCLI for the web to ./built');
console.log('> node node-main.js firefox [directory]');
console.log(' # Builds GCLI for firefox to ./built/mozilla or [directory]');
console.log('> node node-main.js test');
console.log(' # Run GCLI tests using jsdom');
console.log('> node node-main.js serve');
console.log(' # Serve . to http://localhost:9999 for chrome');
process.exit(1);
}
}
/**
* There are 2 important ways to build GCLI.
* The first is for use within a normal web page.
* It has compressed and uncompressed versions of the output script file.
*/
function buildStandard() {
console.log('Building standard outputs to built/gcli[-uncompressed].js');
copy.mkdirSync(gcliHome + '/built', 0755);
var project = copy.createCommonJsProject({
roots: [ gcliHome + '/lib' ]
});
var sources = copy.createDataObject();
copy({
source: {
project: project,
// This list of dependencies should be the same as in index.html
require: [ 'gcli/index', 'demo/index', 'gclitest/index' ]
},
filter: copy.filter.moduleDefines,
dest: sources
});
copy({
source: { root: project, include: /.*\.png$|.*\.gif$/ },
filter: copy.filter.base64,
dest: sources
});
console.log(project.report());
// Create a GraphML dependency report. Directions:
// - Install yEd (http://www.yworks.com/en/products_yed_about.htm)
// - Load gcli/built/gcli.graphml
// - Resize the nodes (Tools->Fit Node to Label)
// - Apply a layout (Layout->Hierarchical)
console.log('Outputting dependency graph to built/gcli.graphml\n');
if (project.getDependencyGraphML) {
copy({
source: { value:project.getDependencyGraphML() },
dest: 'built/gcli.graphml'
});
}
// Create the output scripts, compressed and uncompressed
copy({ source: 'index.html', filter: tweakIndex, dest: 'built/index.html' });
copy({ source: 'scripts/es5-shim.js', dest: 'built/es5-shim.js' });
copy({
source: [ copy.getMiniRequire(), sources ],
dest: 'built/gcli-uncompressed.js'
});
try {
copy({
source: [ copy.getMiniRequire(), sources ],
filter: copy.filter.uglifyjs,
dest: 'built/gcli.js'
});
}
catch (ex) {
console.log('ERROR: Uglify compression fails on windows/linux. ' +
'Skipping creation of built/gcli.js\n');
}
}
/**
* Build the Javascript JSM files for Firefox
* It consists of 1 output file: gcli.jsm
*/
function buildFirefox(destDir) {
if (!destDir && process.env.FIREFOX_HOME) {
destDir = process.env.FIREFOX_HOME;
}
console.log('Building Firefox outputs to ' + (destDir || 'built/ff') + '.\n');
if (!destDir) {
copy.mkdirSync(gcliHome + '/built/ff', 0755);
}
var jsmDir = '/browser/devtools/webconsole';
var winCssDir = '/browser/themes/winstripe/devtools';
var pinCssDir = '/browser/themes/pinstripe/devtools';
var gnomeCssDir = '/browser/themes/gnomestripe/devtools';
var propsDir = '/browser/locales/en-US/chrome/browser/devtools';
var testDir = '/browser/devtools/webconsole/test';
if (destDir) {
var fail = false;
if (!path.existsSync(destDir + jsmDir)) {
console.error('Missing path for JSM: ' + destDir + jsmDir);
fail = true;
}
if (!path.existsSync(destDir + winCssDir)) {
console.error('Missing path for Windows CSS: ' + destDir + winCssDir);
fail = true;
}
if (!path.existsSync(destDir + pinCssDir)) {
console.error('Missing path for Mac CSS: ' + destDir + pinCssDir);
fail = true;
}
if (!path.existsSync(destDir + gnomeCssDir)) {
console.error('Missing path for Gnome CSS: ' + destDir + gnomeCssDir);
fail = true;
}
if (!path.existsSync(destDir + propsDir)) {
console.error('Missing path for l10n string: ' + destDir + propsDir);
fail = true;
}
if (fail) {
process.exit(1);
}
}
var project = copy.createCommonJsProject({
roots: [ gcliHome + '/mozilla', gcliHome + '/lib' ]
});
var ignoreFilter = createIgnoreFilter([
'gcli/ui/inputter.css',
'gcli/ui/menu.css',
'gcli/ui/arg_fetch.css',
'gcli/commands/help.css'
]);
// Package the JavaScript
copy({
source: [
'mozilla/build/prefix-gcli.jsm',
'mozilla/build/console.js',
copy.getMiniRequire(),
// This list of dependencies should be the same as in suffix-gcli.jsm
{ project: project, require: [ 'gcli/index' ] },
'mozilla/build/suffix-gcli.jsm'
],
filter: [ ignoreFilter, copy.filter.moduleDefines ],
dest: (destDir ? destDir + jsmDir : 'built/ff') + '/gcli.jsm'
});
// Package the test files
project.assumeAllFilesLoaded();
var sources = copy.createDataObject();
copy({
// This list of dependencies should be the same as in suffix-test.js
source: { project: project, require: [ 'gclitest/index' ] },
filter: copy.filter.moduleDefines,
dest: sources
});
// This has to be done in 2 steps because createUndefineFunction uses
// project.currentFiles which is populated by using the project as a source
copy({
source: [
'mozilla/build/prefix-test.js',
sources,
createUndefineFunction(project),
'mozilla/build/suffix-test.js'
],
dest: (destDir ? destDir + testDir : 'built/ff') + '/browser_gcli_web.js'
});
// Package the CSS
copy({
source: [
'mozilla/build/license-block.txt',
{ value: '\n/* From: $GCLI/mozilla/gcli/ui/gcliterm.css */\n' },
'mozilla/gcli/ui/gcliterm.css',
{ value: '\n/* From: $GCLI/mozilla/gcli/ui/gcliterm-winstripe.css */\n' },
'mozilla/gcli/ui/gcliterm-winstripe.css',
{ value: '\n/* From: $GCLI/lib/gcli/ui/arg_fetch.css */\n' },
'lib/gcli/ui/arg_fetch.css',
{ value: '\n/* From: $GCLI/lib/gcli/ui/menu.css */\n' },
'lib/gcli/ui/menu.css',
{ value: '\n/* From: $GCLI/lib/gcli/ui/inputter.css */\n' },
'lib/gcli/ui/inputter.css',
{ value: '\n/* From: $GCLI/lib/gcli/commands/help.css */\n' },
'lib/gcli/commands/help.css'
],
filter: removeNonMozPrefixes,
dest: (destDir ? destDir + winCssDir : 'built/ff') + '/gcli.css'
});
copy({
source: [
'mozilla/build/license-block.txt',
{ value: '\n/* From: $GCLI/mozilla/gcli/ui/gcliterm.css */\n' },
'mozilla/gcli/ui/gcliterm.css',
{ value: '\n/* From: $GCLI/mozilla/gcli/ui/gcliterm-pinstripe.css */\n' },
'mozilla/gcli/ui/gcliterm-pinstripe.css',
{ value: '\n/* From: $GCLI/lib/gcli/ui/arg_fetch.css */\n' },
'lib/gcli/ui/arg_fetch.css',
{ value: '\n/* From: $GCLI/lib/gcli/ui/menu.css */\n' },
'lib/gcli/ui/menu.css',
{ value: '\n/* From: $GCLI/lib/gcli/ui/inputter.css */\n' },
'lib/gcli/ui/inputter.css',
{ value: '\n/* From: $GCLI/lib/gcli/commands/help.css */\n' },
'lib/gcli/commands/help.css'
],
filter: removeNonMozPrefixes,
dest: (destDir ? destDir + pinCssDir : 'built/ff') + '/gcli.css'
});
copy({
source: [
'mozilla/build/license-block.txt',
{ value: '\n/* From: $GCLI/mozilla/gcli/ui/gcliterm.css */\n' },
'mozilla/gcli/ui/gcliterm.css',
{ value: '\n/* From: $GCLI/mozilla/gcli/ui/gcliterm-gnomestripe.css */\n' },
'mozilla/gcli/ui/gcliterm-gnomestripe.css',
{ value: '\n/* From: $GCLI/lib/gcli/ui/arg_fetch.css */\n' },
'lib/gcli/ui/arg_fetch.css',
{ value: '\n/* From: $GCLI/lib/gcli/ui/menu.css */\n' },
'lib/gcli/ui/menu.css',
{ value: '\n/* From: $GCLI/lib/gcli/ui/inputter.css */\n' },
'lib/gcli/ui/inputter.css',
{ value: '\n/* From: $GCLI/lib/gcli/commands/help.css */\n' },
'lib/gcli/commands/help.css'
],
filter: removeNonMozPrefixes,
dest: (destDir ? destDir + gnomeCssDir : 'built/ff') + '/gcli.css'
});
// Package the i18n strings
copy({
source: [ 'lib/gcli/nls/strings.js', 'mozilla/gcli/commands/strings.js' ],
filter: tweakI18nStrings,
dest: (destDir ? destDir + propsDir : 'built/ff') + '/gcli.properties'
});
}
/**
* Sometimes we want to exclude modules from the output.
* This replaces the contents of a named set of modules with an empty string.
*/
function createIgnoreFilter(ignoredModules) {
var filter = function(data, location) {
function checkIgnored(ignoredModule) {
return location.path === ignoredModule;
}
if (location != null && ignoredModules.some(checkIgnored)) {
return '';
}
return data;
};
filter.onRead = true;
return filter;
}
/**
* Filter index.html to:
* - Make links relative, we flatten out the scripts directory
* - Replace require.js with the built GCLI script file
* - Remove the RequireJS configuration
*/
function tweakIndex(data) {
return data
.replace(/scripts\/es5-shim.js/, 'es5-shim.js')
.replace(/scripts\/require.js/, 'gcli-uncompressed.js')
.replace(/\s*require\([^;]*;\n/, '');
}
/**
* Regular expression that removes the header/footer from a nls strings file.
* If/when we revert to RequireJS formatted strings files, we'll need to update
* this.
* See lib/gcli/nls/strings.js for an example
*/
var outline = /root: {([^}]*)}/g;
/**
* Regex to match a set of single line comments followed by a name:value
* We run this to fund the list of strings once we've used 'outline' to get the
* main body.
* See lib/gcli/nls/strings.js for an example
*/
var singleString = /((\s*\/\/.*\n)+)\s*([A-z.]+):\s*'(.*)',?\n/g;
/**
* Filter to turn GCLIs l18n script file into a Firefox l10n strings file
*/
function tweakI18nStrings(data) {
// Rip off the CommonJS header/footer
var output = '';
data.replace(outline, function(m, inner) {
// Remove the trailing spaces
output += inner.replace(/ *$/, '');
});
if (output === '') {
console.error('Mismatch in lib/gcli/nls/strings.js');
process.exit(1);
}
// Convert each of the string definitions
output = output.replace(singleString, function(m, note, x, name, value) {
note = note.replace(/\n? *\/\/ */g, ' ')
.replace(/^ /, '')
.replace(/\n$/, '');
note = 'LOCALIZATION NOTE (' + name + '): ' + note;
var lines = '# ' + wordWrap(note, 77).join('\n# ') + '\n';
// Unescape JavaScript strings so they're property values
value = value.replace(/\\\\/g, '\\')
.replace(/\\'/g, '\'');
return lines + name + '=' + value + '\n\n';
});
return '' +
'# LOCALIZATION NOTE These strings are used inside the Web Console\n' +
'# command line which is available from the Web Developer sub-menu\n' +
'# -> \'Web Console\'.\n' +
'# The correct localization of this file might be to keep it in\n' +
'# English, or another language commonly spoken among web developers.\n' +
'# You want to make that choice consistent across the developer tools.\n' +
'# A good criteria is the language in which you\'d find the best\n' +
'# documentation on web development on the web.\n' +
'\n' + output;
}
/**
* Return an input string split into lines of a given length
*/
function wordWrap(input, length) {
// LOOK! Over there! Is it an airplane?
var wrapper = new RegExp('.{0,' + (length - 1) + '}([ $|\\s$]|$)', 'g');
return input.match(wrapper).slice(0, -1).map(function(s) {
return s.replace(/ $/, '');
});
}
/**
* Hack to remove the '-vendorprefix' definitions. This currently works to
* remove -webkit, -ms and -op from CSS values and properties.
*/
function removeNonMozPrefixes(data) {
return data
.replace(/\n?\s*[-a-z]*:\s*-(webkit|op|ms)-[-a-z]*\s*;[ \t]*/g, '')
.replace(/\n?\s*-(webkit|op|ms)-[-a-z]*:\s*[^;]*\s*;[ \t]*/g, '');
}
/**
* A function to create an undefine function that undoes the effect of defining
* all the modules in the given project. This is useful in firefox tests that
* need to undo all the changes they make to avoid memleak detection tests
* @param project The commonjs the we're aggregating together.
*/
function createUndefineFunction(project) {
// This is slightly evil because it digs into the guts of a project
var modules = Object.keys(project.currentModules);
var undefine = '\nfunction undefine() {\n';
modules.forEach(function(module) {
undefine += ' delete define.modules[\'' + module + '\'];\n';
});
undefine += '\n';
modules.forEach(function(module) {
undefine += ' delete define.globalDomain.modules[\'' + module + '\'];\n';
});
undefine += '}\n';
return { value: undefine };
}
/**
* Run the test suite inside node
*/
function test() {
// It's tempting to use RequireJS from npm, however that would break
// running GCLI in Firefox just by opening index.html
var requirejs = require('./scripts/r.js');
requirejs.config({
nodeRequire: require,
paths: { 'text': 'scripts/text', 'i18n': 'scripts/i18n' },
packagePaths: {
'lib': [
{ name: 'gcli', main: 'index', lib: '.' },
{ name: 'test', main: 'index', lib: '.' },
{ name: 'gclitest', main: 'index', lib: '.' },
{ name: 'demo', main: 'index', lib: '.' }
]
}
});
require('jsdom').jsdom.env({
html: fs.readFileSync(gcliHome + '/index.html').toString(),
src: [
fs.readFileSync(gcliHome + '/scripts/html5-shim.js').toString()
],
features: {
QuerySelector: true
},
done: requirejs('gclitest/nodeIndex').run
});
}
/**
* Serve '.' to http://localhost:9999/
*/
function serve() {
var connect = require('connect');
var logger = connect.logger();
var static = connect.static(gcliHome, { maxAge: 0 });
console.log('Serving GCLI to http://localhost:9999/');
connect(logger, static).listen(9999);
}
// Now everything is defined properly, start working
main();