forked from navikt/aksel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
133 lines (110 loc) · 4.24 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
/* eslint-disable strict */
'use strict';
const gulp = require('gulp');
const fs = require('fs');
const through = require('through2');
const newer = require('gulp-newer');
const babel = require('gulp-babel');
const plumber = require('gulp-plumber');
const gutil = require('gulp-util');
const path = require('path');
const chalk = require('chalk');
const cssfont64 = require('gulp-cssfont64-formatter');
const configureSvgIcon = require('react-svg-icon-generator').default;
const scripts = './packages/node_modules/*/src/**/*.js';
const fonts = './packages/node_modules/*/assets/**/*.woff';
const dest = 'packages/node_modules';
let srcEx;
let assetsEx;
let libFragment;
let srcFragment;
if (path.win32 === path) {
srcEx = /(packages\\node_modules\\[^/]+)\\src\\/;
assetsEx = /(packages\\node_modules\\[^/]+)\\assets\\/;
libFragment = '$1\\lib\\';
srcFragment = '$1\\src\\';
} else {
srcEx = new RegExp('(packages/node_modules/[^/]+)/src/');
assetsEx = new RegExp('(packages/node_modules/[^/]+)/assets/');
libFragment = '$1/lib/';
srcFragment = '$1/src/';
}
function mapToDest(filepath) {
return filepath.replace(srcEx, libFragment);
}
function mapSrcToDest(filepath) {
return filepath.replace(assetsEx, srcFragment);
}
function fixErrorHandling() {
return plumber({
errorHandler: (err) => gutil.log(err.stack)
});
}
function onlyNewFiles(map) {
return newer({ map });
}
function logCompiling() {
return through.obj((file, enc, callback) => {
gutil.log('Compiling', `'${chalk.cyan(file.path)}'...`);
callback(null, file);
});
}
function renameUsingMapper(mapper) {
return through.obj((file, enc, callback) => {
file._path = file.path; // eslint-disable-line no-underscore-dangle, no-param-reassign
file.path = mapper(file.path); // eslint-disable-line no-param-reassign
callback(null, file);
});
}
function cssFontfile(filename, mimetype, file64, format) {
const fontFamiliy = 'Source Sans Pro'; // all fonts at this point is Source sans pro
const fontWeight = filename.replace(/\D/g, '') || '400'; // 400 is called regular
const fontStyle = filename.indexOf('italic') >= 0 ? 'italic' : 'normal';
// eslint-disable-next-line max-len
return `@font-face { font-family: '${fontFamiliy}'; font-weight: ${fontWeight}; font-style: ${fontStyle}; src: url(data:${mimetype};base64,${file64}) format("${format}");}`;
}
function test() {
return 0;
}
function build() {
return gulp.src(scripts)
.pipe(fixErrorHandling())
.pipe(onlyNewFiles(mapToDest))
.pipe(logCompiling())
.pipe(babel())
.pipe(renameUsingMapper(mapToDest))
.pipe(gulp.dest(dest));
}
function buildCssfonts() {
return gulp.src(fonts)
.pipe(fixErrorHandling())
.pipe(onlyNewFiles(mapSrcToDest))
.pipe(logCompiling())
.pipe(cssfont64({ formatter: cssFontfile, extention: 'less' }))
.pipe(renameUsingMapper(mapSrcToDest))
.pipe(gulp.dest(dest));
}
function fixIconFile() {
const iconsfile = path.join(__dirname, 'packages', 'node_modules', 'nav-frontend-ikoner-assets', 'src', 'index.js');
const oldContent = fs.readFileSync(iconsfile, 'utf8');
const content = oldContent
.replace(/\(<svg height=\{height \|\| size}/g, '(<svg {...props} height={height || size}')
.replace('width} = this.props;', 'width, ...props} = this.props;')
.replace('height: React.PropTypes.number,',
'height: React.PropTypes.oneOfType([React.PropTypes.number, React.PropTypes.string]),')
.replace('width: React.PropTypes.number,',
'width: React.PropTypes.oneOfType([React.PropTypes.number, React.PropTypes.string]),');
fs.writeFileSync(iconsfile, content, 'utf8');
}
configureSvgIcon({
destination: path.join(__dirname, 'packages', 'node_modules', 'nav-frontend-ikoner-assets', 'src', 'index.js'),
svgDir: path.join(__dirname, 'packages', 'node_modules', 'nav-frontend-ikoner-assets', 'assets'),
keepFillColor: true
});
gulp.task('fixicons', fixIconFile);
gulp.task('test', test);
gulp.task('build', build);
gulp.task('default', ['test', 'build']);
gulp.task('buildicons', ['svg-icon']);
gulp.task('buildtest', fixIconFile);
gulp.task('buildfonts', buildCssfonts);