-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
302 lines (262 loc) · 7.19 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* eslint import/extensions: 0 */
var fs = require('fs')
var gulp = require('gulp')
var gutil = require('gulp-util')
var $ = require('gulp-load-plugins')()
var rename = require('gulp-rename')
var uglify = require('gulp-uglify')
var concat = require('gulp-concat')
var gulpPug = require('gulp-pug')
var pug = require('pug')
var pugLinter = require('gulp-pug-linter')
var standard = require('gulp-standard')
var server = require('gulp-webserver')
var sassError = require('gulp-sass-error').gulpSassError
var exec = require('child_process').exec
/**
* Execute sass-lint, error on build, warn on watch
*/
gulp.task('sass-lint', function (cb) {
exec('./node_modules/.bin/sass-lint -v', function (err, stdout) {
if (err) { gutil.log(err) }
if (stdout) { gutil.log(stdout) }
cb(0)
})
})
/**
* Run es-lint against js files, error on build, warn on watch
*/
gulp.task('js-lint', function () {
return gulp.src([ './js/**/*.js', './schema/**/*.js', './gulpfile.js', '!js/vendor/**' ])
.pipe(standard())
.pipe(standard.reporter('default', {
breakOnError: false,
quiet: true
}))
})
/**
* Run pug-lint against pug files, error on build, warn on watch
*/
gulp.task('pug-lint', function () {
return gulp.src([ './**/*.pug' ])
.pipe(pugLinter())
.pipe(pugLinter.reporter(null))
})
/**
* Generate css for docs/style-guide in docs/css/docs.css
*/
gulp.task('sass', function () {
return gulp.src('docs/scss/docs.scss')
.pipe(
$.sass({
includePaths: [
// Include Foundation Sass
'node_modules/foundation-sites/scss'
],
outputStyle: 'expanded'
})
// Throw error, if not using the default (watch) task
.on('error', sassError(process.argv.length > 2))
)
.pipe(
$.autoprefixer({
browsers: [ 'last 2 versions', 'ie >= 9' ]
})
)
.pipe(gulp.dest('docs/css'))
})
/**
* Generate css for everything in dist/style-guider-demo.min.css
*/
gulp.task('dist-css', function () {
return gulp.src('scss/everything.scss')
.pipe(
$.sass({
includePaths: [
// Include Foundation Sass
'node_modules/foundation-sites/scss'
],
outputStyle: 'compressed'
})
.on('error', sassError(process.argv.length > 2))
)
.pipe(
$.autoprefixer({
browsers: [ 'last 2 versions', 'ie >= 9' ]
})
)
.pipe(rename('style-guider-demo.min.css'))
.pipe(gulp.dest('dist'))
})
/**
* Generate js for everything in docs/js/scripts.js
*/
gulp.task('js', function () {
return gulp.src('js/components/**/*.js')
.pipe(concat('scripts.js'))
.pipe(gulp.dest('docs/js'))
})
/**
* Generate js for everything in dist/style-guider-demo.min.js
*/
gulp.task('dist-js', function () {
return gulp.src('js/components/**/*.js')
.pipe(concat('style-guider-demo.min.js'))
.pipe(uglify())
.pipe(gulp.dest('dist'))
})
/**
* Render docs pug file to index.html
*/
gulp.task('pug', function () {
var data = require('./schema/docs')
if (data.introduction) {
data.introduction.forEach(function (p, i) {
data.introduction[i] = pug.render('| ' + p)
})
}
data.components = {}
if (data.menu) {
Object.keys(data.menu).forEach(function (k, i) {
if (data.components[k]) {
console.log('Duplicate component: ' + k)
}
data.components[k] = extractComponent(k)
if (data.menu[k].length) {
data.menu[k].forEach(function (c, i) {
if (data.components[c]) {
console.log('Duplicate component: ' + c)
}
data.components[c] = extractComponent(c)
})
}
})
data.menuLinks = buildMenu(data.menu, data.components)
}
data.components._parameters = fs.readFileSync('./docs/pug/_parameters.pug', 'utf-8')
var pkg = JSON.parse(fs.readFileSync('./package.json'))
data.version = pkg.version
return gulp.src('docs/pug/docs.pug')
.pipe(gulpPug({ data: data, pretty: true }))
.on('error', onError)
.pipe(rename('index.html'))
.pipe(gulp.dest('.'))
})
/**
* Convert user submitted menu object to array for
* top-menu mixin
* @param {Object} menu data.menu object
* @param {Object} components data.components object (For names)
* @return {Array}
*/
function buildMenu (menu, components) {
var array = []
Object.keys(menu).forEach(function (k, i) {
var parent = {
label: components[k].title
}
if (menu[k].length) {
parent.children = []
menu[k].forEach(function (c, i) {
parent.children.push({
label: components[c].title,
link: '#' + c
})
})
} else {
parent.link = '#' + k
}
array.push(parent)
})
return array
}
/**
* Use the params from the schema to build a string
* for the mixin documentation
* @param {object} schema component schema data
* @param {string} component name of the component
* @return {string} Mixin string
*/
function getMixinString (schema, component) {
let str = '+' + component
if (Object.keys(schema.params).length) {
str += '('
Object.keys(schema.params).forEach(function (key) {
str += JSON.stringify(schema.params[key].default) + ', '
})
str = str.slice(0, -2) + ')'
}
return str
}
/**
* Fetch component module from file then render and necessary pug strings
* @param {string} component Name of component
* @return {object} Component schema object
*/
function extractComponent (component) {
var schema
try {
schema = require('./schema/components/' + component)
} catch (e) {
console.log(component + ' - ' + e.code)
schema = {
name: component,
title: component
}
}
if (schema.docs) {
schema.docs.forEach(function (d, j) {
schema.docs[j] = pug.render('| ' + d)
})
}
var mixin = ''
if (schema.params || schema.mixinPath) {
try {
mixin = fs.readFileSync(schema.mixinPath || './pug/components/_' + component + '.pug') + '\n'
schema.mixin = mixin
} catch (e) {
console.log(e)
}
}
if (schema.demo) {
schema.demoCall = schema.demo
schema.demo = pug.render(mixin + schema.demo)
} else if (mixin) {
schema.demoCall = getMixinString(schema, component)
if (schema.block && typeof schema.block === 'string') {
if (schema.block.includes(' ')) {
schema.demoCall += '\n '
} else {
schema.demoCall += '.\n '
}
schema.demoCall += schema.block.replace(/\n/g, '\n ')
}
schema.demo = pug.render(mixin + schema.demoCall)
}
return schema
}
gulp.task('dist', [ 'dist-css', 'dist-js' ])
gulp.task('build', [ 'sass', 'js', 'pug', 'dist' ])
gulp.task('lint', [ 'sass-lint', 'pug-lint', 'js-lint' ])
gulp.task('default', [ 'lint', 'sass', 'pug' ], function () {
gulp.watch('scss/**/*.scss', [ 'sass-lint', 'sass' ])
gulp.watch('pug/**/*.pug', [ 'pug-lint', 'pug' ])
gulp.watch('schema/**/*.js', [ 'js-lint', 'pug' ])
gulp.watch('js/**/*.js', [ 'js-lint', 'js' ])
gulp.src('')
.pipe(server({
port: 8888,
livereload: true,
open: true
}))
})
/**
* Catch errors
* @param {object} err The error
* @return {null} Print error to console
*/
function onError (err) {
console.log(err.message || err.msg || err.code)
if (err.filename) console.log(err.filename + ':' + err.line + ':' + err.column)
this.emit('end')
}