forked from TerriaJS/TerriaMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
299 lines (253 loc) · 8.84 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*eslint-env node*/
/*eslint no-sync: 0*/
/*eslint no-process-exit: 0*/
"use strict";
/*global require*/
// If gulp tasks are run in a post-install task modules required here must be be a `dependency`
// in package.json, not just a `devDependency`. This is not currently needed.
var fs = require("fs");
var gulp = require("gulp");
var path = require("path");
var PluginError = require("plugin-error");
var terriajsServerGulpTask = require("terriajs/buildprocess/terriajsServerGulpTask");
var watchOptions = {
interval: 1000
};
gulp.task("check-terriajs-dependencies", function (done) {
var appPackageJson = require("./package.json");
var terriaPackageJson = require("terriajs/package.json");
syncDependencies(appPackageJson.dependencies, terriaPackageJson, true);
syncDependencies(appPackageJson.devDependencies, terriaPackageJson, true);
done();
});
gulp.task("write-version", function (done) {
var fs = require("fs");
var spawnSync = require("child_process").spawnSync;
const nowDate = new Date();
const dateString = `${nowDate.getFullYear()}-${
nowDate.getMonth() + 1
}-${nowDate.getDate()}`;
const packageJson = require("./package.json");
const terriajsPackageJson = require("./node_modules/terriajs/package.json");
const isClean =
spawnSync("git", ["status", "--porcelain"]).stdout.toString().length === 0;
const gitHash = spawnSync("git", ["rev-parse", "--short", "HEAD"])
.stdout.toString()
.replace("\n", "");
let version = `${dateString}-${packageJson.version}-${terriajsPackageJson.version}-${gitHash}`;
if (!isClean) {
version += " (plus local modifications)";
}
// Write version.js - which will be injected into `{{version}}` in Terria `brandBarElements`
fs.writeFileSync("version.js", "module.exports = '" + version + "';");
// Also write out a JSON file with all versions into wwwroot
fs.writeFileSync(
"wwwroot/version.json",
JSON.stringify({
date: dateString,
terriajs: terriajsPackageJson.version,
terriamap: packageJson.version,
terriamapCommitHash: gitHash,
hasLocalModifications: !isClean
})
);
done();
});
gulp.task("render-index", function renderIndex(done) {
var ejs = require("ejs");
var minimist = require("minimist");
// Arguments written in skewer-case can cause problems (unsure why), so stick to camelCase
var options = minimist(process.argv.slice(2), {
string: ["baseHref"],
default: { baseHref: "/" }
});
var index = fs.readFileSync("wwwroot/index.ejs", "utf8");
var indexResult = ejs.render(index, { baseHref: options.baseHref });
fs.writeFileSync(path.join("wwwroot", "index.html"), indexResult);
done();
});
gulp.task(
"build-app",
gulp.parallel(
"render-index",
gulp.series(
"check-terriajs-dependencies",
"write-version",
function buildApp(done) {
var runWebpack = require("terriajs/buildprocess/runWebpack.js");
var webpack = require("webpack");
var webpackConfig = require("./buildprocess/webpack.config.js")(true);
checkForDuplicateCesium();
runWebpack(webpack, webpackConfig, done);
}
)
)
);
gulp.task(
"release-app",
gulp.parallel(
"render-index",
gulp.series(
"check-terriajs-dependencies",
"write-version",
function releaseApp(done) {
var runWebpack = require("terriajs/buildprocess/runWebpack.js");
var webpack = require("webpack");
var webpackConfig = require("./buildprocess/webpack.config.js")(false);
checkForDuplicateCesium();
runWebpack(
webpack,
Object.assign({}, webpackConfig, {
plugins: webpackConfig.plugins || []
}),
done
);
}
)
)
);
gulp.task(
"watch-render-index",
gulp.series("render-index", function watchRenderIndex() {
gulp.watch(["wwwroot/index.ejs"], gulp.series("render-index"));
})
);
gulp.task(
"watch-app",
gulp.parallel(
"watch-render-index",
gulp.series("check-terriajs-dependencies", function watchApp(done) {
var fs = require("fs");
var watchWebpack = require("terriajs/buildprocess/watchWebpack");
var webpack = require("webpack");
var webpackConfig = require("./buildprocess/webpack.config.js")(
true,
false
);
checkForDuplicateCesium();
fs.writeFileSync("version.js", "module.exports = 'Development Build';");
watchWebpack(webpack, webpackConfig, done);
})
)
);
gulp.task("copy-terriajs-assets", function () {
var terriaWebRoot = path.join(getPackageRoot("terriajs"), "wwwroot");
var sourceGlob = path.join(terriaWebRoot, "**");
var destPath = path.resolve(__dirname, "wwwroot", "build", "TerriaJS");
return gulp
.src([sourceGlob], { base: terriaWebRoot })
.pipe(gulp.dest(destPath));
});
gulp.task(
"watch-terriajs-assets",
gulp.series("copy-terriajs-assets", function waitForTerriaJsAssetChanges() {
var terriaWebRoot = path.join(getPackageRoot("terriajs"), "wwwroot");
var sourceGlob = path.join(terriaWebRoot, "**");
// gulp.watch as of gulp v4.0.0 doesn't work with backslashes (the task is never triggered).
// But Windows is ok with forward slashes, so use those instead.
if (path.sep === "\\") {
sourceGlob = sourceGlob.replace(/\\/g, "/");
}
gulp.watch(sourceGlob, watchOptions, gulp.series("copy-terriajs-assets"));
})
);
gulp.task("lint", function (done) {
var runExternalModule = require("terriajs/buildprocess/runExternalModule");
runExternalModule("eslint/bin/eslint.js", [
"-c",
path.join(getPackageRoot("terriajs"), ".eslintrc"),
"--ignore-pattern",
"lib/ThirdParty",
"--max-warnings",
"0",
"index.js",
"lib"
]);
done();
});
function getPackageRoot(packageName) {
return path.dirname(require.resolve(packageName + "/package.json"));
}
gulp.task("clean", function (done) {
var fs = require("fs-extra");
// // Remove build products
fs.removeSync(path.join("wwwroot", "build"));
done();
});
gulp.task("sync-terriajs-dependencies", function (done) {
var appPackageJson = require("./package.json");
var terriaPackageJson = require("terriajs/package.json");
syncDependencies(appPackageJson.dependencies, terriaPackageJson);
syncDependencies(appPackageJson.devDependencies, terriaPackageJson);
fs.writeFileSync(
"./package.json",
JSON.stringify(appPackageJson, undefined, " ")
);
console.log(
"TerriaMap's package.json has been updated. Now run yarn install."
);
done();
});
function syncDependencies(dependencies, targetJson, justWarn) {
for (var dependency in dependencies) {
if (dependencies.hasOwnProperty(dependency)) {
var version =
targetJson.dependencies[dependency] ||
targetJson.devDependencies[dependency];
if (version && version !== dependencies[dependency]) {
if (justWarn) {
console.warn(
"Warning: There is a version mismatch for " +
dependency +
". This build may fail or hang. You should run `gulp sync-terriajs-dependencies`, then re-run `npm install`, then run gulp again."
);
} else {
console.log(
"Updating " +
dependency +
" from " +
dependencies[dependency] +
" to " +
version +
"."
);
dependencies[dependency] = version;
}
}
}
}
}
function checkForDuplicateCesium() {
var fse = require("fs-extra");
if (
fse.existsSync("node_modules/terriajs-cesium") &&
fse.existsSync("node_modules/terriajs/node_modules/terriajs-cesium")
) {
console.log(
"You have two copies of terriajs-cesium, one in this application's node_modules\n" +
"directory and the other in node_modules/terriajs/node_modules/terriajs-cesium.\n" +
"This leads to strange problems, such as knockout observables not working.\n" +
"Please verify that node_modules/terriajs-cesium is the correct version and\n" +
" rm -rf node_modules/terriajs/node_modules/terriajs-cesium\n" +
"Also consider running:\n" +
" yarn gulp sync-terriajs-dependencies\n" +
"to prevent this problem from recurring the next time you `npm install`."
);
throw new PluginError(
"checkForDuplicateCesium",
"You have two copies of Cesium.",
{ showStack: false }
);
}
}
gulp.task("terriajs-server", terriajsServerGulpTask(3001));
gulp.task("build", gulp.series("copy-terriajs-assets", "build-app"));
gulp.task("release", gulp.series("copy-terriajs-assets", "release-app"));
gulp.task("watch", gulp.parallel("watch-terriajs-assets", "watch-app"));
// Run render-index before starting terriajs-server because terriajs-server won't
// start if index.html isn't present
gulp.task(
"dev",
gulp.parallel(gulp.series("render-index", "terriajs-server"), "watch")
);
gulp.task("default", gulp.series("lint", "build"));