-
Notifications
You must be signed in to change notification settings - Fork 64
/
gulpfile.js
355 lines (301 loc) · 9.89 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
const gulp = require("gulp");
const fs = require("fs-extra");
const path = require("path");
const chalk = require("chalk");
const archiver = require("archiver");
const stringify = require("json-stringify-pretty-compact");
const cp = require("child_process");
const git = require("gulp-git");
const argv = require("yargs").argv;
function getConfig() {
const configPath = path.resolve(process.cwd(), "foundryconfig.json");
let config;
if (fs.existsSync(configPath)) {
config = fs.readJSONSync(configPath);
return config;
} else {
return;
}
}
function getManifest() {
for (let name of ["system.json", "module.json"]) {
for (let root of ["public", "src", "dist"]) {
const p = path.join(root, name);
if (fs.existsSync(p)) {
return { file: fs.readJSONSync(p), root, name };
}
}
}
throw Error("Could not find manifest file");
}
/********************/
/* BUILDING */
/********************/
function build() {
return cp.spawn("npx", ["vite", "build"], { stdio: "inherit", shell: true });
}
async function rebuild_pack(name) {
console.log(chalk.green(`Rebuilding ${chalk.blueBright(name)}`));
const packPath = path.resolve(".", "dist", "packs", name);
if (await fs.exists(packPath)) {
await fs.remove(packPath);
}
await fs.ensureDir(packPath);
cp.spawnSync(
"npx",
[
"fvtt",
"package",
"pack",
"--type",
"System",
"--id",
"lancer",
"-n",
name,
"--in",
`./src/packs/${name}`,
"--out",
"./dist/packs/",
"--yaml",
],
{ stdio: "inherit", shell: true }
);
return Promise.resolve();
}
async function export_pack(name) {
console.log(chalk.green(`Extracting ${chalk.blueBright(name)}`));
// TODO: specifying output directory doesn't seem to work?
cp.spawnSync("npx", ["fvtt", "package", "unpack", "--type", "System", "--id", "lancer", "-n", name, "--yaml"], {
stdio: "inherit",
shell: true,
});
return Promise.resolve();
}
async function configure_fvtt_cli() {
const config = await fs.readJSON("foundryconfig.json");
if (config.dataPath === "/path/to/foundry/data") {
throw Error("Please configure foundryconfig.json to point to your Foundry data directory");
}
console.log(
chalk.green(`Configuring foundryvtt-cli to use ${chalk.blueBright(config.dataPath)} as Foundry data directory`)
);
cp.spawnSync("npx", ["fvtt", "configure", "set", "dataPath", config.dataPath], {
stdio: "inherit",
shell: true,
});
return Promise.resolve();
}
async function build_packs() {
await rebuild_pack("core_macros");
await rebuild_pack("lancer_info");
return Promise.resolve();
}
async function export_packs() {
await export_pack("core_macros");
await export_pack("lancer_info");
return Promise.resolve();
}
function _distWatcher() {
const publicDirPath = path.resolve(process.cwd(), "public");
const watcher = gulp.watch(["public/**/*.hbs"], { ignoreInitial: false });
watcher.on("change", async function (file, stats) {
console.log(`File ${file} was changed`);
const partial_file = path.relative(publicDirPath, file);
await fs.copy(path.join("public", partial_file), path.join("dist", partial_file));
});
}
function watch() {
_distWatcher();
return cp.spawn("npx", ["vite", "build", "-w"], { stdio: "inherit", shell: true });
}
function serve() {
_distWatcher();
// forward arguments on serves
const serveArg = process.argv[2];
let commands = ["vite", "serve"];
if (serveArg == "serve" && process.argv.length > 3) {
commands = commands.concat(process.argv.slice(3));
}
return cp.spawn("npx", commands, { stdio: "inherit", shell: true });
}
/********************/
/* LINK */
/********************/
/**
* Link build to User Data folder
*/
async function linkUserData() {
const config = fs.readJSONSync("foundryconfig.json");
if (config.dataPath === "/path/to/foundry/data") {
throw Error("Please configure foundryconfig.json to point to your Foundry data directory");
}
let destDir;
try {
if (
fs.existsSync(path.resolve(".", "dist", "module.json")) ||
fs.existsSync(path.resolve(".", "src", "module.json"))
) {
destDir = "modules";
} else if (
fs.existsSync(path.resolve(".", "dist", "system.json")) ||
fs.existsSync(path.resolve(".", "src", "system.json"))
) {
destDir = "systems";
} else {
throw Error(`Could not find ${chalk.blueBright("module.json")} or ${chalk.blueBright("system.json")}`);
}
let linkDir;
if (config.dataPath) {
if (!fs.existsSync(path.join(config.dataPath, "Data")))
throw Error("User Data path invalid, no Data directory found");
linkDir = path.join(config.dataPath, "Data", destDir, config.systemName);
} else {
throw Error("No User Data path defined in foundryconfig.json");
}
if (argv.clean || argv.c) {
console.log(chalk.yellow(`Removing build in ${chalk.blueBright(linkDir)}`));
await fs.remove(linkDir);
} else if (!fs.existsSync(linkDir)) {
console.log(chalk.green(`Copying build to ${chalk.blueBright(linkDir)}`));
await fs.symlink(path.resolve("./dist"), linkDir, "junction");
}
await configure_fvtt_cli();
return Promise.resolve();
} catch (err) {
Promise.reject(err);
}
}
/*********************/
/* PACKAGE */
/*********************/
/**
* Package build
*/
async function packageBuild() {
const manifest = getManifest();
try {
// Remove the package dir without doing anything else
if (argv.clean || argv.c) {
console.log(chalk.yellow("Removing all packaged files"));
await fs.remove("package");
return;
}
// Ensure there is a directory to hold all the packaged versions
await fs.ensureDir("package");
// Copy the manifest file to the package directory
if (await fs.exists(path.join("package", manifest.name))) {
await fs.remove(path.join("package", manifest.name));
}
await fs.copy(path.join(manifest.root, manifest.name), path.join("package", manifest.name));
// Initialize the zip file
const zipName = `${manifest.file.id}-v${manifest.file.version}.zip`;
const zipFile = fs.createWriteStream(path.join("package", zipName));
const zip = archiver("zip", { zlib: { level: 9 } });
zipFile.on("close", () => {
console.log(chalk.green(zip.pointer() + " total bytes"));
console.log(chalk.green(`Zip file ${zipName} has been written`));
return Promise.resolve();
});
zip.on("error", err => {
throw err;
});
zip.pipe(zipFile);
// Add the directory with the final code
zip.directory("dist/", manifest.file.id);
zip.finalize();
} catch (err) {
Promise.reject(err);
}
}
/*********************/
/* PACKAGE */
/*********************/
/**
* Update version and URLs in the manifest JSON
*/
function updateManifest(cb) {
const packageJson = fs.readJSONSync("package.json");
const config = getConfig(),
manifest = getManifest(),
rawURL = config.rawURL,
downloadURL = config.downloadURL,
repoURL = config.repository,
manifestRoot = manifest.root;
if (!config) cb(Error(chalk.red("foundryconfig.json not found")));
if (!manifest) cb(Error(chalk.red("Manifest JSON not found")));
if (!rawURL || !repoURL || !downloadURL) cb(Error(chalk.red("Repository URLs not configured in foundryconfig.json")));
try {
const version = argv.update || argv.u;
/* Update version */
const versionMatch = /^(\d{1,}).(\d{1,}).(\d{1,})(-[a-zA-Z0-9]{0,})?$/;
const currentVersion = manifest.file.version;
let targetVersion = "";
if (!version) {
cb(Error("Missing version number"));
}
if (versionMatch.test(version)) {
targetVersion = version;
} else {
targetVersion = currentVersion.replace(versionMatch, (substring, major, minor, patch) => {
if (version === "major") {
return `${Number(major) + 1}.0.0`;
} else if (version === "minor") {
return `${major}.${Number(minor) + 1}.0`;
} else if (version === "patch") {
return `${major}.${minor}.${Number(minor) + 1}`;
} else {
return "";
}
});
}
if (targetVersion === "") {
return cb(Error(chalk.red("Error: Incorrect version arguments.")));
}
if (targetVersion === currentVersion) {
return cb(Error(chalk.red("Error: Target version is identical to current version.")));
}
console.log(`Updating version number to '${targetVersion}'`);
packageJson.version = targetVersion;
manifest.file.version = targetVersion;
/* Update URLs */
const download = `${downloadURL}/v${manifest.file.version}/${manifest.file.id}-v${manifest.file.version}.zip`;
manifest.file.url = repoURL;
manifest.file.manifest = `${rawURL}/${manifest.name}`;
manifest.file.download = download;
const prettyProjectJson = stringify(manifest.file, { maxLength: 35 });
fs.writeJSONSync("package.json", packageJson, { spaces: 2 });
fs.writeFileSync(path.join(manifest.root, manifest.name), prettyProjectJson, "utf8");
return cb();
} catch (err) {
cb(err);
}
}
function gitAdd() {
return gulp.src(["./public/system.json", "./package.json"]).pipe(git.add({ args: "--no-all" }));
}
function gitCommit() {
return gulp.src("./*").pipe(
git.commit(`v${getManifest().file.version}`, {
args: "-a",
disableAppendPaths: true,
})
);
}
function gitTag() {
const manifest = getManifest();
return git.tag(`v${manifest.file.version}`, `Updated to ${manifest.file.version}`, err => {
if (err) throw err;
});
}
const execGit = gulp.series(gitAdd, gitCommit, gitTag);
exports.build = build;
exports.build_packs = build_packs;
exports.export_packs = export_packs;
exports.watch = watch;
exports.serve = serve;
exports.link = linkUserData;
exports.package = packageBuild;
exports.manifest = updateManifest;
exports.git = execGit;
exports.publish = gulp.series(updateManifest, build, packageBuild, execGit);