forked from EddieDover/fvtt-player-achievements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.mjs
233 lines (201 loc) · 6.31 KB
/
gulpfile.mjs
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
// SPDX-FileCopyrightText: 2022 Johannes Loher
// SPDX-FileCopyrightText: 2022 David Archibald
//
// SPDX-License-Identifier: MIT
import fs from "fs-extra";
import gulp from "gulp";
import { deleteAsync } from "del";
import zip from "gulp-zip";
import rename from "gulp-rename";
import sass from "gulp-dart-sass";
import sourcemaps from "gulp-sourcemaps";
import path from "node:path";
import buffer from "vinyl-buffer";
import source from "vinyl-source-stream";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import rollupStream from "@rollup/stream";
import rollupConfig from "./rollup.config.mjs";
/** ******************/
/* CONFIGURATION */
/** ******************/
const packageId = "fvtt-player-achievements";
const sourceDirectory = "./src";
const distDirectory = "./dist";
const stylesDirectory = `${sourceDirectory}/styles`;
const stylesExtension = "scss";
const sourceFileExtension = "js";
const staticFiles = ["assets", "fonts", "lang", "packs", "templates", "module.json"];
/** ******************/
/* BUILD */
/** ******************/
let cache;
/**
* Build the distributable JavaScript code
* @returns {NodeJS.ReadWriteStream}
*/
function buildCode() {
return rollupStream({ ...rollupConfig(), cache })
.on("bundle", (bundle) => {
cache = bundle;
})
.pipe(source(`${packageId}.js`))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(`${distDirectory}/module`));
}
/**
* Build style sheets
* @returns {NodeJS.ReadWriteStream}
*/
function buildStyles() {
return gulp
.src(`${stylesDirectory}/${packageId}.${stylesExtension}`)
.pipe(sass().on("error", sass.logError))
.pipe(gulp.dest(`${distDirectory}/styles`));
}
/**
* Copy static files
*/
async function copyFiles() {
for (const file of staticFiles) {
if (fs.existsSync(`${sourceDirectory}/${file}`)) {
await fs.copy(`${sourceDirectory}/${file}`, `${distDirectory}/${file}`);
}
}
}
/**
* Cleans the dist folder
* @returns {NodeJS.ReadWriteStream} The cleaned files
*/
async function cleanDist() {
return await deleteAsync([`${distDirectory}/**/*`, `${distDirectory}`]);
}
/**
* Copies the files ot the dist folder in prep for packaging
* @returns {NodeJS.ReadWriteStream} The copied files
*/
function copyDist() {
// Take everything inside the dist folder and zip it into a subfolder named totm.zip
return gulp.src(`${distDirectory}/**/*`).pipe(gulp.dest(`${distDirectory}/${packageId}`));
}
/**
* Packages the dist subfolderfolder into a zip file
* @returns {NodeJS.ReadWriteStream} The zipped files
*/
function zipDist() {
return gulp
.src(`${distDirectory}/${packageId}/**/*`, { base: `${distDirectory}` })
.pipe(zip(`${packageId}.zip`))
.pipe(gulp.dest(`${distDirectory}`));
}
/**
* Watch for changes for each build step
*/
export function watch() {
gulp.watch(`${sourceDirectory}/**/*.${sourceFileExtension}`, { ignoreInitial: false }, buildCode);
gulp.watch(`${stylesDirectory}/**/*.${stylesExtension}`, { ignoreInitial: false }, buildStyles);
gulp.watch(
staticFiles.map((file) => `${sourceDirectory}/${file}`),
{ ignoreInitial: false },
copyFiles,
);
}
export const build = gulp.series(clean, gulp.parallel(buildCode, buildStyles, copyFiles));
/********************/
/* DEV EXPORT */
/********************/
export const devexport = gulp.series(cleanDist, build, copyDist, zipDist);
/** ******************/
/* CLEAN */
/** ******************/
/**
* Remove built files from `dist` folder while ignoring source files
*/
export async function clean() {
const files = [...staticFiles, "module"];
if (fs.existsSync(`${stylesDirectory}/${packageId}.${stylesExtension}`)) {
files.push("styles");
}
console.log(" ", "Files to clean:");
console.log(" ", files.join("\n "));
for (const filePath of files) {
await fs.remove(`${distDirectory}/${filePath}`);
}
}
/** ******************/
/* PACKAGE */
/** ******************/
// Define a task to zip the contents of the /dist folder into a subfolder
gulp.task("zip-dist", () => {
return gulp
.src("dist/**/*")
.pipe(
rename((ipath) => {
// Rename to put the contents inside 'subsubsub' folder
path.dirname = `${packageId}/${ipath.dirname}`;
}),
)
.pipe(zip(`${packageId}.zip`))
.pipe(gulp.dest("."));
});
/** ******************/
/* LINK */
/** ******************/
/**
* Get the data paths of Foundry VTT based on what is configured in `foundryconfig.json`
* @returns {string[]}
*/
function getDataPaths() {
const config = fs.readJSONSync("foundryconfig.json");
const dataPath = config?.dataPath;
if (dataPath) {
const dataPaths = Array.isArray(dataPath) ? dataPath : [dataPath];
return dataPaths.map((vdataPath) => {
if (typeof vdataPath !== "string") {
throw new TypeError(
`Property dataPath in foundryconfig.json is expected to be a string or an array of strings, but found ${vdataPath}`,
);
}
if (!fs.existsSync(path.resolve(dataPath))) {
throw new Error(`The dataPath ${dataPath} does not exist on the file system`);
}
return path.resolve(dataPath);
});
} else {
throw new Error("No dataPath defined in foundryconfig.json");
}
}
/**
* Link build to User Data folder
*/
export async function link() {
let destinationDirectory;
if (fs.existsSync(path.resolve(sourceDirectory, "module.json"))) {
destinationDirectory = "modules";
} else {
throw new Error("Could not find module.json");
}
const linkDirectories = getDataPaths().map((dataPath) =>
path.resolve(dataPath, "Data", destinationDirectory, packageId),
);
const argv = yargs(hideBin(process.argv)).option("clean", {
"alias": "c",
"type": "boolean",
"default": false,
}).argv;
const cclean = argv.c;
for (const linkDirectory of linkDirectories) {
if (cclean) {
console.log(`Removing build in ${linkDirectory}.`);
await fs.remove(linkDirectory);
} else if (fs.existsSync(linkDirectory)) {
console.log(`Skipped linking to ${linkDirectory}, as it already exists.`);
} else {
console.log(`Linking dist to ${linkDirectory}.`);
await fs.ensureDir(path.resolve(linkDirectory, ".."));
await fs.symlink(path.resolve(distDirectory), linkDirectory);
}
}
}