-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathgulpfile.js
235 lines (207 loc) · 7.4 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
var gulp = require("gulp");
var less = require("gulp-less");
var path = require("path");
var fs = require("fs");
var child_process = require("child_process");
var banner = require("gulp-banner");
var pkg = require("./package.json");
var rename = require("gulp-rename");
var browserSync = require("browser-sync").create();
var cleanCSS = require("gulp-clean-css");
var LessPluginAutoPrefix = require("less-plugin-autoprefix");
var autoprefixPlugin = new LessPluginAutoPrefix();
var distTarget = "./dist";
var siteStaticTarget = "./_site/static";
var docsStaticTarget = "./docs/static";
var minifiedFileExtensionName = ".min.css";
var cdnTarget = "./_cdn/skin/v" + pkg.version;
var yargs = require("yargs");
var comment = [
"/*!",
"Skin v<%= pkg.version %>",
"Copyright 2017 eBay! Inc. All rights reserved.",
"Licensed under the BSD License.",
'https://github.com/eBay/skin/blob/master/LICENSE.txt"',
"*/\n",
].join("\n");
const excludedFolders = [
"bundles",
"gh",
"mixins",
"skins",
"variables",
"icon",
];
// use for debug purposes only
const includedFolders = ["badge", "switch", "tabs"];
// filter for removing files and folders under src/less that should not be compiled by lessc
const filterSrc = (dirent) =>
dirent.isDirectory() && !excludedFolders.includes(dirent.name);
async function compileNestedModule(moduleName) {
const name = yargs.argv.name === undefined ? moduleName : yargs.argv.name;
gulp.src([`./src/less/${name}/*.less`])
.pipe(less({ plugins: [autoprefixPlugin] }))
.on("error", console.log)
.pipe(gulp.dest(`${distTarget}/${name}`));
await Promise.resolve();
}
async function compileModule(moduleName) {
const name = yargs.argv.name === undefined ? moduleName : yargs.argv.name;
gulp.src([`./src/less/${name}/${name}.less`])
.pipe(less({ plugins: [autoprefixPlugin] }))
.on("error", console.log)
.pipe(gulp.dest(`${distTarget}/${name}`));
await Promise.resolve();
}
async function compileAllModules() {
fs.readdir(
path.join(__dirname, "src/less"),
{ withFileTypes: true },
(err, files) => {
files.filter(filterSrc).forEach((dirent) => {
compileModule(dirent.name);
});
}
);
// Treat icons differently since it is a nested module
compileNestedModule("icon");
await Promise.resolve();
}
// Compile and minify given bundle to docs/static, _site/static and cdn
async function compileBundle(filename) {
gulp.src(`./src/less/bundles/${filename}`)
.pipe(banner(comment, { pkg: pkg }))
.pipe(rename((path) => (path.extname = minifiedFileExtensionName)))
.pipe(less({ plugins: [autoprefixPlugin] }))
.on("error", console.log)
.pipe(cleanCSS())
.pipe(gulp.dest(`${docsStaticTarget}`))
.pipe(gulp.dest(`${siteStaticTarget}`))
.pipe(gulp.dest(`${cdnTarget}`));
await Promise.resolve();
}
async function compileAllBundles() {
fs.readdir(path.join(__dirname, "src/less/bundles"), (err, files) => {
files.forEach((dirent) => compileBundle(dirent));
});
await Promise.resolve();
}
// Static Server + watching src & docs files
function server() {
// Start the server.
browserSync.init({ server: "_site" });
// Watch less files under src/less. Resync CSS on change.
// TODO: only re-compile modules that changed
gulp.watch("src/less/**/*.less", gulp.series("default", injectSkinCSS));
// Watch css files under src/tokens. Resync CSS on change.
// gulp.watch('src/tokens/*.css', syncTokensCss);
// Watch less files under docs. Resync CSS on change.
gulp.watch("docs/src/less/**/*.less", syncDocsCss);
// Watch js files under docs. Resync JS on change.
gulp.watch("docs/src/js/**/*.js", syncDocsJs);
// Watch svg files under src. Copy to docs on change.
gulp.watch("src/svg/*.svg", syncDocsSvg);
// Watch html files under docs. Regenerate _site on change.
gulp.watch(["docs/**/*.html", "docs/**/*.svg"], syncDocsHtml);
// Watch html & svg files under _site. Resync browser on change.
gulp.watch(["_site/**/*.html", "_site/**/*.svg"]).on(
"change",
browserSync.reload
);
}
// Inject Skin CSS into browsers
function injectSkinCSS() {
return gulp
.src(["_site/**/*.css", "_site/**/*.min.css"])
.pipe(browserSync.stream());
}
// Re-bundle the docs CSS, copy to jekyll _site/static, inject into browsers
function syncDocsCss() {
return child_process
.spawn("npm", ["run", "bundle:css"], { stdio: "inherit" })
.on("close", function () {
gulp.src(["./docs/static/docs.min.css"])
.pipe(gulp.dest(siteStaticTarget))
.pipe(browserSync.stream());
});
}
// Re-bundle the docs CSS, copy to jekyll _site/static, inject into browsers
/*
function syncTokensCss() {
return child_process
.spawn('npm', ['run', 'copy:tokensToDocs'], { stdio: 'inherit' })
.on('close', function () {
gulp.src(['./docs/static/evo-core.css', './docs/static/evo-light.css'])
.pipe(gulp.dest(siteStaticTarget))
.pipe(browserSync.stream());
});
}
*/
// Re-bundle the docs JS, copy it to jekyll _site/static, then reload browsers
function syncDocsJs() {
return child_process
.spawn("npm", ["run", "bundle:js"], { stdio: "inherit" })
.on("close", function () {
gulp.src(["./docs/static/docs.min.js"]).pipe(
gulp.dest(siteStaticTarget)
);
browserSync.reload();
});
}
// Re-bundle the docs SVG, copy it to /docs
function syncDocsSvg() {
return child_process.spawn("npm", ["run", "copy:svgToDocs"], {
stdio: "inherit",
});
}
// Run Jekyll build
function syncDocsHtml(cb) {
return child_process
.spawn(
"bundler",
[
"exec",
"jekyll",
"build",
"--config",
"docs/_config.yml,docs/_config.localhost.yml",
],
{ stdio: "inherit" }
)
.on("close", cb);
}
async function runSnapshots(storiesList, isDryRun) {
const stories =
yargs.argv.stories === undefined ? storiesList : yargs.argv.stories;
const aStories = stories.split(",");
const storiesReg = aStories.join("|");
const storiesRX = `^\\bSkin/\(?:${storiesReg})\\b`;
const storiesRXString = "\\bSkin/\\(?:" + storiesReg + ")\\b";
const dryRun = yargs.argv.dry === undefined ? isDryRun : yargs.argv.dry;
const percyExecutable = dryRun
? "snapshots:execute:dry"
: "snapshots:execute";
console.log(`
************************************************************
Running Percy Snapshot(s)...
Snapshot(s): ${stories}
Percy Stories Regex: ${storiesRXString}
Percy Dry Run: ${dryRun}
Running Snapshot(s)...
************************************************************
`);
return child_process
.spawn("npm", ["run", percyExecutable, storiesRX], { stdio: "inherit" })
.on("close", function () {
console.log("\nCompleted\n\n");
});
}
// public tasks
exports.compileModule = compileModule;
exports.compileAllModules = compileAllModules;
exports.compileAllBundles = compileAllBundles;
exports.server = server;
exports.runSnapshots = runSnapshots;
exports.default = gulp.series(
gulp.parallel(compileAllModules, compileAllBundles)
);