-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
323 lines (301 loc) · 9.6 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
const gulp = require("gulp");
const path = require("path");
var fs = require("fs");
const del = require("del");
const ts = require("gulp-typescript");
const sm = require("gulp-sourcemaps");
const zip = require("gulp-zip");
const rename = require("gulp-rename");
const minify = require("gulp-minify");
const tabify = require("gulp-tabify");
const stringify = require("json-stringify-pretty-compact");
const GLOB = "**/*";
const DIST = "dist/";
const BUNDLE = "package/";
const SOURCE = "src/";
const LANG = "lang/";
const TEMPLATES = "templates/";
const CSS = "css/";
const ICON = "icons/";
const PACK = "packs/";
const CONFIG = "sample-config/";
const SOUND = "sounds/";
var PACKAGE = JSON.parse(fs.readFileSync("package.json"));
function reloadPackage(cb) {
PACKAGE = JSON.parse(fs.readFileSync("package.json"));
cb();
}
function DEV_DIST() {
return path.join(PACKAGE.devDir, PACKAGE.name) + "/";
}
String.prototype.replaceAll = function (pattern, replace) {
return this.split(pattern).join(replace);
};
function pdel(patterns, options) {
return () => {
return del(patterns, options);
};
}
function plog(message) {
return (cb) => {
console.log(message);
cb();
};
}
/**
* Compile the source code into the distribution directory
* @param {Boolean} keepSources Include the TypeScript SourceMaps
*/
function buildSource(keepSources, minifySources = false, output = null) {
return () => {
var stream = gulp.src(SOURCE + GLOB);
// if (keepSources) stream = stream.pipe(sm.init())
stream = stream.pipe(ts.createProject("tsconfig.json")());
// if (keepSources) stream = stream.pipe(sm.write())
if (minifySources)
stream = stream.pipe(
minify({
ext: { min: ".js" },
mangle: false,
noSource: true,
}),
);
else stream = stream.pipe(tabify(4, false));
return stream.pipe(gulp.dest((output || DIST) + SOURCE));
};
}
exports.step_buildSourceDev = buildSource(false);
exports.step_buildSource = buildSource(false);
exports.step_buildSourceMin = buildSource(false, true);
/**
* Builds the module manifest based on the package, sources, and css.
*/
function buildManifest(output = null) {
const files = []; // Collector for all the file paths
return (cb) =>
gulp
.src(PACKAGE.main) // collect the source files
.pipe(rename({ extname: ".js" })) // rename their extensions to `.js`
.pipe(gulp.src(CSS + GLOB)) // grab all the CSS files
.on("data", (file) => files.push(path.relative(file.cwd, file.path))) // Collect all the file paths
.on("end", () => {
// output the filepaths to the module.json
if (files.length == 0)
throw Error("No files found in " + SOURCE + GLOB + " or " + CSS + GLOB);
const js = files.filter((e) => e.endsWith("js")); // split the CSS and JS files
const css = files.filter((e) => e.endsWith("css"));
fs.readFile("module.json", (err, data) => {
const module = data
.toString() // Inject the data into the module.json
.replaceAll("{{name}}", PACKAGE.name)
.replaceAll("{{title}}", PACKAGE.title)
.replaceAll("{{version}}", PACKAGE.version)
.replaceAll("{{description}}", PACKAGE.description)
.replace('"{{sources}}"', stringify(js, null, "\t").replaceAll("\n", "\n\t"))
.replace('"{{css}}"', stringify(css, null, "\t").replaceAll("\n", "\n\t"));
fs.writeFile((output || DIST) + "module.json", module, cb); // save the module to the distribution directory
});
});
}
exports.step_buildManifest = buildManifest();
function outputLanguages(output = null) {
return () => gulp.src(LANG + GLOB).pipe(gulp.dest((output || DIST) + LANG));
}
function outputTemplates(output = null) {
return () => gulp.src(TEMPLATES + GLOB).pipe(gulp.dest((output || DIST) + TEMPLATES));
}
function outputStylesCSS(output = null) {
return () => gulp.src(CSS + GLOB).pipe(gulp.dest((output || DIST) + CSS));
}
function outputMetaFiles(output = null) {
return () => gulp.src(["LICENSE", "README.md"]).pipe(gulp.dest(output || DIST));
}
function outputIconFiles(output = null) {
return () => gulp.src(ICON + GLOB).pipe(gulp.dest((output || DIST) + ICON));
}
function outputPackFiles(output = null) {
return () => gulp.src(PACK + GLOB).pipe(gulp.dest((output || DIST) + PACK));
}
function outputSoundFiles(output = null) {
return () => gulp.src(SOUND + GLOB).pipe(gulp.dest((output || DIST) + SOUND));
}
function outputConfigFiles(output = null) {
return () => gulp.src(CONFIG + GLOB).pipe(gulp.dest((output || DIST) + CONFIG));
}
/**
* Copy files to module named directory and then compress that folder into a zip
*/
function compressDistribution() {
return gulp.series(
// Copy files to folder with module's name
() => gulp.src(DIST + GLOB).pipe(gulp.dest(DIST + `${PACKAGE.name}/${PACKAGE.name}`)),
// Compress the new folder into a ZIP and save it to the `bundle` folder
() =>
gulp
.src(DIST + PACKAGE.name + "/" + GLOB)
.pipe(zip(PACKAGE.name + ".zip"))
.pipe(gulp.dest(BUNDLE)),
// Copy the module.json to the bundle directory
() => gulp.src(DIST + "/module.json").pipe(gulp.dest(BUNDLE)),
// Cleanup by deleting the intermediate module named folder
pdel(DIST + PACKAGE.name),
);
}
exports.step_compressDistribution = compressDistribution();
/**
* Simple clean command
*/
exports.clean = pdel([DIST, BUNDLE]);
exports.devClean = pdel([DEV_DIST()]);
/**
* Default Build operation
*/
exports.default = gulp.series(
pdel([DIST]),
gulp.parallel(
buildSource(true, false),
buildManifest(),
outputLanguages(),
outputTemplates(),
outputStylesCSS(),
outputMetaFiles(),
outputIconFiles(),
outputPackFiles(),
outputSoundFiles(),
outputConfigFiles(),
),
);
/**
* Extends the default build task by copying the result to the Development Environment
*/
exports.dev = gulp.series(
pdel([DEV_DIST() + GLOB], { force: true }),
gulp.parallel(
buildSource(true, false, DEV_DIST()),
buildManifest(DEV_DIST()),
outputLanguages(DEV_DIST()),
outputTemplates(DEV_DIST()),
outputStylesCSS(DEV_DIST()),
outputMetaFiles(DEV_DIST()),
outputIconFiles(DEV_DIST()),
outputPackFiles(DEV_DIST()),
outputSoundFiles(DEV_DIST()),
outputConfigFiles(DEV_DIST()),
),
);
/**
* Performs a default build and then zips the result into a bundle
*/
exports.zip = gulp.series(
pdel([DIST]),
gulp.parallel(
buildSource(false, false),
buildManifest(),
outputLanguages(),
outputTemplates(),
outputStylesCSS(),
outputMetaFiles(),
outputIconFiles(),
outputPackFiles(),
outputSoundFiles(),
outputConfigFiles(),
),
compressDistribution(),
pdel([DIST]),
);
/**
* Sets up a file watch on the project to detect any file changes and automatically rebuild those components.
*/
exports.watch = function () {
exports.default();
gulp.watch(SOURCE + GLOB, gulp.series(pdel(DIST + SOURCE), buildSource(true, false)));
gulp.watch([CSS + GLOB, "module.json", "package.json"], buildManifest());
gulp.watch(LANG + GLOB, gulp.series(pdel(DIST + LANG), outputLanguages()));
gulp.watch(TEMPLATES + GLOB, gulp.series(pdel(DIST + TEMPLATES), outputTemplates()));
gulp.watch(CSS + GLOB, gulp.series(pdel(DIST + CSS), outputStylesCSS()));
gulp.watch(["LICENSE", "README.md", "Changelog.md"], outputMetaFiles());
gulp.watch(ICON + GLOB, gulp.series(pdel(DIST + ICON), outputIconFiles()));
gulp.watch(PACK + GLOB, gulp.series(pdel(DIST + PACK), outputPackFiles()));
gulp.watch(SOUND + GLOB, gulp.series(pdel(DIST + SOUND), outputSoundFiles()));
gulp.watch(CONFIG + GLOB, gulp.series(pdel(DIST + CONFIG), outputConfigFiles()));
};
/**
* Sets up a file watch on the project to detect any file changes and automatically rebuild those components, and then copy them to the Development Environment.
*/
exports.devWatch = function () {
const devDist = DEV_DIST();
exports.dev();
gulp.watch(
SOURCE + GLOB,
gulp.series(
plog("deleting: " + devDist + SOURCE + GLOB),
pdel(devDist + SOURCE + GLOB, { force: true }),
buildSource(true, false, devDist),
plog("sources done."),
),
);
gulp.watch(
[CSS + GLOB, "module.json", "package.json"],
gulp.series(reloadPackage, buildManifest(devDist), plog("manifest done.")),
);
gulp.watch(
LANG + GLOB,
gulp.series(
pdel(devDist + LANG + GLOB, { force: true }),
outputLanguages(devDist),
plog("langs done."),
),
);
gulp.watch(
TEMPLATES + GLOB,
gulp.series(
pdel(devDist + TEMPLATES + GLOB, { force: true }),
outputTemplates(devDist),
plog("templates done."),
),
);
gulp.watch(
CSS + GLOB,
gulp.series(
pdel(devDist + CSS + GLOB, { force: true }),
outputStylesCSS(devDist),
plog("css done."),
),
);
gulp.watch(
["LICENSE", "README.md", "CHANGELOG.md"],
gulp.series(outputMetaFiles(devDist), plog("metas done.")),
);
gulp.watch(
ICON + GLOB,
gulp.series(
pdel(devDist + ICON + GLOB, { force: true }),
outputIconFiles(devDist),
plog("icon done."),
),
);
gulp.watch(
PACK + GLOB,
gulp.series(
pdel(devDist + PACK + GLOB, { force: true }),
outputPackFiles(devDist),
plog("packs done."),
),
);
gulp.watch(
SOUND + GLOB,
gulp.series(
pdel(devDist + SOUND + GLOB, { force: true }),
outputSoundFiles(devDist),
plog("sounds done."),
),
);
gulp.watch(
CONFIG + GLOB,
gulp.series(
pdel(devDist + CONFIG + GLOB, { force: true }),
outputConfigFiles(devDist),
plog("config done."),
),
);
};