-
Notifications
You must be signed in to change notification settings - Fork 254
feat: allow optional destination field for asar extract-file
/ asar ef
#358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: allow optional destination field for asar extract-file
/ asar ef
#358
Conversation
.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', |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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));
});
There was a problem hiding this comment.
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
…file-optional-destination # Conflicts: # package.json
Adds an optional parameter to
asar ef
using[destination]
optional arg syntaxResolves 2
it.todo
unit tests with the support of an output directory