From 68b849df0c691f052b74aa2b3ead3d8b2dba2ca8 Mon Sep 17 00:00:00 2001 From: Moritz Ruth Date: Tue, 16 Aug 2022 16:18:18 +0200 Subject: [PATCH] Implement overrides for packwiz export --- src/commands/packwiz.ts | 52 ++++++++++++++++++- .../overrides/client-server/config/charm.ini | 1 + test-pack/overrides/server/server.properties | 1 + 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 test-pack/overrides/client-server/config/charm.ini create mode 100644 test-pack/overrides/server/server.properties diff --git a/src/commands/packwiz.ts b/src/commands/packwiz.ts index 04bfe5b..d2aa768 100644 --- a/src/commands/packwiz.ts +++ b/src/commands/packwiz.ts @@ -5,6 +5,8 @@ import dedent from "dedent" import kleur from "kleur" import { getSha512HexHash } from "../utils.js" import { output } from "../output.js" +import { Visitor, walk } from "@root/walk" +import { Path } from "../path.js" const packwizCommand = new Command("packwiz") @@ -17,12 +19,28 @@ interface IndexedFile { packwizCommand.command("import") .description("Import a packwiz pack.") .action(async () => { + output.failAndExit("Not implemented.") // TODO: Import packwiz pack }) +packwizCommand.command("serve") + .description("Start an HTTP server in the packwiz directory.") + .action(async () => { + output.failAndExit("Not implemented.") + // TODO: serve + }) + +packwizCommand.command("dev") + .description("serve + export with hot-reloading.") + .action(async () => { + output.failAndExit("Not implemented.") + // TODO: serve and export with hot-reloading + }) + packwizCommand.command("export") .description("Generate a packwiz pack in the packwiz directory.") - .action(async () => { + .option("-s, --server", "Use server overrides instead of client overrides.") + .action(async options => { const pack = await usePack() if (pack.horizrFile.loader !== "fabric") @@ -63,6 +81,38 @@ packwizCommand.command("export") innerLoader.stop() } + loader.setText(`Copying and hashing ${options.server ? "server" : "client"} overrides`) + + const createVisitor = (overridesDirectoryPath: Path): Visitor => async (error, path, dirent) => { + const relativePath = overridesDirectoryPath.relative(Path.create(path)) + + if (error) output.warn(`${kleur.yellow(relativePath.toString())}: ${error.message}`) + else { + if (dirent.name.startsWith(".")) return false + if (dirent.isFile()) { + const outputPath = outputDirectoryPath.resolve(relativePath) + await fs.mkdirp(outputPath.getParent().toString()) + console.log(path) + console.log(outputPath.toString()) + await fs.copy(path, outputPath.toString()) + + indexedFiles.push({ + path: relativePath.toString(), + isMeta: false, + sha512HashHex: await getSha512HexHash(await fs.readFile(overridesDirectoryPath.resolve(path).toString())) + }) + } + } + } + + const specificOverridesDirectoryPath = pack.paths.overrides[options.server ? "server" : "client"] + const universalOverridesDirectoryPath = pack.paths.overrides["client-server"] + + if (await fs.pathExists(specificOverridesDirectoryPath.toString())) await walk(specificOverridesDirectoryPath.toString(), createVisitor(specificOverridesDirectoryPath)) + if (await fs.pathExists(universalOverridesDirectoryPath.toString())) await walk(universalOverridesDirectoryPath.toString(), createVisitor(universalOverridesDirectoryPath)) + + loader.setText(`Writing ${kleur.yellow("index.toml")}`) + const index = dedent` hash-format = "sha512" diff --git a/test-pack/overrides/client-server/config/charm.ini b/test-pack/overrides/client-server/config/charm.ini new file mode 100644 index 0000000..a17e4d4 --- /dev/null +++ b/test-pack/overrides/client-server/config/charm.ini @@ -0,0 +1 @@ +test=42 diff --git a/test-pack/overrides/server/server.properties b/test-pack/overrides/server/server.properties new file mode 100644 index 0000000..f80ee21 --- /dev/null +++ b/test-pack/overrides/server/server.properties @@ -0,0 +1 @@ +server-file=test