-
Notifications
You must be signed in to change notification settings - Fork 6
/
gulpfile.js
192 lines (175 loc) · 5.15 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
/* Build Process
1. Clean amp folder to avoid replication during build
2. Compile the js files using Google Closure Compiler
3. Jekyll build
4. Copy the generated amp files back to the amp/ folder
5. Generate service worker using sw-precache
6. Add all unwatched files to git and commmit
7. Push to source (mukilane/mukilane.github.io)
8. Upload an index of posts to firebase for use in search
9. Deploy the generated amp files to separate repository (mukilane/amp)
*/
const gulp = require('gulp');
const exec = require('child_process').exec;
const gutil = require('gulp-util');
const del = require('del');
const compiler = require('google-closure-compiler-js').gulp();
const spawn = require('child_process').spawn;
const ngAnnotate = require('gulp-ng-annotate');
const replace = require('gulp-replace');
const concat = require('gulp-concat');
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const append = require('gulp-append');
const inquirer = require('inquirer');
// Browserify
gulp.task('browserify', (callback) => {
exec('browserify scripts/vendor.module.js -o scripts/vendor.js', (err, stdout, stderr) => {
gutil.log(stderr);
gutil.log(stdout);
callback(err);
});
});
// Bundle
gulp.task('bundle', ['browserify'], () => {
gulp.src([
'./scripts/angular/app.js',
'./scripts/angular/*.ctrl.js',
'./scripts/angular/directives.js',
'./scripts/angular/factories.js',
'./scripts/angular/filters.js',
'./scripts/angular/serviceworker.js'
])
.pipe(concat('main.js'))
.pipe(gulp.dest('./scripts'));
gulp.src([
'./scripts/ApiAi.min.js',
'./scripts/pjax-standalone.min.js',
'./scripts/vendor.js'
])
.pipe(concat('vendor.bundle.js'))
.pipe(gulp.dest('./scripts/'))
});
// Compile JS using Google Closure Compiler
// Using ng-Annotate to annotate angular dependencies
gulp.task('compile', ['bundle'], () => {
return gulp.src('./scripts/main.js', { base: './' })
.pipe(ngAnnotate())
.pipe(replace(/["']ngInject["'];*/g, ""))
.pipe(compiler({
compilationLevel: 'SIMPLE',
warningLevel: 'DEFAULT',
jsOutputFile: 'main.bundle.js',
createSourceMap: true
}))
.pipe(gulp.dest('./scripts'));
});
// Clean the amp folder to prevent dulpication during Jekyll build
gulp.task('clean', ['compile'], () => {
return del([
'amp/**/*',
'_project/*',
'scripts/vendor.js'
]);
});
// Jekyll Pagemaster ( For generating project pages )
// Generates .md files into a collection '_project'
gulp.task('pagemaster', ['clean'], (callback) => {
exec('bundle exec jekyll pagemaster project', (err, stdout, stderr) => {
if(!err) {
exec('yaml2json _data/projects.yml > assets/projects.json', (e, sOut, sErr) => {
gutil.log(sErr);
gutil.log(sOut);
});
}
gutil.log(stderr);
gutil.log(stdout);
callback(err);
})
});
// Jekyll Build
gulp.task('jekyll', ['pagemaster'], (callback) => {
exec('bundle exec jekyll build', (err, stdout, stderr) => {
gutil.log(stderr);
gutil.log(stdout);
callback(err);
});
});
// Copy generated amp files to the source
// Done because of Github's restriction on custom plugins
gulp.task('amp', ['jekyll'], () => {
gulp.src('_site/amp/blog/**/*.html').pipe(gulp.dest('amp'));
});
// Generate Service worker using sw-precache
gulp.task('generate-sw', ['amp'], (callback) => {
exec('sw-precache --config=sw-precache-config.js', (err, stdout, stderr) => {
gutil.log(stdout);
callback(err);
});
});
// Add untracked files
gulp.task('commit', ['generate-sw'], (callback) => {
inquirer.prompt([{
type: 'input',
name: 'message',
message: 'Enter commit message:',
}]).then((result) => {
exec('git add -A && git commit -m "' + result.message + '"', (err, stdout, stderr) => {
gutil.log(stdout);
callback(err);
});
});
});
// Push to origin
gulp.task('push', ['commit'], (callback) => {
exec('git push origin HEAD', (err, stdout, stderr) => {
gutil.log(stdout);
callback(err);
});
});
// Upload index of posts to Firebase database
// Task now moved to Travis
gulp.task('fireDB', (callback) => {
exec("firebase database:set /data/posts _site/assets/posts.json --confirm", (err, stdout, stderr) => {
gutil.log(stdout);
gutil.log(stderr);
callback(err);
});
});
// Tasks run sequentially using dependencies
// Reminder: Update to gulp.series on 4.x
gulp.task('default', ['push', 'ampdeploy']);
// Bundle for testing
gulp.task('bundletest', () => {
gulp.src([
'./scripts/angular/app.js',
'./scripts/angular/*.ctrl.js',
'./scripts/angular/directives.js',
'./scripts/angular/factories.js',
'./scripts/angular/serviceworker.js'
])
.pipe(concat('main.bundle.js'))
.pipe(gulp.dest('./scripts'));
});
// Jekyll Serve
gulp.task('serve', ['bundletest'], (callback) => {
exec('bundle exec jekyll serve', (err, stdout, stderr) => {
gutil.log(stderr);
gutil.log(stdout);
callback(err);
});
});
// Deploy amp-ed files to separate repository
gulp.task('ampdeploy', (callback) => {
// Check if any new files are generated
exec('cd amp && git status --porcelain', (err, stdout, stderr) => {
if (err) {
exec('git add -A && git commit && git push', (e, sOut, sErr) => {
gutil.log(sOut);
callback(e);
})
}
gutil.log("Nothing to commit in amp");
callback(err);
});
});