Electron and local environments
After a few years of dealing with a rather large Electron application. I’ve found a few different ways to approach things – as with anyone dealing with large bohemoth codebases, it feels natural to carve it up and chunk out the code into more managable pieces. This leads us from having one large, insanely unplanned renderer, to having “nicer” (though not great) smaller packages which can be reused across the renderer, the main process and even outside in other non-electron focused services. This can lead to interesting problems though, such as making the code aware (without caring) where it is being executed. The electron
package itself tends to abstract these things away whether the code is run on Windows/MacOS/Linux - however what about when you aren’t utilizing the target code within Electron
anymore?
isPackaged
Electron
gives us a method, app.isPackaged through it’s main API, however this isn’t useful to use unless we want to import electron
into all of our code. Specifically it let’s us determine
1 | A boolean property that returns true if the app is packaged, false otherwise. For many apps, this property can be used to distinguish development and production environments. |
Are we in electron or not?
There are a few process properties which are interesting in Electron. Specifically process.resourcePath and process.type. These are both extremely interesting to me, as often in code I need to get an asset file - though the path for such a file will change depending on if it is packaged, running as development electron
or as node-js
.
So, what we want to do, is check to see if process.resourcesPath
returns anything - which it will not in node.js
. If it does return something, we want to check if it returns something with app.asar
inside it. This would mean (based on how we bundle things for distribution) that electron
is bundled. This does mean, we will need to ignore expected typescript
errors, as the type checking doesn’t expect resourcePath
to ever exist on the process
object. This lands us to this type of statement;
1 | // @@ts-expect-error |
Now you can properly write functions to handle code differently based on whether the code is inside the main
process, renderer
or being run by some of your node-js
code directly. Here is a prototype function I came up with which is semi-specific for my usecase, due to the local compile locations (elect/
vs elect/js
vs src/cli/
);
1 | /** |