forked from vttred/ose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
188 lines (161 loc) · 4.23 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
const fs = require("fs-extra");
const gulp = require("gulp");
const sass = require("gulp-dart-sass");
const sourcemaps = require("gulp-sourcemaps");
const path = require("path");
const buffer = require("vinyl-buffer");
const source = require("vinyl-source-stream");
const yargs = require("yargs/yargs");
const { hideBin } = require("yargs/helpers");
const rollupStream = require("@rollup/stream");
const rollupConfig = require("./rollup.config.js");
/********************/
/* CONFIGURATION */
/********************/
const name = "ose";
const sourceDirectory = "./src";
const distDirectory = "./dist";
const stylesDirectory = `scss`;
const stylesExtension = "scss";
const sourceFileExtension = "js";
const staticFiles = ["lang", "packs", "templates"];
/********************/
/* BUILD */
/********************/
let cache;
/**
* Build the distributable JavaScript code
*/
function buildCode() {
return rollupStream({ ...rollupConfig(), cache })
.on("bundle", (bundle) => {
cache = bundle;
})
.pipe(source(`${name}.js`))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(`${distDirectory}/module`));
}
/**
* Build style sheets
*/
function buildStyles() {
return gulp
.src(`${sourceDirectory}/${name}.${stylesExtension}`)
.pipe(sass().on("error", sass.logError))
.pipe(gulp.dest(`${distDirectory}/`));
}
/**
* Copy static files
*/
async function copyFiles() {
for (const file of staticFiles) {
if (fs.existsSync(`${sourceDirectory}/${file}`)) {
await fs.copy(`${sourceDirectory}/${file}`, `${distDirectory}/${file}`);
}
}
}
/**
* Watch for changes for each build step
*/
function watch() {
gulp.watch(
`${sourceDirectory}/**/*.${sourceFileExtension}`,
{ ignoreInitial: false },
buildCode
);
gulp.watch(
`${sourceDirectory}/**/*.${stylesExtension}`,
{ ignoreInitial: false },
buildStyles
);
gulp.watch(
staticFiles.map((file) => `${sourceDirectory}/${file}`),
{ ignoreInitial: false },
copyFiles
);
}
const build = gulp.series(
clean,
gulp.parallel(buildCode, buildStyles, copyFiles)
);
/********************/
/* CLEAN */
/********************/
/**
* Remove built files from `dist` folder while ignoring source files
*/
async function clean() {
const files = [...staticFiles, "module"];
if (fs.existsSync(`${stylesDirectory}/${name}.${stylesExtension}`)) {
files.push("styles");
}
console.log(" ", "Files to clean:");
console.log(" ", files.join("\n "));
for (const filePath of files) {
await fs.remove(`${distDirectory}/${filePath}`);
}
}
/********************/
/* LINK */
/********************/
/**
* Get the data path of Foundry VTT based on what is configured in `foundryconfig.json`
*/
function getDataPath() {
const config = fs.readJSONSync("foundryconfig.json");
if (config?.dataPath) {
if (!fs.existsSync(path.resolve(config.dataPath))) {
throw new Error("User Data path invalid, no Data directory found");
}
return path.resolve(config.dataPath);
} else {
throw new Error("No User Data path defined in foundryconfig.json");
}
}
function getSymLinkName() {
const config = fs.readJSONSync("foundryconfig.json");
if (config?.symLinkName) {
return config.symLinkName;
} else {
return "ose";
}
}
/**
* Link build to User Data folder
*/
async function link() {
let destinationDirectory;
if (fs.existsSync(path.resolve("./", "system.json"))) {
destinationDirectory = "systems";
} else {
throw new Error("Could not find system.json");
}
const linkDirectory = path.resolve(
getDataPath(),
"Data",
destinationDirectory,
getSymLinkName()
);
const argv = yargs(hideBin(process.argv)).option("clean", {
alias: "c",
type: "boolean",
default: false,
}).argv;
const clean = argv.c;
if (clean) {
console.log(`Removing build in ${linkDirectory}.`);
await fs.remove(linkDirectory);
} else if (!fs.existsSync(linkDirectory)) {
console.log(`Linking dist to ${linkDirectory}.`);
await fs.ensureDir(path.resolve(linkDirectory, ".."));
await fs.symlink(path.resolve("."), linkDirectory);
}
}
module.exports = {
watch,
build,
clean,
link,
};