-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathGruntfile.js
330 lines (294 loc) · 9.33 KB
/
Gruntfile.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
"use strict";
var async = require( "async" ),
fs = require( "node:fs" ),
fsp = require( "node:fs/promises" ),
path = require( "node:path" );
module.exports = function( grunt ) {
grunt.loadNpmTasks( "grunt-check-modules" );
grunt.loadNpmTasks( "grunt-contrib-clean" );
grunt.loadNpmTasks( "grunt-contrib-copy" );
grunt.loadNpmTasks( "grunt-contrib-handlebars" );
grunt.loadNpmTasks( "grunt-eslint" );
grunt.initConfig( {
pkg: grunt.file.readJSON( "package.json" ),
handlebars: {
options: {
// Use basename as the key for the precompiled object.
processName: function( filepath ) {
return path.basename( filepath );
},
// Wrap preprocessed template functions in Handlebars.template function.
wrapped: true
},
compile: {
files: {
"tmp/app/template/download.js": [ "template/download/components.html", "template/download/service_status.html", "template/download/theme.html" ],
"tmp/app/template/themeroller.js": [ "template/themeroller/rollyourown.html", "template/themeroller/_rollyourown_group_corner.html", "template/themeroller/_rollyourown_group_default.html", "template/themeroller/_rollyourown_group_dropshadow.html", "template/themeroller/_rollyourown_group_font.html", "template/themeroller/_rollyourown_group_modaloverlay.html" ]
}
}
},
eslint: {
all: [ "*.js", "test/*js", "lib/**/*.js", "app/src/*.js" ]
},
copy: {
appExternalFarbtastic: {
src: "external/farbtastic/farbtastic.css",
dest: "app/dist/external/farbtastic.css"
},
appImages: {
expand: true,
cwd: "app/src",
src: [ "images/**/*" ],
dest: "app/dist"
},
appImagesExternalFarbtastic: {
expand: true,
cwd: "external/farbtastic",
src: [ "marker.png", "mask.png", "wheel.png" ],
dest: "app/dist/images/farbtastic"
},
appStyles: {
expand: true,
cwd: "app/src",
src: [ "download.css", "themeroller.css" ],
dest: "app/dist"
}
},
minify: {
// DownloadBuilder minified frontend bundle
download: {
src: [ "node_modules/wolfy87-eventemitter/EventEmitter.js", "node_modules/handlebars/dist/handlebars.runtime.js", "tmp/app/template/download.js", "node_modules/lzma/src/lzma.js", "app/src/hash.js", "app/src/querystring.js", "app/src/model.js", "app/src/download.js" ],
dest: "app/dist/download.all.min.js"
},
// ThemeRoller minified frontend bundle
themeroller: {
src: [ "node_modules/wolfy87-eventemitter/EventEmitter.js", "node_modules/handlebars/dist/handlebars.runtime.js", "tmp/app/template/themeroller.js", "external/farbtastic/farbtastic.js", "node_modules/lzma/src/lzma.js", "app/src/hash.js", "app/src/querystring.js", "app/src/model.js", "app/src/themeroller.js" ],
dest: "app/dist/themeroller.all.min.js"
},
external_lzma_worker: {
src: [ "node_modules/lzma/src/lzma_worker.js" ],
dest: "app/dist/external/lzma_worker.min.js"
}
},
clean: {
appDist: [ "app/dist" ]
}
} );
// local tasks
grunt.loadTasks( "grunt-tasks" );
function log( callback, successMsg, errorMsg ) {
return function( error, result, code ) {
if ( error && errorMsg ) {
grunt.log.error( errorMsg );
grunt.log.error( error );
grunt.log.error( result.stdout );
grunt.log.error( result.stderr );
} else if ( !error && successMsg ) {
grunt.log.ok( successMsg );
}
callback( error, result, code );
};
}
function cloneOrFetch( callback ) {
if ( fs.existsSync( "tmp/jquery-ui" ) ) {
grunt.log.writeln( "Fetch updates for jquery-ui repo" );
async.series( [
// Fetch branch heads (even if not referenced by tags), see c08cf67.
function( callback ) {
grunt.util.spawn( {
cmd: "git",
args: [ "fetch" ],
opts: {
cwd: "tmp/jquery-ui"
}
}, callback );
},
// Fetch tags not referenced by heads. Yes, we need both.
function( callback ) {
grunt.util.spawn( {
cmd: "git",
args: [ "fetch", "-t" ],
opts: {
cwd: "tmp/jquery-ui"
}
}, callback );
}
], log( callback, "Fetched repo", "Error fetching repo" ) );
} else {
grunt.log.writeln( "Cloning jquery-ui repo" );
grunt.util.spawn( {
cmd: "git",
args: [ "clone", "https://github.com/jquery/jquery-ui.git", "jquery-ui" ],
opts: {
cwd: "tmp"
}
}, log( callback, "Cloned repo", "Error cloning repo" ) );
}
}
function checkout( jqueryUi ) {
var ref = jqueryUi.ref;
return function( callback ) {
grunt.log.writeln( "Checking out jquery-ui branch/tag: " + ref );
grunt.util.spawn( {
cmd: "git",
args: [ "checkout", "-f", ref ],
opts: {
cwd: "tmp/jquery-ui"
}
}, log( callback, "Done with checkout", "Error checking out" ) );
};
}
function copy( jqueryUi ) {
var ref = jqueryUi.ref;
return function( callback ) {
var version = grunt.file.readJSON( "tmp/jquery-ui/package.json" ).version;
grunt.file.mkdir( "jquery-ui" );
async.series( [
function( next ) {
if ( fs.existsSync( "jquery-ui/" + ref ) ) {
grunt.log.writeln( "Cleaning up existing jquery-ui/" + ref );
const rmCallback = log( next, "Cleaned", "Error cleaning" );
fsp.rm( `jquery-ui/${ ref }`, { recursive: true, force: true } )
.then( () => {
rmCallback( null, "OK", 0 );
} )
.catch( error => {
rmCallback( error, null, 1 );
} );
} else {
next();
}
},
function() {
var from = "tmp/jquery-ui",
to = "jquery-ui/" + ref;
grunt.log.writeln( "Copying jQuery UI " + version + " over to jquery-ui/" + ref );
try {
grunt.file.recurse( from, function( filepath ) {
// Skip files from the `.git` directory; we don't need them,
// there may be a lot of them and them may include IPC files
// that cannot be copied.
if ( filepath.indexOf( "/.git/" ) === -1 ) {
grunt.file.copy( filepath, filepath.replace( new RegExp( "^" + from ), to ) );
}
} );
} catch ( e ) {
grunt.log.error( "Error copying", e.toString() );
return callback( e );
}
grunt.log.ok( "Done copying" );
callback();
}
] );
};
}
function prepareAll( callback ) {
var config = require( "./lib/config" )();
async.forEachSeries( config.jqueryUi, function( jqueryUi, callback ) {
async.series( [
checkout( jqueryUi ),
copy( jqueryUi )
], function( err ) {
// Go to next ref
callback( err );
} );
}, function( err ) {
// Done
callback( err );
} );
}
function packagerZip( packageModule, zipBasedir, themeVars, folder, jqueryUi, callback ) {
var Package = require( packageModule );
var Packager = require( "node-packager" );
var filename = path.join( folder, zipBasedir + ".zip" );
grunt.log.ok( "Building \"" + filename + "\"" );
if ( fs.existsSync( filename ) ) {
grunt.log.warn( filename + "\" already exists. Skipping..." );
return callback();
}
var target = fs.createWriteStream( filename );
var packager = new Packager( jqueryUi.files().cache, Package, {
components: jqueryUi.components().map( function( component ) {
return component.name;
} ),
jqueryUi: jqueryUi,
themeVars: themeVars
} );
packager.ready.then( function() {
packager.toZip( target, {
basedir: zipBasedir
}, function( error ) {
if ( error ) {
return callback( error );
}
callback();
} );
} );
}
function buildPackages( folder, callback ) {
var JqueryUi = require( "./lib/jquery-ui" ),
ThemeGallery = require( "./lib/themeroller-themegallery" );
// For each jQuery UI release specified in the config file:
async.forEachSeries( JqueryUi.all(), function( jqueryUi, callback ) {
async.series( [
// (a) Build jquery-ui-[VERSION].zip;
function( callback ) {
packagerZip( "./lib/package", "jquery-ui-" + jqueryUi.pkg.version,
new ThemeGallery( jqueryUi )[ 0 ].vars, folder, jqueryUi, callback );
},
// (b) Build themes package jquery-ui-themes-[VERSION].zip;
function( callback ) {
packagerZip( "./lib/package-themes", "jquery-ui-themes-" + jqueryUi.pkg.version,
null, folder, jqueryUi, callback );
}
], function( error ) {
if ( error ) {
grunt.log.error( error.message );
}
return callback();
} );
}, callback );
}
grunt.registerTask( "default", [ "check-modules", "eslint", "test" ] );
grunt.registerTask( "build-app", [ "clean", "handlebars", "copy", "minify" ] );
grunt.registerTask( "build-packages", "Builds zip package of each jQuery UI release specified in config file with all components and lightness theme, inside the given folder", function( folder ) {
var done = this.async();
buildPackages( folder, function( err ) {
// Make grunt to quit properly. Here, a proper error message should have been printed already.
// 1: true on success, false on error
done( !err /* 1 */ );
} );
} );
grunt.registerTask( "mkdirs", "Create directories", function() {
[ "log", "tmp" ].forEach( function( dir ) {
if ( !fs.existsSync( dir ) ) {
grunt.file.mkdir( dir );
}
} );
} );
grunt.registerTask( "prepare", [
"check-modules",
"eslint",
"mkdirs",
"prepare-jquery-ui",
"build-app"
] );
grunt.registerTask( "prepare-jquery-ui", "Fetches and builds jQuery UI releases specified in config file", function() {
var done = this.async();
async.series( [
cloneOrFetch,
prepareAll
], function( err ) {
// Make grunt to quit properly. Here, a proper error message should have been printed already.
// 1: true on success, false on error
done( !err /* 1 */ );
} );
} );
grunt.registerTask( "test", "Runs npm test", function() {
var done = this.async();
grunt.util.spawn( {
cmd: "npm",
args: [ "test" ]
}, done );
} );
};