-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
217 lines (171 loc) · 5.42 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
/**
* Gulpfile for meels recipe book project
*/
'use strict';
/* ************************************************************************** */
/* GULP CONFIG */
/* Dependencies */
// gulp itself
var gulp = require('gulp');
// utilities
var fs = require('fs');
var request = require('request');
var shell = require('gulp-shell');
var open = require('gulp-open');
// css
var sass = require('gulp-sass');
// js
var babel = require('gulp-babel');
var eslint = require('gulp-eslint');
// compilation utilities
var sourcemaps = require('gulp-sourcemaps');
var concat = require('gulp-concat');
var browserify = require('browserify');
// helpers
var watch = require('gulp-watch');
var expect = require('gulp-expect-file');
var confirm = require('gulp-confirm');
// server
var spawn = require('child_process').spawn;
/* ************************************************************************** */
/* Variables */
var node;
var filePaths = {
serverConfig: 'server/server.js',
recipesDataSeed: 'data/recipes-seed.json',
categoriesDataSeed: 'data/categories-seed.json',
restApiDataUrl: 'https://console.firebase.google.com/project/meels-f1766/database/data',
recipesDataUrl: 'https://meels-f1766.firebaseio.com/recipes.json',
categoriesDataUrl: 'https://meels-f1766.firebaseio.com/categories.json',
tests: 'test/**/*.js'
}
/* ************************************************************************** */
/* DATABASE */
/**
* Task to backup the REST API data to a seed file
*/
gulp.task('backup-recipes-data', function() {
return request(filePaths.recipesDataUrl)
.pipe(fs.createWriteStream(filePaths.recipesDataSeed));
});
/**
* Task to import recipes data to the REST API should anything be lost
*/
gulp.task('import-recipes-data', function() {
var data = fs.readFileSync(filePaths.recipesDataSeed, "utf-8");
return gulp.src(filePaths.recipesDataSeed, {read: false})
.pipe(expect(filePaths.recipesDataSeed))
.pipe(confirm({
question: 'Are you sure you want to import this data? All existing data will be lost! (y/n)',
input: '_key:y'
}))
.pipe(shell([
"curl -X PUT -d '" + data + "' " + filePaths.recipesDataUrl
]));
});
/**
* Task to import categories data to the REST API should anything be lost
*/
gulp.task('import-categories-data', function() {
var data = fs.readFileSync(filePaths.categoriesDataSeed, "utf-8");
return gulp.src(filePaths.categoriesDataSeed, {read: false})
.pipe(expect(filePaths.categoriesDataSeed))
.pipe(confirm({
question: 'Are you sure you want to import this data? All existing data will be lost! (y/n)',
input: '_key:y'
}))
.pipe(shell([
"curl -X PUT -d '" + data + "' " + filePaths.categoriesDataUrl
]));
});
/*
Task to show REST API data in browser (Firebase)
*/
gulp.task('show-data', function() {
gulp.src(__filename)
.pipe(open({uri: filePaths.restApiDataUrl}));
});
/* ************************************************************************** */
/* SERVER */
gulp.task('start-server', function() {
gulp.start('kill-server');
node = spawn('node', [filePaths.serverConfig], {stdio: 'inherit'});
node.on('close', function(code) {
if (code === 8) {
gulp.log('Error detected, waiting for changes...');
}
});
});
gulp.task('kill-server', function() {
if (node) node.kill();
});
gulp.task('kill-server-process', shell.task([
'killall node'
]));
/* ************************************************************************** */
/* CSS */
/**
* Task to compile Sass
*/
gulp.task('sass', function() {
return gulp.src('./app/src/css/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write())
.pipe(gulp.dest('./app/dist/css'));
});
/* ************************************************************************** */
/* JS */
// to add source maps
gulp.task("js", ['eslint'], function () {
browserify("app/src/js/app.js")
.transform('babelify', {presets: ["es2015", "react"]})
.bundle()
.pipe(fs.createWriteStream("app/dist/js/app.js"));
});
// es2015 linting
gulp.task('eslint', function() {
return gulp.src(['app/src/js/**/*.babel.js'])
.pipe(eslint())
.pipe(eslint.format());
});
/* ************************************************************************** */
/* PROCESSING */
/**
* Task to watch for changes in files and trigger events
*/
gulp.task('watch', function() {
// watch for css changes
watch(['app/src/css/**/*.scss'], function() {
gulp.start('sass');
});
// watch for js changes
watch(['app/src/js/**/*.js'], function() {
gulp.start('js');
});
// watch for server config changes
watch([filePaths.serverConfig], function() {
gulp.start('start-server');
});
// watch for changes to test files
watch([filePaths.tests], function() {
gulp.start('test');
});
// start app server
gulp.start('start-server');
});
/*
Task to force kill all node processes and start the watch
*/
gulp.task('force-watch', ['kill-server-process'], function() {
gulp.start('watch');
});
/* ************************************************************************** */
/* TESTING */
/**
* Task to run mocha tests
*/
gulp.task('test', ['js'], shell.task([
"mocha --compilers js:babel-core/register"
]));
/* ************************************************************************** */