From bbe96fae5cb50cc1816e838c0dc99cf0062002c2 Mon Sep 17 00:00:00 2001 From: zhenshuai Date: Mon, 15 May 2017 12:04:33 +0800 Subject: [PATCH] add setSync function --- .gitignore | 1 + lib/storage.js | 28 ++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b38069d..cb96681 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,4 @@ build/Release # Dependency directory # https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git node_modules +.idea/ \ No newline at end of file diff --git a/lib/storage.js b/lib/storage.js index abb89d1..9aa629e 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -200,6 +200,34 @@ exports.set = function(key, json, callback) { ], callback); }; +/** + * @summary Write user data sync + * @function + * @public + * + * @param {String} key - key + * @param {Object} json - json object + * @param {Function} callback - callback (error) + * + * @example + * const storage = require('electron-json-storage'); + * + * storage.setSync('foobar', { foo: 'bar' }, function(error) { + * if (error) throw error; + * }); + */ +exports.setSync = function(key, json, callback) { + const fileName = utils.getFileName(key); + const data = JSON.stringify(json); + try{ + mkdirp.sync(path.dirname(fileName)); + fs.writeFileSync(fileName, data, callback); + } + catch (error){ + callback(error); + } +}; + /** * @summary Check if a key exists * @function diff --git a/package.json b/package.json index 5307681..1226367 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "electron-json-storage", - "version": "3.0.5", + "version": "3.0.6", "description": "Easily write and read user settings in Electron apps", "main": "lib/storage.js", "homepage": "https://github.com/jviotti/electron-json-storage",