forked from AusDTO/gov-au-ui-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGulpfile.js
281 lines (245 loc) · 8.1 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
'use strict';
var gulp = require('gulp'),
sass = require('gulp-sass'),
rename = require('gulp-rename'),
cssnano = require('gulp-cssnano'),
kss = require('kss'),
sassLint = require('gulp-sass-lint'),
autoprefixer = require('gulp-autoprefixer'),
gitVersion = require('gulp-gitversion'),
scssMerge = require('./lib/gulp-scss-merge.js'),
uglify = require('gulp-uglify'),
del = require('del'),
runSequence = require('run-sequence'),
connect = require('gulp-connect'),
svg2png = require('gulp-svg2png'),
webpack = require('webpack-stream'),
zip = require('gulp-zip'),
imagemin = require('gulp-imagemin'),
handlebars = require('gulp-compile-handlebars')
;
var paths = {
assets: './assets/**/*.*',
assetsDir: './assets',
scssDir: './assets/sass/**/*.scss',
scssTemplatesDir: './assets/sass/components/templates',
kssScssDir: './kss-builder/kss-assets/*.scss',
kssCssDir: './kss-builder/kss-assets',
examples: './examples/*.hbs',
examplePartialsDir: './examples/partials',
kssBuilderDir: './kss-builder/**/*.*',
images: './assets/img/**/*.+(png|svg|jpg)',
scss: './assets/sass/ui-kit.scss',
js: './assets/js/ui-kit.js',
markdown: './*.md',
readme: './README.md',
outputAssets: './build/latest',
outputCSS: './build/latest/*.css',
outputJS: './build/latest/*.js',
outputHTML: './build'
};
var options = {
autoprefixer: {
browsers: ['last 2 versions', 'ie 7-10', 'iOS >= 4']
},
sass: {
functions: {
'image-url($img)': function(img) {
var sass = require('node-sass');
var String = sass.types.String;
return new String('/latest/img/' + img.getValue());
}
},
includePaths: [
'./node_modules'
]
},
webpack: {
output: {
filename: 'ui-kit.js',
}
},
handlebars: {
batch: [ paths.scssTemplatesDir, paths.examplePartialsDir ]
}
};
gulp.task('lint', function () {
return gulp.src([paths.scssDir, paths.kssScssDir, '!./assets/sass/vendor/**/*.scss'])
.pipe(sassLint({
configFile: '.sass-lint.yml'
}))
.pipe(sassLint.format())
.pipe(sassLint.failOnError());
});
gulp.task('ui-kit', function () {
gulp.start(['ui-kit.scss', 'ui-kit.js']);
});
gulp.task('ui-kit.scss', function () {
return gulp.src(paths.scssDir)
.pipe(sass.sync().on('error', sass.logError))
.pipe(autoprefixer(options.autoprefixer))
.pipe(gulp.dest(paths.outputAssets));
});
gulp.task('ui-kit.js', function () {
return gulp.src(paths.js)
.pipe(webpack(options.webpack))
.pipe(gulp.dest(paths.outputAssets));
});
gulp.task('ui-kit.scssmerge', function () {
return gulp.src(paths.scss)
.pipe(scssMerge('_ui-kit.scss'))
.pipe(gulp.dest(paths.outputAssets));
});
gulp.task('ui-kit.min', function () {
gulp.start(['ui-kit.min.scss', 'ui-kit.min.js']);
});
gulp.task('ui-kit.min.scss', ['ui-kit.scss'], function () {
return gulp.src([paths.outputCSS, '!./**/*.min.css'])
.pipe(cssnano())
.pipe(gitVersion())
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest(paths.outputAssets));
});
gulp.task('ui-kit.min.js', ['ui-kit.js'], function () {
return gulp.src(paths.outputJS)
.pipe(uglify())
.pipe(gitVersion())
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest(paths.outputAssets));
});
gulp.task('ui-kit.img', ['svg2png'], function () {
return gulp.src(paths.images)
.pipe(imagemin())
.pipe(gulp.dest(paths.outputAssets + '/img/'));
});
gulp.task('ui-kit.img.zip', ['ui-kit.img'], function () {
return gulp.src(paths.outputAssets + '/img/**/*')
.pipe(zip('images.zip'))
.pipe(gulp.dest(paths.outputAssets));
});
gulp.task('svg2png', function () {
return gulp.src(paths.assetsDir + '/img/icons/*.svg')
.pipe(svg2png())
.pipe(gulp.dest(paths.outputAssets + '/img/icons/'));
});
gulp.task('examples', function () {
return gulp.src(paths.examples)
.pipe(handlebars({}, options.handlebars))
.pipe(rename({
extname: '.html'
}))
.pipe(gulp.dest(paths.outputHTML + '/examples')).pipe(connect.reload());
});
gulp.task('markdown', function () {
return gulp.src(paths.markdown)
.pipe(gulp.dest(paths.outputHTML));
});
gulp.task('nginx', function () {
return gulp.src('nginx.conf')
.pipe(gulp.dest(paths.outputHTML));
});
gulp.task('htmlvalidate', ['examples', 'styleguide'], function (cb) {
try {
var validator = require('gulp-html');
return gulp.src(['build/*.html', 'build/**/*.html'])
.pipe(validator({'verbose': true}));
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
console.log('WARNING: optional HTML validator not installed, to resolve run:');
console.log('> npm install AusDTO/gulp-html');
return cb;
}
else {
throw err;
}
}
});
gulp.task('styleguide', ['styleguide.scss'], function () {
var kssresult = kss({
source: 'assets/sass',
destination: paths.outputHTML,
homepage: '../../README.md',
builder: 'kss-builder'
});
kssresult.then(function () {
gulp.src('./build/*.html').pipe(connect.reload());
});
});
gulp.task('styleguide.data', function () {
var fs = require('fs');
var source = 'assets/sass';
var outputFile = 'data-sections.json';
var customFields = ['tags', 'collection'];
var output = [];
var data;
// From: http://kss-node.github.io/kss-node/api/master/module-kss.html
// - The traverse() function reads all the source directories and calls parse()
// - The parse() function finds the KSS comments in the provided text, creates a
// JSON object containing all the parsed data and passes it the new KssStyleGuide(data)
// constructor to create a style guide object.
kss.traverse(source, {'custom': customFields}).then(function(styleData) {
data = JSON.parse(JSON.stringify(styleData.data.sections));
data.map(section => {
if ( section.depth === 1 ) {
section.children = []
output.push(section)
console.log('section', section.referenceNumber, section.header);
} else {
let node = output.find(parentSection => {
return parentSection.referenceNumber.startsWith(section.referenceNumber.split('.')[0])
})
node.children.push(section)
console.log('child section', section.referenceNumber, section.header);
}
})
fs.writeFile(outputFile, JSON.stringify(output), (err) => {
if (err) throw err;
console.log(outputFile, 'saved successfully')
});
});
});
gulp.task('styleguide.scss', function () {
return gulp.src(paths.kssScssDir)
.pipe(sass(options.sass).on('error', sass.logError))
.pipe(autoprefixer(options.autoprefixer))
.pipe(gitVersion())
.pipe(gulp.dest(paths.kssCssDir));
});
gulp.task('clean', function (done) {
return del([paths.outputAssets, paths.outputHTML], done);
});
gulp.task('default', ['build']);
gulp.task('build', ['lint', 'ui-kit', 'markdown', 'examples', 'styleguide']);
gulp.task('build.prod', function (callback) {
runSequence('clean',
['lint', 'nginx', 'ui-kit', 'ui-kit.min', 'ui-kit.img.zip', 'ui-kit.scssmerge', 'markdown', 'htmlvalidate'],
callback);
});
gulp.task('watch', ['watch.build']);
gulp.task('watch.build', function () {
gulp.watch([
paths.assets,
paths.examples,
paths.readme,
paths.kssBuilderDir,
'!./kss-builder/kss-assets/kss.css'
],
{verbose: true},
['build']);
});
gulp.task('serve', ['webserver', 'build', 'watch.build', 'livereload']);
gulp.task('livereload', function () {
gulp.watch(['./build/latest/'], () => {
gulp.src('./build/latest/**/*').pipe(connect.reload());
});
});
gulp.task('webserver', function () {
connect.server({
livereload: true,
root: 'build'
});
});