forked from opus1269/screensaver
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
405 lines (349 loc) · 11.1 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
/*
* Copyright (c) 2015-2019, Michael A. Updike All rights reserved.
* Licensed under the BSD-3-Clause
* https://opensource.org/licenses/BSD-3-Clause
* https://github.com/opus1269/screensaver/blob/master/LICENSE.md
*/
'use strict';
/* eslint no-console: 0 */
/* eslint require-jsdoc: 0 */
// paths and files
const base = {
app: 'screensaver',
src: './app/',
dist: './build/prod/app',
dev: './build/dev/app',
store: 'store/',
docs: 'docs/',
};
const path = {
scripts: `${base.src}scripts/`,
html: `${base.src}html/`,
elements: `${base.src}elements/`,
images: `${base.src}images/`,
assets: `${base.src}assets/`,
lib: `${base.src}lib/`,
locales: `${base.src}_locales/`,
css: `${base.src}css/`,
font: `${base.src}font/`,
};
const files = {
manifest: `${base.src}manifest.json`,
scripts: `${path.scripts}**/*.js`,
scripts_ts: `${path.scripts}**/*.ts`,
html: `${path.html}**/*.html`,
elements: `${path.elements}**/*.js`,
elements_ts: `${path.elements}**/*.ts`,
images: `${path.images}*.*`,
assets: `${path.assets}**/*`,
lib: `${path.lib}**/*.*`,
locales: `${path.locales}**/*.*`,
css: `${path.css}**/*.*`,
font: `${path.font}**/*.*`,
};
files.tmpJs = [files.scripts, files.elements];
files.ts = [files.scripts_ts, files.elements_ts];
files.devjs = ['./gulpfile.js'];
// flag for production release build
let isProd = false;
// flag to keep key in production build for testing purposes
let isProdTest = false;
const process = require('process');
const childProcess = require('child_process');
const spawn = childProcess.spawn;
const gulp = require('gulp');
const del = require('del');
const noop = require('gulp-noop');
const plumber = require('gulp-plumber');
const replace = require('gulp-replace');
const eslint = require('gulp-eslint');
const stripLine = require('gulp-strip-line');
const removeCode = require('gulp-remove-code');
const zip = require('gulp-zip');
// TypeScript
const ts = require('gulp-typescript');
const tsProject = ts.createProject('tsconfig.json');
const tslint = require('gulp-tslint');
const typedoc = require('gulp-typedoc');
let buildName = 'prod';
let zipDirectory = 'build/prod';
let manifestDest = 'build/prod/app';
// for copying prodTest to the prod
const srcDir = 'build/prodTest';
const destDir = 'build/prod';
// code replacement
const SRCH_DEBUG = ' isDevelopmentBuild: false,';
const REP_DEBUG = ' isDevelopmentBuild: true,';
const SS_ENV = process.env.KEY_SCREENSAVER;
const SRCH_SS = 'KEY_SCREENSAVER';
const REP_SS = `${SS_ENV}`;
const CLIENT_ID_ENV = process.env.OAUTH_CLIENT_ID;
const SRCH_CLIENT_ID = 'OAUTH_CLIENT_ID';
const REP_CLIENT_ID = `${CLIENT_ID_ENV}`;
const UNSPLASH_ENV = process.env.KEY_UNSPLASH;
const SRCH_UNSPLASH = 'const KEY = \'KEY_UNSPLASH\'';
const REP_UNSPLASH = `const KEY = '${UNSPLASH_ENV}'`;
const FLICKR_ENV = process.env.KEY_FLICKR;
const SRCH_FLICKR = 'const KEY = \'KEY_FLICKR\'';
const REP_FLICKR = `const KEY = '${FLICKR_ENV}'`;
const REDDIT_ENV = process.env.KEY_REDDIT;
const SRCH_REDDIT = 'const KEY = \'KEY_REDDIT\'';
const REP_REDDIT = `const KEY = '${REDDIT_ENV}'`;
const WTHR_ENV = process.env.KEY_WEATHER;
const SRCH_WTHR = 'const KEY = \'KEY_WEATHER\'';
const REP_WTHR = `const KEY = '${WTHR_ENV}'`;
// copy a directory - Windows only
// preconditions: srcDir and destDir set
function copyDirWindows(done) {
const from = srcDir.replace(/\//gim, '\\');
const to = destDir.replace(/\//gim, '\\');
const command = 'xcopy';
console.log(`copying ${from} to ${to} ...`);
const args = [
'/y',
'/q',
'/s',
`${from}\\*`,
`${to}\\`,
];
const copy = spawn(command, args);
copy.stdout.on('data', (data) => {
console.log(data.toString());
});
copy.stderr.on('data', (data) => {
console.log(`stderr: ${data.toString()}`);
});
copy.on('exit', (code) => {
console.log(`${command} exited with code ${code.toString()}`);
done();
});
}
// Development build
function buildDev(done) {
isProd = false;
isProdTest = false;
buildName = 'dev';
manifestDest = `build/${buildName}/app`;
return gulp.series(jsLint, tsLint, tsCompile,
polyBuildDev, manifest, jsDelete)(done);
}
// production test build - still has manifest key
function buildProdTest(done) {
isProd = true;
isProdTest = true;
buildName = 'prodTest';
zipDirectory = `build/${buildName}`;
manifestDest = `build/${buildName}/app`;
return gulp.series(jsLint, tsLint, tsCompile,
polyBuildProd, manifest, jsDelete, zipBuild)(done);
}
// production build - for release to Chrome store
function buildProd(done) {
isProd = true;
isProdTest = false;
// do the test build and copy it over to prod and finish processing
buildName = 'prodTest';
zipDirectory = 'build/prod';
manifestDest = 'build/prodTest/app';
return gulp.series(jsLint, tsLint, tsCompile, polyBuildProd, manifest,
copyDirWindows, docs, jsDelete, zipBuild, prodTestDelete)(done);
}
// watch for file changes
function watch() {
gulp.watch(files.ts, gulp.series(tsLint, tsCompileDev));
gulp.watch(files.devjs, gulp.series(jsLint));
gulp.watch(files.manifest, gulp.series(manifest));
gulp.watch(files.assets, gulp.series(assets));
gulp.watch(files.css, gulp.series(css));
gulp.watch(files.font, gulp.series(fonts));
gulp.watch(files.html, gulp.series(html));
gulp.watch(files.images, gulp.series(images));
gulp.watch(files.lib, gulp.series(libs));
gulp.watch(files.locales, gulp.series(locales));
}
// Generate Typedoc
function docs() {
const input = files.ts;
return gulp.src(input).pipe(typedoc({
mode: 'modules',
module: 'system',
target: 'ES6',
out: 'docs/gen',
name: 'Photo Screensaver',
readme: 'README.md',
tsconfig: 'tsconfig.json',
}));
}
// Lint development js files
function jsLint() {
const input = files.devjs;
return gulp.src(input, {base: base.src}).
pipe(eslint()).
pipe(eslint.formatEach()).
pipe(eslint.failOnError());
}
// Lint TypeScript files
function tsLint() {
const input = files.ts;
return gulp.src(input, {base: base.src}).
pipe(tslint({
formatter: 'verbose',
})).
pipe(plumber()).
pipe(tslint.report({emitError: false}));
}
// Compile the typescript to js in place
function tsCompile() {
console.log('compiling ts to js...');
const input = files.ts;
return gulp.src(input, {base: base.src}).
pipe(tsProject(ts.reporter.longReporter())).js.
pipe((!isProd ? replace(SRCH_DEBUG, REP_DEBUG) : noop())).
pipe(replace(SRCH_UNSPLASH, REP_UNSPLASH)).
pipe(replace(SRCH_FLICKR, REP_FLICKR)).
pipe(replace(SRCH_REDDIT, REP_REDDIT)).
pipe(replace(SRCH_WTHR, REP_WTHR)).
pipe(removeCode({always: true})).
pipe(gulp.dest(base.src));
}
// Compile the typescript to js and output to development build
function tsCompileDev() {
console.log('compiling ts to js...');
const input = files.ts;
return gulp.src(input, {base: base.src}).
pipe(tsProject(ts.reporter.longReporter())).js.
pipe(replace(SRCH_DEBUG, REP_DEBUG)).
pipe(replace(SRCH_UNSPLASH, REP_UNSPLASH)).
pipe(replace(SRCH_FLICKR, REP_FLICKR)).
pipe(replace(SRCH_REDDIT, REP_REDDIT)).
pipe(replace(SRCH_WTHR, REP_WTHR)).
pipe(removeCode({always: true})).
pipe(gulp.dest(base.dev));
}
// polymer development build
// use command line arguments rather than polymer.json
function polyBuildDev(done) {
console.log('running polymer build...');
// run 'polymer build' with command line arguments
// see: https://stackoverflow.com/a/17537559/4468645
const polymer = (process.platform === 'win32') ? 'polymer.cmd' : 'polymer';
const args = [
'build', '--name', `${buildName}`, '--root', './app',
'--entrypoint', 'html/options.html', '--shell', 'html/options.html',
'--sources', 'scripts/**/*.js', '--sources', 'elements/**/*.js',
'--fragment', 'html/background.html',
'--fragment', 'html/screensaver.html',
'--fragment', 'html/update3.html',
'--module-resolution', 'node', '--npm',
'--extra-dependencies', 'assets/**/*',
'--extra-dependencies', 'css/**/*',
'--extra-dependencies', 'font/**/*',
'--extra-dependencies', 'images/**/*',
'--extra-dependencies', '_locales/**/*',
'--extra-dependencies', 'lib/**/*',
];
const build = spawn(polymer, args);
build.stdout.on('data', (data) => {
console.log(data.toString());
});
build.stderr.on('data', (data) => {
console.log(`stderr: ${data.toString()}`);
});
build.on('exit', (code) => {
console.log(`${polymer} exited with code ${code.toString()}`);
done();
});
}
// Polymer production build - use polymer.json
function polyBuildProd(done) {
console.log('running polymer build...');
// run 'polymer build'
// see: https://stackoverflow.com/a/17537559/4468645
const polymer = (process.platform === 'win32') ? 'polymer.cmd' : 'polymer';
const build = spawn(polymer, ['build']);
build.stdout.on('data', (data) => {
console.log(data.toString());
});
build.stderr.on('data', (data) => {
console.log(`stderr: ${data.toString()}`);
});
build.on('exit', (code) => {
console.log(`${polymer} exited with code ${code.toString()}`);
done();
});
}
// Delete local js files
function jsDelete() {
console.log('deleting local js files...');
const input = files.tmpJs;
return del(input);
}
// Delete the prodTest build
function prodTestDelete() {
console.log('deleting prodTest...');
const input = 'build/prodTest';
return del(input);
}
// manifest.json processing
function manifest() {
const input = files.manifest;
return gulp.src(input, {base: base.src}).
pipe((isProd && !isProdTest) ? stripLine('"key":') : noop()).
pipe(replace(SRCH_SS, REP_SS)).
pipe(replace(SRCH_CLIENT_ID, REP_CLIENT_ID)).
pipe(gulp.dest(manifestDest));
}
// assets
function assets() {
const input = files.assets;
return gulp.src(input, {base: base.src}).
pipe(gulp.dest(base.dev));
}
// css
function css() {
const input = files.css;
return gulp.src(input, {base: base.src}).
pipe(gulp.dest(base.dev));
}
// fonts
function fonts() {
const input = files.font;
return gulp.src(input, {base: base.src}).
pipe(gulp.dest(base.dev));
}
// images
function images() {
const input = files.images;
return gulp.src(input, {base: base.src}).
pipe(gulp.dest(base.dev));
}
// libs
function libs() {
const input = files.lib;
return gulp.src(input, {base: base.src}).
pipe(gulp.dest(base.dev));
}
// locales
function locales() {
const input = files.locales;
return gulp.src(input, {base: base.src}).
pipe(gulp.dest(base.dev));
}
// html
function html() {
const input = files.html;
return gulp.src(input, {base: base.src}).
pipe(gulp.dest(base.dev));
}
// compress a build directory
function zipBuild() {
return gulp.src(`${zipDirectory}/app/**`).
pipe(!isProdTest ? zip('store.zip') : zip('store-test.zip')).
pipe(!isProdTest ? gulp.dest(base.store) : gulp.dest(zipDirectory));
}
exports.default = gulp.series(buildDev, watch);
exports.develop = gulp.series(buildDev, watch);
exports.buildDev = buildDev;
exports.buildProdText = buildProdTest;
exports.buildProd = buildProd;
exports.docs = docs;