Initial import with skill sheet working
This commit is contained in:
64
tools/CompendiumsManager.mjs
Normal file
64
tools/CompendiumsManager.mjs
Normal file
@ -0,0 +1,64 @@
|
||||
import { extractPack, compilePack } from '@foundryvtt/foundryvtt-cli';
|
||||
import { promises as fs } from 'fs';
|
||||
import path from "path";
|
||||
|
||||
const MODULE_ID = process.cwd();
|
||||
|
||||
export class CompendiumsManager {
|
||||
|
||||
static async packToDistDir(srcDir = 'packs_src', distDir = 'packs', mode = 'yaml') {
|
||||
const yaml = mode === 'yaml'
|
||||
const packs = await fs.readdir('./' + srcDir);
|
||||
for (const pack of packs) {
|
||||
if (pack === '.gitattributes') continue;
|
||||
console.log('Packing ' + pack);
|
||||
await compilePack(
|
||||
`${MODULE_ID}/${srcDir}/${pack}`,
|
||||
`${MODULE_ID}/${distDir}/${pack}`,
|
||||
{ yaml }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static async unpackToSrcDir(srcDir = 'packs_src', distDir = 'packs', mode = 'yaml') {
|
||||
const yaml = mode === 'yaml'
|
||||
const packs = await fs.readdir("./" + distDir);
|
||||
for (const pack of packs) {
|
||||
if (pack === ".gitattributes") continue;
|
||||
if (pack === ".directory") continue;
|
||||
if (pack.endsWith(".db")) continue;
|
||||
console.log("Unpacking " + pack);
|
||||
const directory = `./${srcDir}/${pack}`;
|
||||
// Create the directory if it doesn't exist
|
||||
await fs.mkdir(directory, { recursive: true });
|
||||
try {
|
||||
for (const file of await fs.readdir(directory)) {
|
||||
await fs.unlink(path.join(directory, file));
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.code === "ENOENT") console.log("No files inside of " + pack);
|
||||
else console.log(error);
|
||||
}
|
||||
await extractPack(
|
||||
`${MODULE_ID}/${distDir}/${pack}`,
|
||||
`${MODULE_ID}/${srcDir}/${pack}`,
|
||||
{
|
||||
yaml: mode === 'yaml',
|
||||
transformName: doc => CompendiumsManager.transformName(doc, mode === 'yaml'),
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prefaces the document with its type
|
||||
* @param {object} doc - The document data
|
||||
*/
|
||||
static transformName(doc, yaml) {
|
||||
const safeFileName = doc.name.replace(/[^a-zA-Z0-9А-я]/g, "_");
|
||||
const type = doc._key.split("!")[1];
|
||||
const prefix = ["actors", "items"].includes(type) ? doc.type : type;
|
||||
|
||||
return `${doc.name ? `${prefix}_${safeFileName}_${doc._id}` : doc._id}.${yaml ? "yml" : "json"}`;
|
||||
}
|
||||
}
|
3
tools/packCompendiumsToDist.mjs
Normal file
3
tools/packCompendiumsToDist.mjs
Normal file
@ -0,0 +1,3 @@
|
||||
import { CompendiumsManager } from './CompendiumsManager.mjs';
|
||||
|
||||
CompendiumsManager.packToDistDir()
|
3
tools/unpackCompendiumsFromDist.mjs
Normal file
3
tools/unpackCompendiumsFromDist.mjs
Normal file
@ -0,0 +1,3 @@
|
||||
import { CompendiumsManager } from './CompendiumsManager.mjs';
|
||||
|
||||
CompendiumsManager.unpackToSrcDir()
|
Reference in New Issue
Block a user