This repository has been archived by the owner on Mar 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
411 lines (366 loc) · 10.5 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
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
/* jshint node: true, laxbreak: true */
var _ = require('lodash'),
Q = require('q'),
path = require('path'),
fs = require('fs'),
grunt = require('grunt'),
recess = require('recess'),
Lang = require('./lang/messages');
// Grunt tasks don't need heavy performance, so make errors more helpful.
Q.longStackSupport = true;
module.exports = function(grunt) {
"use strict";
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
banner:
"/*!\n" +
" * Manga Performer v<%= pkg.version %>\n" +
" *\n" +
" * A well featured web based comic/manga reader.\n" +
" *\n" +
" * Copyright © 2013 – <%= pkg.author.name %>\n" +
// Start using this second line when 2013 ends
// " * Copyright © 2013-<%= grunt.template.today('yyyy') %> – <%= pkg.author.name %>\n" +
"<%= pkg.contributors.map(function(author) {" +
" return ' * @author ' + author.name + (author.url ? ' (' + author.url + ')' : '') + '\\n';" +
" }).join('') %>" +
" *\n" +
" * Manga Performer is dual-licensed under the following licenses:\n" +
"<%= pkg.licenses.map(function(license) {" +
" return ' * @license ' + license.url + ' ' + license.type + '\\n';" +
" }).join('') %>" +
" *\n" +
" * Requires:\n" +
" * - Underscore.js or Lo-Dash\n" +
" * - jQuery (jquery.js) >=1.8\n" +
" * - jQuery Hotkeys Plugin (jquery.hotkeys.js)\n" +
" * - Hammer.js + jquery.hammer.js\n" +
" */\n",
fnwrapper: {
open: '(function( window, $, _, undefined ) {\n' +
' "use strict";',
close: '})( window, jQuery, _ );'
},
clean: {
dist: ['dist']
},
copy: {
icons: {
expand: true,
cwd: 'icons/',
src: ['*.svg', '*.png'],
dest: 'dist/icons/'
},
jshintrc: {
src: ['dist.jshintrc'],
dest: 'dist/.jshintrc'
}
},
'mangaperformer-js': {
options: {
banner: "<%= banner %><%= fnwrapper.open %>\n",
footer: "\n\twindow.MangaPerformer = MangaPerformer;\n\n" +
"<%= fnwrapper.close %>\n"
},
dist: {
src: [
'src/depcheck.js',
'src/functions.js',
'src/namespace.js',
'src/supports.js',
'src/event.js',
'src/i18n.js',
'src/extractor.js',
'src/page.js',
'src/manga.js',
'src/preloader.js',
'src/ui.js',
'src/ui/fullscreen.js',
'src/ui/autohide.js',
'src/ui/component.js',
'src/ui/button.js',
'src/ui/tooltip.js',
'src/ui/slider.js',
'src/ui/paneslider.js',
'src/ui/layout.js',
'src/ui/interface.js',
'src/ui/readerinterface.js',
'src/viewport.js',
'src/pageoverview.js',
'src/performer.js'
],
dest: 'dist/<%= pkg.name %>.js'
}
},
'mangaperformer-lang': {
dist: {
options: {
skip: ['en'],
destPattern: 'mangaperformer.lang.{code}.js'
},
dest: 'dist'
},
},
jshint: {
options: {
jshintrc: true,
},
src: ['src/*.js', 'src/**/*.js'],
dist: ['dist/<%= pkg.name %>.js'],
grunt: 'Gruntfile.js'
},
watch: {
dist: {
files: [
'src/*.js',
'src/**/*.js',
'src/*.less',
'icons/*.svg',
'lang/*.yaml'
],
tasks: ['dist']
},
'meta-sync': {
files: [
'package.json',
],
tasks: ['meta-sync']
}
}
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerMultiTask('mangaperformer-js', 'Build mangaperformer.js file.', function() {
var f = this.files,
options = this.options({
banner: "",
footer: ""
}),
done = this.async();
var promises = this.files.map(function(f) {
var promises = [
// Banner
Q.when(options.banner),
Q.fcall(function() {
var promises = _.flatten([
// Concatenate JS files
f.src.map(function(filename) {
return Q.nfcall(fs.readFile, filename, "utf-8")
.then(function(src) {
return src.replace(/^"use strict";\n?/, '');
});
}),
// Output the list of known language codes.
Q.fcall(function() {
return [
'\n',
'// Valid language codes\n',
'i18n.languages = ',
JSON.stringify(
Lang.listLanguages()
.map(function(code) { return code.toLowerCase(); })
.sort(),
undefined, '\t'),
';\n'
].join('');
}),
// Output the fingerprints for known language codes
Q.fcall(function() {
var languageFingerprints = {};
Lang.listLanguages()
.forEach(function(code) {
if ( code === 'en' ) { return; }
languageFingerprints[code.toLowerCase()] = Lang.load(code).fingerprint;
});
return [
'\n',
'// Language fingerprints\n',
'i18n.languageFingerprints = ',
JSON.stringify(languageFingerprints, undefined, '\t'),
';\n'
].join('');
}),
// Build the English i18n data then embed it in a JavaScript variable.
Q.fcall(function() {
return [
'\n',
'// Canonical English message texts\n',
'i18n.messages.en = ',
JSON.stringify(Lang.load('en').messages, undefined, '\t'),
';\n'
].join('');
}),
// Embed the mangaperformer.css file inside of a JavaScript variable.
Q.fcall(function() {
var filename = 'src/mangaperformer.less';
var deffered = Q.defer();
var opts = {
compile: true,
compress: false
};
recess(filename, opts, function(err, obj) {
if ( err ) {
console.warn(err[0]);
deffered.reject(new Error(err[0].message));
return;
}
deffered.resolve([
'\n',
'// ' + filename + '\n',
'var MANGAPERFORMER_CSS = ',
JSON.stringify(String(obj[0].output)),
';\n'
].join(''));
});
return deffered.promise;
}),
// Output the contents of the icons/*.svg files in data: URI strings.
Q.fcall(function() {
var icons = Object.create(null);
grunt.file.expand({}, 'icons/*.svg')
.forEach(function(filename) {
var name = path.basename(filename).replace(/\.svg$/, '');
var data = grunt.file.read(filename, { encoding: null });
icons[name] = 'data:image/svg+xml;base64,' + data.toString('base64');
});
return [
'\n',
'// Cleaned up SVG icons from icons/\n',
'var MANGAPERFORMER_ICONS = ',
JSON.stringify(icons, undefined, '\t'),
';\n'
].join('');
})
]);
return Q.all(promises)
.then(function(sources) {
var exports = [];
sources = sources
.join('')
.replace(/(\s*)\bexport\s+((?:function|var)\s+([_$0-9a-zA-Z]+))\b/gm,
function(m, a, b, name) {
exports.push(name);
return a + b;
});
sources += [
'\n',
'// Exports\n',
exports
.map(function(ident) {
return ['MangaPerformer.', ident, ' = ', ident, ';\n'].join('');
})
.join('')
].join('');
// Indent stuff between the banner/footer with a tab.
return sources
.replace(/^(?=.)/gm, '\t');
});
}),
// Footer
Q.when(options.footer)
];
return Q.all(promises)
.then(function(sources) {
grunt.file.write(f.dest, sources.join(''));
grunt.log.writeln('File "' + f.dest + '" created.');
});
});
Q.all(promises)
.then(function() {
done();
})
.fail(function(reason) {
done(reason instanceof Error ? reason : false);
})
.done();
});
grunt.registerMultiTask('mangaperformer-lang', 'Build language .js files.', function() {
var f = this.files,
options = this.options({
skip: [],
destPattern: '{code}.js'
});
options.skip = options.skip.map(function(code) {
return String(code).toLowerCase();
});
this.files.forEach(function(f) {
Lang.listLanguages()
.filter(function(code) {
return !_.contains(options.skip, code.toLowerCase());
})
.forEach(function(code) {
var lang = Lang.load(code),
src = [
'/*!',
lang.commentHeader
.replace(/\s*$/, '')
.split(/(?:\r\n|\r|\n)/)
.map(function(line) {
return line
.replace(/^#\s*/, '')
.replace(/^[Aa]uthor:\s*/, '@author ')
.replace(/^/, ' * ')
.replace(/\s$/, '')
// Just some random paranoia ;)
.replace(/\*\//g, '');
})
.join('\n'),
' */',
[
'MangaPerformer.i18n.messages[',
JSON.stringify(lang.code),
'] = ',
JSON.stringify(lang.messages, undefined, '\t'),
';'
].join(''),
'' // Trailing newline at end of file
].join('\n'),
dest = path.join(f.dest, options.destPattern.replace('{code}', lang.code));
grunt.file.write(dest, src);
grunt.log.writeln('File "' + dest + '" created.');
});
});
});
grunt.registerTask('dist:icons', ['copy:icons']);
grunt.registerTask('dist:jshintrc', ['copy:jshintrc']);
grunt.registerTask('dist:js', ['mangaperformer-js']);
grunt.registerTask('dist:lang', ['mangaperformer-lang']);
// Full distribution task.
grunt.registerTask('dist', ['clean:dist', 'dist:icons', 'dist:jshintrc', 'dist:js', 'dist:lang']);
// Sync metadata from package.json into bower.json
grunt.registerTask('meta-sync', 'Sync metadata from package.json into bower.json.', function() {
var pkg = grunt.file.readJSON('package.json');
var bower = grunt.file.readJSON('bower.json');
// Description
bower.description = pkg.description;
// Version
bower.version = pkg.version;
// Homepage
bower.homepage = pkg.homepage;
// Author and contributors
bower.authors = [pkg.author].concat(pkg.contributors).map(function(c) {
var author = {};
if ( c.name ) author.name = c.name;
if ( c.email )author.email = c.email;
if ( c.url ) author.homepage = c.url;
return author;
});
// License
bower.license = pkg.licenses.map(function(license) {
return license.url;
});
// Save
grunt.file.write('bower.json', JSON.stringify(bower, null, 2) + '\n');
grunt.log.writeln('File "bower.json" updated.');
});
// JSHint aliases
grunt.registerTask('lint:src', ['jshint:src']);
grunt.registerTask('lint:dist', ['jshint:dist']);
grunt.registerTask('lint:grunt', ['jshint:grunt']);
grunt.registerTask('lint', ['jshint']);
// Default task.
grunt.registerTask('default', ['dist', 'meta-sync']);
};