From 7d3027c9bdaeb9abf74cf8f15af7fb6de7757399 Mon Sep 17 00:00:00 2001 From: lafkpages Date: Sun, 15 Oct 2023 23:13:43 +0200 Subject: [PATCH] Unfinished CLI stuff --- bun.lockb | Bin 10878 -> 12331 bytes package.json | 5 +++++ scripts/build.ts | 2 +- src/cli/help.ts | 41 +++++++++++++++++++++++++++++++++++++++++ src/cli/index.ts | 14 ++++++++++++++ src/log/colors.ts | 26 ++++++++++++++++++++++++++ src/log/index.ts | 9 +++++++++ 7 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/cli/help.ts create mode 100644 src/cli/index.ts create mode 100644 src/log/colors.ts create mode 100644 src/log/index.ts diff --git a/bun.lockb b/bun.lockb index 5782428c57d2e13015dd32758c5a6836eadb4e02..f3577859682ca69891c209f989e56615e7fa0606 100755 GIT binary patch delta 2334 zcmdT_dr*{B6u)=fh5dF#dAK0Y-33IQbr)rU)z(-EQ^+V@se$2(4R*vucfovAYgQUM zGT=#b#wSi@fus{r>No>q{UKy2j5B3ei8=+5ugQsVLg<|D;~SW!X{J9rGv}Ur?m72& z?mhROdxr`dSJYajr+%-v)8BEv=W5HMy(esgw|gqjcjj$c*p{LEsjDTpujfD_6Uk7w zL?3A|NQ%@7jBQQ9Za7L)U0s;3i!(M6l?x~XQGSMUJj!DzCy^6Mnlxve=Uh=r*iQHQDwoIUdEhT$+0>H6xJ(N#@mmIc(}sO3k_X+ zez1GNz>#&If0WR>yYs>&n5W3(&q1}q!JD8R+lL^GbMOkt8s`vtaH|V2oEqvBHa->F zMT?+x@Kcb5?J!hhn+fgM9t1(<;CCPkTO(9sn+xsO?gPQk!LLCUwjogM=MYM86LS%j zL8sEjkAmRu5c+W}6UVqXW`$~hhibJSW62Ppvh(*qP&;@hWT_o00k=9EzE;^)>DWty z-3mMRfgspmt3n-cmaK@Qzyv5ENHEc;@F$K^L_b0o(lJS-sYnZW4Yf4XFr$n^t}4-p z&WxC7ti&bKML^s`f*wu^Y9FSD1tF6RMke|NME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&<)jrD@+gHNM<@ZQQmVkv+Hi(NSXEGw(kM?+xX{pEz}5_#G;I}> zDm0YV0wLy6L7%bkdUFf@K6V)33;5 z)Jh<2uBAzTLnO{U6q2r6%jlL>9YB-BG`4`zMPUIKgP9m{ERnkxjO7b!8kWO`rpgYI>S=K4 zSc9MRd0)Jy`fx}46j_PPL$I8oq{k@-Y0%?z085^-aC`Te7)lFXiGV1i#&pr*6f5Ah+`446Pq;$}2 ndWg4`8MH3i6KvA!Xo=fl6`j%EzB$p}XwT4l|K&oWxo3X@sNC_! diff --git a/package.json b/package.json index 64c6717..f02e978 100644 --- a/package.json +++ b/package.json @@ -23,5 +23,10 @@ "pretest": "bun scripts/cleanupTestData.ts", "test": "bun test", "prepublishOnly": "bun run build" + }, + "bin": "dist/cli/index.js", + "optionalDependencies": { + "ansi-colors": "^4.1.3", + "enquirer": "^2.4.1" } } diff --git a/scripts/build.ts b/scripts/build.ts index 53b6ef6..4ef0311 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -4,7 +4,7 @@ await tsc.exited; // Build the project await Bun.build({ - entrypoints: ["src/index.ts"], + entrypoints: ["src/index.ts", "src/cli/index.ts"], root: "src", target: "node", minify: true, diff --git a/src/cli/help.ts b/src/cli/help.ts new file mode 100644 index 0000000..c7611fb --- /dev/null +++ b/src/cli/help.ts @@ -0,0 +1,41 @@ +import { colors } from "../log"; + +export function help(exit: number | false = 0) { + // If exiting with a non-zero exit code, + // print the error message to stderr + console[exit !== false && exit !== 0 ? "error" : "log"]( + colors.reset(`${colors.magenta.bold("Usage:")} ${colors.green( + "fast-asar" + )} ${colors.blue("")} + +${colors.blue.bold("Commands:")} + ${colors.blue("extract")} ${colors.yellow( + " " + )} Extract an archive + ${colors.blue("pack")} ${colors.yellow( + " " + )} Pack a directory into an archive + ${colors.blue("list")} ${colors.yellow( + "" + )} List the contents of an archive + ${colors.blue("help")} Show this help message + +${colors.magenta.bold("Examples:")} + ${colors.magenta("bunx")} ${colors.green("fast-asar")} ${colors.blue( + "extract" + )} ${colors.yellow("app.asar app/")} + ${colors.magenta("bunx")} ${colors.green("fast-asar")} ${colors.blue( + "pack" + )} ${colors.yellow("app/ app.asar")} + ${colors.magenta("bunx")} ${colors.green("fast-asar")} ${colors.blue( + "list" + )} ${colors.yellow("app.asar")} + ${colors.magenta("bunx")} ${colors.green("fast-asar")} ${colors.blue( + "help" + )}`) + ); + + if (exit !== false) { + process.exit(exit); + } +} diff --git a/src/cli/index.ts b/src/cli/index.ts new file mode 100644 index 0000000..1330f11 --- /dev/null +++ b/src/cli/index.ts @@ -0,0 +1,14 @@ +#!/usr/bin/env node + +import { help } from "./help"; +import { colors } from "../log"; + +const command = process.argv[2]; + +if (!command) { + console.error( + colors.red("error") + colors.gray(":"), + "No command specified\n" + ); + help(2); +} diff --git a/src/log/colors.ts b/src/log/colors.ts new file mode 100644 index 0000000..069e253 --- /dev/null +++ b/src/log/colors.ts @@ -0,0 +1,26 @@ +let colors: typeof import("ansi-colors"); +try { + colors = await import("ansi-colors"); +} catch { + // If ansi-colors is not installed, return a proxy that returns itself + // when any property is accessed + // @ts-expect-error + colors = new Proxy((a: string) => a, { + get() { + return colors; + }, + }); + + // This is done so that if color functions are called, they will + // just return the string that was passed to them without any + // colors, instead of throwing an error because the function + // doesn't exist. + + // So for example, the following code would log "Hello, world!" + // in red if ansi-colors is installed, and just "Hello, world!" + // with no colors if it isn't installed: + // + // console.log(colors.red("Hello, world!")); +} + +export { colors }; diff --git a/src/log/index.ts b/src/log/index.ts new file mode 100644 index 0000000..c2a87de --- /dev/null +++ b/src/log/index.ts @@ -0,0 +1,9 @@ +import { colors } from "./colors"; + +export function error(message: string) { + console.error(colors.red("error") + colors.gray(":"), message); +} + +export const ok = colors.ok; + +export { colors };