loading...

Writing OS-specific code in Electron

maciejcieslar profile image Maciej Cieślar Originally published at maciejcieslar.com ・2 min read

One of the advantages of using Electron is that – since it's cross-platform – we don't have to worry about the operating system on which our application is going to be run.

However, sometimes we need our code to be OS-specific if, for example, we are going to be using command console or need to retrieve some information about the system.

Having to write multiple ifs each time we want to have some functionality on a given OS seems excess work and it quickly obfuscates the code rendering it difficult to be understood and analyzed.

In order to keep the code clean and readable, we can create a little helper and remove the ifs and any "OS-related" logic altogether.

Implementing Platforms

Platforms implementation


First, we see the platforms object which contains names of all supported operating systems. It is made only for convenience. We can then use platforms.WINDOWS instead of typing 'WINDOWS' each time into our object with handlers we pass to the byOS function.

Next, notice the platformsNames object, the keys of which are the result of calling os.platform(), and whose values are the keys from the platforms object. We simply map it to a more user-friendly name.

For example when os.platform() returns win32 we map it to platforms.WINDOWS by calling platformsNames[os.platform()].

In currentPlatform we save the platform that we are using right now, so then we can match it against a given object with handlers.


Implementing Releases

One might go even further and try to differentiate between releases of a given OS, for example, Windows 7, 8 and 10.

Releases implementation


Now we can use os.release() to check for the system's release.

We can split the resulting string and check the Windows version. A complete list can be found here. As for Linux/Mac, I didn't really see how that could be useful, so I left it at releases.ANY.

In whatWindowsIsHeUsing you can see that we are only checking for different Windows' releases if we are running the app on Windows.

You can see the code in the repository.


Thank you very much for reading! If you have better ideas on how to write OS specific code, share them down below!

If you have any questions or comments feel free to put them in the comment section below or send me a message.

Check out my social media!

Join my newsletter!

Discussion

pic
Editor guide
Collapse
tux0r profile image
tux0r

If you have better ideas on how to write OS specific code, share them down below!

Stop using Electron. Write in cross-platform languages like C, C++ or Delphi instead. Delphi can even produce iOS and Android builds for you.

(C and C++ probably need an #ifdef construct for various OSs, but at least they're standardized. Example here.)

Collapse
smuschel profile image
smuschel

Out of curiosity - why do you advise to not use Electron? (besides memory/CPU/resource usage)

Collapse
tux0r profile image
tux0r

If memory/CPU/resource usage are not already bad enough for you (whyever), there's that security problem you get with embedding a quickly outdated web browser.

Thread Thread
smuschel profile image
smuschel

Thank you for sharing that link. You'd probably be shocked if I told you why we're considering Electron. But Thema requirements we have are as they are...

Thread Thread
tux0r profile image
tux0r

You'd probably be shocked if I told you why we're considering Electron.

For more security? :-D

Thread Thread
smuschel profile image
smuschel

Probably not. I need to be able to launch applications on Windows without user interaction in a browser application (and other things I'm not going to mention). We used to do that with Java Applets and later on Java Webstart, but both are dead. Don't judge me...

Thread Thread
tux0r profile image
tux0r

I need to be able to launch applications (...) in a browser application

You should probably quit your job. ;-)

Thread Thread
smuschel profile image
smuschel

Maybe :)
@Maciej: sorry for hijacking your post - was interesting to read for me

(And probably I should take the time to figure out how this editor works...)

Thread Thread
noahlaux profile image
Noah Laux

and kudos on keeping your senses during the troll behavior of @tuxOr!

Thread Thread
simonhaisz profile image
simonhaisz

@smuschel if your talking about the custom protocol my-app://some-path support, technically you don't need Electron to use it. You just need to add a registry setting that points to your exe which can be built on anything. So if you only care about Windows and not Mac then you do have other options, though they involve vendor lock-in.

I know Electron can do that for you but I recommend you have the installer do it instead so that the uninstall can remove the settings along with your program. The built in Electron registering adds a bunch of duplicate settings on different paths for reasons I don't understand and that doesn't follow MS documentation. And it doesn't provide an API to remove them as far as I know.

And I say this as another long term Java Web Start developer moving to new options thanks to Oracle 😢

Thread Thread
smuschel profile image
smuschel

Thanks for pointing that out - I'll have to read up on custom protocol handlers. But the overall story is long and complicated (need to do more unspeakable things without user interaction... lost that battle more than a decade ago).