-
Notifications
You must be signed in to change notification settings - Fork 71
/
gulpfile.js
182 lines (142 loc) · 5.93 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
const fs = require("fs");
const path = require("path");
const { globSync } = require("glob");
const gulp = require("gulp");
// Settings
const paths = {
sass: {
all: "packages/*/scss/**/*.scss",
assets: "packages/*/scss/**/*.{png,gif,ttf,woff}",
themes: "packages/!(html)",
theme: "scss/all.scss",
swatches: "lib/swatches/*.json",
inline: "dist/all.scss",
dist: "dist"
}
};
function getArg(key, argsArr) {
const args = argsArr || process.argv;
const index = args.indexOf(key);
const next = args[index + 1];
return (index < 0) ? null : (!next || next[0] === '-') ? true : next; // eslint-disable-line no-nested-ternary
}
// #region helpers
function flattenAll( cwds, options ) {
const fileContent = `@forward "../scss/index.scss";\n@use "../scss/index.scss" as *;\n\n@include kendo-theme--styles();`
const utilsFileContent = `@use "../scss/index.import.scss" as *;\n\n@include kendo-utils();`
cwds.forEach( cwd => {
let file = path.resolve( cwd, options.file );
let output = { path: path.resolve( cwd, options.output.path ), filename: 'all.scss' };
if (fs.existsSync( file )) {
fs.mkdirSync( output.path, { recursive: true } );
if ( cwd.includes('utils') ) {
fs.writeFileSync(path.resolve( output.path, output.filename ), utilsFileContent);
} else {
fs.writeFileSync( path.resolve( output.path, output.filename), fileContent);
}
}
});
};
function distAll() {
let file = paths.sass.theme;
let output = { path: paths.sass.dist };
let themes = globSync( paths.sass.themes );
flattenAll( themes, { file, output } );
return Promise.resolve();
}
gulp.task("dist:all", distAll);
function writeSwatches( cwds, options ) {
cwds.forEach( cwd => {
let fileGlob = path.resolve( cwd, options.swatches ).split(path.sep).join(path.posix.sep);
let files = globSync( fileGlob );
files.forEach( file => {
let json = JSON.parse( fs.readFileSync( file, 'utf-8' ) );
if ( json.hidden === true ) {
return;
}
let sassFile = path.resolve( cwd, options.output.path, `${path.basename( file, '.json')}.scss` );
let sassContent = swatchJsonTransformer( json );
fs.writeFileSync( sassFile, sassContent );
});
});
}
function swatchJsonTransformer( json ) {
const variables = [];
const universal = [];
const colorSystem = [];
const colorsMap = [];
json.groups.forEach( (group) => {
if ( group.colorsMap ) {
for ( const [ name, meta ] of Object.entries(group.colorsMap) ) {
colorsMap.push({ name: name, value: meta.value });
}
} else if ( group.colorSystem ) {
for ( const [ name, meta ] of Object.entries(group.colorSystem) ) {
colorSystem.push({ name: name, value: meta.value });
}
} else {
if (group.variables) {
for ( const [ name, meta ] of Object.entries(group.variables) ) {
variables.push({ name: name, value: meta.value });
}
}
if (group.universal) {
for ( const [ name, meta ] of Object.entries(group.universal) ) {
universal.push({ name: name, value: meta.value });
}
}
}
});
const templates = {
modern: () => {
const sassContent = [];
sassContent.push(`@forward "../scss/index.scss" with (`);
sassContent.push(colorSystem.map( (variable) => `\t$${variable.name}: ${variable.value} !default,`).join( '\n' ));
if ( colorsMap.length ) {
sassContent.push(`\t$kendo-colors: (`);
sassContent.push(colorsMap.map( (color) => `\t${color.name}: ${color.value},`).join( '\n' ));
sassContent.push(`\t) !default,`);
}
// Universal variables are also included here as they are part of the a11y swatch
sassContent.push(universal.map( (variable) => `\t$${variable.name}: ${variable.value} !default,`).join( '\n' ));
sassContent.push(`);\n`);
sassContent.push(`@use "../scss/index.scss" as *;\n`);
sassContent.push(`@include kendo-theme--styles();`);
return sassContent.join( '\n' );
},
// TODO remove legacy
legacy: () => {
const sassContent = [];
if ( !colorSystem.length ) {
sassContent.push(`$kendo-enable-color-system: false;\n`);
}
sassContent.push(colorSystem.map( (variable) => `$${variable.name}: ${variable.value};`).join( '\n' ));
if ( colorsMap.length ) {
sassContent.push(`\n$kendo-colors: (`);
sassContent.push(colorsMap.map( (color) => `\t${color.name}: ${color.value},`).join( '\n' ));
sassContent.push(`);\n`);
}
sassContent.push(`@if not ($kendo-enable-color-system) {`);
sassContent.push(variables.map( (variable) => `\t$${variable.name}: ${variable.value} !global;`).join( '\n' ));
sassContent.push(`};`);
sassContent.push(universal.map( (variable) => `$${variable.name}: ${variable.value};`).join( '\n' ));
sassContent.push(`\n@import "all.scss";`);
return sassContent.join( '\n' );
}
};
if ( json.api === 'modern' ) {
return templates.modern();
}
return templates.legacy();
}
// #endregion
// #region dist
function distSwatches() {
let output = { path: getArg('--output-path') || paths.sass.dist };
let themes = globSync( getArg('--theme') || paths.sass.themes );
let swatches = paths.sass.swatches;
writeSwatches( themes, { swatches, output } );
return Promise.resolve();
}
gulp.task("dist:swatches", distSwatches);
// #endregion