Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions commands/package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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", {
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -464,3 +465,35 @@ async function handlePack(argv) {
process.exitCode = 1;
}
}


/* -------------------------------------------- */
/* Repair */
/* -------------------------------------------- */

/**
* Repair database
*
* @param {CLIArgs} argv The command line arguments
* @returns {Promise<void>}
* @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 });
}
2 changes: 1 addition & 1 deletion index.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { compilePack, extractPack } from "./lib/package.mjs";
export { compilePack, extractPack, repairPack } from "./lib/package.mjs";
12 changes: 12 additions & 0 deletions lib/package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down