diff options
Diffstat (limited to 'gulpfile.js')
-rw-r--r-- | gulpfile.js | 234 |
1 files changed, 234 insertions, 0 deletions
diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..4ea26d3 --- /dev/null +++ b/gulpfile.js | |||
@@ -0,0 +1,234 @@ | |||
1 | (function initializeGulp () { | ||
2 | 'use strict'; | ||
3 | |||
4 | var fs = require('fs'); | ||
5 | var cheerio = require('cheerio'); | ||
6 | var rimraf = require('rimraf'); | ||
7 | var mainBowerFiles = require('main-bower-files'); | ||
8 | |||
9 | var gulp = require('gulp'); | ||
10 | var git = require('gulp-git'); | ||
11 | var filter = require('gulp-filter'); | ||
12 | var less = require('gulp-less'); | ||
13 | var webserver = require('gulp-webserver'); | ||
14 | var streamqueue = require('streamqueue'); | ||
15 | var ignore = require('gulp-ignore'); | ||
16 | var prompt = require('gulp-prompt'); | ||
17 | var handlebars = require('gulp-compile-handlebars'); | ||
18 | var rename = require('gulp-rename'); | ||
19 | |||
20 | var dir = { | ||
21 | 'dist': './dist', | ||
22 | 'src': './src' | ||
23 | }; | ||
24 | |||
25 | // List of file paths. | ||
26 | var paths = { | ||
27 | 'html': dir.src + '/**/*.html', | ||
28 | 'hbs': dir.src + '/index.hbs', | ||
29 | 'index': dir.src + '/index.hbs' | ||
30 | }; | ||
31 | |||
32 | // Contents of all our bower dependencies' main:[] fields. | ||
33 | var bowerFiles = mainBowerFiles(); | ||
34 | |||
35 | // The current package.json file. | ||
36 | var packageJson = require('./package.json'); | ||
37 | |||
38 | /** | ||
39 | * The handlebars configuration object. | ||
40 | */ | ||
41 | var handlebarsConfig = { | ||
42 | helpers: { | ||
43 | 'datetime': function (object) { | ||
44 | return object.toLocaleDateString(); | ||
45 | } | ||
46 | } | ||
47 | }; | ||
48 | |||
49 | /** | ||
50 | * This method parses through discovered presentations, reads their | ||
51 | * html metadata, and returns an array of that metadata. | ||
52 | */ | ||
53 | function buildPresentationManifest () { | ||
54 | var presentations = []; | ||
55 | var files = fs.readdirSync(dir.src); | ||
56 | |||
57 | for (var i = 0; i < files.length; i++) { | ||
58 | var file = dir.src + '/' + files[i] + '/index.html'; | ||
59 | try { | ||
60 | var stat = fs.statSync(file); | ||
61 | var $ = cheerio.load(fs.readFileSync(file)); | ||
62 | presentations.push({ | ||
63 | 'title': $("head title").text(), | ||
64 | 'description': $("head meta[name='description']").attr('content'), | ||
65 | 'author': $('head meta[name="author"]').attr('content'), | ||
66 | 'mtime': stat.mtime, | ||
67 | 'path': files[i] + '/index.html' | ||
68 | }); | ||
69 | } catch (e) { | ||
70 | // Do nothing | ||
71 | } | ||
72 | } | ||
73 | presentations.sort(function (a, b) { | ||
74 | return a.mtime >= b.mtime ? 1 : -1; | ||
75 | }); | ||
76 | return presentations; | ||
77 | } | ||
78 | |||
79 | /** | ||
80 | * Clean the output directory. | ||
81 | * | ||
82 | * @param {Function} cb callback. | ||
83 | * @return {*} A gulp stream that performs this action. | ||
84 | */ | ||
85 | gulp.task('clean', function (cb) { | ||
86 | rimraf(dir.dist, cb); | ||
87 | }); | ||
88 | |||
89 | /** | ||
90 | * Build the static file structure from our bower dependencies. Reveal.js | ||
91 | * is given a special snowflake status, because it doesn't observe the | ||
92 | * standard packaging format that bower files like. | ||
93 | */ | ||
94 | gulp.task('package:libs', function (cb) { | ||
95 | var resolveCSS = gulp.src(bowerFiles) | ||
96 | .pipe(filter('*.css')) | ||
97 | .pipe(gulp.dest(dir.dist + '/css')); | ||
98 | |||
99 | var resolveLESS = gulp.src(bowerFiles) | ||
100 | .pipe(filter('*.less')) | ||
101 | .pipe(less()) | ||
102 | .pipe(gulp.dest(dir.dist + '/css')); | ||
103 | |||
104 | var resolveFonts = gulp.src(bowerFiles) | ||
105 | .pipe(filter(['*.otf', '*.eot', '*.svg', '*.ttf', '*.woff', '*.woff2'])) | ||
106 | .pipe(gulp.dest(dir.dist + '/fonts')); | ||
107 | |||
108 | var resolveLibs = gulp.src(bowerFiles) | ||
109 | .pipe(filter('*.js')) | ||
110 | .pipe(gulp.dest(dir.dist + '/js')); | ||
111 | |||
112 | // Reveal.js is a special snowflake. | ||
113 | var resolveReveal = gulp.src('./bower_components/reveal.js/*/**/*.*', | ||
114 | {'base': './bower_components/reveal.js/'}) | ||
115 | .pipe(ignore(['**/test/**', '*.js'])) | ||
116 | .pipe(filter([ | ||
117 | '**/*.js', | ||
118 | '**/*.css', | ||
119 | '**/*.eot', | ||
120 | '**/*.ttf', | ||
121 | '**/*.woff' | ||
122 | ])) | ||
123 | .pipe(gulp.dest(dir.dist)); | ||
124 | |||
125 | return streamqueue({'objectMode': true}, resolveCSS, resolveLESS, | ||
126 | resolveReveal, resolveLibs, resolveFonts); | ||
127 | }); | ||
128 | |||
129 | /** | ||
130 | * Package the handlebars files. | ||
131 | */ | ||
132 | gulp.task('package:hbs', function () { | ||
133 | |||
134 | var templateData = { | ||
135 | 'presentations': buildPresentationManifest(), | ||
136 | 'author': packageJson.author | ||
137 | }; | ||
138 | |||
139 | // Automatically build the site list. | ||
140 | return gulp.src(paths.hbs, {'base': dir.src}) | ||
141 | .pipe(handlebars(templateData, handlebarsConfig)) | ||
142 | .pipe(rename(function (path) { | ||
143 | path.extname = ".html"; | ||
144 | })) | ||
145 | .pipe(gulp.dest(dir.dist)); | ||
146 | }); | ||
147 | |||
148 | /** | ||
149 | * Copy the HTML files into the dist folder. | ||
150 | */ | ||
151 | gulp.task('package:html', function () { | ||
152 | return gulp.src(paths.html, {'base': dir.src}) | ||
153 | .pipe(gulp.dest(dir.dist)); | ||
154 | }); | ||
155 | |||
156 | /** | ||
157 | * This task builds a new presentation from the base presentation template. | ||
158 | */ | ||
159 | gulp.task('new', function () { | ||
160 | var templateData = { | ||
161 | author: packageJson.author | ||
162 | }; | ||
163 | var destinationFolder = ''; | ||
164 | return gulp.src(dir.src + '/template/index.hbs') | ||
165 | .pipe(prompt.prompt([ | ||
166 | { | ||
167 | type: 'input', | ||
168 | name: 'folderName', | ||
169 | message: 'Presentation Folder Name (/^[a-z][a-z_]+$/):', | ||
170 | validate: function (value) { | ||
171 | var result = value.match(/^([a-z][a-z_]+)$/); | ||
172 | return result !== null; | ||
173 | } | ||
174 | }, | ||
175 | { | ||
176 | type: 'input', | ||
177 | name: 'title', | ||
178 | message: 'Presentation Title:' | ||
179 | }, | ||
180 | { | ||
181 | type: 'input', | ||
182 | name: 'description', | ||
183 | message: 'Presentation Description:' | ||
184 | }, | ||
185 | { | ||
186 | type: 'input', | ||
187 | name: 'event', | ||
188 | message: 'First presented at:' | ||
189 | }, | ||
190 | { | ||
191 | type: 'input', | ||
192 | name: 'event', | ||
193 | message: 'First presented on (date):' | ||
194 | }], | ||
195 | function (res) { | ||
196 | destinationFolder = res.folderName; | ||
197 | templateData.presentation = { | ||
198 | 'title': res.title, | ||
199 | 'description': res.description, | ||
200 | 'event': res.event | ||
201 | } | ||
202 | })) | ||
203 | .pipe(handlebars(templateData, handlebarsConfig)) | ||
204 | .pipe(rename(function (path) { | ||
205 | path.dirname += '/' + destinationFolder; | ||
206 | path.basename = "index"; | ||
207 | path.extname = ".html"; | ||
208 | })) | ||
209 | .pipe(gulp.dest(dir.src)) | ||
210 | .pipe(git.add()); | ||
211 | }); | ||
212 | |||
213 | /** | ||
214 | * Package the entire site into the dist folder. | ||
215 | */ | ||
216 | gulp.task('package', ['package:html', 'package:hbs', 'package:libs']); | ||
217 | |||
218 | /** | ||
219 | * Start a local server and serve the application code. This is | ||
220 | * equivalent to opening index.html in a browser. | ||
221 | * | ||
222 | * @return {*} A gulp stream that performs this action. | ||
223 | */ | ||
224 | gulp.task('serve', function () { | ||
225 | gulp.watch(paths.html, ['package:html']); | ||
226 | gulp.watch(paths.hbs, ['package:hbs']); | ||
227 | |||
228 | return gulp.src(dir.dist) | ||
229 | .pipe(webserver({ | ||
230 | 'livereload': true, | ||
231 | 'open': true | ||
232 | })); | ||
233 | }); | ||
234 | })(); | ||