-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgulpfile.js
More file actions
281 lines (261 loc) · 8.24 KB
/
gulpfile.js
File metadata and controls
281 lines (261 loc) · 8.24 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import gulp from "gulp";
import fs from "fs";
import path from "path/posix";
import mkdirp from "mkdirp";
import webpack from "webpack";
import git from "git-last-commit";
import { execa } from "execa";
import { fileURLToPath } from "url";
import jsonfile from "jsonfile";
import webpackElectronConfig from "./webpack.electron.config.js";
import helper from "./src/utils/buildhelper_util.js";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
let config = helper.rebuildCablesJson(__dirname, process.env.npm_config_apiconfig, "../gen/electron/");
let distConfig = helper.rebuildCablesJson(__dirname, "dist", "./dist/", "cables.json");
let analyze = false;
const isLiveBuild = config.env === "electron";
const minify = config.hasOwnProperty("minifyJs") ? config.minifyJs : false;
const watchers = [];
function _watch(done)
{
const watchOptions = { "ignored": (file) =>
{
const basename = path.basename(file);
return file.includes("/node_modules/") || basename.startsWith(".");
} };
watchers.push(gulp.watch(["src_client/**/*.js", "../shared/shared_constants.json", "../shared/client/**/*.js"], watchOptions, gulp.series(defaultSeries)));
watchers.push(gulp.watch(["src/**/*.js", "../shared/api/**/*.js"], watchOptions, gulp.series(electronChanges)));
done();
}
function electronChanges(done)
{
console.log("\x1b[33m Registered changes that require a restart of electron! \x1b[0m");
done();
}
function _analyze(done)
{
analyze = true;
done();
}
function _serve(done)
{
let args = process.argv.slice(3);
execa(
"electron",
[".", ...args],
{ "preferLocal": true, "stdout": "inherit", "stderr": "inherit" }).then((o, te, thr) =>
{
watchers.forEach((watcher) =>
{
watcher.close();
});
});
done();
}
function _create_ops_dirs(done)
{
const opsPath = path.join("./src", config.path.ops);
fs.rmSync("ops", { "recursive": true, "force": true });
mkdirp.sync(path.join(opsPath, "/base/"));
mkdirp.sync(path.join(opsPath, "/extensions/"));
mkdirp.sync(path.join(opsPath, "/patches/"));
mkdirp.sync(path.join(opsPath, "/teams/"));
mkdirp.sync(path.join(opsPath, "/users/"));
done();
}
function _libs_copy(done)
{
const source = path.join("./src", distConfig.sourcePath.libs);
const target = path.join("./src", distConfig.path.libs);
mkdirp.sync(target);
if (fs.existsSync(source))
{
console.info("copying libs from", source, "to", target);
return gulp.src(source + "**", { "encoding": false }).pipe(gulp.dest(target));
}
else
{
console.error("FAILED to copy libs from", source, "to", target);
done();
}
}
function _corelibs_copy(done)
{
const source = path.join("./src", distConfig.sourcePath.corelibs);
const target = path.join("./src", distConfig.path.corelibs);
mkdirp.sync(target);
if (fs.existsSync(source))
{
console.info("copying corelibs from", source, "to", target);
return gulp.src(source + "**", { "encoding": false }).pipe(gulp.dest(target));
}
else
{
console.error("FAILED to copy corelibs from", source, "to", target);
done();
}
}
function _core_ops_copy(done)
{
const source = path.join("./src", distConfig.sourcePath.ops, "/base/");
const target = path.join("./src", distConfig.path.ops, "/base/");
mkdirp.sync(target);
if (fs.existsSync(source))
{
console.info("copying ops from", source, "to", target);
return gulp.src(source + "**", { "encoding": false }).pipe(gulp.dest(target));
}
else
{
console.error("FAILED to copy ops from", source, "to", target);
done();
}
}
function _extension_ops_copy(done)
{
const source = path.join("./src", distConfig.sourcePath.ops, "/extensions/");
const target = path.join("./src", distConfig.path.ops, "/extensions/");
mkdirp.sync(target);
if (fs.existsSync(source))
{
const extensionGlobs = [];
const allExtensions = fs.readdirSync(source);
allExtensions.forEach((extension) =>
{
if (extension.startsWith("Ops."))
{
const configFile = path.join(source, extension, extension + ".json");
if (fs.existsSync(configFile))
{
try
{
const extensionConfig = jsonfile.readFileSync(configFile);
if (extensionConfig.visibility !== "private")
{
extensionGlobs.push(path.join(source, extension, "**"));
}
}
catch (e)
{
console.warn("FAILED to parse extension config", configFile, "treating as public extension");
extensionGlobs.push(path.join(source, extension, "**"));
}
}
else
{
extensionGlobs.push(path.join(source, extension, "**"));
}
}
});
console.info("copying public/unlisted extensions from", source, "to", target);
return gulp.src(extensionGlobs, { "encoding": false, "base": source }).pipe(gulp.dest(target));
}
else
{
console.warn("FAILED to copy extensions from", source, "to", target);
done();
}
}
function _ui_copy(done)
{
const source = path.join("./src", distConfig.sourcePath.uiDist);
const target = path.join("./src", distConfig.path.uiDist);
mkdirp.sync(target);
if (fs.existsSync(source))
{
console.info("copying ui from", source, "to", target);
return gulp.src(source + "**", { "encoding": false }).pipe(gulp.dest(target));
}
else
{
console.error("FAILED to copy ui from", source, "to", target);
done();
}
}
function _editor_scripts_webpack(done)
{
getBuildInfo((buildInfo) =>
{
webpack(webpackElectronConfig(isLiveBuild, buildInfo, minify, analyze, config.sourceMap), (err, stats) =>
{
if (err) done(err);
if (stats.hasErrors())
{
done(new Error(getWebpackErrorMessage(stats)));
}
else
{
done();
}
});
});
}
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;
const em = m.message.split("\n");
errorMessage = filename + ":" + m.loc + " - " + em[0] || m.message;
});
}
}
return errorMessage;
}
const getBuildInfo = (cb) =>
{
const date = new Date();
git.getLastCommit((err, commit) =>
{
const info = {
"timestamp": date.getTime(),
"created": date.toISOString(),
"git": {
"branch": commit.branch,
"commit": commit.hash,
"date": commit.committedOn,
"message": commit.subject
}
};
if (commit.tags && commit.tags.length > 0)
{
info.git.tag = commit.tags[0];
}
cb(info);
});
};
/*
* -------------------------------------------------------------------------------------------
* MAIN TASKS
* -------------------------------------------------------------------------------------------
*/
const defaultSeries = gulp.series(
_editor_scripts_webpack,
);
gulp.task("build", gulp.series(
_create_ops_dirs,
gulp.parallel(
defaultSeries,
_corelibs_copy,
_core_ops_copy,
_extension_ops_copy,
_libs_copy,
_ui_copy
),
));
gulp.task("analyze", gulp.series(_analyze, defaultSeries));
gulp.task("watch", gulp.series(
defaultSeries,
gulp.parallel(
_serve,
_watch
)
));