diff --git a/lib/storage.js b/lib/storage.js index aed7588..b37c920 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -37,6 +37,46 @@ const mkdirp = require('mkdirp'); const path = require('path'); const utils = require('./utils'); +/** + * @summary Set data path + * @function + * @public + * + * @description + * Pass the directory you want to call the functions from. + * If no directory is passed, returns to default 'storage' + * + * @param {String} directory - directory + * + * @example + * const storage = require('electron-json-storage'); + * + * storage.setDataPath('foo'); + */ +exports.setDataPath = function(dir = ""){ + utils.setUserDataPath(dir); +}; + +/** + * @summary Get data path + * @function + * @public + * + * @description + * Returns the current directory being used. + * + * @param {String} directory - directory + * + * @example + * const storage = require('electron-json-storage'); + * + * let dataPath = storage.getDataPath(); + * console.log(dataPath); + */ +exports.getDataPath = function(){ + return utils.getUserDataPath(); +}; + /** * @summary Read user data * @function @@ -121,7 +161,7 @@ exports.getMany = function(keys, callback) { if (error) { return callback(error); } - + // console.log(data) return callback(null, _.set(reducer, key, data)); }); }, callback); @@ -255,9 +295,12 @@ exports.keys = function(callback) { }, fs.readdir, function(keys, callback) { - callback(null, _.map(keys, function(key) { - return path.basename(key, '.json'); - })); + callback(null, keys.filter(function(key){ + return path.extname(key) !== ""; + }).map(function(key) { + return path.basename(key, '.json'); + } + )); } ], callback); }; diff --git a/lib/utils.js b/lib/utils.js index 6425628..f3145b3 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -29,18 +29,53 @@ const path = require('path'); const electron = require('electron'); const app = electron.app || electron.remote.app; +// define the defaultDataPath and current directory +const defaultBasePath = 'storage'; +let currentDirectory = ''; + +/** + * @summary Parse directory to avoid parent directories + * @function + * @private + * + * @returns {Strings} parsed user data path without parent directories + */ +function getDirectory(directory){ + const directoryArray = directory.split(path.posix.sep); + let newDirectory = ""; + for(var dir of directoryArray){ + if(dir !== ".."){ + newDirectory += dir + "/"; + } + } + return newDirectory; +} + +/** + * @summary Set default user data directory path + * @function + * @public + * + * @example + * utils.getUserDataPath('newDirectory'); + */ +exports.setUserDataPath = function(directory) { + currentDirectory = getDirectory(directory); +}; + /** - * @summary Get user data directory path + * @summary Get default user data directory path * @function * @public * * @returns {Strings} user data path * + * @example * let userDataPath = utils.getUserDataPath(); * console.log(userDataPath); */ exports.getUserDataPath = function() { - return path.join(app.getPath('userData'), 'storage'); + return path.join(app.getPath('userData'), defaultBasePath, currentDirectory); }; /**