feat: allow optional destination field for asar extract-file / asar ef#358
feat: allow optional destination field for asar extract-file / asar ef#358mmaietta wants to merge 4 commits intoelectron:mainfrom
asar extract-file / asar ef#358Conversation
| .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.
Should we do an fs.existsSync here to handle non existent paths more gracefully?
There was a problem hiding this comment.
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.
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 efusing[destination]optional arg syntaxResolves 2
it.todounit tests with the support of an output directory