-
Notifications
You must be signed in to change notification settings - Fork 42
/
gulpfile.js
242 lines (216 loc) · 4.66 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
/*eslint no-console: 0 camelcase: 0*/
var assert = require('assert');
var assign = require('object-assign');
var browserify = require('browserify');
var concat = require('gulp-concat');
var dtj = require('directory-to-json');
var envify = require('envify/custom');
var flatten = require('gulp-flatten');
var frontMatter = require('gulp-front-matter');
var gulp = require('gulp');
var gulpsmith = require('gulpsmith');
var highlight = require('metalsmith-metallic');
var livereload = require('gulp-livereload');
var markdown = require('metalsmith-markdown');
var myth = require('gulp-myth');
var path = require('path');
var source = require('vinyl-source-stream');
var spawn = require('child_process').spawn;
var streamify = require('gulp-streamify');
var templates = require('metalsmith-templates');
var through = require('through2');
var uglify = require('gulp-uglify');
module.exports = gulp;
/**
* Paths.
*/
var jsFiles = [
'*.js*',
'**/*.js*',
'!node_modules/**/*.js',
'!**/node_modules/*.js*'
];
var moduleEntryPoint = [
path.join(__dirname, 'index.js')
];
var docs = {
docs: [
'content/docs/**/*.md',
'content/docs/*.md'
],
api: [
'content/api/**/*.md',
'content/api/*.md'
],
index: [
'content/index/**/*.md',
'content/index/*.md'
],
learn: [
'content/learn/**/*.md',
'content/learn/*.md'
],
error: [
'content/error/*.md'
]
};
var styleFiles = ['index.css'];
var imageFiles = [
'**/*.jpg',
'**/*.png',
'**/*.gif',
'**/*.svg',
'**/*.ico',
'content/**/*.jpg',
'content/**/*.png',
'content/**/*.gif',
'content/**/*.svg'
];
var fontFiles = [
'**/*.eot',
'**/*.ttf',
'**/*.woff',
'**/*.woff2'
];
/**
* Compile CSS
*/
gulp.task('styles', function() {
return gulp
.src(styleFiles)
.pipe(concat('build.css'))
.pipe(myth())
.pipe(gulp.dest(path.join(__dirname, '/build/')));
});
/**
* Compile JS
*/
gulp.task('modules', function() {
var env = process.env.NODE_ENV || 'development';
var debug = (env === 'development');
var opts = {
path: path.resolve('./content'),
noDot: true
}
dtj(opts, './build/db.json', function(err) {
if (err) return console.log(err);
bundle()
});
function bundle() {
browserify(moduleEntryPoint)
.transform('brfs')
.transform(envify({NODE_ENV: env}))
.bundle()
.pipe(source('bundle.js'))
.pipe(debug ? through.obj() : streamify(uglify()))
.pipe(gulp.dest(path.join(__dirname, '/build/')));
}
});
/**
* Compile docs.
*/
gulp.task('docs', function() {
Object.keys(docs).forEach(function(key) {
buildTemplate(key);
});
});
/**
* Copy assets
*/
gulp.task('assets', function() {
return gulp
.src(imageFiles)
.pipe(flatten())
.pipe(gulp.dest(path.join(__dirname, '/build/images')));
});
/**
* Copy fonts
*/
gulp.task('fonts', function() {
return gulp
.src(fontFiles)
.pipe(flatten())
.pipe(gulp.dest(path.join(__dirname, '/build/fonts')));
});
/**
* Lint files
*/
gulp.task('lint', function() {
var childProcess = Object.create(process);
childProcess.env.NODE_ENV = 'test';
var args = [
path.join(__dirname, './node_modules/.bin/eslint'),
'.'
];
spawn(process.argv[0], args, {stdio: [0, 1, 2], env: childProcess.env});
});
/**
* Watch for file changes
*/
gulp.task('watch', function() {
// determine which folders to watch for doc changes.
var watchDocs = [];
Object.keys(docs).forEach(function(key) {
watchDocs.push('content/' + key + '/*.md');
watchDocs.push('content/' + key + '/**/*.md');
watchDocs.push('content/' + key + '/index.html');
});
gulp.watch(['/build/**']).on('change', livereload.changed);
gulp.watch(jsFiles, ['modules']);
gulp.watch([watchDocs], ['docs']);
gulp.watch(styleFiles, ['styles']);
livereload.listen();
});
/**
* build
*/
gulp.task('build', [
'content',
'assets',
'fonts'
]);
/**
* content
*/
gulp.task('content', [
'docs',
'modules',
'styles'
]);
/**
* Default
*/
gulp.task('default', [
'build',
'watch'
]);
/**
* Build a template.
* TODO: move to module.
*
* @param {String} tn
* @api private
*/
function buildTemplate(tn) {
assert('string' == typeof tn, 'TemplateName should be a string');
var outDir = 'index' == tn ? '' : tn;
var metalPipe = gulpsmith()
.use(highlight())
.use(markdown({
smartypants: true,
gfm: true
}))
.use(templates({
engine: 'mustache',
directory: 'content/' + tn
}));
function parseFile(file) {
assign(file, {template: 'index.html'});
delete file.frontMatter;
}
gulp
.src(docs[tn])
.pipe(frontMatter()).on('data', parseFile)
.pipe(metalPipe)
.pipe(gulp.dest('./build/' + outDir));
}