From 9e037818a688bac93b4b13f3a71a38404f08b73c Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Fri, 30 May 2025 23:50:59 -0600 Subject: [PATCH] Finish creating the build step for prod to handle the compendia pack(s) --- scripts/buildCompendia.mjs | 8 ++++++-- scripts/extractCompendia.mjs | 7 +++++-- vite.config.js | 31 +++++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/scripts/buildCompendia.mjs b/scripts/buildCompendia.mjs index ea0cae0..8ce251c 100644 --- a/scripts/buildCompendia.mjs +++ b/scripts/buildCompendia.mjs @@ -2,14 +2,16 @@ import { existsSync } from "fs"; import { readFile } from "fs/promises"; import { join } from "path"; import { compilePack } from "@foundryvtt/foundryvtt-cli"; +import { pathToFileURL } from "url"; -async function main() { +export async function buildCompendia() { const manifest = JSON.parse(await readFile(`./public/module.json`, `utf-8`)); if (!manifest.packs || manifest.packs.length === 0) { console.log(`No compendium packs defined`); process.exit(0); }; + console.log(`Packing compendia`); for (const compendium of manifest.packs) { console.debug(`Packing ${compendium.label} (${compendium.name})`); @@ -29,4 +31,6 @@ async function main() { console.log(`Finished packing compendia`) }; -main(); +if (import.meta.url === pathToFileURL(process.argv[1]).href) { + buildCompendia(); +}; diff --git a/scripts/extractCompendia.mjs b/scripts/extractCompendia.mjs index 939c31d..df3b6a6 100644 --- a/scripts/extractCompendia.mjs +++ b/scripts/extractCompendia.mjs @@ -2,13 +2,14 @@ import { readFile } from "fs/promises"; import { join } from "path"; import { extractPack } from "@foundryvtt/foundryvtt-cli"; -async function main() { +export async function extractCompendia() { const manifest = JSON.parse(await readFile(`./public/module.json`, `utf-8`)); if (!manifest.packs || manifest.packs.length === 0) { console.log(`No compendium packs defined`); process.exit(0); }; + console.log(`Extracting compendia`); for (const compendium of manifest.packs) { console.debug(`Unpacking ${compendium.label} (${compendium.name})`); @@ -24,4 +25,6 @@ async function main() { console.log(`Finished unpacking compendia`); }; -main(); +if (import.meta.url === pathToFileURL(process.argv[1]).href) { + extractCompendia(); +}; diff --git a/vite.config.js b/vite.config.js index d849e07..5bdc955 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,6 +1,8 @@ /* eslint-disable no-undef */ +import { cp, rm } from "fs/promises"; import { readFileSync, symlinkSync } from "fs"; +import { buildCompendia } from "./scripts/buildCompendia.mjs"; import { defineConfig } from "vite"; import { glob } from "glob"; import path from "path"; @@ -46,10 +48,9 @@ The intent of this plugin is to handle the symlinking of the compendium packs so that they can modified during dev without needing to worry about the rebuild destroying the in-progress compendia data. */ -function handlePacks() { +function symlinkPacks() { return { writeBundle(options) { - console.log(options.dir); symlinkSync( path.resolve(__dirname, `packs`), `${options.dir}/packs`, @@ -59,15 +60,37 @@ function handlePacks() { }; }; +/* +The intent of this plugin is to handle the copying, cleaning and compiling of +compendia packs for production +*/ +function buildPacks() { + return { + async writeBundle(options) { + // console.log(options); + const buildDir = options.dir; + await buildCompendia(); + await cp(`${__dirname}/packs`, `${buildDir}/packs`, { recursive: true, force: true }); + for (const file of glob.sync(`${buildDir}/packs/**/_source/`)) { + await rm(file, { recursive: true, force: true }); + }; + }, + }; +}; + // MARK: config export default defineConfig(({ mode }) => { const isProd = mode === `prod`; const plugins = []; - if (!isProd) { + if (isProd) { plugins.push( - handlePacks(), + buildPacks(), + ); + } else { + plugins.push( + symlinkPacks(), watcher( `./public`, ),