From d34c1169ac71a0d3869b8fe9fb471ae2e135506b Mon Sep 17 00:00:00 2001 From: gumi Date: Tue, 1 Oct 2019 17:15:09 -0400 Subject: [frob] allow to dry-run --- server/frob/char.ts | 13 +++++++++---- server/frob/index.ts | 32 +++++++++++++++++++++++++++++--- server/frob/storage.ts | 13 +++++++++---- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/server/frob/char.ts b/server/frob/char.ts index e5bb1e9..67751dc 100644 --- a/server/frob/char.ts +++ b/server/frob/char.ts @@ -136,13 +136,18 @@ class CharWriter { } } - async finalize() { + async finalize(dry_run: boolean = false) { console.info("appending %newid%..."); await Deno.write(this.file.rid, this.encoder.encode(`${this.highest + 1}\t%newid%\n`)); this.file.close(); - console.info("overwriting athena.txt..."); - await Deno.rename("world/save/athena.txt", "world/save/athena.txt_pre-frob"); - await Deno.rename("world/save/athena.txt.tmp", "world/save/athena.txt"); + + if (dry_run) { + Deno.removeSync("world/save/athena.txt.tmp"); + } else { + console.info("overwriting athena.txt..."); + await Deno.rename("world/save/athena.txt", "world/save/athena.txt_pre-frob"); + await Deno.rename("world/save/athena.txt.tmp", "world/save/athena.txt"); + } } } diff --git a/server/frob/index.ts b/server/frob/index.ts index 332989a..e1b3ce5 100644 --- a/server/frob/index.ts +++ b/server/frob/index.ts @@ -5,7 +5,6 @@ import { ItemDB } from "./itemdb.ts"; const args: string[] = Deno.args.slice(1); const to_remove: Set = new Set(); -const is_alpha: boolean = isNaN(args.join() as unknown as number); const char_parser = new CharParser(); const storage_parser = new StorageParser(); const char_writer = new CharWriter(); @@ -29,6 +28,10 @@ const stats = { }, }; +const flags = { + dry_run: false, +}; + (async () => { const items_by_id = new Map(); // holds full item info @@ -73,6 +76,25 @@ const stats = { throw new RangeError("no items given!"); } + // flag parsing + for (let opt of args) { + if (opt.startsWith("--")) { + switch (opt.slice(2)) { + case "dry": + case "dry-run": + flags.dry_run = true; + break; + default: + throw new SyntaxError(`unknown flag: ${opt}`); + } + + args.shift(); + } + + break; // no more flags + } + + // item parsing for (let arg of args.join(",").split(",")) { if (arg.includes("-") || arg.includes("..")) { const range = arg.split("-").join("..").split(".."); @@ -125,7 +147,7 @@ const stats = { await char_writer.write(char); } - await char_writer.finalize(); + await char_writer.finalize(!!flags.dry_run); // storage: for await (let storage of storage_parser.readDB()) { @@ -173,11 +195,15 @@ const stats = { } } - await storage_writer.finalize(); + await storage_writer.finalize(!!flags.dry_run); console.info("\n=== all done ==="); console.info(`removed ${stats.inventory.removed} existant, ${stats.inventory.pruned} non-existant and ${stats.inventory.stub} stub items from the inventory of ${stats.inventory.chars} characters`); console.info(`removed ${stats.storage.removed} existant, ${stats.storage.pruned} non-existant and ${stats.storage.stub} stub items from the storage of ${stats.storage.accounts} accounts`); console.info(`removed ${stats.storage.wiped} empty storage entries from the storage db`); console.info(`fixed storage sync of ${stats.storage.synced} accounts`); + + if (flags.dry_run) { + console.warn("(DRY RUN) no file modified"); + } })() diff --git a/server/frob/storage.ts b/server/frob/storage.ts index f8a5351..315d656 100644 --- a/server/frob/storage.ts +++ b/server/frob/storage.ts @@ -103,11 +103,16 @@ class StorageWriter { await Deno.write(this.file.rid, this.encoder.encode(line)); } - async finalize() { + async finalize(dry_run: boolean = false) { this.file.close(); - console.info("overwriting storage.txt..."); - await Deno.rename("world/save/storage.txt", "world/save/storage.txt_pre-frob"); - await Deno.rename("world/save/storage.txt.tmp", "world/save/storage.txt"); + + if (dry_run) { + Deno.removeSync("world/save/storage.txt.tmp"); + } else { + console.info("overwriting storage.txt..."); + await Deno.rename("world/save/storage.txt", "world/save/storage.txt_pre-frob"); + await Deno.rename("world/save/storage.txt.tmp", "world/save/storage.txt"); + } } } -- cgit v1.2.3-60-g2f50