This repository has been archived by the owner on Mar 20, 2020. It is now read-only.
forked from ampproject/amp-by-example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
431 lines (384 loc) · 12.3 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
"use strict";
const gulp = require('gulp-help')(require('gulp'));
const gls = require('gulp-live-server');
const file = require('gulp-file');
const rename = require('gulp-rename');
const del = require('del');
const cache = require('gulp-cached');
const shell = require('gulp-shell');
const jasmine = require('gulp-jasmine');
const eslint = require('gulp-eslint');
const gutil = require('gulp-util');
const gulpIf = require('gulp-if');
const favicons = require("gulp-favicons");
const runSequence = require('run-sequence');
const argv = require('yargs').argv;
const path = require('path');
const diff = require('gulp-diff');
const change = require('gulp-change');
const bower = require('gulp-bower');
const compileExample = require('./tasks/compile-example');
const sitemap = require('./tasks/compile-sitemap');
const validateExample = require('./tasks/validate-example');
const createExample = require('./tasks/create-example');
const FileName = require('./tasks/lib/FileName');
const Metadata = require('./tasks/lib/Metadata');
const ExampleFile = require('./tasks/lib/ExampleFile');
const paths = {
dist: {
dir: 'dist',
html: 'dist/**/*.html',
img: 'dist/img',
video: 'dist/video',
json: 'dist/json',
scripts: 'dist/scripts',
favicons: 'dist/favicons',
fonts: 'dist/fonts',
wellknown: 'dist/.well-known'
},
images: 'src/img/*.{png,jpg,gif,svg}',
favicon: 'src/img/favicon.png',
samples: 'src/**/*.html',
metadata: 'src/**/*.json',
src: 'src',
scripts: ['tasks/**/*.js', 'gulpfile.js'],
static: 'static/*.*',
templates: {
dir: 'templates',
files: ['templates/**/*.css', 'templates/**/*.html']
},
api: {
conf: 'api/conf.json'
},
tmp: {
dir: 'tmp'
},
videos: 'src/video/*.{mp4,webm}',
json: 'src/json/*.json',
scripts: 'src/scripts/*.js',
fonts: 'src/fonts/*.ttf',
wellknown: '.well-known/assetlinks.json'
};
const config = {
templates: {
root: paths.templates.dir,
index: 'index.html',
example: 'example.html',
newExample: 'new-example.html',
preview: 'preview.html',
},
api: {
host: 'https://amp-by-example-api.appspot.com',
dist: 'api/dist'
},
a4a: {
template: 'preview-a4a.html',
defaultWidth: 300,
defaultHeight: 250,
adContainerLabelHeight: 22
},
host: 'https://ampbyexample.com'
};
gulp.task('serve', 'starts a local webserver (--port specifies bound port)',
function() {
const port = argv.port || 8000;
const server = gls.static(paths.dist.dir, port);
config.host = "http://localhost:" + port;
server.start();
gulp.watch([paths.dist.html, paths.dist.scripts], function(file) {
setTimeout(function() {
/* eslint-disable */
server.notify.apply(server, [file]);
/* eslint-enable */
}, 500);
});
});
gulp.task('deploy:prod', 'deploy to production server', function(callback) {
runSequence('clean',
'robots:allow',
'build',
'deploy:site:prod',
'deploy:api:prod',
callback);
});
gulp.task('deploy:staging', 'deploy to staging server', function(callback) {
config.host = 'https://amp-by-example-staging.appspot.com';
runSequence('clean',
'robots:disallow',
'build',
'deploy:site:staging',
callback);
});
gulp.task('conf:encode', 'encode the config file', shell.task([
'openssl aes-256-cbc -e -in ' + paths.api.conf + ' -out ' +
paths.api.conf + '.enc -pass env:AMP_BY_EXAMPLE_DEPLOY_KEY'
]));
gulp.task('conf:decode', 'decode the config file', shell.task([
'openssl aes-256-cbc -d -in ' + paths.api.conf + '.enc -out ' +
paths.api.conf + ' -pass env:AMP_BY_EXAMPLE_DEPLOY_KEY'
]));
gulp.task('deploy:site:prod', 'deploy to production site', shell.task([
'goapp deploy -application amp-by-example -version 1'
]));
gulp.task('deploy:api:prod', 'deploy to production api app engine', shell.task([
'cd api && goapp deploy -application amp-by-example-api -version 1'
]));
gulp.task('deploy:site:staging', 'deploy to staging app engine', shell.task([
'goapp deploy -application amp-by-example-staging -version 1'
]));
gulp.task('copy:images', 'copy example images', function() {
return gulp.src(paths.images)
.pipe(cache('img'))
.pipe(gulp.dest(paths.dist.img));
});
gulp.task('copy:videos', 'copy example videos', function() {
return gulp.src(paths.videos)
.pipe(cache('video'))
.pipe(gulp.dest(paths.dist.video));
});
gulp.task('copy:json', 'copy example json', function() {
return gulp.src(paths.json)
.pipe(cache('json'))
.pipe(gulp.dest(paths.dist.json));
});
gulp.task('copy:fonts', 'copy example fonts', function() {
return gulp.src(paths.fonts)
.pipe(cache('fonts'))
.pipe(gulp.dest(paths.dist.fonts));
});
gulp.task('copy:well-known', 'copy well-known folder', function() {
return gulp.src(paths.wellknown)
.pipe(cache('wellknown'))
.pipe(gulp.dest(paths.dist.wellknown));
});
gulp.task('copy:scripts', 'copy scripts', function() {
return gulp.src(paths.scripts)
.pipe(cache('scripts'))
.pipe(gulp.dest(paths.dist.scripts));
});
gulp.task('copy:license', 'copy license', function() {
return gulp.src('LICENSE')
.pipe(cache('static'))
.pipe(rename(function(path) {
path.extname = ".txt";
}))
.pipe(gulp.dest(paths.dist.dir));
});
gulp.task('copy:static', 'copy static files', function() {
return gulp.src(paths.static)
.pipe(cache('static'))
.pipe(gulp.dest(paths.dist.dir));
});
gulp.task("compile:favicons", function() {
return gulp.src(paths.favicon)
.pipe(cache('static'))
.pipe(favicons({
appName: "AMP by Example",
appDescription: "Accelerated Mobile Pages in Action",
developerName: "Sebastian Benz",
developerURL: "http://sebastianbenz.de/",
background: "#607D8B",
path: "/favicons/",
url: Metadata.HOST,
display: "standalone",
orientation: "none",
version: 1.0,
logging: false,
online: false,
html: "favicons.html",
pipeHTML: true,
replace: true,
"icons": {
"opengraph": false,
"twitter": false
}
}))
.on("error", gutil.log)
.pipe(gulp.dest(paths.dist.favicons));
});
gulp.task('validate:example', 'validate example html files', function() {
return gulp.src(paths.samples)
.pipe(compileExample(config))
.pipe(validateExample());
});
gulp.task('compile:example', 'generate index.html and examples', function() {
return gulp.src(paths.samples)
.pipe(compileExample(config))
.pipe(gulp.dest(paths.dist.dir));
});
gulp.task('compile:sitemap', 'generate sitemap.xml', function() {
return gulp.src(paths.samples)
.pipe(sitemap(config))
.pipe(gulp.dest(paths.dist.dir));
});
gulp.task('create', 'create a new AMP example', function() {
const title = argv.n || argv.name;
const fileName = FileName.fromString(title);
if (!fileName) {
throwInvalidArgumentError('example name missing');
}
let examplePath;
const dest = argv.d || argv.dest;
const category = argv.c || argv.category;
if (dest) {
examplePath = path.join(path.basename(dest), fileName);
} else if (category) {
examplePath = FileName.fromString(category, title);
} else {
throwInvalidArgumentError('example category or directory missing');
}
return file(examplePath, '', {src: true})
.pipe(createExample(config))
.pipe(gulp.dest(paths.src));
});
function throwInvalidArgumentError(message) {
throw new gutil.PluginError({
plugin: 'create',
message: gutil.colors.red('\nError: ' + message + '\n\n') +
gutil.colors.blue('create a new category:\n') +
'gulp create -n "The Name" -c "The Category"\n\n' +
gutil.colors.blue('add to existing category:\n') +
'gulp create -n "The Name" -d src/directory'
});
}
gulp.task('clean', 'delete all generated resources', function() {
cache.caches = {};
return del([paths.dist.dir, config.api.dist]);
});
gulp.task('watch', 'watch for changes in the examples', function() {
gulp.watch([paths.samples, paths.templates.files, paths.metadata],['compile:example']);
gulp.watch(paths.images, ['copy:images']);
gulp.watch(paths.videos, ['copy:videos']);
gulp.watch(paths.scripts, ['copy:scripts']);
gulp.watch(paths.static, ['copy:static']);
});
gulp.task('test', function() {
return gulp.src('spec/**/*Spec.js')
.pipe(jasmine());
});
gulp.task('test2', function() {
return gulp.src('spec/**/*Spec.js')
.pipe(jasmine({includeStackTrace: true}));
});
gulp.task('lint', function() {
const hasFixFlag = argv.fix;
let errorsFound = false;
return gulp.src(paths.scripts, {base: './'})
.pipe(eslint({fix: hasFixFlag}))
.pipe(eslint.formatEach('stylish', function(msg) {
errorsFound = true;
gutil.log(gutil.colors.red(msg));
}))
.pipe(gulpIf(isFixed, gulp.dest('.')))
.on('end', function() {
if (errorsFound && !hasFixFlag) {
gutil.log(gutil.colors.blue('Run `gulp lint --fix` to ' +
'fix some of these lint warnings/errors. This is a destructive ' +
'operation (operates on the file system) so please make sure ' +
'you commit before running.'));
process.exit(1);
}
});
});
gulp.task('default', 'Run a webserver and watch for changes', [
'build',
'watch',
'serve']);
gulp.task('backend:watch', 'run the go backend and watch for changes', function(callback) {
config.host = 'http://localhost:8080';
runSequence(
'build',
'watch',
'backend:serve',
callback);
});
gulp.task('backend:serve', 'Run the go backend', shell.task([
'goapp serve'
]));
gulp.task('api:serve', 'Run the go api backend', shell.task([
'cd api && goapp serve -admin_port=8100'
]));
gulp.task('validate', 'runs all checks', ['lint', 'test', 'validate:example']);
gulp.task('snapshot',
'Saves a snapshot of the generated sample files',
function() {
return gulp.src(paths.samples)
.pipe(compileExample(config, false))
.pipe(gulp.dest(paths.tmp.dir));
}
);
gulp.task('snapshot:verify',
'Compares generated samples against snapshot',
function() {
return gulp.src(paths.samples)
.pipe(compileExample(config, false))
.pipe(diff(paths.tmp.dir))
.pipe(diff.reporter({fail: true}));
}
);
gulp.task('robots:disallow', 'generate robots.txt disallowing robots to access', function() {
return generateRobotsTxt(`User-Agent: *
Disallow: /
`);
});
gulp.task('robots:allow', 'generate robots.txt allowing robots to access', function() {
return generateRobotsTxt(`User-Agent: *
Disallow:
`);
});
function generateRobotsTxt(contents) {
return file('robots.txt', contents, { src: true })
.pipe(gulp.dest('dist'));
}
/* adds a canonical link to sample files */
function performChange(content) {
const exampleFile = ExampleFile.fromPath(this.file.path);
const canonical = config.host + exampleFile.url();
if (!/<link rel="canonical"/.test(content)) {
content = content.replace(/<meta charset="utf-8">/g,
'<meta charset="utf-8">\n <link rel="canonical" href="'
+ canonical + '">');
gutil.log("updating canonical: " + this.file.relative);
}
return content;
}
gulp.task('change', 'use this task to batch change samples', function() {
return gulp.src('src/**/*.html')
.pipe(change(performChange))
.pipe(gulp.dest('src/'));
});
gulp.task('bower', function() {
return bower()
});
gulp.task('build', 'build all resources', [
'bower',
'copy:images',
'copy:videos',
'copy:json',
'copy:fonts',
'copy:scripts',
'copy:license',
'copy:static',
'compile:favicons',
'compile:sitemap',
'compile:example',
'copy:well-known'
]);
function isFixed(file) {
return file.eslint.fixed;
}