electron-directory
Introduction
When I started using Electron, I came accros some difficulties to work with path/fs because of the three different states of the application: developpement, packaged or asar packed. Each one of them has its own way around with directories. That's how I ended up coding this little module to help unify the way I accessed files.
Initialization
The module is wrapped within a promise, it take the application cwd path as argument as shown here:
var electronDirectory = dirHelper electronExecPath applicationJsPath ; ;
Read the methods section for more info.
Methods
getElectronPath(path)
Returns the full path relative to the executing Electron directory.
electronDirectoryInstance ;
getApplicationPath(path)
Returns the full path relative to the application directory, whenever it's within the asar or not.
electronDirectoryInstance ;
Usage sample
Here is a use case that illustrate the whole process. We will grab a default JSON config file in the application directory and, if the config.json doesn't exist yet, will create it into the Electron directory.
/app.js
#!/usr/bin/env node try var app = BrowserWindow = windows = {} ; app; app;catchex console; exstack && console;
/js/main.js
try var configHelperModule = configHelper config { return { ; }; } ; ;catchex console; exstack && console;
/js/modules/config-helper.js
var path = fs = electronDirectory = { return { fs; }; } { return { fs; }; } _eventsNames = addingConfigServer: 'adding-config-server' addedConfigServer: 'added-config-server' { return { var configBase = 'config.json' defaultConfigBase = 'config/config.default.json' defaultConfigPath configPath config dirHelper { return { ifconfig return ; ; }; } { return { var data = JSON; config = newConfig; ; }; } { return { if!configservers || !configserverslength return ; return ; }; } { return { configservers; return ; } } { return { process; return ; }; } { return { var { ifresult && resultlength return Promise; return Promise; }; if!serveraddress return ; ; }; } { return { process; return ; }; } handlers = { ; } modulePublics = getConfig: getConfig setConfig: setConfig getServerByAddress: getServerByAddress eventsNames: _eventsNames ; ; }; } ; moduleexports = configHelper;