Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions bin/asar.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,32 @@ program.command('list <archive>')
}
})

program.command('extract-file <archive> <filename>')
.alias('ef')
.description('extract one file from archive')
.action(function (archive, filename) {
require('fs').writeFileSync(require('path').basename(filename),
asar.extractFile(archive, filename))
})
program
.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

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

// extract first to memory so that error is thrown if file does not exist
const fileData = asar.extractFile(archive, filename)
const file = path.basename(filename);
destination = destination?.trim()
const out = destination ? path.join(destination, file) : file;

// check if destination exists. when destination is not provided, we will write to cwd
if (destination && !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, fileData);
});


program.command('extract <archive> <dest>')
.alias('e')
Expand Down
37 changes: 21 additions & 16 deletions test/cli-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,32 @@ describe('command line interface', function () {
'test/expected/packthis-unicode-path-filelist.txt',
);
});
// we need a way to set a path to extract to first, otherwise we pollute our project dir
// or we fake it by setting our cwd, but I don't like that
/*
it('should extract a text file from archive', async () => {
await execAsar('ef test/input/extractthis.asar dir1/file1.txt')
const actual = await fs.readFile('tmp/file1.txt', 'utf8')
let expected = await fs.readFile('test/expected/extractthis/dir1/file1.txt', 'utf8')
fs.mkdirSync(TEST_APPS_DIR);
await execAsar(`ef test/input/extractthis.asar dir1/file1.txt ${TEST_APPS_DIR}`);
const actual = await fs.readFile(path.join(TEST_APPS_DIR, 'file1.txt'), 'utf8');
let expected = await fs.readFile('test/expected/extractthis/dir1/file1.txt', 'utf8');
// on windows replace crlf with lf
if (os.platform() === 'win32') {
expected = expected.replace(/\r\n/g, '\n')
expected = expected.replace(/\r\n/g, '\n');
}
assert.strictEqual(actual, expected)
})
assert.strictEqual(actual, expected);
});
it('should extract a binary file from archive', async () => {
fs.mkdirSync(TEST_APPS_DIR);
await execAsar(`ef test/input/extractthis.asar dir2/file2.png ${TEST_APPS_DIR}`);
await compFiles(
path.join(TEST_APPS_DIR, 'file2.png'),
'test/expected/extractthis/dir2/file2.png',
);
});
it('should throw error when extracting file to non-existant destination directory', async () => {
await assert.rejects(
execAsar(`ef test/input/extractthis.asar dir2/file2.png ${TEST_APPS_DIR}`),
/destination directory does not exist, please create before attempting extraction/,
);
});

it('should extract a binary file from archive', async () => {
await execAsar('ef test/input/extractthis.asar dir2/file2.png')
const actual = await fs.readFile('tmp/file2.png', 'utf8')
const expected = await fs.readFile('test/expected/extractthis/dir2/file2.png', 'utf8')
assert.strictEqual(actual, expected)
})
*/
it('should extract an archive', async () => {
await execAsar('e test/input/extractthis.asar tmp/extractthis-cli/');
return compDirs('tmp/extractthis-cli/', 'test/expected/extractthis');
Expand Down