npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-cfg

v1.2.7

Published

Simple key-value storage for electron

Downloads

114

Readme

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.

Param | Type | Description --------------|--------|------------ key | string | Key name, use dot in key to return nested value of some object defaultValue? | any | Return 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.

Param | Type | Description --------------|-----------------------------------|------------ key | string | Key name handler | (newValue, oldValue, key) => void | Observer

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

Param | Type | Description --------------------------|------------------|------------ options = { | object | -- name: 'main' | string | Useful when store settings of multiple windows -- saveFullscreen: true | boolean | Whether to restore fullscreen state -- saveMaximize: true | boolean | Whether 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

Related project

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.