-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.js
248 lines (225 loc) · 6.45 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
import gulp from "gulp";
import sass from "gulp-sass-no-nodesass";
import webpack from "webpack";
import git from "git-last-commit";
import autoprefixer from "gulp-autoprefixer";
import replace from "gulp-replace";
import fs from "fs";
import svgmin from "gulp-svgmin";
import svgcss from "gulp-svg-css";
import rename from "gulp-rename";
import concat from "gulp-concat";
import sassCompiler from "sass";
import webpackConfig from "./webpack.config.js";
import webpackTalkerApiConfig from "./webpack.talkerapi.config.js";
import webpackLibsConfig from "./webpack.libs.config.js";
sass.compiler = sassCompiler;
let configLocation = "../cables_api/cables.json";
if (process.env.npm_config_apiconfig) configLocation = "../cables_api/cables_env_" + process.env.npm_config_apiconfig + ".json";
let analyze = false;
let isLiveBuild = false;
let minify = false;
let config = {};
if (fs.existsSync(configLocation))
{
config = JSON.parse(fs.readFileSync(configLocation, "utf-8"));
isLiveBuild = config.env === "live";
minify = config.hasOwnProperty("minifyJs") ? config.minifyJs : false;
}
function _scripts_libs_ui(done)
{
getBuildInfo((buildInfo) =>
{
webpack(webpackLibsConfig(isLiveBuild, buildInfo, minify, analyze), (err, stats) =>
{
if (err) done(err);
if (stats.hasErrors())
{
done(new Error(getWebpackErrorMessage(stats)));
}
else
{
done();
}
});
});
}
function _scripts_talkerapi(done)
{
getBuildInfo((buildInfo) =>
{
webpack(webpackTalkerApiConfig(isLiveBuild, buildInfo, minify, analyze), (err, stats) =>
{
if (err) done(err);
if (stats.hasErrors())
{
done(new Error(getWebpackErrorMessage(stats)));
}
else
{
done();
}
}
);
});
}
function _scripts_core()
{
return gulp
.src(["../cables/build/**/*.*", "!../cables/build/libs/*", "!../cables/build/*.html"])
.pipe(gulp.dest("dist/js/"));
}
function _scripts_ui_webpack(done)
{
getBuildInfo((buildInfo) =>
{
webpack(webpackConfig(isLiveBuild, buildInfo, minify, analyze), (err, stats) =>
{
if (err) done(err);
if (stats.hasErrors())
{
done(new Error(getWebpackErrorMessage(stats)));
}
else
{
done();
}
});
});
}
function getBuildInfo(cb)
{
const date = new Date();
git.getLastCommit((err, commit) =>
{
const buildInfo = {
"timestamp": date.getTime(),
"created": date.toISOString(),
"git": {
"branch": commit.branch,
"commit": commit.hash,
"date": commit.committedOn,
"message": commit.subject
}
};
fs.writeFile("./dist/buildinfo.json", JSON.stringify(buildInfo), () =>
{
cb(buildInfo);
});
});
}
function getWebpackErrorMessage(stats)
{
let errorMessage = stats.compilation.errors.join("\n");
const errorsWarnings = stats.toJson("errors-warnings");
if (errorsWarnings && errorsWarnings.errors)
{
const modules = errorsWarnings.errors.filter((e) => { return !!e.moduleIdentifier; });
if (modules && modules.length > 0)
{
modules.forEach((m) =>
{
const parts = m.moduleIdentifier.split("|");
const filename = parts.length > 0 ? parts[1] : m.moduleIdentifier;
errorMessage = filename + ":" + m.loc + " - " + m.message;
});
}
}
return errorMessage;
}
function _html_ui(done)
{
return gulp
.src(["html/ui/header.html", "html/ui/templates/*.html", "html/ui/footer.html"])
.pipe(concat("index.html"))
.pipe(gulp.dest("dist/"));
}
function _sass(done)
{
return gulp
.src("scss/style-dark.scss")
.pipe(sass().on("error", sass.logError))
.pipe(rename("style-dark.css"))
.pipe(
autoprefixer({
"cascade": false,
})
)
.pipe(gulp.dest("dist/css"));
}
function _svgcss(done)
{
const task = gulp
.src("icons/**/*.svg")
.pipe(svgmin())
.pipe(
svgcss({
"fileName": "icons",
"cssPrefix": "icon-",
"addSize": false,
})
)
.pipe(replace("background-image", "mask"))
.pipe(
autoprefixer({
"cascade": false,
})
)
.pipe(rename("svgicons.scss"))
.pipe(gulp.dest("scss/"));
if (!process.env.cables_electron || process.env.cables_electron === "false")
{
return task.pipe(gulp.dest("../cables_api/scss/"));
}
else
{
return task;
}
}
function _watch(done)
{
const watchOptions = { "usePolling": true, "ignored": (fileName) => { return fileName.includes("node_modules"); } };
gulp.watch(["src/ui/**/*.js", "src/ui/*.js", "src/ui/**/*.json", "src/ui/**/*.frag", "src/ui/**/*.vert", "../shared/client/*.js", "../shared/client/**/*.js"], watchOptions, gulp.series(_scripts_ui_webpack));
gulp.watch(["scss/**/*.scss", "scss/*.scss"], watchOptions, gulp.series(_sass));
gulp.watch(["html/**/*.html", "html/*.html"], watchOptions, gulp.series(_html_ui));
gulp.watch("../shared/client/src/talkerapi.js", watchOptions, gulp.series(_scripts_talkerapi));
gulp.watch("libs/**/*", watchOptions, gulp.series(_scripts_libs_ui));
gulp.watch("icons/**/*.svg", watchOptions, gulp.series(_svgcss));
done();
}
function _analyze(done)
{
analyze = true;
done();
}
/*
* -------------------------------------------------------------------------------------------
* MAIN TASKS
* -------------------------------------------------------------------------------------------
*/
const defaultSeries = gulp.series(
_svgcss,
_html_ui,
_scripts_libs_ui,
_scripts_core,
_scripts_ui_webpack,
_scripts_talkerapi,
_sass,
);
/**
* Run "gulp build"
*/
gulp.task("build", defaultSeries);
gulp.task("analyze", gulp.series(_analyze, defaultSeries));
/**
* Default Task, for development
* Run "gulp"
*/
gulp.task("default", gulp.series(
defaultSeries,
_watch
));
gulp.task("svgcss", _svgcss);
gulp.task("testui", gulp.series(
_scripts_ui_webpack
));