chore: replace gulp by a pure rollup based build

This commit is contained in:
Johannes Loher 2021-11-30 17:30:25 +01:00
parent 294fbefd0a
commit 0d6b89a3ed
16 changed files with 1772 additions and 2573 deletions

84
tools/bump-version.js Normal file
View file

@ -0,0 +1,84 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import fs from "fs-extra";
import path from "node:path";
import semver from "semver";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { sourceDirectory } from "./const.js";
const getDownloadURL = (version) => `https://git.f3l.de/dungeonslayers/ds4/-/releases/${version}/downloads/ds4.zip`;
const getChangelogURL = (version) => `https://git.f3l.de/dungeonslayers/ds4/-/releases/${version}`;
/**
* Get the contents of the manifest file as object.
* @returns {{file: unknown, name: string}} An object describing the manifest
*/
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.
* @param {string} currentVersion The current version
* @param {semver.ReleaseType | string} release Either a semver release type or a valid semver version
* @returns {string | null} The target version
*/
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.
* @param {semver.ReleaseType | string} release Either a semver release type or a valid semver version
*/
function bumpVersion(release) {
if (!release) {
throw new Error("Missing release type");
}
const packageJson = fs.readJSONSync("package.json");
const manifest = getManifest();
if (!manifest) throw new Error("Manifest JSON not found");
const currentVersion = packageJson.version;
const targetVersion = getTargetVersion(currentVersion, release);
if (!targetVersion) {
throw new Error("Incorrect version arguments");
}
if (targetVersion === currentVersion) {
throw new Error("Target version is identical to current version");
}
console.log(`Bumping version number to '${targetVersion}'`);
packageJson.version = targetVersion;
fs.writeJSONSync("package.json", packageJson, { spaces: 4 });
manifest.file.version = targetVersion;
manifest.file.download = getDownloadURL(targetVersion);
manifest.file.changelog = getChangelogURL(targetVersion);
fs.writeJSONSync(path.join(sourceDirectory, manifest.name), manifest.file, { spaces: 4 });
}
const argv = yargs(hideBin(process.argv)).usage("Usage: $0").option("release", {
alias: "r",
type: "string",
demandOption: true,
description: "Either a semver release type or a valid semver version",
}).argv;
const release = argv.r;
bumpVersion(release);

9
tools/const.js Normal file
View file

@ -0,0 +1,9 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
export const name = "ds4";
export const sourceDirectory = "./src";
export const distDirectory = "./dist";
export const destinationDirectory = "systems";
export const foundryconfigFile = "./foundryconfig.json";

View file

@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import promises from "node:fs/promises";
import path from "node:path";
import { distDirectory, sourceDirectory } from "./const.js";
import { convertPackFileToJSONFile } from "./json-pack-tools.js";
const packsDistDirectory = path.join(distDirectory, "packs");
const packsSourceDirectory = path.join(sourceDirectory, "packs");
console.log(`Converting pack files in ${packsDistDirectory} to json files in ${packsSourceDirectory}:`);
const conversionPromises = (await promises.readdir(packsDistDirectory, { withFileTypes: true }))
.filter((dirent) => dirent.isFile() && path.extname(dirent.name))
.map(async (dirent) => convertPackFileToJSONFile(path.join(packsDistDirectory, dirent.name), packsSourceDirectory));
await Promise.all(conversionPromises);

95
tools/json-pack-tools.js Normal file
View file

@ -0,0 +1,95 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import promises from "node:fs/promises";
import path from "node:path";
import Datastore from "@seald-io/nedb";
/**
* Removes unwanted data from a pack entry
*/
function cleanPackEntry(entry, cleanSourceId = true) {
if (cleanSourceId) {
delete entry.flags?.core?.sourceId;
}
Object.keys(entry.flags).forEach((scope) => {
if (Object.keys(entry.flags[scope]).length === 0) {
delete entry.flags[scope];
}
});
if (entry.permission) entry.permission = { default: 0 };
const embeddedDocumentCollections = [
"drawings",
"effects",
"items",
"lights",
"notes",
"results",
"sounds",
"templates",
"tiles",
"tokens",
"walls",
];
embeddedDocumentCollections
.flatMap((embeddedDocumentCollection) => entry[embeddedDocumentCollection] ?? [])
.forEach((embeddedEntry) => cleanPackEntry(embeddedEntry, false));
return entry;
}
/**
* Converts a JSON string containing an array to a Pack (NeDB) string.
* @param {string} jsonString The input JSON string
* @returns {string} The resulting Pack string
*/
export function convertJSONToPack(jsonString) {
return (
JSON.parse(jsonString)
.map((entry) => cleanPackEntry(entry))
.map((entry) => JSON.stringify(entry))
.join("\n") + "\n"
);
}
/**
* Converts a pack file (NeDB) to a JSON string.
* @param {string} filename The name of the pack file
* @returns {Promise<Array<unknown>>} A promise that resolves to an array of the documents in the pack file
*/
function convertPackFileToJSON(filename) {
const db = new Datastore({ filename, autoload: true });
return new Promise((resolve, reject) => {
db.find({}, (err, docs) => {
if (err) {
reject(err);
} else {
resolve(
JSON.stringify(
docs.map((entry) => cleanPackEntry(entry)),
undefined,
4,
) + "\n",
);
}
});
});
}
/**
* Converts a pack file (NeDB) to a JSON file and puts it in the given directory. If no directory is given, it is put
* into the same directory as the pack file.
* @param {string} filename The name of the pack file
* @param {string} [directory] A directory to put the json file into
* @returns {Promise<void>} A promise that resolves once the JSON file has been written
*/
export async function convertPackFileToJSONFile(filename, directory) {
if (directory === undefined) {
directory = path.dirname(filename);
}
console.log(" ", path.basename(filename));
const jsonFilePath = path.join(directory, `${path.basename(filename, path.extname(filename))}.json`);
const json = await convertPackFileToJSON(filename);
await promises.writeFile(jsonFilePath, json);
}

55
tools/link-package.js Normal file
View file

@ -0,0 +1,55 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import fs from "fs-extra";
import path from "node:path";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { destinationDirectory, distDirectory, foundryconfigFile, name, sourceDirectory } from "./const.js";
/**
* Get the data path of Foundry VTT based on what is configured in the {@link foundryconfigFile}.
*/
function getDataPath() {
const config = fs.readJSONSync(foundryconfigFile);
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 ${foundryconfigFile}`);
}
}
/**
* Link the built package to the user data folder.
* @param {boolean} clean Whether to remove the link instead of creating it
*/
async function linkPackage(clean) {
if (!fs.existsSync(path.resolve(sourceDirectory, "system.json"))) {
throw new Error("Could not find system.json");
}
const linkDirectory = path.resolve(getDataPath(), destinationDirectory, name);
if (clean) {
console.log(`Removing link to built package at ${linkDirectory}.`);
await fs.remove(linkDirectory);
} else if (!fs.existsSync(linkDirectory)) {
console.log(`Linking built package to ${linkDirectory}.`);
await fs.ensureDir(path.resolve(linkDirectory, ".."));
await fs.symlink(path.resolve(".", distDirectory), linkDirectory);
}
}
const argv = yargs(hideBin(process.argv)).usage("Usage: $0").option("clean", {
alias: "c",
type: "boolean",
default: false,
description: "Remove the link instead of creating it",
}).argv;
const clean = argv.c;
await linkPackage(clean);