This repository has been archived by the owner on Dec 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
201 lines (173 loc) · 4.65 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
// Taken from tutorial at browsersync.io
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');
var rename = require('gulp-rename');
var twig = require('gulp-twig');
var prettify = require('gulp-prettify');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var data = require('gulp-data');
var fs = require('fs');
var path = require('path');
var twigMarkdown = require('twig-markdown');
var jsoncombine = require("gulp-jsoncombine");
var prettyUrl = require("gulp-pretty-url");
/**
* Support Functions and Data
*/
var PUBLISHED_DIR = "published/";
/**
* Gets data from global JSON file.
*/
function getGlobalJsonData(file) {
return getFile('src/data/global.json');
}
/**
* Gets data from JSON, depending on file path
*/
function getJsonData(file) {
return getFile('src/data/' + path.basename(file.path) + '.json');
}
/**
* Gets data from a filepath
*/
function getFile(path) {
return JSON.parse(fs.readFileSync(path));
}
/**
* Returns list of all values in dictionary.
*/
function values(obj) {
return Object.keys(obj || {}).map(function(key) {
return obj[key];
});
}
/**
* Returns slide.text_<size> and defaults to
* slide.text otherwise.
*/
function getText(slide, attr) {
if (attr in slide) {
return slide[attr];
}
return slide.text;
}
/**
* Computes the minimum of a list.
*/
function min(values) {
return Math.min.apply(null, values);
}
function range(cap) {
var numbers = [];
for (var i = 0; i < cap; i++) {
numbers.push(i);
}
return numbers;
}
// Define functions to use in Twig templates
twigFunctions = [
{
name: "getText",
func: getText
},
{
name: "values",
func: values
},
{
name: "min",
func: min
},
{
name: "range",
func: range
}
];
// Generate twig objects
function twigGenerator() {
return twig({
extend: twigMarkdown,
functions: twigFunctions
});
}
/**
* Server, Gulp-specific Functions
*/
// Static Server + watching scss/html files
gulp.task('serve', ['preview'], function() {
browserSync.init({
server: "./" + PUBLISHED_DIR,
});
gulp.watch("src/data/global/*.json", ['global_json']);
gulp.watch("src/static/**/*.*", ['copy']);
gulp.watch("src/scss/**/*.scss", ['sass']);
gulp.watch("src/js/**/*.js", ['js']);
gulp.watch(["src/html/**/*.html", "src/data/*.json"], ['html'])
.on('change', browserSync.reload);
});
gulp.task('preview',
['global_json', 'js', 'sass', 'html', 'copy', 'CNAME-staging',
'gitignore']);
gulp.task('production',
['global_json', 'js', 'sass', 'html', 'copy', 'CNAME-production',
'gitignore'])
gulp.task('copy', function() {
return gulp.src("src/static/**/*.*", {base: "src"})
.pipe(gulp.dest(PUBLISHED_DIR))
});
gulp.task('CNAME-staging', function() {
return gulp.src('CNAME-staging')
.pipe(rename(function(path) {
path.basename = path.basename.replace('-staging', '');
}))
.pipe(gulp.dest(PUBLISHED_DIR));
});
gulp.task('CNAME-production', function() {
return gulp.src('CNAME-production')
.pipe(rename(function(path) {
path.basename = path.basename.replace('-production', '');
}))
.pipe(gulp.dest(PUBLISHED_DIR));
})
gulp.task('gitignore', function() {
return gulp.src('.gitignore').pipe(gulp.dest(PUBLISHED_DIR));
});
// Compile sass into minified CSS & auto-inject into browsers
gulp.task('sass', function() {
return gulp.src("src/scss/*.scss")
.pipe(sass())
.pipe(minifyCss({compatibility: 'ie8', keepBreaks: false}))
.pipe(rename({suffix: '.min' }))
.pipe(gulp.dest(PUBLISHED_DIR + "css"))
.pipe(browserSync.stream());
});
// Compile Twig templates to HTML
gulp.task('html', function() {
return gulp.src('src/html/*.html')
.pipe(data(getGlobalJsonData))
.pipe(data(getJsonData))
.pipe(twigGenerator())
.pipe(prettyUrl())
.pipe(prettify({indent_size: 2}))
.pipe(gulp.dest(PUBLISHED_DIR));
});
// Compile into minified JS & auto-inject into browsers
gulp.task('js', function() {
return gulp.src("src/js/**/*.js")
.pipe(concat('script.min.js'))
.pipe(uglify())
.pipe(gulp.dest(PUBLISHED_DIR + "js"))
.pipe(browserSync.stream());
});
// Combine all global JSON files into one.
gulp.task('global_json', function() {
return gulp.src('src/data/global/*.json')
.pipe(jsoncombine("global.json", function(data){
return new Buffer(JSON.stringify(data));
}))
.pipe(gulp.dest('src/data'));
});
gulp.task('default', ['serve']);