From fb5ecc2d6f5682fea2ed8987bf1ff8cb20505c85 Mon Sep 17 00:00:00 2001 From: Arexon Date: Thu, 1 Sep 2022 17:24:13 +0200 Subject: [PATCH] feat(cli): allow saving/testing multiple worlds --- cli/commands/world.ts | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/cli/commands/world.ts b/cli/commands/world.ts index b274c0c..f30064a 100644 --- a/cli/commands/world.ts +++ b/cli/commands/world.ts @@ -11,16 +11,16 @@ export const world = new Command() 'Saves or tests a world from or to the `com.mojang` directory respectively.', ) .option( - '-s, --save ', + '-s, --save ', 'Saves a world by copying it from from `com.mojang` directory.', { required: true }, ) .option( - '-t, --test ', + '-t, --test ', 'Tests a world by copying it to the `com.mojang` directory.', { conflicts: ['save'], required: true }, ) - .action(async ({ save, test }) => { + .action(({ save, test }) => { const { packs } = loadConfig('config.json', sedgeFileSystem); const getTargetWorldPath = (name: string) => { @@ -30,40 +30,43 @@ export const world = new Command() name, ); }; - - if (save) { - const targetPath = getTargetWorldPath(save); - const worldPath = join(packs.worldTemplate, save); + const saveWorld = async (world: string): Promise => { + const targetPath = getTargetWorldPath(world); + const worldPath = join(packs.worldTemplate, world); try { Deno.statSync(targetPath); } catch { logger.error( - `Could not find world [${save}] in the [com.mojang] directory.`, + `Could not find world [${world}] in the [com.mojang] directory.`, ); Deno.exit(0); } - await copy(targetPath, worldPath); + await copy(targetPath, worldPath, { overwrite: true }); - logger.success(`World [${save}] was saved to (${worldPath}).`); - } else if (test) { - const worldPath = join(packs.worldTemplate, test); - const targetPath = getTargetWorldPath(test); + logger.success(`World [${world}] was saved to (${worldPath}).`); + }; + const testWorld = async (world: string): Promise => { + const worldPath = join(packs.worldTemplate, world); + const targetPath = getTargetWorldPath(world); try { Deno.statSync(worldPath); } catch { logger.error( - `Could not find world [${test}] in (${worldPath}).`, + `Could not find world [${world}] in (${worldPath}).`, ); Deno.exit(0); } - await copy(worldPath, targetPath); + await copy(worldPath, targetPath, { overwrite: true }); logger.success( - `World [${test}] was copied to the [com.mojang] directory.`, + `World [${world}] was copied to the [com.mojang] directory.`, ); - } + }; + + if (save) save.forEach(async (world) => await saveWorld(world)); + else if (test) test.forEach(async (world) => await testWorld(world)); });