diff --git a/commands/package.mjs b/commands/package.mjs index bb00708..3cccb1d 100644 --- a/commands/package.mjs +++ b/commands/package.mjs @@ -2,7 +2,7 @@ import Config from "../config.mjs"; import path from "path"; import fs from "fs"; import chalk from "chalk"; -import { compilePack, extractPack, TYPE_COLLECTION_MAP } from "../lib/package.mjs"; +import { compilePack, extractPack, repairPack, TYPE_COLLECTION_MAP } from "../lib/package.mjs"; /** * @typedef {"Module"|"System"|"World"} PackageType @@ -61,7 +61,7 @@ export function getCommand() { yargs.positional("action", { describe: "The action to perform", type: "string", - choices: ["workon", "clear", "unpack", "pack"] + choices: ["workon", "clear", "unpack", "pack", "repair"] }); yargs.positional("value", { @@ -163,6 +163,7 @@ export function getCommand() { case "clear": handleClear(); break; case "unpack": await handleUnpack(argv); break; case "pack": await handlePack(argv); break; + case "repair" await handleRepair(argv); break; default: if ( !currentPackageId ) { @@ -464,3 +465,35 @@ async function handlePack(argv) { process.exitCode = 1; } } + + +/* -------------------------------------------- */ +/* Repair */ +/* -------------------------------------------- */ + +/** + * Repair database + * + * @param {CLIArgs} argv The command line arguments + * @returns {Promise} + * @private + */ +async function handleRepair(argv) { + const { pack } = determinePaths(argv, "pack"); + const { nedb } = argv; + if ( nedb || !pack ) { + process.exitCode = 1; + return; + } + + // Assume the lock does not persist even for corrupt database. + // And if it does, user must manually remove it to acknowledge the potential danger of running this. + if ( isFileLocked(path.join(pack, "LOCK")) ) { + console.error(chalk.red(`The pack "${chalk.blue(pack)}" is currently in use by Foundry VTT. ` + + "Please close Foundry VTT and try again.")); + process.exitCode = 1; + return; + } + + return repairPack(pack, { log: true }); +} diff --git a/index.mjs b/index.mjs index 372d936..064a8eb 100644 --- a/index.mjs +++ b/index.mjs @@ -1 +1 @@ -export { compilePack, extractPack } from "./lib/package.mjs"; +export { compilePack, extractPack, repairPack } from "./lib/package.mjs"; diff --git a/lib/package.mjs b/lib/package.mjs index 0421926..b32ff51 100644 --- a/lib/package.mjs +++ b/lib/package.mjs @@ -658,6 +658,18 @@ async function extractAdventure(doc, dest, { folderMap }={}, { /* Utilities */ /* -------------------------------------------- */ +/** + * Repair pack + * @param {string} pack The source compendium pack. + * @param {object} options Additional options + * @param {boolean} [log] Log progress + */ +export async function repairPack(pack, { log = false } = {}) { + if ( log ) console.log(`Repairing ${chalk.blue(pack)}`); + await ClassicLevel.repair(pack); + if ( log ) console.log("Repair complete"); +} + /** * Wrap a function so that it can be applied recursively to a Document's hierarchy. * @param {HierarchyApplyCallback} fn The function to wrap.