Skip to content

Commit

Permalink
CLI extract command
Browse files Browse the repository at this point in the history
  • Loading branch information
lafkpages committed Oct 16, 2023
1 parent 82824ef commit 86a6e67
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/cli/commands/extract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Asar, BaseEntry } from "../..";

import { readFile, writeFile, mkdir } from "fs/promises";

import { join as joinPaths, dirname } from "path";

export default async function extract(...args: string[]) {
const [archive, output] = args;

const asarBytes = await readFile(archive);

const asar = new Asar(asarBytes);

for (const [, filePathChunks, fileEntry] of asar.walkFiles(true)) {
if (BaseEntry.isDirectory(fileEntry)) {
const fileDirPath = joinPaths(output, ...filePathChunks);

await mkdir(fileDirPath, {
recursive: true,
});
} else {
const filePath = joinPaths(output, ...filePathChunks);

const fileData = asar.readFile(fileEntry);

await writeFile(filePath, fileData);
}
}
}
14 changes: 12 additions & 2 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
import { help } from "./help";
import { colors, error } from "../log";

import extract from "./commands/extract";

const command = process.argv[2];

if (!command) {
error("No command specified\n");
help(2);
}

const commands = ["extract", "pack", "list", "help"];
const commands = {
extract: extract,
pack: console.log, // TODO
list: console.log, // TODO
help: console.log, // TODO
};
type Command = keyof typeof commands;

if (!commands.includes(command)) {
if (!(command in commands)) {
error(`Unknown command ${colors.bold(`"${command}"`)}\n`);
help(2);
}

commands[command as Command](...process.argv.slice(3));

0 comments on commit 86a6e67

Please sign in to comment.