forked from amazon-archives/web-app-starter-kit-for-fire-tv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
301 lines (263 loc) · 11.7 KB
/
gulpfile.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
/*
Automation of common build tasks.
Gulp requires node and npm (node package manager), if you've never used node/npm before please start here: http://nodejs.org/
If you've never used gulp before, background is here: https://github.com/gulpjs/gulp/blob/master/docs/getting-started.md,
tl;dr: just do this
npm install -g gulp
Next, install all the dependencies for this project (see package.json for the list):
npm install
Now you can run the gulp default task to copy all:
gulp build
see what else you can do:
gulp help
*/
try {
var gulp = require('gulp-help')(require('gulp'), { description: 'Display this', aliases: ['h', '?'] });
var jshint = require('gulp-jshint');
var uglify = require('gulp-uglifyjs');
var sass = require('gulp-sass');
var jsdoc = require("gulp-jsdoc");
//var sourcemaps = require('gulp-sourcemaps');
var inlinesource = require('gulp-inline-source');
var htmlreplace = require('gulp-html-replace');
var debug = require('gulp-debug');
var merge = require('merge-stream');
var del = require('del');
var vinylPaths = require('vinyl-paths');
var rename = require("gulp-rename");
var fs = require('fs');
var packageJSON = require('./package.json');
var template_info = '<!-- \n Web App Starter Kit for Fire TV \n\n Name: ' + packageJSON.name + '\n Version: ' + packageJSON.version +
'\n\n https://github.com/amzn/web-app-starter-kit-for-fire-tv \n\n The project is released as open source under the Creative Commons License CC0 \n\n http://creativecommons.org/publicdomain/zero/1.0/\n-->';
var path = require('path');
} catch (e) {
if (e.code && e.code === "MODULE_NOT_FOUND") {
// one of the dependencies couldn't be found, suggest that the user run npm install
console.log(e.message + ". Please try the command 'npm install' to resolve this dependency.");
process.exit();
} else {
// unknown error occurred, rethrow
throw e;
}
}
// from https://github.com/gulpjs/gulp/blob/master/docs/recipes/using-external-config-file.md
var config = {};
var pathSettings = ["html", "sass", "assets", "root", "appJS", "libJS"];
var projectsPath = "./src/projects";
var projectDirs = fs.readdirSync(projectsPath);
for (var i = 0; i < projectDirs.length; i++) {
var project = projectDirs[i];
if (project[0] !== ".") {
var configFile = projectsPath + '/' + project + '/build.json';
if(fs.existsSync(configFile) == false){
continue;
}
config[project] = require(configFile);
if (!config[project].dest) {
config[project].dest = "./out/" + project;
}
if (!config[project].appTitle) {
config[project].appTitle = project;
}
// expand project paths
for(var k in pathSettings){
var setting = pathSettings[k];
var paths = config[project][setting] ;
for(var n in paths){
paths[n] = path.resolve(projectsPath + "/" + project, paths[n])
}
config[project][setting] = paths;
}
// console.log(config[project]);
}
}
// MAIN TASKS
gulp.task('default', false, ['help']);
gulp.task('build', 'minimal build and copy (default) - ', ['copy-assets', 'copy-root', 'sass-css', 'copy-js', 'build-html'],
function(){}, {aliases: ['b', 'debug', 'd']});
gulp.task('watch', 'execute the build task when any source file changes - ', ['copy-assets-watch', 'copy-root-watch', 'sass-css-watch', 'copy-js-watch', 'build-html-watch'],
function(){}, {aliases: ['w']});
gulp.task('minify', 'generate html with minified js - ', ['copy-assets', 'copy-root', 'sass-css', 'minify-js', 'minify-html'],
function(){}, {aliases: ['m']});
gulp.task('minify-watch', 'execute minify when any source file changes - ', ['copy-assets-watch', 'copy-root-watch', 'sass-css-watch', 'minify-js-watch', 'minify-html-watch']);
gulp.task('inline', 'minify js and inline it and css into final html - ', ['copy-assets', 'copy-root', 'sass-css', 'minify-js', 'inline-html'],
function(){}, {aliases: ['i']});
gulp.task('inline-watch', 'execute inline when any source file changes - ', ['copy-assets-watch', 'copy-root-watch', 'sass-css-watch', 'minify-js-watch', 'inline-html-watch']);
// see https://github.com/gulpjs/gulp/blob/master/docs/recipes/delete-files-folder.md
gulp.task('clean', 'remove all config.dest directories', forAllTargets(doClean), {aliases: ['c']});
function doClean(cfg) {
return gulp.src(cfg.dest)
.pipe(vinylPaths(del));
}
// UTILITY TASKS
// tasks to modify the target.html files to inline the .css and .js items into a single file, using minified js
gulp.task('build-html', false, forAllTargets(doBuildHTML));
gulp.task('build-html-watch', false, forAllTargetsWatch(getSrcHTML, ['build-html']));
function getSrcHTML(cfg) {
return cfg.html;
}
function getInjectJSPaths(cfg) {
var paths = getSrcJS(cfg);
return paths.map(function(jsPath) {return 'js/'+jsPath.slice(jsPath.lastIndexOf(path.sep)+1)});
}
function doBuildHTML(cfg) {
return gulp.src(getSrcHTML(cfg))
.pipe(htmlreplace({
js: getInjectJSPaths(cfg),
template_info: template_info,
app_title: cfg.appTitle}))
//.pipe(debug({verbose: false}))
.pipe(gulp.dest(cfg.dest));
}
// tasks to modify the target.html files to replace specifed sections with all.min.js, the result of the minify-js task
gulp.task('minify-html', false, ['minify-js'], forAllTargets(doMinifyHTML));
gulp.task('minify-html-nodeps', false, forAllTargets(doMinifyHTML));
gulp.task('minify-html-watch', false, forAllTargetsWatch(getSrcMinifyHTML, ['minify-html-nodeps']));
function doMinifyHTML(cfg) {
return gulp.src(getSrcHTML(cfg), {base: cfg.source})
.pipe(htmlreplace({
js: {src: 'all.min.js', tpl: '<script src="%s"></script>'},
template_info: template_info,
app_title: cfg.appTitle}))
.pipe(gulp.dest(cfg.dest));
}
function getSrcMinifyHTML(cfg) {
return getSrcHTML(cfg).concat(cfg.dest + '/all.min.js');
}
// tasks to modify the target.html files to inline the .css and .js items into a single file, using minified js
gulp.task('inline-html', false, ['sass-css', 'minify-js'], forAllTargets(doInlineHTML));
gulp.task('inline-html-nodeps', false, forAllTargets(doInlineHTML));
gulp.task('inline-html-watch', false, forAllTargetsWatch(getInlineSrcHTML, ['inline-html-nodeps']));
function getInlineSrcHTML(cfg) {
return getSrcHTML(cfg).concat([cfg.dest + '/all.min.js', cfg.dest + '/*.css']);
}
function doInlineHTML(cfg) {
return gulp.src(getSrcHTML(cfg), {base: '.'})
.pipe(rename({dirname: cfg.dest}))
.pipe(htmlreplace({
js: {src: 'all.min.js', tpl: '<script inline src="%s"></script>'},
template_info: template_info,
app_title: cfg.appTitle}))
//.pipe(debug({verbose: false}))
.pipe(inlinesource())
.pipe(gulp.dest('.'));
}
// task to copy the js sources from target.libJS and target.appJS to the dest, for use when inline/minify not used
gulp.task('copy-js', false, forAllTargets(doCopyJS));
gulp.task('copy-js-watch', false, forAllTargetsWatch(getSrcJS, ['copy-js']));
function doCopyJS(cfg) {
return gulp.src(getSrcJS(cfg))
.pipe(rename({dirname: 'js'}))
.pipe(gulp.dest(cfg.dest));
}
// task to run the gulp-uglify process on all target.libJS and target.appJS files, using target.uglifyOpts
gulp.task('minify-js', false, forAllTargets(doMinifyJS));
gulp.task('minify-js-watch', false, forAllTargetsWatch(getSrcJS, ['minify-js']));
function getSrcJS(cfg) {
return cfg.libJS.concat(cfg.appJS);
}
function doMinifyJS(cfg) {
return gulp.src(getSrcJS(cfg))
.pipe(rename({dirname: 'js'}))
.pipe(uglify('all.min.js', {
"sourceRoot": ".",
"outSourceMap": false
}))
//.pipe(debug({verbose: false}))
.pipe(gulp.dest(cfg.dest));
}
gulp.task('jshint', false, forAllTargets(doJSHint));
function doJSHint(cfg) {
return gulp.src(cfg.appJS)
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'));
}
gulp.task('jsdoc', false, forAllTargets(doJSDoc));
function doJSDoc(cfg) {
return gulp.src(cfg.appJS)
.pipe(jsdoc('./jsdoc'))
}
// task to copy all assets named in target.assets to target.dest/assets directory
gulp.task('copy-assets', false, forAllTargets(doCopyAssets));
gulp.task('copy-assets-watch', false, forAllTargetsWatch(getSrcAssets, ['copy-assets']));
function getSrcAssets(cfg) {
return cfg.assets || [];
}
function doCopyAssets(cfg) {
return gulp.src(getSrcAssets(cfg))
.pipe(rename({dirname: 'assets'}))
.pipe(gulp.dest(cfg.dest));
}
// task to copy all files named in target.root to target.dest directory
gulp.task('copy-root', false, forAllTargets(doCopyRoot));
gulp.task('copy-root-watch', false, forAllTargetsWatch(getSrcRoot, ['copy-root']));
function getSrcRoot(cfg) {
return cfg.root || [];
}
function doCopyRoot(cfg) {
return gulp.src(getSrcRoot(cfg))
.pipe(gulp.dest(cfg.dest));
}
// task to run sass processor on target.sass sources, putting result in target.dest directory
gulp.task('sass-css', false, forAllTargets(doSassCSS));
gulp.task('sass-css-watch', false, forAllTargetsWatch(getSrcSass, ['sass-css']));
function getSrcSass(cfg) {
return cfg.sass;
}
function doSassCSS(cfg) {
return gulp.src(cfg.sass)
//.pipe(sourcemaps.init())
.pipe(sass())
//.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(cfg.dest));
}
// HELPER FUNCTIONS
// see https://github.com/gulpjs/gulp/blob/master/docs/recipes/using-multiple-sources-in-one-task.md
// use merge-stream module to combine an array of 0 or more streams into a single stream, so gulp can
// properly wait until all streams in the set are complete.
// result is a merge of all non-falsy elements in the streams array, falsy elements are ignored
function mergeStreams(streams) {
streams = streams.filter(function(e) {return !!e});
if (!streams || streams.length == 0) {
return;
}
if (streams.length == 1) {
return streams[0];
}
var result = merge(streams[0], streams[1]);
for (var i = 2; i < streams.length; i++) {
result.add(streams[i]);
}
return result;
}
// returns a function that calls the doSomething function for each target and collects the results into a single stream
// the config target is passed to the doSomething callback, which is expected to return a stream or a falsy result if
// nothing was done
// This is called at init time when the tasks are defined, so it returns a function that will actually perform the task
// when needed, rather than performing it immediately.
function forAllTargets(doSomething) {
return function() {
var streams = [];
for (var target in config) {
streams.push(doSomething(config[target]));
}
return mergeStreams(streams);
}
}
// returns a function that calls the passed getSources function for each target in the config and combines the results
// into a single array of sources for gulp.watch. If any of the sources change, it executes the tasks specified by the
// tasks argument.
// Is called at init time when a task to create the watcher is defined, so it returns a function that will actually
// set up the watcher when the taks is invoked, rather than setting it up immediately.
function forAllTargetsWatch(getSources, tasks) {
return function() {
var src = [];
for (var target in config) {
src = src.concat(getSources(config[target]));
}
watcher = gulp.watch(src, tasks);
watcher.on('change', function(event) {
console.log('file ' + event.path + ' was ' + event.type);
});
}
}