diff --git a/bin/asar.js b/bin/asar.js index 8efcc1a9..56270c04 100755 --- a/bin/asar.js +++ b/bin/asar.js @@ -61,8 +61,12 @@ program.command('extract-file ') .alias('ef') .description('extract one file from archive') .action(function (archive, filename) { - require('fs').writeFileSync(require('path').basename(filename), - asar.extractFile(archive, filename)) + try { + const fileData = asar.extractFile(archive, filename) + require('fs').writeFileSync(require('path').basename(filename), fileData) + } catch (err) { + console.log(err.message) + } }) program.command('extract ') diff --git a/lib/asar.js b/lib/asar.js index 050e1a76..37f643c3 100644 --- a/lib/asar.js +++ b/lib/asar.js @@ -167,7 +167,12 @@ module.exports.listPackage = function (archive, options) { module.exports.extractFile = function (archive, filename) { const filesystem = disk.readFilesystemSync(archive) - return disk.readFileSync(filesystem, filename, filesystem.getFile(filename)) + const file = filesystem.getFile(filename) + if (typeof file === 'undefined') { + return undefined + } + + return disk.readFileSync(filesystem, filename, file) } module.exports.extractAll = function (archive, dest) { diff --git a/lib/filesystem.js b/lib/filesystem.js index 85e1eb01..da776f05 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -139,6 +139,10 @@ class Filesystem { followLinks = typeof followLinks === 'undefined' ? true : followLinks const info = this.getNode(p) + if (typeof info === 'undefined') { + throw new Error(`The file "${p}" was not found in the archive.`) + } + // if followLinks is false we don't resolve symlinks if (info.link && followLinks) { return this.getFile(info.link) diff --git a/test/cli-spec.js b/test/cli-spec.js index 2017d323..da88b2cf 100644 --- a/test/cli-spec.js +++ b/test/cli-spec.js @@ -75,6 +75,9 @@ describe('command line interface', function () { assert.strictEqual(actual, expected) }) */ + it('should throw an error when trying to extract a non-existent file', async () => { + return assertAsarOutputMatches('ef test/input/extractthis.asar dir1/non-existent', 'test/expected/extractthis-non-existent-file.txt') + }) it('should extract an archive', async () => { await execAsar('e test/input/extractthis.asar tmp/extractthis-cli/') return compDirs('tmp/extractthis-cli/', 'test/expected/extractthis') diff --git a/test/expected/extractthis-non-existent-file.txt b/test/expected/extractthis-non-existent-file.txt new file mode 100644 index 00000000..2ea1fc6d --- /dev/null +++ b/test/expected/extractthis-non-existent-file.txt @@ -0,0 +1 @@ +The file "dir1/non-existent" was not found in the archive. \ No newline at end of file