Skip to content

Commit

Permalink
feat(cli): initial sedge world subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
arexon committed Sep 1, 2022
1 parent 17d9a80 commit 236e6a3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions cli/commands/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './build.ts';
export * from './clean.ts';
export * from './dev.ts';
export * from './init.ts';
export * from './world.ts';
69 changes: 69 additions & 0 deletions cli/commands/world.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Command } from 'cliffy/command/mod.ts';
import { copy } from 'fs';
import { join, resolve } from 'path';
import { sedgeFileSystem } from '../../compiler/fs.ts';
import { loadConfig } from '../../compiler/loaders.ts';
import { findMojangDir } from '../../compiler/path.ts';
import { logger } from '../../shared/mod.ts';

export const world = new Command()
.description(
'Saves or tests a world from or to the `com.mojang` directory respectively.',
)
.option(
'-s, --save <world:string>',
'Saves a world by copying it from from `com.mojang` directory.',
{ required: true },
)
.option(
'-t, --test <world:string>',
'Tests a world by copying it to the `com.mojang` directory.',
{ conflicts: ['save'], required: true },
)
.action(async ({ save, test }) => {
const { packs } = loadConfig('config.json', sedgeFileSystem);

const getTargetWorldPath = (name: string) => {
return resolve(
findMojangDir(sedgeFileSystem),
'minecraftWorlds',
name,
);
};

if (save) {
const targetPath = getTargetWorldPath(save);
const worldPath = join(packs.worldTemplate, save);

try {
Deno.statSync(targetPath);
} catch {
logger.error(
`Could not find world [${save}] in the [com.mojang] directory.`,
);
Deno.exit(0);
}

await copy(targetPath, worldPath);

logger.success(`World [${save}] was saved to (${worldPath}).`);
} else if (test) {
const worldPath = join(packs.worldTemplate, test);
const targetPath = getTargetWorldPath(test);

try {
Deno.statSync(worldPath);
} catch {
logger.error(
`Could not find world [${test}] in (${worldPath}).`,
);
Deno.exit(0);
}

await copy(worldPath, targetPath);

logger.success(
`World [${test}] was copied to the [com.mojang] directory.`,
);
}
});
3 changes: 2 additions & 1 deletion cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
UpgradeCommand,
} from 'cliffy/command/upgrade/mod.ts';
import { SEDGE_VERSION } from '../shared/mod.ts';
import { build, clean, dev, init } from './commands/mod.ts';
import { build, clean, dev, init, world } from './commands/mod.ts';

await new Command()
.name('sedge')
Expand All @@ -26,6 +26,7 @@ await new Command()
)
.command('build', build)
.command('dev', dev)
.command('world', world)
.command('init', init)
.command('clean', clean)
.parse(Deno.args);

0 comments on commit 236e6a3

Please sign in to comment.