This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
167 lines (141 loc) · 4.16 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
var gulp = require('gulp'),
gulpFilter = require('gulp-filter'),
gutil = require('gulp-util'),
debug = require('gulp-debug'),
ejs = require('gulp-ejs'),
express = require('express'),
app = express(),
deploy = require('gulp-gh-pages');
var es = require('event-stream');
var MetalSmith = require('metalsmith'),
autoprefixer = require('metalsmith-autoprefixer'),
collection = require('metalsmith-collections'),
handlebars = require('handlebars'),
ignore = require('metalsmith-ignore'),
sass = require('metalsmith-sass'),
templates = require('metalsmith-templates'),
q = require('q'),
_ = require('lodash');
var tmp = './.tmp';
var prod = './build';
var base = {
production: '//redhatbrand.github.io/PartnerConferenceTemplates',
development: ''
};
handlebars.registerHelper('json', function(context) {
return JSON.stringify(context);
});
function url(){
return function addUrl(files, metalsmith, done){
for (var file in files) {
files[file].url = file;
}
done();
};
}
function addSiblings (file, files) {
var path = file.split('/');
var siblings = [];
path.pop();
for (var possibleSibling in files) {
var possibleSiblingPath = possibleSibling.split('/');
var name = possibleSiblingPath.pop();
if ((file !== possibleSibling) &&
_.isEqual(path, possibleSiblingPath)) {
siblings.push(files[possibleSibling]);
}
}
files[file].siblings = siblings;
}
function tree() {
return function tree(files, metalsmith, done){
for (var file in files) {
addSiblings(file, files);
}
done();
};
}
function dataToPreviewIndex (file, files) {
if ((file.split('/').pop() === 'data.json')) {
var data = _.extend({}, files[file], JSON.parse(files[file].contents));
data.template = data.template || 'preview.hbt';
files[file.replace('data.json', 'index.html')] = data;
delete files[file];
}
}
function previewIndexes () {
return function previewIndexes(files, metalsmith, done){
for (var file in files) {
dataToPreviewIndex(file, files);
}
done();
};
}
gulp.task('smith', function () {
var defered = q.defer();
MetalSmith(__dirname)
.use(ignore(['**/_*.scss', '**/.**.**.swp', '**/.DS*']))
.use(previewIndexes())
.use(url())
.use(tree())
.use(collection({
templates: 'templates/**/index.html'
}))
.use(sass({
outputStyle: "expanded"
}))
.use(autoprefixer())
.use(templates({
engine: 'handlebars',
directory: 'layouts'
}))
.destination(tmp)
.build(function () {
return defered.resolve.apply(defered, arguments);
});
return defered.promise;
});
gulp.task('build-tmp', ['smith'], function () {
var xmlFilter = gulpFilter(['**/*.html', '**/*.svg']);
return gulp.src(tmp + '/**/*', { base: tmp })
.pipe(xmlFilter)
.pipe(ejs({
baseUrl: base.development,
version: Date.now()
})
.on('error', gutil.log))
.pipe(xmlFilter.restore())
.pipe(gulp.dest(tmp));
});
gulp.task('watch', ['build-tmp'], function() {
gulp.watch('./src/**/*.*', ['build-tmp']);
});
gulp.task('serve', ['watch'], function () {
return app
.use(express.static(tmp))
.listen(4000, function () {
console.log('Express Server listening on 4000');
});
});
gulp.task('ejs', ['smith'], function () {
var xmlFilter = gulpFilter(['**/*.html', '**/*.svg']);
return gulp.src(tmp + '/**/*', { base: './.tmp' })
.pipe(xmlFilter)
.pipe(ejs({
baseUrl: base.production,
version: Date.now(),
color: 'hsl(0, 100%, 30%)'
})
.on('error', gutil.log))
.pipe(xmlFilter.restore())
.pipe(gulp.dest(tmp));
});
gulp.task('build', ['ejs'], function () {
return gulp.src(tmp + '/**/*', { base: tmp })
.pipe(gulp.dest(prod));
});
gulp.task('deploy', function () {
return gulp.src(prod + '/**/*.*')
.pipe(deploy());
});
gulp.task('default', ['serve']);