-
Notifications
You must be signed in to change notification settings - Fork 7
/
gulpfile.js
143 lines (125 loc) · 4.12 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
var autoPrefixer = require('gulp-autoprefixer');
var browserSync = require('browser-sync').create();
var clean = require('gulp-clean');
var concat = require('gulp-concat');
var consolidate = require('gulp-consolidate');
var fs = require('fs');
var globbing = require('node-sass-globbing');
var gulp = require('gulp');
var iconFont = require('gulp-iconfont');
var runSequence = require('run-sequence');
var sass = require('gulp-sass');
var shell = require('gulp-shell');
var _ = require('lodash');
var iconFontSettings = {
fontClassName: 'icon',
fontFileName: 'hy-icons',
fontName: 'hy-icons',
iconsPath: './icons/',
svgSrc: './icons/dest/*.svg',
fontDestination: './fonts/',
fontCssPath: '../fonts/',
appendUnicode: true
};
var generateIconSrcPath = iconFontSettings.iconsPath + 'src';
var generateIconDestPath = iconFontSettings.iconsPath + 'dest';
var unicodesJsonFileName = iconFontSettings.iconsPath + 'unicodes.json';
var sass_config = {
importer: globbing,
outputStyle: 'expanded',
includePaths: [
'node_modules/normalize.css/',
'node_modules/breakpoint-sass/stylesheets/',
]
};
// Compile sass.
gulp.task('sass', function () {
gulp.src('sass/**/*.scss')
.pipe(sass(sass_config)
.on('error', sass.logError))
.pipe(autoPrefixer({
browsers: ['last 4 versions']
}))
.pipe(gulp.dest('css'));
});
// Generate icons.
gulp.task('generateUnicodeIconFiles', function () {
if (!fs.existsSync(generateIconDestPath)) {
fs.mkdirSync(generateIconDestPath);
}
var iconFiles = fs.readdirSync(generateIconSrcPath);
var unicodes = require('./' + unicodesJsonFileName);
var unicodeIntValues = _.map(_.values(unicodes), function (unicode) {
return parseInt(unicode.split('u')[1], 16);
});
var nextUnicodeIntValue = _.max(unicodeIntValues);
_.each(iconFiles, function (file) {
var unicodeValue;
if (file.startsWith('.')) {
return;
}
if (_.has(unicodes, file)) {
unicodeValue = unicodes[file];
} else {
nextUnicodeIntValue++;
unicodeValue = 'u' + nextUnicodeIntValue.toString(16).toUpperCase();
unicodes[file] = unicodeValue;
}
fs.createReadStream(generateIconSrcPath + '/' + file)
.pipe(fs.createWriteStream(generateIconDestPath + '/' + unicodeValue + '-' + file));
});
fs.writeFile(unicodesJsonFileName, JSON.stringify(unicodes));
});
// Create icon font from generated icons.
gulp.task('iconFont', function () {
gulp.src([iconFontSettings.svgSrc])
.pipe(iconFont({
fontName: iconFontSettings.fontFileName,
appendUnicode: true,
fontHeight: 1000,
normalize: true,
descent: 6
}))
.on('glyphs', function (glyphs) {
gulp.src(iconFontSettings.iconsPath + '__icon_font.scss')
.pipe(consolidate('lodash', {
glyphs: glyphs,
fontFileName: iconFontSettings.fontFileName,
fontName: iconFontSettings.fontName,
fontPath: iconFontSettings.fontCssPath,
className: iconFontSettings.fontClassName
}))
.pipe(gulp.dest('sass/icons/'));
gulp.src(iconFontSettings.iconsPath + '__variables.scss')
.pipe(consolidate('lodash', {
glyphs: glyphs,
className: iconFontSettings.fontClassName
}))
.pipe(gulp.dest('sass/icons/'));
})
.pipe(gulp.dest(iconFontSettings.fontDestination));
});
// Main tasks.
gulp.task('clean', function () {
return gulp.src(iconFontSettings.fontDestination + '/' + iconFontSettings.fontFileName + '.*', {read: false})
.pipe(clean())
.pipe(gulp.src(generateIconDestPath, {read: false}))
.pipe(clean());
});
gulp.task('nodeBuild', function () {
return gulp.src('')
.pipe(shell(['node build'], {cwd: './styleguide'}));
});
gulp.task('build', function(cb) {
runSequence('clean', 'generateUnicodeIconFiles', 'iconFont', 'sass', 'nodeBuild', cb);
});
gulp.task('serve', ['build'], function () {
browserSync.init({
server: {
baseDir: './'
}
});
gulp.watch(['./sass/**/*.scss', '!./sass/icons/**/*.scss', './templates/**/*.html'], ['build']);
gulp.watch(['index.html']).on('change', browserSync.reload);
});
gulp.task('default', ['build']);