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

Getting path to asar.unpacked #6262

Closed
kneumei opened this issue Jun 27, 2016 · 18 comments
Closed

Getting path to asar.unpacked #6262

kneumei opened this issue Jun 27, 2016 · 18 comments

Comments

@kneumei
Copy link

kneumei commented Jun 27, 2016

  • Electron version: 0.36.11
  • Operating system: windows 10

My app is using node-notifier to create toaster notifications in windows 10. Node notifier eventually ends up calling an exe file with some parameters. One of the parameters I'm passing in is a path to a .png to use as an icon in the toaster. However, when I package my app up in an asar, the icon doesn't display. I believe this is because it doesn't have an understanding of how to read the asar. So then I added the .png to an unpacked folder. However, I don't know how to get the "real" path to the unpacked asar folder. I'm using path.join(app.getAppPath(), 'assets/img/my.png'), which gets translated to C:\Users\kyle\Documents\Github\project\pack\v0.36.12\win32-x64\resources\app.asar\assets\img\my.png, which is almost correct, I just need it to be app.asar.unpacked.

@kneumei
Copy link
Author

kneumei commented Jun 28, 2016

FWIW, I ended up just doing path.join(app.getAppPath(), 'assets/img/my.png').replace('app.asar', 'app.asar.unpacked')

I see edge-cs is doing the same thing to load binaries: anaisbetts/edge-cs@686758a

Just felt like I might be missing an API to get the path of a file without having to care if it's in an unpacked asar.

@kevinsawicki
Copy link
Contributor

FWIW, I ended up just doing path.join(app.getAppPath(), 'assets/img/my.png').replace('app.asar', 'app.asar.unpacked')

Yeah, this is the way many apps convert to raw file paths.

There should probably be an API (or better docs) added for this since it is a common case when you need the actual path on disk

@Skywalker13
Copy link

Hello, this problem should be fixed directly with the path lib of electron.

When doing something like path.join (__dirname, 'myfile'), and myfile is in asar.unpacked, then the path lib should be able to see that the physical file is unpacked and returns the right path.

@Skywalker13
Copy link

My workaround for this problem: https://github.com/epsitec-sa/hazardous

@quanglam2807
Copy link
Contributor

@Skywalker13 Does it affect performance?

@Skywalker13
Copy link

I've optimized as much as possible (I use a cache in hazardous) but yes, it affects a little bit the performances when bootstrapping the app (it's not magic, when hundred join () are done the first time with all the requires).

In order to be sure to keep very good performances, it's a good idea to avoid adding all requires always at the top of the files but only where it's really used (function scope if possible). Then the performance cost is no longer visible (at least for my projects and some are relatively large).

You can easily test hazardous. Maybe the cost is not visible in your case.

@pmoleri
Copy link

pmoleri commented May 10, 2017

@Skywalker13 thanks, hazardous helped me solve a problem with electron-devtools-installer and 7zip using child_process.spawn.

I agree with @kevinsawicki, electron should provide an API to do the path translation, so hazardous and other 3rd parties can rely on such API.

@Skywalker13
Copy link

@pmoleri you're welcome

@rameerez
Copy link

@Skywalker13 thank you! hazardous completely solved my problem with asar.unpacked and a .sh script I was trying to execute :) I feel like this should be merged into the electron path lib.

Worked for electron 1.4.12 on macOS 10.12.5.

@Skywalker13
Copy link

@rameerez you're welcome ...

@fiorinin
Copy link

@Skywalker13 Thank you so much, I truly believe that’s how it should work natively! Hazardous saved my life.

@Skywalker13
Copy link

you're welcome

@whyboris
Copy link

whyboris commented Feb 15, 2018

@Skywalker13 -- you saved me a headache!
In my app I needed to refer to ffmpeg and it didn't work from within the asar file. I was already resigning myself to turning off asar, when you saved the day!

ps - getting something out of node_modules and out of asar worked for me thus: electron-userland/electron-builder#390 (comment)

@codebytere
Copy link
Member

Given the conversation above, it appears that the actionable issues in this have been addressed. In that event, i'm going to go ahead and close this, but please feel free to @ me if something else arises!

@Yahav
Copy link

Yahav commented Jul 19, 2020

for future reference.
i tried using hazardous but i seem to have broken other functionality.
ended up making a function to resolve path for unpacked assets:
function getImagesPath(relativePath) { if ((app || remote.app).isPackaged===false) { return path.join(__dirname, relativePath); } else { let asarUnpackedPath = __dirname.replace (/\.asar([\\/])/, '.asar.unpacked$1'); return path.join(asarUnpackedPath, relativePath); } }

@Skywalker13
Copy link

Hazardous is broken since electron >= v2

@he-jialin
Copy link

Hazardous is broken since electron >= v2

I look forward to your continued maintenance to the latest version of electron.
thank you for your help

@toddwong
Copy link

This is really rediculous. The files in asar.unpacked should be prior to the ones in asar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests