forked from mozilla/butter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.js
executable file
·400 lines (310 loc) · 10.8 KB
/
make.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
#!/usr/bin/env node
var path = require( "path" ),
normalize = function( p ){ return '"' + path.normalize( p ) + '"'; },
join = path.join,
// Make Windows happy, use `node <path>`
nodeExec = function( p ){ return 'node "' + p + '"'; },
SLICE = Array.prototype.slice,
JSLINT = nodeExec( normalize( "./node_modules/jshint/bin/hint" ) ),
CSSLINT = nodeExec( normalize( "./node_modules/csslint/cli.js" ) ),
UGLIFY = nodeExec( normalize( "./node_modules/uglify-js/bin/uglifyjs" ) ),
RJS = nodeExec( normalize( "./node_modules/requirejs/bin/r.js" ) ),
LESS = nodeExec( normalize( "./node_modules/less/bin/lessc" ) ),
DOX = normalize( "./tools/dox.py" ),
SRC_DIR = 'src',
TEMPLATES_DIR = 'templates',
DIST_DIR = 'dist',
DOCS_DIR = 'docs',
TEST_DIR = 'test',
DEFAULT_CONFIG = './src/default-config',
CSS_DIR = 'css',
BUTTER_LESS_FILE = join( CSS_DIR, "butter.ui.less" ),
BUTTER_CSS_FILE_COMMENT = "/* THIS FILE WAS GENERATED BY A TOOL, DO NOT EDIT. SEE " +
BUTTER_LESS_FILE + " */",
BUTTER_CSS_FILE = join( CSS_DIR, "/butter.ui.css" ),
BUTTERED_POPCORN = join( DIST_DIR, '/buttered-popcorn.js' ),
BUTTERED_POPCORN_MIN = join( DIST_DIR, '/buttered-popcorn.min.js' ),
PACKAGE_NAME = "butter";
require('shelljs/make');
function checkCSS() {
echo('### Linting CSS files');
var dirs = SLICE.call( arguments ).join( ' ' );
// see cli.js --list-rules.
var warnings = [
// "important",
// "adjoining-classes",
// "duplicate-background-images",
// "qualified-headings",
// "fallback-colors",
// "empty-rules",
// "shorthand",
// "overqualified-elements",
// "import",
// "regex-selectors",
// "rules-count",
// "font-sizes",
// "universal-selector",
// "unqualified-attributes",
"zero-units"
].join(",");
var errors = [
"known-properties",
"compatible-vendor-prefixes",
"display-property-grouping",
"duplicate-properties",
"errors",
"gradients",
"font-faces",
"floats",
"vendor-prefix"
].join(",");
exec(CSSLINT + ' --warnings=' + warnings +
' --errors=' + errors +
' --quiet --format=compact' +
' ' + dirs);
}
function checkJS(){
// Takes a string or an array of strings referring to directories.
echo('### Linting JS files');
var dirs = SLICE.call( arguments );
// Get all js and json files in dirs
var files = "";
[ /\.js$/, /\.json$/ ].forEach( function( regexp ){
files += find( dirs ).filter( function( file ) {
return file.match( regexp );
}).join(' ') + ' ';
});
// jshint with non-errors plus linting of json files
exec(JSLINT + ' ' + files + ' --show-non-errors --extra-ext json');
}
target.all = function() {
target.submodules();
target.check();
target.build();
};
target.clean = function() {
rm('-fr', DIST_DIR);
};
target.dist = function() {
mkdir('-p', DIST_DIR);
};
target.submodules = function() {
echo('### Updating git submodules');
exec('git submodule update --init --recursive');
};
target.docs = function() {
echo('### Creating documentation from src...');
mkdir('-p', DOCS_DIR);
var files = find( SRC_DIR ).filter( function( file ) {
return file.match(/\.js$/);
});
var docTypes = [
'md'
];
for (var i = files.length - 1; i >= 0; i--) {
echo('### Processing documentation for ' + files[i]);
for (var j = docTypes.length - 1; j >= 0; j--) {
var newFileName = DOCS_DIR + '/' + files[i].substring(4).replace(/\//g, '-').replace(/\.js$/, '.' + docTypes[j]),
command = 'python ' + DOX + ' -t ' + docTypes[j] + ' -o '+ newFileName + ' -i ' + files[i];
exec(command);
};
};
};
target.check = function() {
checkJS( SRC_DIR );
checkCSS( CSS_DIR );
};
target['check-templates'] = function() {
checkJS( TEMPLATES_DIR );
checkCSS( TEMPLATES_DIR );
};
target['check-css'] = function( dirs ) {
checkCSS( CSS_DIR );
};
target['check-lint'] = function( dir ) {
checkJS( SRC_DIR );
};
target['check-tests'] = function( dir ) {
checkJS( TEST_DIR );
};
// If compress is true, crush CSS down, otherwise leave expanded.
function lessToCSS( compress ){
echo( "### Building CSS using LESS (" +
( compress ? "with" : "without" ) +
" compression)" );
var args = compress ? " --yui-compress " : " ",
result = exec(LESS + args + BUTTER_LESS_FILE, {silent:true});
if( result.code === 0 ){
var css = BUTTER_CSS_FILE_COMMENT + "\n\n" + result.output;
css.to( BUTTER_CSS_FILE );
target['check-css']();
} else {
echo( result.output );
}
}
target.css = function() {
// Leave CSS expanded if building in tree (for debugging)
lessToCSS( false );
};
function build( version ){
echo('### Building butter');
target.clean();
target.dist();
exec(RJS + ' -o tools/build.js');
exec(RJS + ' -o tools/build.optimized.js');
// Stamp Butter.version with supplied version, or git info
if( !version ){
version = exec('git describe',
{silent:true}).output.replace(/\r?\n/m, "");
}
sed('-i', '@VERSION@', version, 'dist/butter.js');
sed('-i', '@VERSION@', version, 'dist/butter.min.js');
// Compress CSS for deployment
lessToCSS( true );
cp( BUTTER_CSS_FILE, DIST_DIR );
}
target.build = function(){
build();
};
target.server = function() {
echo('### Serving butter');
cd('cornfield');
exec('node app.js', { async: true });
};
target.package = function() {
echo('### Making Butter Package');
target.build();
cp('-R', 'resources', DIST_DIR);
cp('-R', 'templates', DIST_DIR);
echo('### Creating butter.zip');
cd(DIST_DIR);
exec('zip -r ' + PACKAGE_NAME + '.zip ' + ls('.').join(' '));
};
target['buttered-popcorn'] = function(){
echo('### Making Combined Popcorn for Butter: Buttered Popcorn');
var defaultConfig = require( DEFAULT_CONFIG ),
popcornDir = defaultConfig.dirs['popcorn-js'].replace( '{{baseDir}}', './' ),
players = defaultConfig.player.players,
plugins = defaultConfig.plugin.plugins,
popcornFiles = [];
// Popcorn License Header
popcornFiles.push( popcornDir + '/LICENSE_HEADER' );
// classList shim
popcornFiles.push( './tools/classlist-shim.js' );
// popcorn IE8 shim
popcornFiles.push( popcornDir + '/ie8/popcorn.ie8.js' );
// popcorn.js
popcornFiles.push( popcornDir + '/popcorn.js' );
// plugins
plugins.forEach( function( plugin ){
popcornFiles.push( plugin.path.replace( '{{baseDir}}', './' ) );
});
// module for baseplayer
popcornFiles.push( popcornDir + '/modules/player/popcorn.player.js' );
// players
players.forEach( function( player ){
popcornFiles.push( player.path.replace( '{{baseDir}}', './' ) );
});
// Stamp Popcorn.version with the git commit sha we are using
var cwd = pwd();
cd( popcornDir );
var popcornVersion = exec('git describe',
{silent:true}).output.replace(/\r?\n/m, "");
cd( cwd );
// Write out dist/buttered-popcorn.js
cat( popcornFiles ).to( BUTTERED_POPCORN );
sed('-i', '@VERSION', popcornVersion, BUTTERED_POPCORN);
// Write out dist/buttered-popcorn.min.js
exec( UGLIFY + ' --output ' + BUTTERED_POPCORN_MIN + ' ' + BUTTERED_POPCORN );
};
target.release = function() {
echo('### Making Butter Release');
// To pass a release version number, use:
// $ VERSION=0.5 node make release
var version = env['VERSION'];
if( !version ){
console.log( "ERROR: Must provide a version when building a release: VERSION=XXX node make release" );
return;
}
build( version );
// Build buttered-popcorn.js
target['buttered-popcorn']();
// Copy over templates and other resources
cp('-R', 'resources', DIST_DIR);
cp('-R', 'templates', DIST_DIR);
echo('### Creating butter.zip');
cd(DIST_DIR);
exec('zip -r ' + PACKAGE_NAME + '.zip ' + ls('.').join(' '));
};
target.beautify = function( a ) {
echo('### Beautifying butter');
cd('tools');
exec('./beautify.sh');
};
target.test = function() {
var unbeautified = [ "if.js", "for.js", "while.js", "array.js", "function.js", "object.js", "comments.js", "eolspace.js" ],
beautified = [ "if.expected.js", "for.expected.js", "while.expected.js", "array.expected.js", "function.expected.js", "object.expected.js", "comments.expected.js", "eolspace.expected.js" ],
result;
echo('### Testing Beautifier');
for( var i = 0, l = unbeautified.length; i < l; i++ ) {
result = exec('bash test/beautifier/test.sh ' + unbeautified[ i ] + ' ' + beautified[ i ]);
rm('tmp.txt');
// checking against a length of 1 because if the output is empty a newline character gets returned
if( result.output.length === 1 ) {
echo(unbeautified[ i ] + ' was beautified correctly');
} else {
echo(unbeautified[ i ] + ' did not beautify correctly');
}
}
};
target.storycamp = function(){
echo('### Making single file version of Butter + Popcorn (use UNMINIFIED=1 for unminified)');
// To get unminified butter.js, use the UNMINIFIED env variable:
// $ UNMINIFIED=1 node make storycamp
var unminified = env['UNMINIFIED'] === "1";
build( 'storycamp' );
target['buttered-popcorn']();
var storyCamp = 'butter.js',
storyCampMin = 'butter.min.js';
function makeButterJS( keepMe, deleteMe ){
echo( '### Cleaning temp files' );
cd( DIST_DIR );
rm( '-f', deleteMe );
// Mirror layout in butter/ so templates are happy, renaming to src/butter.js
mkdir( 'src' );
mv( keepMe, './src/butter.js' );
}
var cwd = pwd();
// Depending on whether we want minified source, keep one, delete one.
if( unminified ){
makeButterJS( storyCamp, storyCampMin );
} else {
// Write out dist/storycamp-butter.min.js
exec( UGLIFY + ' --output ' + DIST_DIR + '/' + storyCampMin + ' ' + DIST_DIR + '/' + storyCamp );
makeButterJS( storyCampMin, storyCamp );
}
// Move css files into dist/css
mkdir('css');
mv('*.css', './css');
// Copy other assets over
cd(cwd);
cp('-R', 'resources', DIST_DIR);
cp('-R', 'templates', DIST_DIR);
cp('-R', 'cornfield', DIST_DIR);
// Copy the popcorn test videos over
mkdir('-p', './dist/external/popcorn-js/test');
cp('external/popcorn-js/test/trailer.*', './dist/external/popcorn-js/test');
// Copy the rest of the popcorn plugins over in case templates look for them
mkdir('-p', './dist/external/popcorn-js/plugins');
cp('-R', 'external/popcorn-js/plugins/*', './dist/external/popcorn-js/plugins');
// Export will need a version of popcorn.js where the templates expect it
// at dist/external/popcorn-js/popcorn.js
cd( DIST_DIR );
if( unminified ){
mv( 'buttered-popcorn.js', './external/popcorn-js/popcorn.js' );
rm( '-f', 'buttered-popcorn.min.js' );
} else {
mv( 'buttered-popcorn.min.js', './external/popcorn-js/popcorn.js' );
rm( '-f', 'buttered-popcorn.js' );
}
};