-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
192 lines (165 loc) · 6.33 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
// Copyright 2015 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';
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var shell = require('gulp-shell');
var minimist = require('minimist');
var rename = require('gulp-rename');
var debug = require('gulp-debug');
var del = require('del');
var replace = require('gulp-replace');
var path = require('path');
var include = require('@wabson/gulp-include');
var webserver = require('gulp-webserver');
var flatten = require('gulp-flatten');
var sass = require('gulp-sass');
var gulpIgnore = require('gulp-ignore');
var browserify = require('browserify');
var gasify = require('gasify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var gutil = require('gulp-util');
// minimist structure and defaults for this task configuration
var knownOptions = {
string: ['env'],
'default': {
env: 'local'
}
};
var options = minimist(process.argv.slice(2), knownOptions);
// The root working directory where code is edited
var srcRoot = 'src';
var testRoot = 'test';
// The root staging folder for gapps configurations
var buildRoot = 'build/' + options.env;
var dstRoot = buildRoot + '/src';
var compileRoot = buildRoot + '/includes';
var cssRoot = buildRoot + '/css';
var clientJsFilesGlob = '**/*.@(client|spec).js';
var cssFilesGlob = '**/*.css';
// Runs the copy-latest task, then calls gapps upload in the correct
// configuration directory based on the target environment
gulp.task('upload-latest', ['compile-latest'], shell.task(['../../node_modules/node-google-apps-script/bin/gapps upload'],
{cwd: buildRoot}));
// Compiles all HTML files by processing build-time includes
gulp.task('compile-latest', ['copy-latest'], function() {
var clientExcludes = options.env === 'local' ? '' : clientJsFilesGlob;
return gulp.src(dstRoot + '/**')
.pipe(include({
includePaths: dstRoot,
hardFail: false
}))
.pipe(gulpIgnore.exclude(clientExcludes))
.pipe(gulp.dest(compileRoot))
});
// Copies all files based on the current target environment.
// Completion of "clean-deployment" is a prerequisite for starting the copy
// process.
gulp.task('copy-latest', ['clean-deployment', 'sass', 'copy-code']);
gulp.task('copy-code', ['gasify-server-code', 'copy-client-code', 'copy-environment-specific-code']);
// Copies all .js that will be run by the Apps Script runtime
gulp.task('copy-server-code', function copyServerCode() {
return gulp.src([
srcRoot + '/server/*.js',
srcRoot + '/libs/*.js',
srcRoot + '/ui/**/*.server.js'])
.pipe(gulp.dest(dstRoot));
});
gulp.task('gasify-server-code', function () {
var b = browserify({
entries: srcRoot+ '/server/main.js',
debug: true,
plugin: [gasify]
});
return b.bundle()
.pipe(source('Code.js'))
.pipe(buffer())
.pipe(gulp.dest(dstRoot));
});
gulp.task('copy-client-code', function copyClientCode() {
var src = gulp.src([
cssRoot + '/ui/' + cssFilesGlob,
srcRoot + '/ui/' + cssFilesGlob,
srcRoot + '/ui/' + clientJsFilesGlob,
srcRoot + '/ui/**/*.html']);
// Emulate runtime includes for local env and replace JS libs with local equivalents
return options.env === 'local' ? src
.pipe(replace(/<\?!= ?include\('([-\.\/\w]+)'\);? ?\?>/g, '<!--=include $1.html -->'))
.pipe(replace('https://ssl.gstatic.com/docs/script/css', ''))
.pipe(replace('//ajax.googleapis.com/ajax/libs/jquery/3.0.0', '/jquery/dist/'))
.pipe(replace('//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3', '/underscore/'))
.pipe(replace('//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3', '/backbone/'))
.pipe(gulp.dest(dstRoot))
: src.pipe(gulp.dest(dstRoot));
});
// Does any environment specific work
gulp.task('copy-environment-specific-code', function copyEnvironmentSpecific() {
var envFiles = srcRoot + '/env/' + options.env + '/*.js';
var mockFiles = testRoot + '/ui/**/*.@(html|client.js)';
var testFiles = srcRoot + '/tests/*.js';
var unitTestRunner = srcRoot + '/unit-tests.html';
switch (options.env) {
case 'local':
return gulp.src([envFiles, mockFiles, unitTestRunner])
.pipe(gulp.dest(dstRoot));
case 'dev':
break;
case 'test':
return gulp.src([envFiles, testFiles])
.pipe(gulp.dest(dstRoot));
break;
default:
break;
}
return gulp.src(envFiles).pipe(gulp.dest(dstRoot));
});
gulp.task('clean-deployment', function(cb) {
return del([
dstRoot + '/**/*',
compileRoot + '/**/*'
//cssRoot + '/**/*'
]);
});
gulp.task('clean-deployments', function(cb) {
return del('build/*/@(src|includes|css)');
});
gulp.task('lint', function() {
return gulp.src(srcRoot + '/**/*.js')
.pipe(jshint.extract())
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish', {
verbose: true
}));
});
gulp.task('sass', function () {
return gulp.src(srcRoot + '/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest(cssRoot));
});
gulp.task('ui-server', ['lint', 'compile-latest'], function() {
return gulp.src([compileRoot, 'node_modules', 'coverage'])
.pipe(webserver({
livereload: true,
directoryListing: {
enable: true,
path: compileRoot
},
open: false
}));
});
gulp.task('watch', function() {
gulp.watch([srcRoot + '/ui/**/*.html', srcRoot + '/ui/' + clientJsFilesGlob, srcRoot + '/ui/**/*.scss', testRoot + '/ui/**/*.html'], ['lint', 'compile-latest']);
});
gulp.task('ui-server-watch', ['ui-server', 'watch']);