New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

electron-cfg

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

electron-cfg

Simple key-value storage for electron

  • 1.2.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
36
decreased by-14.29%
Maintainers
1
Weekly downloads
 
Created
Source

electron-cfg

Build Status npm version Dependencies status

Description

Just a very easy solution for storing settings for an electron application.

Key features

  • Settings observer
  • Built-in helper for saving and restoring window state
  • No dependencies

Installation

Install with npm:

npm install --save electron-cfg

Usage

const config = require('electron-cfg');

const downloadsPath = config.get('downloads.path', process.cwd());

config.observe('album', (current, old) => {
  console.log(`Previous: ${old.name}, current: ${current.name}`);
});

config.set('album', { name, photos: photos.length });

Methods

create(filePath, logger = null)

Create a new config instance with different file path.

get(key, defaultValue = null): any

Returns a value associated with the key.

ParamTypeDescription
keystringKey name, use dot in key to return nested value of some object
defaultValue?anyReturn this values instead if key not found
set(key, value): electron-cfg

Sets a value.

has(key): boolean

Is key exists in the config.

delete(key): electron-cfg

Removes values associated with the key.

getAll(): Object

Gets the root object of the config

setAll(data): ElectronCfg

Sets the root object of the config

file(filePath = null): string

Gets / Sets config's file path. If relative, it uses app.getPath('userData') to resolve the full path.

observe(key, handler): electron-cfg

Attaches a handler on keyName property changes. Changes are observable only in the same process.

ParamTypeDescription
keystringKey name
handler(newValue, oldValue, key) => voidObserver
purge(): electron-cfg

Removes all data from config

logger(logger = console)

Gets / Sets a logger (object with error, warn and debug methods)

window(options?): WindowManager

Allow to save/restore window size and position. See next section for details

ParamTypeDescription
options = {object
-- name: 'main'stringUseful when store settings of multiple windows
-- saveFullscreen: truebooleanWhether to restore fullscreen state
-- saveMaximize: truebooleanWhether to restore maximized state
}
resolveUserDataPath(filePath, appName?)

Return file path relative to userData directory, similar to

path.join(app.getPath('userData'), filePath)

If appName is set, electron-cfg uses its own code to find userData directory for appName without calling app.getPath. Can be helpful in dev environment when app.getPath('userData') resolves an incorrect path.

Save/restore window state

const { BrowserWindow } = require('electron');
const cfg = require('electron-cfg');

function createWindow() {
  const winCfg = cfg.window();
  const window = new BrowserWindow({
    width: 800,  // default, optional
    height: 600, // default, optional
    ...winCfg.options(),
  });
  winCfg.assign(window);
  return window;
}

or it can be simplified using the create shortcut:

const cfg = require('electron-cfg');

function createWindow() {
  return cfg.window().create({ width: 800, height: 600 });
}

Remarks:

  • Don't set useContentSize to true at creating BrowserWindow instance because it changes how to calculate window size.
  • Don't call cfg.window() before the ready event is fired.
WindowManager methods
  • create(options): BrowserWindow - shortcut (see example above)
  • assign(window: BrowserWindow) - start handling size/position change
  • options(): Rectangle | object - get options for BrowserWindow constructor
  • unassign() - stop handling size/position change

Here is a few alternatives which you can try:

A lot of code of Saving/restoring window state is based on electron-window-state

License

Licensed under MIT.

Keywords

FAQs

Package last updated on 05 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc