forked from skistz/predix-ui.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
340 lines (297 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
'use strict';
/*******************************************************************************
* PREDIX UI CATALOG BUILD TOOLS
*
* The following gulp tasks are available for this project:
*
* - `gulp serve` - Run when developing the project. Continuously builds *.scss
* files to the css/ directory. Runs BrowserSync to reload the
* page whenever a file is changed.
*
* - `gulp build` - Run before releasing a new version of the project to
* production. Builds *.scss files to the css/ directory,
* processes _*.html files (minify, etc.) into
* *.html files.
******************************************************************************/
const path = require('path');
const gulp = require('gulp');
const pkg = require('./package.json');
const $ = require('gulp-load-plugins')();
const gulpSequence = require('gulp-sequence');
const importOnce = require('node-sass-import-once');
const stylemod = require('gulp-style-modules');
const browserSync = require('browser-sync').create();
const gulpif = require('gulp-if');
const combiner = require('stream-combiner2');
const bump = require('gulp-bump');
const argv = require('yargs').argv;
const rename = require('gulp-rename');
const symlink = require("gulp-sym");
const chmod = require('gulp-chmod');
const stream = require('merge-stream')();
const del = require('del');
const gitSync = require('gulp-git');
const execSync = require('child_process').execSync;
var request = require('request');
/*******************************************************************************
* SETTINGS
*
* Configuration settings for various libraries used to process source files.
* Read the documentation for each library for more information on what
* specific configuration flags do.
******************************************************************************/
const sassOptions = {
importer: importOnce,
importOnce: {
index: true,
bower: true
}
};
const browserSyncOptions = {
port: 8080,
notify: false,
reloadOnRestart: true,
logPrefix: `${pkg.name}`,
https: false,
files: ['*.*'],
server: ['./', 'bower_components']
};
var src = ['index.html', 'favicon.ico', 'manifest.json', 'pages/**/*.html', 'elements/**/*.{html,json}', 'service-worker.js', 'type/**/*.*', 'bower_components/**/*.*', 'img/**/*.*', 'css/**/*.*', 'CNAME', 'sw.tmpl'];
/*******************************************************************************
* BASIC UTILITIES
*
* Tasks and functions used across tasks to accomplish simple things.
******************************************************************************/
function handleError(err){
console.log(err.toString());
this.emit('end');
}
/*******************************************************************************
* SASS BUILD PIPELINE
*
* Builds the *.scss files to the css/ directory. Runs appropriate preprocessing
* and postprocessing to turn files in to style modules, etc.
******************************************************************************/
function buildCSS(){
return combiner.obj([
$.sass(sassOptions),
$.autoprefixer({
browsers: ['last 2 versions'],
cascade: false,
flexbox: false
}),
gulpif(!argv.debug, $.cssmin())
])
.on('error', handleError);
}
gulp.task('sass', function() {
return gulp.src(['./sass/*.scss'])
.pipe(buildCSS())
.pipe(stylemod({
moduleId: function(file) {
return path.basename(file.path, path.extname(file.path)) + '-styles';
}
}))
.pipe(gulp.dest('css'))
.pipe(browserSync.stream({match: 'css/*.html'}));
});
gulp.task('sass:clean', function() {
return gulp.src(['.tmp', 'css'], {
read: false
})
.pipe($.clean());
});
/*******************************************************************************
* VERSION BUMP PIPELINE
*
* Run the appropriate `gulp bump:*` command to update project verion numbers in
* bower.json and package.json manifest files before release.
******************************************************************************/
gulp.task('bump:patch', function(){
gulp.src(['./bower.json', './package.json'])
.pipe(bump({type:'patch'}))
.pipe(gulp.dest('./'));
});
gulp.task('bump:minor', function(){
gulp.src(['./bower.json', './package.json'])
.pipe(bump({type:'minor'}))
.pipe(gulp.dest('./'));
});
gulp.task('bump:major', function(){
gulp.src(['./bower.json', './package.json'])
.pipe(bump({type:'major'}))
.pipe(gulp.dest('./'));
});
/*******************************************************************************
* COPY FILES INTO DIST
* This task loops through an array of all the files that need to be in the dist folder, merges them into a stream, and returns that.
* @usage Run `gulp copyFilesIntoDist` to copy files into the dist folder.
* files/folders:
* index.html
* the pages folder (html files only)
* the elements folder (html and json)
* the css folder
* the img folder
* the type folder
* the bower_components
******************************************************************************/
gulp.task('copyFilesIntoDist', ['sass'], function() {
//the full array of what we want to end up in the dist folder/
let copyFrom = src;
//loop through our array to add each stream into the mergeStream process.
copyFrom.forEach((fileOrFolder) => {
let current;
//do we have globbing? if not, just use the file/folder name.
if (fileOrFolder.indexOf('*') > -1) {
let firstIndex = fileOrFolder.indexOf('/');
let copyName = fileOrFolder.substr(0, firstIndex);
current = gulp.src([copyName + '/**/*.*']).pipe(gulp.dest('./dist/' + copyName));
} else {
current = gulp.src([fileOrFolder]).pipe(gulp.dest('./dist/'));
}
//add the current file/folder to the stream
stream.add(current);
});
//and make sure it's not empty before we return it.
return stream.isEmpty() ? null : stream;
});
/*******************************************************************************
* DEVELOPMENT SERVE PIPELINE
*
* Run `gulp serve` during development to continuously process files and reload
* the browser when files are updated.
******************************************************************************/
gulp.task('serve', function() {
browserSync.init(browserSyncOptions);
gulp.watch(['css/*-styles.html', '*.html', 'pages/**/*.html', 'pages/*.html']).on('change', browserSync.reload);
gulp.watch(['sass/*.scss'], ['sass']);
});
/*******************************************************************************
* GIT PRODUCTION BUILD PIPELINE
*
* this task creates an orphan git branch, and does a git add/commit/push
******************************************************************************/
gulp.task('gitStuff', function() {
gitSync.checkout('master',{args : '--orphan', cwd : '.'}, (err) => {
if (err) {
console.log('git checkout error:' + err);
}
console.log('finished checkout successfully');
//set the source to our working directory and exclude node_modules
execSync(`rm -f .gitignore`);
execSync(`touch .gitignore`);
const gitIgnore = [
'node_modules/',
'dist/',
'caddyfile',
'cert.crt',
'cert.key',
'HISTORY.md',
'LICENSE.md',
'README.md',
'bower.json',
'gulpfile.js',
'id_rsa.enc',
'package.json',
'sass/*.*',
'yarn.lock'];
gitIgnore.forEach((val) =>{
execSync(`echo "${val}" >> .gitignore`);
});
execSync(`git add --all`);
execSync(`git commit -m 'master rebuild'`);
execSync(`git push origin master --force`);
});
});
gulp.task('resetCloudflareCache', function() {
var cloudflare_zone_identifier = process.env.cloudflare_zone_identifier,
cloudflare = process.env.cloudflare;
request({
uri: 'https://api.cloudflare.com/client/v4/zones/' + cloudflare_zone_identifier + '/purge_cache',
method:'DELETE',
headers: {
"X-Auth-Email" : "[email protected]",
"X-Auth-Key" : cloudflare,
"Content-Type" : "application/json"
},
body: '{"purge_everything" :true}'
}, function(err, res, body) {
if (err) {
console.log(err);
} else {
console.log(res.body);
}
});
});
/*******************************************************************************
* LOCAL BUILD PIPELINE
*
* Run `gulp` or `gulp localBuild` to emulate files for dist folder locally.
******************************************************************************/
gulp.task('localBuild', function(callback) {
gulpSequence('sass', 'copyFilesIntoDist', 'generate-service-worker')(callback);
});
/*******************************************************************************
* PRODUCTION BUILD PIPELINE
*
* Run `gulp` or `gulp prodBuild` to prepare files for production before releasing
* a new version of the project.
******************************************************************************/
gulp.task('prodBuild', function(callback) {
gulpSequence('sass', 'generate-service-worker', 'gitStuff', 'resetCloudflareCache')(callback);
});
gulp.task('default', ['localBuild']);
/*******************************************************************************
* ARE WE INSIDE TRAVIS?
*
* checks if we are inside the travis VM by testing the process.env.TRAVIS
*
******************************************************************************/
var isTravis = function() {
return process.env.IS_TRAVIS && process.env.IS_TRAVIS.toString() === "true";
};
/*******************************************************************************
* SERVICE WORKER
*
* Builds (or rebuilds) a service worker to cache files on the site's app shell.
* Rebuilding the service worker invalidates the user's cache on the next
* visit to the site ensuring they get new files.
******************************************************************************/
gulp.task('generate-service-worker', function(callback) {
var swPrecache = require('sw-precache'),
rootDir = (isTravis()) ? '.' : './dist';
console.log("isTravis = " + isTravis() );
swPrecache.write(path.join(rootDir, '/service-worker.js'), {
staticFileGlobs: [rootDir + '/index.html',
rootDir + '/manifest.json',
rootDir + '/img/**',
rootDir + '/type/**',
rootDir + '/pages/**',
rootDir + '/elements/**/*.{html,json}',
rootDir + '/css/**',
rootDir + '/bower_components/font-awesome/fonts/fontawesome*',
rootDir + '/bower_components/px-theme/**/*.html',
rootDir + '/bower_components/px-demo/*.html',
rootDir + '/bower_components/px-demo/css/*.html',
rootDir + '/bower_components/px-spinner/**/*.html',
rootDir + '/bower_components/polymer/polymer*.html',
rootDir + '/bower_components/webcomponentsjs/webcomponents-lite.js',
rootDir + '/bower_components/webcomponentsjs/webcomponents-lite.min.js',
rootDir + '/bower_components/hydrolysis/hydrolysis.*',
rootDir + '/bower_components/app-route/app-*.html',
rootDir + '/bower_components/iron-ajax/iron-*.html',
rootDir + '/bower_components/iron-location/iron-*.html',
rootDir + '/bower_components/iron-collapse/iron-collapse.html',
rootDir + '/bower_components/iron-iconset-svg/iron-iconset-svg.html',
rootDir + '/bower_components/iron-icon/iron-icon.html',
rootDir + '/bower_components/iron-meta/iron-meta.html',
rootDir + '/bower_components/promise-polyfill/promise-polyfill-lite.html',
rootDir + '/bower_components/promise-polyfill/Promise.js',
rootDir + '/bower_components/iron-flex-layout/iron-flex-layout.html',
rootDir + '/bower_components/iron-resizable-behavior/iron-resizable-behavior.html',
rootDir + '/bower_components/px-polymer-font-awesome/*polymer-font-awesome.html'],
stripPrefix: rootDir,
maximumFileSizeToCacheInBytes: 6000000, //this needed so hydrolysis is cached...
templateFilePath: rootDir + '/sw.tmpl'
}, callback);
});