ds4/gulpfile.js

228 lines
6.4 KiB
JavaScript
Raw Normal View History

2021-06-26 22:02:00 +02:00
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
2021-03-17 12:15:25 +01:00
const { rollup } = require("rollup");
const argv = require("yargs").argv;
const chalk = require("chalk");
2021-01-06 01:31:23 +01:00
const fs = require("fs-extra");
2021-03-17 12:15:25 +01:00
const gulp = require("gulp");
2021-01-06 01:31:23 +01:00
const path = require("path");
2021-03-17 12:15:25 +01:00
const rollupConfig = require("./rollup.config");
const semver = require("semver");
2021-06-27 00:54:04 +02:00
const sass = require("gulp-sass")(require("sass"));
2021-01-06 01:31:23 +01:00
2021-03-17 12:15:25 +01:00
/********************/
/* CONFIGURATION */
/********************/
2021-01-06 01:31:23 +01:00
2021-03-17 12:15:25 +01:00
const name = path.basename(path.resolve("."));
const sourceDirectory = "./src";
const distDirectory = "./dist";
const stylesDirectory = path.join(sourceDirectory, "scss");
const stylesExtension = ".scss";
const sourceFileExtension = ".ts";
const staticFiles = ["assets", "fonts", "lang", "packs", "templates", "system.json", "template.json"];
2021-09-14 11:16:43 +02:00
const getDownloadURL = (version) => `https://git.f3l.de/dungeonslayers/ds4/-/releases/${version}/downloads/ds4.zip`;
2021-01-06 01:31:23 +01:00
/********************/
2021-03-17 12:15:25 +01:00
/* BUILD */
2021-01-06 01:31:23 +01:00
/********************/
/**
2021-03-17 12:15:25 +01:00
* Build the distributable JavaScript code
2021-01-06 01:31:23 +01:00
*/
2021-03-17 12:15:25 +01:00
async function buildCode() {
const build = await rollup({ input: rollupConfig.input, plugins: rollupConfig.plugins });
return build.write(rollupConfig.output);
2021-01-06 01:31:23 +01:00
}
/**
2021-03-17 12:15:25 +01:00
* Build style sheets
2021-01-06 01:31:23 +01:00
*/
2021-03-17 12:15:25 +01:00
function buildStyles() {
return gulp
.src(path.join(stylesDirectory, `${name}${stylesExtension}`))
.pipe(sass().on("error", sass.logError))
.pipe(gulp.dest(path.join(distDirectory, "css")));
2021-01-06 01:31:23 +01:00
}
/**
* Copy static files
*/
async function copyFiles() {
2021-03-17 12:15:25 +01:00
for (const file of staticFiles) {
if (fs.existsSync(path.join(sourceDirectory, file))) {
await fs.copy(path.join(sourceDirectory, file), path.join(distDirectory, file));
2021-01-06 01:31:23 +01:00
}
}
}
/**
* Watch for changes for each build step
*/
function buildWatch() {
2021-01-21 01:24:17 +01:00
gulp.watch(
2021-03-17 12:15:25 +01:00
path.join(sourceDirectory, "**", `*${sourceFileExtension}`).replace(/\\/g, "/"),
{ ignoreInitial: false },
buildCode,
);
gulp.watch(
path.join(stylesDirectory, "**", `*${stylesExtension}`).replace(/\\/g, "/"),
{ ignoreInitial: false },
buildStyles,
);
gulp.watch(
staticFiles.map((file) => path.join(sourceDirectory, file).replace(/\\/g, "/")),
2021-01-21 01:24:17 +01:00
{ ignoreInitial: false },
copyFiles,
);
2021-01-06 01:31:23 +01:00
}
/********************/
2021-03-17 12:15:25 +01:00
/* CLEAN */
2021-01-06 01:31:23 +01:00
/********************/
/**
2021-03-17 12:15:25 +01:00
* Remove built files from `dist` folder while ignoring source files
2021-01-06 01:31:23 +01:00
*/
async function clean() {
2021-03-17 12:15:25 +01:00
const files = [...staticFiles, "module"];
2021-01-06 01:31:23 +01:00
2021-03-17 12:15:25 +01:00
if (fs.existsSync(path.join(stylesDirectory, `${name}${stylesExtension}`))) {
files.push("css");
2021-01-06 01:31:23 +01:00
}
console.log(" ", chalk.yellow("Files to clean:"));
console.log(" ", chalk.blueBright(files.join("\n ")));
2021-03-17 12:15:25 +01:00
for (const filePath of files) {
await fs.remove(path.join(distDirectory, filePath));
2021-01-06 01:31:23 +01:00
}
}
/********************/
2021-03-17 12:15:25 +01:00
/* LINK */
2021-01-06 01:31:23 +01:00
/********************/
/**
2021-03-17 12:15:25 +01:00
* Get the data path of Foundry VTT based on what is configured in `foundryconfig.json`
2021-01-06 01:31:23 +01:00
*/
2021-03-17 12:15:25 +01:00
function getDataPath() {
2021-01-06 01:31:23 +01:00
const config = fs.readJSONSync("foundryconfig.json");
2021-03-17 12:15:25 +01:00
if (config?.dataPath) {
if (!fs.existsSync(path.resolve(config.dataPath))) {
throw new Error("User Data path invalid, no Data directory found");
2021-01-06 01:31:23 +01:00
}
2021-03-17 12:15:25 +01:00
return path.resolve(config.dataPath);
} else {
throw new Error("No User Data path defined in foundryconfig.json");
}
}
2021-01-06 01:31:23 +01:00
2021-03-17 12:15:25 +01:00
/**
* Link build to User Data folder
*/
async function linkUserData() {
let destinationDirectory;
if (fs.existsSync(path.resolve(".", sourceDirectory, "system.json"))) {
destinationDirectory = "systems";
} else {
throw new Error(`Could not find ${chalk.blueBright("system.json")}`);
}
2021-01-06 01:31:23 +01:00
2021-03-17 12:15:25 +01:00
const linkDirectory = path.resolve(getDataPath(), destinationDirectory, name);
2021-01-06 01:31:23 +01:00
2021-03-17 12:15:25 +01:00
if (argv.clean || argv.c) {
console.log(chalk.yellow(`Removing build in ${chalk.blueBright(linkDirectory)}.`));
await fs.remove(linkDirectory);
} else if (!fs.existsSync(linkDirectory)) {
console.log(chalk.green(`Copying build to ${chalk.blueBright(linkDirectory)}.`));
await fs.ensureDir(path.resolve(linkDirectory, ".."));
await fs.symlink(path.resolve(".", distDirectory), linkDirectory);
2021-01-06 01:31:23 +01:00
}
}
2021-03-17 12:15:25 +01:00
/********************/
/* VERSIONING */
/********************/
2021-01-06 01:31:23 +01:00
/**
2021-03-17 12:15:25 +01:00
* Get the contents of the manifest file as object.
2021-01-06 01:31:23 +01:00
*/
2021-03-17 12:15:25 +01:00
function getManifest() {
const manifestPath = path.join(sourceDirectory, "system.json");
if (fs.existsSync(manifestPath)) {
return {
file: fs.readJSONSync(manifestPath),
name: "system.json",
};
}
}
/**
* Get the target version based on on the current version and the argument passed as release.
*/
function getTargetVersion(currentVersion, release) {
if (["major", "premajor", "minor", "preminor", "patch", "prepatch", "prerelease"].includes(release)) {
return semver.inc(currentVersion, release);
} else {
return semver.valid(release);
}
}
/**
* Update version and download URL.
*/
function bumpVersion(cb) {
2021-01-06 01:31:23 +01:00
const packageJson = fs.readJSONSync("package.json");
2021-01-09 18:08:24 +01:00
const manifest = getManifest();
2021-01-06 01:31:23 +01:00
if (!manifest) cb(Error(chalk.red("Manifest JSON not found")));
try {
2021-03-17 12:15:25 +01:00
const release = argv.release || argv.r;
2021-01-06 01:31:23 +01:00
2021-03-17 12:15:25 +01:00
const currentVersion = packageJson.version;
2021-01-06 01:31:23 +01:00
2021-03-17 12:15:25 +01:00
if (!release) {
return cb(Error("Missing release type"));
2021-01-06 01:31:23 +01:00
}
2021-03-17 12:15:25 +01:00
const targetVersion = getTargetVersion(currentVersion, release);
2021-01-06 01:31:23 +01:00
2021-03-17 12:15:25 +01:00
if (!targetVersion) {
return cb(new Error(chalk.red("Error: Incorrect version arguments")));
2021-01-06 01:31:23 +01:00
}
if (targetVersion === currentVersion) {
2021-03-17 12:15:25 +01:00
return cb(new Error(chalk.red("Error: Target version is identical to current version")));
2021-01-06 01:31:23 +01:00
}
2021-03-17 12:15:25 +01:00
2021-01-06 01:31:23 +01:00
console.log(`Updating version number to '${targetVersion}'`);
packageJson.version = targetVersion;
2021-03-17 12:18:03 +01:00
fs.writeJSONSync("package.json", packageJson, { spaces: 4 });
2021-01-06 01:31:23 +01:00
2021-03-17 12:15:25 +01:00
manifest.file.version = targetVersion;
manifest.file.download = getDownloadURL(targetVersion);
2021-03-17 12:18:03 +01:00
fs.writeJSONSync(path.join(sourceDirectory, manifest.name), manifest.file, { spaces: 4 });
2021-01-06 01:31:23 +01:00
return cb();
} catch (err) {
cb(err);
}
}
2021-03-17 12:15:25 +01:00
const execBuild = gulp.parallel(buildCode, buildStyles, copyFiles);
2021-01-06 01:31:23 +01:00
exports.build = gulp.series(clean, execBuild);
exports.watch = buildWatch;
exports.clean = clean;
exports.link = linkUserData;
2021-03-17 12:15:25 +01:00
exports.bumpVersion = bumpVersion;