Skip to content

Commit

Permalink
Unfinished CLI stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
lafkpages committed Oct 15, 2023
1 parent 072f19c commit 7d3027c
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 1 deletion.
Binary file modified bun.lockb
Binary file not shown.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions src/cli/help.ts
Original file line number Diff line number Diff line change
@@ -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("<command>")}
${colors.blue.bold("Commands:")}
${colors.blue("extract")} ${colors.yellow(
"<archive> <output>"
)} Extract an archive
${colors.blue("pack")} ${colors.yellow(
"<input> <archive>"
)} Pack a directory into an archive
${colors.blue("list")} ${colors.yellow(
"<archive>"
)} 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);
}
}
14 changes: 14 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -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);
}
26 changes: 26 additions & 0 deletions src/log/colors.ts
Original file line number Diff line number Diff line change
@@ -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 };
9 changes: 9 additions & 0 deletions src/log/index.ts
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit 7d3027c

Please sign in to comment.