Skip to content

Conversation

@mmaietta
Copy link
Contributor

@mmaietta mmaietta commented Mar 7, 2025

Adds an optional parameter to asar ef using [destination] optional arg syntax

Resolves 2 it.todo unit tests with the support of an output directory

@mmaietta mmaietta marked this pull request as ready for review March 7, 2025 19:29
@mmaietta mmaietta requested a review from a team as a code owner March 7, 2025 19:29
.command('extract-file <archive> <filename> [destination]')
.alias('ef')
.description(
'extract one file from archive. Optionally output to (already-existing) destination folder, otherwise cwd will be used',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do an fs.existsSync here to handle non existent paths more gracefully?

Copy link
Contributor Author

@mmaietta mmaietta Mar 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great callout. Definitely can do! Okay with this error message to be thrown?

Related, what if we also throw an error if the destination file already exists? So that the user doesn't accidentally overwrite a file (like a sensitive/system resource if they enter a path with a space in it and don't wrap it in quotes lol?)
Alternatively, I add an optional --force arg to bypass the destination-file-already-exists error I'm proposing to also throw

  .action(function (archive, filename, destination) {
    const path = require('path');
    const fs = require('fs');

    const file = path.basename(filename);
    destination = destination?.trim()
    const out = destination ? path.join(destination, file) : file;
    if (!fs.existsSync(destination)) {
      throw new Error("destination directory does not exist, please create before attempting extraction")
    }
    if (fs.existsSync(out)) {
      throw new Error("destination file already exists")
    }
    fs.writeFileSync(out, asar.extractFile(archive, filename));
  });

Copy link
Contributor Author

@mmaietta mmaietta Mar 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the logic! Note: I had to asar.extractFile before fs.existsSync to retain previous behavior/unit test of "error thrown when file does not exist in asar"

Added additional unit test as well for the new throw Error logic path

@mmaietta mmaietta requested a review from erickzhao April 1, 2025 22:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants