This repository has been archived by the owner on May 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
286 lines (248 loc) · 6.98 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
// Node modules
var browserify = require("browserify");
var source = require("vinyl-source-stream");
var browserSync = require("browser-sync");
var reload = browserSync.reload;
var nib = require("nib");
var del = require("del");
// Gulp-related
var gulp = require("gulp");
var changed = require("gulp-changed");
var jade = require("gulp-jade");
var preprocess = require("gulp-preprocess");
var shell = require("gulp-shell");
var stylus = require("gulp-stylus");
// Non-gulp NPM modules
var fs = require("fs");
var archieml = require("archieml");
var request = require('request');
var http = require('http');
// Local modules
var args = require("./gulp/cli").parse();
var config = require("./gulp/config");
var utils = require("./gulp/utils");
// Configuration
var gdoc_id = "";
var gdoc_host = "127.0.0.1:6006";
var gdoc_url = "http://"+gdoc_host+"/"+ gdoc_id;
var content = {};
var qzdataPath = process.env.QZDATA_PATH || "~/qzdata";
var thingName = "presidents-economy";
var thingPath = qzdataPath + "/" + "2016/" + thingName;
var isProd = args.build ? true : false;
var preprocessOpts = {
context: {
ENV: isProd ? "prod" : "dev"
}
};
var allTasks = [
"get-content",
"jade",
"stylus",
"browserify",
"copy-libs",
"copy-assets",
"copy-fonts",
"copy-data"
];
var shellCmd = utils.generateShellCmd(args.build, thingPath, thingName);
/**
* Reads compiled ArchieML content from the local JSON file.
*/
function readContentFromFile(doneCallback) {
fs.readFile("content.json", function(err, data){
if (!err) {
content = JSON.parse(data);
doneCallback();
}
else {
console.log("Cannot load content from file");
doneCallback(err);
}
});
}
/**
* Fetch ArchieML data from Google Docs.
*/
function getContentTask(doneCallback) {
if(gdoc_id !== "") {
request.get({
"url": gdoc_url
},
function(error, resp, body) {
if(!error && resp.statusCode < 400) {
content = JSON.parse(body);
fs.writeFileSync("content.json", JSON.stringify(content, null, 4), "utf-8");
doneCallback();
}
else {
// if the server isn't up load from file
if(resp && resp.statusCode >= 400) {
console.log(body);
}
console.log("Cannot load content from server, loading from file");
readContentFromFile(doneCallback);
}
});
}
else {
console.log("No google doc specified, loading from file");
readContentFromFile(doneCallback);
}
}
getContentTask.description = "Fetch ArchieML data from Google Docs";
gulp.task("get-content", getContentTask);
/**
* Compile Jade HTML templates. (Also triggers a refetch of the ArchieML doc.)
*/
function compileJadeTask() {
return gulp.src(config.paths.src.jade + "/index.jade")
.pipe(jade({ pretty: true, locals: content }))
.pipe(gulp.dest(config.dirs.build))
.pipe(reload({ stream: true }));
}
compileJadeTask.description = "Compile Jade HTML templates (also triggers 'get-content')";
gulp.task("jade", ["get-content"], compileJadeTask);
/**
* Compile Stylus CSS meta-language.
*/
function compileStylusTask() {
return gulp.src(config.paths.src.styl + "/main.styl")
.pipe(stylus({
use: [nib()],
"include css": true,
errors: true
}))
.pipe(gulp.dest(config.paths.build.css))
.pipe(reload({ stream: true }));
}
compileStylusTask.description = "Compile Stylus CSS meta-language";
gulp.task("stylus", compileStylusTask);
/**
* Bundle Javascript with browserify.
*/
function compileJavascriptTask(doneCallback) {
var bundler = browserify({
entries: [config.paths.src.js + "/thing.js"],
debug: !isProd
});
if (isProd && !args["dont-minify"]) {
bundler.transform({ global: true }, "uglifyify");
}
return bundler
.bundle()
.on('error', function(err) {
console.error('ERROR IN JS');
console.error(err.message);
doneCallback();
})
.pipe(source("thing.js"))
.pipe(gulp.dest(config.paths.build.js))
.pipe(reload({ stream: true }));
}
compileJavascriptTask.description = "Bundle Javascript with browserify";
gulp.task("browserify", ["preprocess"], compileJavascriptTask);
/**
* Clear files from the build directory.
*/
function cleanTask() {
return del([
config.dirs.build + "/**"
]);
}
cleanTask.description = "Clear files from the build directory";
gulp.task("clean", cleanTask);
/**
* Copy Javascript libraries to build path.
*/
function copyLibrariesTask() {
return gulp.src(config.paths.src.js + "/libs/*")
.pipe(gulp.dest(config.paths.build.js + "/libs"))
.pipe(reload({ stream: true }));
}
copyLibrariesTask.description = "Copy Javascript libraries to build path";
gulp.task("copy-libs", copyLibrariesTask);
/**
* Copy static assets to build path.
*/
function copyAssetsTask() {
return gulp.src(config.paths.src.assets + "/**")
.pipe(gulp.dest(config.paths.build.assets))
.pipe(reload({ stream: true }));
}
copyAssetsTask.description = "Copy static assets to build path";
gulp.task("copy-assets", copyAssetsTask);
/**
* Copy font files to build path.
*/
function copyFontsTask() {
return gulp.src(config.paths.src.fonts + "/**")
.pipe(gulp.dest(config.paths.build.fonts))
.pipe(reload({ stream: true }));
}
copyFontsTask.description = "Copy font files to build path";
gulp.task("copy-fonts", copyFontsTask);
/**
* Copy data files to build path.
*/
function copyDataTask() {
return gulp.src(config.paths.src.data + "/**")
.pipe(gulp.dest(config.paths.build.data))
.pipe(reload({ stream: true }));
}
copyDataTask.description = "Copy data files to build path"
gulp.task("copy-data", copyDataTask);
/**
* Writes environment configuration variables to config.js and puts it in
* the build directory.
*/
function preprocessTask() {
return gulp.src([config.paths.src.js + "/config.js"])
.pipe(preprocess(preprocessOpts))
.pipe(gulp.dest(config.paths.build.js));
}
preprocessTask.description = "Preprocess dev/prod conditional code";
gulp.task("preprocess", preprocessTask);
/**
* Watches project files for changes and runs the appropriate copy/compile
* tasks.
*/
function watchTask(done) {
gulp.watch(config.paths.src.libs + "/**", ["copy-libs"]);
gulp.watch(config.paths.src.fonts + "/**", ["copy-fonts"]);
gulp.watch(config.paths.src.data + "/**", ["copy-data"]);
gulp.watch(config.paths.src.assets + "/**", ["copy-assets"]);
gulp.watch(config.paths.src.js + "/**", ["browserify"]);
gulp.watch(config.paths.src.styl + "/**", ["stylus"]);
gulp.watch(config.paths.src.jade + "/**", ["jade"]);
done();
}
watchTask.description = "Watch local files for changes and re-build as necessary."
gulp.task("watch", allTasks, watchTask);
/**
* Starts the browsersync server.
*/
function browserSyncTask() {
browserSync({
server: {
baseDir: "build"
},
open: false
});
}
browserSyncTask.description = "Serve the built project using BrowserSync";
gulp.task("browser-sync", ["watch"], browserSyncTask);
/**
* Other tasks
*/
gulp.task("shell", allTasks, shell.task(shellCmd));
gulp.task("build", ["shell"]);
if (args.build) {
gulp.task("default", ["clean"], function () {
gulp.start("build");
});
} else {
gulp.task("default", ["clean"], function () {
gulp.start("browser-sync");
});
}