Skip to content

Instantly share code, notes, and snippets.

@tomprogers
Last active April 30, 2017 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomprogers/8b0269f7d3ef2c63cb249554ba107b64 to your computer and use it in GitHub Desktop.
Save tomprogers/8b0269f7d3ef2c63cb249554ba107b64 to your computer and use it in GitHub Desktop.
I wish this existed so I could use it in my Electron app
import Path from 'path';
import AppPaths from 'app-paths';
import Persist from 'auto-persister';
let settings = Persist.manageFile({
path: Path.join(ApPaths.ConfigFile, 'default.myapp-settings'),
json: true, // read & write will marshall JSON automatically
autocreate: true, // file (and parent dirs) will be immediately created if they don't already exist
initialData: DefaultSettings, // value that will be returned if file doesn't exist, and that will be written if autocreate true
// all other options are passed to underlying FS methods
encoding: 'utf8',
...
});
// invoke whenever to write data into the file
settings.write({ data: someObj, async: true })
// invoke whenever to read data from file
settings.read({ async: true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment