forked from tobiasahlin/SpinKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
97 lines (76 loc) · 2.5 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
'use strict';
var _ = require('lodash');
var fs = require('fs');
var Q = require('q');
var gulp = require('gulp');
var autoprefixer = require('gulp-autoprefixer');
var sass = require('gulp-sass');
var del = require('del');
var htmlDir = './examples',
cssDir = './css',
scssDir = './scss',
htmlTmpl = '<!DOCTYPE html>\n\
<html>\n\
<head>\n\
<title><%= title %></title>\n\
<style type="text/css">\n\
<%= css %>\n\
</style>\n\
</head>\n\
<body>\n\
<%= bodyContent %>\n\
</body>\n\
</html>';
var getHtmlUsageExample = function(cssContent) {
var match = cssContent.match(/Usage:([\s\S]+\*[.\s\S]+\>)/);
return match && match.length > 1 ? match[1].replace(/ \*/g, '') : '';
};
gulp.task('clean-styles', function(cb) {
del([cssDir], cb);
});
gulp.task('styles', ['clean-styles'], function() {
return gulp.src(scssDir + '/**/*.scss')
.pipe(sass({errLogToConsole: true}))
.pipe(autoprefixer('last 2 versions', {map: false}))
.pipe(gulp.dest(cssDir));
});
gulp.task('clean-html', function(cb) {
del([htmlDir], cb);
});
// Generates HTML files from the usage examples found in the CSS files
gulp.task('html', ['styles', 'clean-html'], function() {
var spinnersDir = cssDir + '/spinners/';
var allDeferred = Q.defer();
fs.mkdirSync(htmlDir);
fs.readdir(spinnersDir, function(err, filenames) {
var promises = filenames.map(function(filename) {
if (filename.indexOf('.css') === -1) { return; }
var deferred = Q.defer();
var title = filename.replace('.css', '').replace(/-/g, ' ');
var cssFilepath = spinnersDir + filename;
var htmlFilename = filename.replace('.css', '.html');
var readCssFile = function(err, cssContent) {
if (err) { console.log(err); deferred.reject(err); }
var bodyContent = getHtmlUsageExample(cssContent);
var html = _.template(htmlTmpl)({
css: cssContent,
title: title,
bodyContent: bodyContent
});
fs.writeFile(htmlDir + '/' + htmlFilename, html, function(err, data) {
if (err) { console.log(err); deferred.reject(err); }
deferred.resolve();
});
};
fs.readFile(cssFilepath, {encoding: 'utf8'}, readCssFile);
return deferred.promise;
});
Q.all(promises).then(allDeferred.resolve);
});
return allDeferred;
});
gulp.task('build', ['html', 'styles']);
gulp.task('default', ['build']);
gulp.task('watch', ['build'], function() {
gulp.watch(scssDir + '/**/*.scss', ['build']);
});