forked from quentro/jwdeveloper-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
301 lines (296 loc) · 9.57 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
module.exports = function (grunt) {
'use strict';
// initial grunt configuration
grunt.initConfig({
'pkg': grunt.file.readJSON('package.json'),
'config': grunt.file.readJSON('config.json'),
'clean': {
build: 'build/*',
tmp: 'tmp'
},
'connect': {
serve: {
options: {
hostname: '127.0.0.1',
port: 8000,
base: 'build',
useAvailablePort: true,
open: true,
liveReload: true
}
}
},
less: {
compile: {
options: {
paths: ['less']
},
files: {
'build/template-css/jw-demos.css': 'less/build.less',
}
}
},
watch: {
config: {
files: [
'demos/**/*.*'
],
tasks: [
'default'
]
}
},
});
// load grunt plugins
require('load-grunt-tasks')(grunt);
// grunt default task
grunt.registerTask('default', function() {
// config vars
var categories = grunt.config('config.categories'),
demos = {
'all': []
},
copy = [],
concat = {},
mustacheRender = [],
path = {
file: '//developer.jwplayer.com/',
href: '../../',
};
// sort array/object alphabetically on the `directory` property
function sortABC(a, b) {
var prop = 'directory';
var sortStatus = 0;
if (a[prop] < b[prop]) {
sortStatus = -1;
} else if (a[prop] > b[prop]) {
sortStatus = 1;
}
return sortStatus;
}
// loop categories and compile index and single pages for each child demo
for (var i = 0; i < categories.length; i++) {
// // if category is the full index
if (!categories[i].directory) continue;
// store category data in namespace
var cat = categories[i];
// namespace for category demo list
demos[cat.directory] = [];
// if directory does not exist even despite being in category list
if (!grunt.file.isDir('demos/' + cat.directory)) continue;
// scan category sub-directories to locate valid demos
grunt.file.recurse('demos/' + cat.directory,
function callback(absPath, rootDir, subDir, filename) {
// we are looking for a config file, which is require per demo
// when we find the config file, we process that directory as a demo
if (filename == 'config.json') {
// define demo-specific directory shortcuts
var srcDir = 'demos/' + cat.directory + '/' + subDir + '/';
var buildDir = 'build/' + cat.directory + '/' + subDir + '/';
// get demo config json
var demo = grunt.file.readJSON(srcDir + filename);
// if demo has apiCalls key, filter empty array values
if (demo.apiCalls) {
demo.apiCalls = demo.apiCalls.filter(Boolean);
} else {
demo.apiCalls = [];
}
// append demo obj with demo category and directory
demo['category'] = cat;
demo['directory'] = subDir;
// push demo data to both category and master demo list
demos[cat.directory].push(demo);
demos['all'].push(demo);
// concat config for demo js
grunt.file.write(buildDir + 'js/build.js', '');
concat[buildDir + 'js/build.js'] = srcDir + 'js/*.js'
// concat config for demo css
grunt.file.write(buildDir + 'css/build.css', '');
concat[buildDir + 'css/build.css'] = srcDir + 'css/*.css';
// copy any additional directories developer has included in demo
copy.push({
expand: true,
cwd: srcDir,
src: ['**/*', '!js/**/*', '!css/**/*', '!config.json', '!index.html'],
dest: buildDir
});
// mustache config for demo detail page
mustacheRender.push({
data: {
path: path,
html: '<%= grunt.file.read("' + srcDir + 'index.html") %>',
js: '\r\r<%= grunt.file.read("' + buildDir + 'js/build.js") %>\r',
css: buildDir + 'css/build.css',
category: demo.category,
directory: demo.directory,
title: demo.title,
description: demo.description,
license: demo.license,
isApiCalls: demo.apiCalls.length,
apiCalls: demo.apiCalls,
author: function() {
if (!demo.author || !demo.author.name) return null;
return {
name: demo.author.name,
githubUsername: demo.author.githubUsername || null,
email: demo.author.email || null
};
},
showCode: function() {
return typeof demo.showCode !== 'undefined' ? demo.showCode : true;
},
layout: function() {
if (!this.showCode()) return 'vertical';
return demo.layout || 'horizontal';
}
},
template: '_templates/single.mustache',
dest: buildDir + 'index.html'
});
}
});
// sort demos within this category
demos[cat.directory].sort(sortABC);
// mustache config for category demo index
mustacheRender.push({
data: {
indexType: 'category',
title: 'JW Player Demos & Code Examples',
description: categories[i].description,
path: path,
directory: cat.directory,
categories: function() {
var cats = [];
for (var i = 0; i < categories.length; i++) {
cats.push(categories[i]);
if (categories[i].directory == this.directory) {
cats[i]['current'] = true;
cats[i]['currentDescription'] = categories[i].description;
} else {
cats[i]['current'] = false;
cats[i]['currentDescription'] = null;
}
}
return cats;
},
demos: function() {
var catDemos = demos[this.directory];
for (var i = 0; i < catDemos.length; i++) {
var descLength = catDemos[i].description.length;
if (descLength > 70) {
catDemos[i].description = catDemos[i].description.substring(0, 65);
catDemos[i].description = catDemos[i].description.trim();
catDemos[i].description = catDemos[i].description.slice(-1) == '.' ?
catDemos[i].description + '..' : catDemos[i].description + '...';
}
}
return catDemos;
}
},
template: '_templates/category-index.mustache',
dest: 'build/' + cat.directory + '/index.html'
});
}
// sort complete list of demos
demos.all.sort(sortABC);
// mustache config for search index
mustacheRender.push({
data: {
indexType: 'search-results',
title: 'JW Player Demos & Code Examples',
description: 'Explore demos and code examples extending JW Player feature functionality.',
path: path,
directory: '',
categories: function() {
var cats = [];
for (var i = 0; i < categories.length; i++) {
cats.push(categories[i]);
if (categories[i].directory == this.directory) {
cats[i]['current'] = true;
} else {
cats[i]['current'] = false;
}
}
return cats;
}
},
template: '_templates/search.mustache',
dest: 'build/search/index.html'
});
// mustache config for complete demo index
mustacheRender.push({
data: {
indexType: 'all',
title: 'JW Player Demos & Code Examples',
description: 'Explore demos and code examples extending JW Player feature functionality.',
path: path.file,
directory: '',
categories: function() {
var cats = [];
for (var i = 0; i < categories.length; i++) {
if (!categories[i].directory) this.description = categories[i].description;
cats.push(categories[i]);
if (categories[i].directory == this.directory) {
cats[i]['current'] = true;
cats[i]['currentDescription'] = categories[i].description;
} else {
cats[i]['current'] = false;
cats[i]['currentDescription'] = null;
}
}
return cats;
},
demos: demos.all
},
template: '_templates/index.mustache',
dest: 'build/index.html'
});
// create JSON file from demos data
grunt.file.write('tmp/data.json', JSON.stringify(demos['all'], null, 2));
// add demos JSON data file to copy task
copy.push({
expand: true,
cwd: 'tmp',
src: 'data.json',
dest: 'build'
});
// set copy config
grunt.config('copy', {
build: {
files: [
copy,
{expand: true, src: ['js/*'], dest: 'build/', filter: 'isFile'} ]
}
});
// set concat config
grunt.config('concat', {
build: {
files: concat
}
});
// set mustache config
grunt.config('mustache_render', {
options: {
directory: '_templates'
},
build: {
files: mustacheRender
}
});
// run the task list
grunt.task.run([
'clean:build',
'copy',
'concat',
'mustache_render',
'clean:tmp',
'less:compile',
]);
});
// build and serve locally
grunt.registerTask('serve', [
'default',
'connect',
'watch'
]);
};