Skip to content

Commit

Permalink
Add JSON pack files to release artifacts (#8262)
Browse files Browse the repository at this point in the history
  • Loading branch information
In3luki committed Jun 13, 2023
1 parent 65499e8 commit c263fb9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
npm ci
npm run build
- name: Build JSON packs
run: npm run build:packs:json

- name: Update Manifest
run: |
SYSTEM_VERSION=$(grep -oP '(?<="version": ")[^"]+' dist/system.json | tr -d '\n')
Expand All @@ -34,6 +37,10 @@ jobs:
working-directory: ./dist
run: zip -r ./pf2e.zip ./*

- name: Zip JSON Packs
working-directory: ./packs-json
run: zip -r ./packs-json.zip ./*

- name: Create Version Release
id: create_version_release
uses: ncipollo/release-action@v1
Expand All @@ -43,5 +50,5 @@ jobs:
draft: false
prerelease: false
token: ${{ secrets.GITHUB_TOKEN }}
artifacts: './dist/system.json,./dist/pf2e.zip'
artifacts: "./dist/system.json,./dist/pf2e.zip,./packs-json/packs-json.zip"
tag: ${{ env.systemVersion }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ foundryconfig.json
node_modules/
package/
packs-temp/
packs-json/
types/foundry/node_modules/
types/foundry/package-lock.json

Expand Down
3 changes: 2 additions & 1 deletion build/build-packs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import path from "path";
import url from "url";
import { CompendiumPack, PackError } from "./lib/compendium-pack.ts";

const asJson = process.argv[2]?.toLowerCase() === "json";
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
const packsDataPath = path.resolve(__dirname, "../packs");
const packDirPaths = fs.readdirSync(packsDataPath).map((dirName) => path.resolve(__dirname, packsDataPath, dirName));

// Loads all packs into memory for the sake of making all document name/id mappings available
const packs = packDirPaths.map((p) => CompendiumPack.loadJSON(p));
const documentCounts = await Promise.all(packs.map((p) => p.save()));
const documentCounts = await Promise.all(packs.map((p) => p.save(asJson)));
const total = documentCounts.reduce((total, c) => total + c, 0);

if (documentCounts.length > 0) {
Expand Down
22 changes: 21 additions & 1 deletion build/lib/compendium-pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,10 @@ class CompendiumPack {
}
}

async save(): Promise<number> {
async save(asJson?: boolean): Promise<number> {
if (asJson) {
return this.saveAsJSON();
}
if (!fs.lstatSync(CompendiumPack.outDir, { throwIfNoEntry: false })?.isDirectory()) {
fs.mkdirSync(CompendiumPack.outDir);
}
Expand All @@ -379,6 +382,23 @@ class CompendiumPack {
return this.data.length;
}

async saveAsJSON(): Promise<number> {
const outDir = path.resolve(process.cwd(), "packs-json");
if (!fs.lstatSync(outDir, { throwIfNoEntry: false })?.isDirectory()) {
fs.mkdirSync(outDir);
}

const outFile = path.resolve(outDir, `${this.packDir}.json`);
if (fs.existsSync(outFile)) {
fs.rmSync(outFile, { force: true });
}
const finalized = this.data.map((datum) => this.#finalize(datum));
fs.writeFileSync(outFile, `[${finalized.join(",\n")}]`.concat("\n"));
console.log(`File "${this.packDir}.json" with ${this.data.length} entries created successfully.`);

return this.data.length;
}

#isDocumentSource(maybeDocSource: unknown): maybeDocSource is PackEntry {
if (!isObject(maybeDocSource)) return false;
const checks = Object.entries({
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"build": "npm run clean && npm run build:packs && vite build",
"build:packs": "esno ./build/build-packs.ts",
"build:packs:json": "esno ./build/build-packs.ts json",
"clean": "esno ./build/clean.ts",
"watch": "npm run clean && npm run build:packs && vite build --watch --mode development",
"hot": "vite serve",
Expand Down

0 comments on commit c263fb9

Please sign in to comment.