Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 85 additions & 14 deletions lib/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,46 @@ const RWlock = require('rwlock');
const utils = require('./utils');
const lock = new RWlock();

/**
* @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
Expand All @@ -64,9 +104,13 @@ const lock = new RWlock();
* console.log(data);
* });
*/
exports.get = function(key, callback) {
exports.get = function(key, directory, callback) {
if(arguments.length === 2){
callback = directory;
directory = "";
}
async.waterfall([
async.asyncify(_.partial(utils.getFileName, key)),
async.asyncify(_.partial(utils.getFileName, key, directory)),
function(fileName, callback) {
fs.readFile(fileName, {
encoding: 'utf8'
Expand Down Expand Up @@ -122,7 +166,6 @@ exports.getMany = function(keys, callback) {
if (error) {
return callback(error);
}

return callback(null, _.set(reducer, key, data));
});
}, callback);
Expand All @@ -136,6 +179,7 @@ exports.getMany = function(keys, callback) {
* @description
* This function returns an empty object if there is no data to be read.
*
* @param {Function} directory - optional parameter, to get data in a different folder than the current user data
* @param {Function} callback - callback (error, data)
*
* @example
Expand All @@ -147,13 +191,17 @@ exports.getMany = function(keys, callback) {
* console.log(data);
* });
*/
exports.getAll = function(callback) {
exports.getAll = function(directory, callback) {
if(arguments.length === 1){
callback = directory;
directory = "";
}
async.waterfall([
exports.keys,
_.partial(exports.keys, directory),
function(keys, callback) {
async.reduce(keys, {}, function(reducer, key, callback) {
async.waterfall([
_.partial(exports.get, key),
_.partial(exports.get, key, directory),
function(contents, callback) {
return callback(null, _.set(reducer, key, contents));
}
Expand Down Expand Up @@ -251,6 +299,7 @@ exports.has = function(key, callback) {
* @function
* @public
*
* @param {Function} directory - optional parameter, to get keys of a different folder than the current user data
* @param {Function} callback - callback (error, keys)
*
* @example
Expand All @@ -264,18 +313,29 @@ exports.has = function(key, callback) {
* }
* });
*/
exports.keys = function(callback) {
exports.keys = function(directory, callback) {

if(arguments.length === 1){
callback = directory;
directory = "";
}
async.waterfall([
async.asyncify(utils.getUserDataPath),
function(userDataPath, callback) {
mkdirp(userDataPath, function(error) {
function (done) {
if (directory !== "") {
return done(null, directory);
} else {
return done(null, utils.getUserDataPath());
}
},
function (userDataPath, callback) {
mkdirp(userDataPath, function (error) {
return callback(error, userDataPath);
});
},
fs.readdir,
function(keys, callback) {
callback(null, _.map(_.reject(keys, function(key) {
return _.includes([ '.DS_Store' ], key);
return _.includes([ '.DS_Store' ], key) || path.extname(key) !== '.json';
}), function(key) {
return path.basename(decodeURIComponent(key), '.json');
}));
Expand Down Expand Up @@ -310,7 +370,7 @@ exports.remove = function(key, callback) {
};

/**
* @summary Clear all stored data
* @summary Clear all stored data in current user data path
* @function
* @public
*
Expand All @@ -323,8 +383,19 @@ exports.remove = function(key, callback) {
* if (error) throw error;
* });
*/
exports.clear = function(callback) {
const userData = utils.getUserDataPath();
exports.clear = function(directory, callback) {
if(arguments.length === 1){
callback = directory;
directory = "";
}

let userData = "";
if (directory !== "") {
userData = directory;
} else {
userData = utils.getUserDataPath();
}

const jsonFiles = path.join(userData, '*.json');
rimraf(jsonFiles, callback);
};
47 changes: 36 additions & 11 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@
const _ = require('lodash');
const path = require('path');
const electron = require('electron');
const app = electron.app || electron.remote.app;

const defaultBasePath = path.join(app.getPath('userData'), 'storage');
let currentDirectory = defaultBasePath;

/**
* @summary Set default user data directory path
* @function
* @public
*
* @example
* utils.getUserDataPath('newDirectory');
*/
exports.setUserDataPath = function (directory = "") {
if (directory === "") {
currentDirectory = defaultBasePath;
} else {
if (path.isAbsolute(directory)) {
currentDirectory = path.normalize(directory);
} else {
throw new Error('Not an absolute directory');
}
}

};

/**
* @summary Get user data directory path
Expand All @@ -35,18 +60,13 @@ const electron = require('electron');
*
* @returns {Strings} user data path
*
* @example
* let userDataPath = utils.getUserDataPath();
* console.log(userDataPath);
*/
exports.getUserDataPath = function() {
const app = electron.app || (electron.remote && electron.remote.app);

if (app) {
return path.join(app.getPath('userData'), 'storage');
} else {
return '/tmp/storage';
}
};
exports.getUserDataPath = function() {
return currentDirectory;
};

/**
* @summary Get storage file name for a key
Expand All @@ -60,7 +80,7 @@ const electron = require('electron');
* let fileName = utils.getFileName('foo');
* console.log(fileName);
*/
exports.getFileName = function(key) {
exports.getFileName = function(key, directory = "") {
if (!key) {
throw new Error('Missing key');
}
Expand All @@ -78,5 +98,10 @@ exports.getFileName = function(key) {
// See: https://en.wikipedia.org/wiki/Filename#Reserved%5Fcharacters%5Fand%5Fwords
const escapedFileName = encodeURIComponent(keyFileName);

return path.join(exports.getUserDataPath(), escapedFileName);
if (directory === "") {
return path.join(exports.getUserDataPath(), escapedFileName);
} else {
return path.join(directory, escapedFileName);
}

};
87 changes: 87 additions & 0 deletions tests/storage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,27 @@ describe('Electron JSON Storage', function() {

describe('.getAll()', function() {

const newpath = path.join(utils.getUserDataPath(), "newfolder");

beforeEach(function(done) {
async.waterfall( [
async.asyncify(_.partial(utils.setUserDataPath, newpath)),
function (err,done) {
storage.clear("", done);
},
async.asyncify(_.partial(utils.setUserDataPath,"")),
function (err,done) {
storage.clear("", done);
},
], done);
});

afterEach(function (done) {
async.waterfall( [
async.asyncify(_.partial(utils.setUserDataPath))
], done);
});

describe('given the user data path does not exist', function() {

beforeEach(function(done) {
Expand Down Expand Up @@ -348,6 +369,64 @@ describe('Electron JSON Storage', function() {

});

describe('given many stored keys in multiple directory', function() {

beforeEach(function(done) {
async.parallel([
_.partial(storage.set, 'foo', { name: 'foo' }),
_.partial(storage.set, 'bar', { name: 'bar' }),
], done);
});

it('should return only the keys stored in the current user directory', function(done) {

async.waterfall([
async.asyncify(_.partial(utils.setUserDataPath, newpath)),
function(err, done) {
storage.set('baz', { name: 'baz' }, done);
},
function (done) {
storage.getAll(function (error, data) {
m.chai.expect(error).to.not.exist;
m.chai.expect(data).to.deep.equal({
baz: { name: 'baz' }
});
done();
});
}
], done);
});
});

describe('given many stored keys in multiple directory', function() {

beforeEach(function(done) {
async.parallel([
_.partial(storage.set, 'foo', { name: 'foo' }),
_.partial(storage.set, 'bar', { name: 'bar' }),
], done);
});

it('should return only the keys stored in the specified directory', function(done) {

async.waterfall([
async.asyncify(_.partial(utils.setUserDataPath, newpath)),
function(err, done) {
storage.set('baz', { name: 'baz' }, done);
},
async.asyncify(_.partial(utils.setUserDataPath)),
function (err, done) {
storage.getAll(newpath, function (error, data) {
m.chai.expect(error).to.not.exist;
m.chai.expect(data).to.deep.equal({
baz: { name: 'baz' }
});
done();
});
}
], done);
});
});
});

describe('.set()', function() {
Expand Down Expand Up @@ -579,6 +658,14 @@ describe('Electron JSON Storage', function() {

describe('.keys()', function() {

beforeEach(function () {
utils.setUserDataPath();
});

afterEach(function () {
utils.setUserDataPath();
});

describe('given a file name with colons', function() {

beforeEach(function(done) {
Expand Down
Loading