Client configuration in Electron - electron

I am quite new to Electron and I have two questions regarding configuration of my app.
1) Looking for a way to store client configuration (similar to app.config of .Net Apps).
Why I need this:
I am working on a desktop electron app. This app will be distributed to a number of machines and each of them need to have different configuration values.
2) Need a way to package electron app with predefined configuration for a specific machine.
Example:
Machine 1's config: MachineID='M01', MachineType='A'
Machine 2's config: MachineID='M02', MachineType='B'
Appreciate the support!

For me it was the same but I needed to save settings about the window so I made a settings.js file and stored my settings in there.
For you your settings file would contain something like this:
exports.machineType = 'A';
And you would get it in your app like this:
var settings = require('./settings.js');
var machineType = settings.machineType;
if (machineType == 'A') {
// Do things for machine type A.
} else {
// Do things for machine type B.
}

Related

Can I force applications to run in a different locale to the system setting?

I am developing a C# .NET 6 client-server product using VS2022 with multi-language support. I've set up locale-specific resource strings but at the last minute I realized a problem: while client workstations are set up in local language, the app-server is always in English - so back-end code is using the English localization!
Servers are in English for a good reason (and we can't change this) so is there a way to force a deployed application to use a specified locale? Perhaps in a config file somewhere?
I know I can do this in code by changing Thread.CurrentCulture or similar, but the whole point is I don't want to hard-code it, I want it to be config-driven in a way that overrides the default system setting.
I want it to be config-driven in a way that overrides the default system setting.
You can always add custom setting to your config file and then read it on start up and set needed culture.
For example something like this:
appsettings.json
{
"LocaleOverride" : "en-US",
// rest of settings
}
And somewhere at the start of app (depends on how it is started, if generic/minimal hosting is used, configuration can be read from there, otherwise - manually):
var locale = Configuration["LocaleOverride"];
if(!string.IsNullOrEmpty(locale))
{
var cultureInfo = new CultureInfo(locale);
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
}

How to read config file in electronjs app

It's my first time using Electron JS and nodejs. I've built a small app that reads some records from a database and updates them. Everything is working fine. I have a config file with the database credentials but when I build a portable win app, I cannot figure out how to read the config file that I would like to place next to the exe. I would like to have easy access to the file, so I could run the same app on different databases.
Can anyone tell me if what I want is possible and how? I already tried to get the exe location but I couldn't. I also read a lot of topics here but nothing seems to solve my problem (I might be doing something wrong).
I'm using electron-builder to build my app.
Thanks in advance.
Edit #1
My Config file is
{
"user" :"X",
"password" :"X",
"server":"X",
"database":"X",
"options":
{
"trustedconnection": true,
"enableArithAbort" : true,
"trustServerCertificate": true
}
}
This is what I've and works when I run the project with npm start
const configRootPath = path.resolve(__dirname,'dbConfig.json');
dbConfig = JSON.parse(fs.readFileSync(configRootPath, { encoding: 'utf-8' }));
However, when I build it, the app is looking for the file in another location different from the one where the executable is.
Use of Electron's app.getPath(name) function will get you the path(s) you are after, irrespective of which OS (Operating System) you are using.
Unless your application writes your dbConfig.json file, it may be difficult for your user to understand exactly where they should place their database config file as each OS will run and store your application data in a different directory. You would need to be explicit to the user as to where to place their config file(s). Alternatively, your application could create the config file(s) on the user's behalf (automatically or through a html form) and save it to a location 'known' to the application.
A common place where application specific config files are stored is in the user's application data directory. With the application name automatically amended to the directory, it can be found as shown below.
const electronApp = require('electron').app;
let appUserDataPath = electronApp.getPath('userData');
console.log(appUserDataPath );
In your use case, the below would apply.
const electronApp = require('electron').app;
const nodeFs = require('fs');
const nodePath = require('path');
const configRootPath = nodePath.join(electronApp.getPath('userData'), 'dbConfig.json');
dbConfig = JSON.parse(nodeFs.readFileSync(configRootPath, 'utf-8'));
console.log(configRootPath);
console.log(dbConfig);
You can try electron-store to store config.
Electron doesn't have a built-in way to persist user preferences and other data. This module handles that for you, so you can focus on building your app. The data is saved in a JSON file named config.json in app.getPath('userData').

Best Approach for maintaining configuration safely

Overview:
I have an iOS project which contains the server configurations in a Plist. I have different plists for different targets (Dev / Stage etc)
Problem:
This plist gets copied to the main bundle, so I feel it is not so safe as one can get it from the ipa file. I would like it not be accessible.
Question:
What would be the best approach to maintain server configurations for different targets ?
Can plists be used if so how to make it not accessible via the ipa file and yet be readable through code ?
Is there a better approach to solve this problem ?
You can avoid using plists by hardcoding the config. This way it is compiled into the app and not easily accessible via the ipa file. It's not entirely secure, but less accessible than a plist.
Create a file MyConfigDev.swift
// this file is added to the Dev target
class Config {
private init() {}
static let shared = Config()
let myParam = "valueForDev"
}
And another file MyConfigStaging.swift
// this file is added to the Staging target
class Config {
private init() {}
static let shared = Config()
let myParam = "valueForStaging"
}
If you build the Dev target the file MyConfigDev.swiftis used, if you build the Staging target the file MyConfigStaging.swiftis used.
In your code you can access the config:
let p = Config.shared.myParam
To get access to your configuration hackers will now need to decompile your ipa.

How can I locate a file to use with the system/child_process API within a Firefox add-on?

I would like to write a Firefox add-on that communicates with a locally installed program to exchange data. It looks like this can be done using either js-ctypes or the low-level system/child_process API, with the latter being the recommended solution.
The child_process API appeals because it sends and receives data abstractly over a pipe rather than directly at the C interface level. However, to use it you need (it seems) to supply the full path to the executable within your code:
var child_process = require("sdk/system/child_process");
var ls = child_process.spawn('/bin/ls', ['-lh', '/usr']);
In my case, the executable is installed by another application and we don't know it's exact location - it will differ according to OS, the user's drives, and possibly the user's preference. I imagine this problem will be common to most executables that are not built in to the OS. So my question is: what means do I have to locate the full path of the executable I want to use? I will need to support multiple OSes but presumably could have different solutions for each if needed.
Thanks!
Here's the code I used on Windows - the key was being able to read an environment variable to find the location of the appropriate application folder. After that I assume that my application is stored under a well-known subpath (we don't allow customization of it).
var system = require("sdk/system");
var iofile = require('sdk/io/file');
var child_process = require('sdk/system/child_process');
var progFilesFolder = system.env["programfiles(x86)"],
targetFile = iofile.join(progFilesFolder, 'FolderName', 'Program.exe');
targetFileExists = iofile.exists(targetFile);
if (targetFileExists) {
var p = child_process.spawn(targetFile);
}
I haven't written the code for Mac yet but I expect it to be similar, with the difference being that there are no drive letters to worry about and the system folders in OS X have standard names (even on localized systems).

Setting/overriding an app deployment folder

Is there a way to set or override a project deployment folder in Mono for Android? For example, my application right now deploys to /data/data/SolutionEngine/files/.__override__
The nature of the application is that it loads plug-ins using Reflection, and by default it looks in the /Adapters sub-folder from the app root. This is how it works on the desktop and the Compact Framework, so for simplicity we'd like to continue to do the same on Android.
If I have a single solution that has the app and some plug-ins in it, I'd like those files to get deployed in the proper structure when I start debugging.
You could write out the plugins as android assets (see screenshot below). Please Note: You might need to change the extension to .mp3. See here. I didn't have this issue though.
Once you do that, you should be able to get the assets by using the Asset Manager. You can copy them to a different folder or do whatever with them. Here is a sample of reading them into memory and them writing out the name.
const String pluginPath = "Plugins";
var pluginAssets = Assets.List(pluginPath);
foreach (var pluginAsset in pluginAssets)
{
var file = Assets.Open(pluginPath + Java.IO.File.Separator + pluginAsset);
using (var memStream = new MemoryStream())
{
file.CopyTo(memStream);
//do something fun.
var assembly = System.Reflection.Assembly.Load(memStream.ToArray());
Console.WriteLine(String.Format("Loaded: {0}", assembly.FullName));
}
}

Resources