Skip to content
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

fix: add error message for file not found #227

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
8 changes: 6 additions & 2 deletions bin/asar.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ 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))
try {
const fileData = asar.extractFile(archive, filename)
require('fs').writeFileSync(require('path').basename(filename), fileData)
} catch (err) {
console.log(err.message)
}
})

program.command('extract <archive> <dest>')
Expand Down
7 changes: 6 additions & 1 deletion lib/asar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions lib/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions test/cli-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions test/expected/extractthis-non-existent-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The file "dir1/non-existent" was not found in the archive.