-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgulpfile.js
218 lines (189 loc) · 5.73 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
var gulp = require('gulp');
var gutil = require('gulp-util');
var sourcemaps = require('gulp-sourcemaps');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var watchify = require('watchify');
var browserify = require('browserify');
var webserver = require('gulp-webserver');
var pug = require('gulp-pug');
var data = require('gulp-data');
var concatJson = require('gulp-concat-json');
var mergeJson = require('gulp-merge-json');
var stylus = require('gulp-stylus');
var autoprefixer = require('gulp-autoprefixer');
var minifyCSS = require('gulp-minify-css');
var deploy = require('gulp-gh-pages');
var uglify = require('gulp-uglify');
var assignToPug = require('gulp-assign-to-pug');
var clean = require('gulp-clean');
var imagemin = require('gulp-imagemin');
var inline = require('gulp-inline-source');
var tap = require('gulp-tap');
var rename = require('gulp-rename');
var utils = require('./data/utils.js');
gulp.task('build-js', ['build-json'], bundle); // so you can run `gulp js` to build the file
function bundle() {
var bundler = watchify(browserify('./src/index.js', watchify.args));
bundler.on('update', bundle); // on any dep update, runs the bundler
return bundler.bundle()
// log errors if they happen
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source('app.js'))
// optional, remove if you dont want sourcemaps
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true})) // loads map from browserify file
.pipe(sourcemaps.write('./')) // writes .map file
.pipe(gulp.dest('./build'));
}
function bundleProd() {
var bundler = browserify('./src/index.js');
return bundler.bundle()
// log errors if they happen
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source('app.js'))
.pipe(buffer())
.pipe(uglify())
.pipe(gulp.dest('./build'));
}
function cleanJson() {
return gulp.src('build/data/index.json', {read: false})
.pipe(clean());
}
function buildMoviesJson() {
return gulp.src('./data/movies/**/*.json')
.pipe(concatJson('index.json'))
.pipe(gulp.dest('./build/data/movies'));
}
function buildActorsJson() {
return gulp.src('./data/actors/**/*.json')
.pipe(mergeJson('index.json', function (parsedJson, file) {
var output = {};
output[parsedJson.name] = parsedJson;
return output;
}))
.pipe(gulp.dest('./build/data/actors'));
}
function buildJson() {
return gulp.src('./build/data/**/*.json')
.pipe(mergeJson('index.json', function (parsedJson, file) {
var output = {};
if (parsedJson.push) {
output.movies = parsedJson;
} else {
output.actors = parsedJson;
}
return output;
}))
.pipe(gulp.dest('./build/data'));
}
function buildStatic() {
return gulp.src([
'./assets/images/**/*',
'./assets/favicon.ico',
'./assets/CNAME',
'_redirects'
])
.pipe(imagemin())
.pipe(gulp.dest('build/'));
}
function buildStylus() {
return gulp.src('./assets/app.styl')
.pipe(stylus({
url: { name: 'url', limit: false }
}))
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(minifyCSS())
.pipe(gulp.dest('build'));
}
function buildPug() {
return gulp.src('./build/data/index.json')
.pipe(data(function() {
return {utils: utils};
}))
.pipe(assignToPug('./src/views/templates/index.pug', {
varName: 'actorsAndMovies'
}))
.pipe(gulp.dest('./build'));
}
function buildCsv() {
return gulp.src('./build/data/index.json')
.pipe(tap(function(file) {
var movieList = JSON.parse(file.contents.toString());
movieList = utils.toSortedMovieList(movieList);
file.contents = new Buffer(utils.movieListToCsv(movieList), 'utf8');
}))
.pipe(rename({
basename: 'movies',
extname: '.csv'
}))
.pipe(gulp.dest('./build'));
}
function inlineSource() {
return gulp.src('./build/*.html')
.pipe(inline())
.pipe(gulp.dest('./build'));
}
gulp.task('webserver', function() {
var stylWatcher = gulp.watch('assets/**/*.styl', ['build-stylus']);
var imageWatcher = gulp.watch('assets/**/*', ['build-static']);
var pugWatcher = gulp.watch('src/views/templates/**/*.pug', ['build-templates']);
var jsonWatcher = gulp.watch('data/**/*.json', ['build-templates', 'build-csv']);
gulp.src('build')
.pipe(webserver({
port: 3456,
livereload: false,
host: '0.0.0.0',
directoryListing: false,
open: false
}));
});
gulp.task('clean:json', function() {
return cleanJson();
});
gulp.task('build-actors-json', function() {
return buildActorsJson();
});
gulp.task('build-movies-json', function() {
return buildMoviesJson();
});
gulp.task('build-json', ['build-movies-json', 'build-actors-json', 'clean:json'], function() {
return buildJson();
});
gulp.task('build-static', function() {
return buildStatic();
});
gulp.task('build-templates', ['build-json'], function() {
return buildPug();
});
gulp.task('build-stylus', ['build-static'], function() {
return buildStylus();
});
gulp.task('build-csv', ['build-json'], function() {
return buildCsv();
});
gulp.task('default', ['build-stylus', 'build-js', 'build-templates', 'build-csv', 'webserver']);
gulp.task('build-dev', ['build-stylus', 'build-templates'], function() {
bundle();
});
gulp.task('build', ['do-build'], function() {
inlineSource();
});
gulp.task('do-build', ['build-stylus', 'build-static', 'build-json', 'build-templates', 'build-csv'], function() {
return bundleProd();
});
gulp.task('deploy', function () {
return gulp.src([
"./build/**/*",
"!./build/app.js",
"!./build/app.css",
"!./build/data/**/*",
"!./build/*.svg"
])
.pipe(deploy({
cacheDir: './tmp'
}));
});