-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ran Yitzhaki
committed
Jul 5, 2016
1 parent
2886e29
commit b141df6
Showing
9 changed files
with
290 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "airbnb-base", | ||
"env": { | ||
"mocha": "true" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
'use strict'; | ||
|
||
var _fs = require('fs'); | ||
|
||
var _fs2 = _interopRequireDefault(_fs); | ||
|
||
var _mkdirp = require('mkdirp'); | ||
|
||
var _mkdirp2 = _interopRequireDefault(_mkdirp); | ||
|
||
var _path = require('path'); | ||
|
||
var _path2 = _interopRequireDefault(_path); | ||
|
||
var _utils = require('./utils'); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
/** | ||
* _get - function the gets filePath and a callback and returns a parse json | ||
* from the file in the specefied path | ||
* | ||
* @param {string} filePath - path to the file relative to the `userData` folder | ||
* @param {function} cb - a callback function | ||
*/ | ||
/* eslint no-underscore-dangle: 0 */ | ||
|
||
function _get(filePath, cb) { | ||
var fullPath = (0, _utils.getElectronFullPath)(filePath); | ||
return _fs2.default.readFile(fullPath, { encoding: 'utf8' }, function (err, json) { | ||
if (!err) { | ||
var object = (0, _utils.tryParseJson)(json); | ||
|
||
if (object instanceof Error) { | ||
return cb(new Error('The file in path ' + fullPath + ' is not a valid json file'), null); | ||
} | ||
|
||
return cb(null, object); | ||
} | ||
|
||
if (err.code === 'ENOENT') { | ||
return cb(new Error('The file in path ' + fullPath + ' doesn\'t exist'), null); | ||
} | ||
|
||
return cb(err); | ||
}); | ||
} | ||
|
||
/** | ||
* get - function the gets filePath and a callback and returns a parse json | ||
* from the file in the specefied path | ||
* | ||
* @param {string} filePath - path to the file relative to the `userData` folder | ||
* @param {undefined | function} cb - an optional callback function | ||
* @return {undefined | Promise} if there is only the first argument, the function | ||
* will return a thenable Promise object | ||
*/ | ||
function get(filePath, cb) { | ||
if (cb && (0, _utils.isFunction)(cb)) { | ||
return _get(filePath, cb); | ||
} | ||
|
||
return new Promise(function (resolve, reject) { | ||
return _get(filePath, function (err, data) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
|
||
return resolve(data); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* _set - sets a json file into the storage at a path (relative to the `userData` folder) | ||
* also stringify the json if needed and create any folder that is required in the path | ||
* | ||
* @param {string} filePath - path to the file relative to the `userData` folder | ||
* @param {string | object} data - the data to save in the file path, can be parseJson or an object | ||
* @param {type} cb - a callback function | ||
*/ | ||
function _set(filePath, data, cb) { | ||
var fullPath = (0, _utils.getElectronFullPath)(filePath); | ||
var json = (0, _utils.tryStringifyJson)(data); | ||
|
||
if (json instanceof Error) { | ||
return cb(new Error('The file you trying to save at path ' + fullPath + ' is not valid json')); | ||
} | ||
|
||
var dir = _path2.default.dirname(fullPath); | ||
|
||
return _fs2.default.access(dir, _fs2.default.F_OK, function (notExists) { | ||
if (!notExists) return _fs2.default.writeFile(fullPath, json, cb); | ||
return (0, _mkdirp2.default)(dir, function (err) { | ||
if (err) return cb(err); | ||
return _fs2.default.writeFile(fullPath, json, cb); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* set - sets a json file into the storage at a path (relative to the `userData` folder) | ||
* also stringify the json if needed and create any folder that is required in the path | ||
* | ||
* @param {string} filePath - path to the file relative to the `userData` folder | ||
* @param {string | object} data - the data to save in the file path, can be parseJson or an object | ||
* @param {undefined | function} cb - an optional callback function | ||
* @return {undefined | Promise} if there are only two first arguments, the function | ||
* will return a thenable Promise object | ||
*/ | ||
|
||
function set(filePath, data, cb) { | ||
if (cb && (0, _utils.isFunction)(cb)) { | ||
return _set(filePath, data, cb); | ||
} | ||
|
||
return new Promise(function (resolve, reject) { | ||
return _set(filePath, data, function (err) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
|
||
return resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* _isPathExists - check whether the path inserted is exist in the userData directory | ||
* | ||
* @param {string} fileOrDirPath - a path inside of the userData directory | ||
* @param {func} cb - a callback function | ||
*/ | ||
function _isPathExists(fileOrDirPath, cb) { | ||
var fullPath = (0, _utils.getElectronFullPath)(fileOrDirPath); | ||
|
||
return _fs2.default.exists(fullPath, function (exists) { | ||
if (exists) { | ||
return cb(true); | ||
} | ||
|
||
return cb(false); | ||
}); | ||
} | ||
|
||
/** | ||
* isPathExists - check whether the path inserted is exist in the userData directory | ||
* | ||
* @param {string} fileOrDirPath - a path inside of the userData directory | ||
* @param {undefined|func} cb - an optional callback function | ||
* @return {undefined | Promise} if there are only two first arguments, the function | ||
* will return a thenable Promise object | ||
*/ | ||
function isPathExists(fileOrDirPath, cb) { | ||
if (cb && (0, _utils.isFunction)(cb)) { | ||
return _isPathExists(fileOrDirPath, cb); | ||
} | ||
|
||
return new Promise(function (resolve) { | ||
return _isPathExists(fileOrDirPath, function (result) { | ||
return resolve(result); | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
get: get, | ||
set: set, | ||
isPathExists: isPathExists | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
/* eslint object-shorthand: 0 */ | ||
/* eslint no-param-reassign: 0 */ | ||
/* eslint import/no-unresolved: 0 */ | ||
|
||
var electron = require('electron'); | ||
var app = electron.app || electron.remote.app; | ||
var userData = app.getPath('userData'); | ||
|
||
function isFunction(functionToCheck) { | ||
var getType = {}; | ||
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'; | ||
} | ||
|
||
function tryParseJson(stringJson) { | ||
var object = void 0; | ||
try { | ||
object = JSON.parse(stringJson); | ||
} catch (e) { | ||
return e; | ||
} | ||
|
||
return object; | ||
} | ||
|
||
function tryStringifyJson(objectJson) { | ||
if (typeof objectJson === 'string') { | ||
return objectJson; | ||
} | ||
|
||
var string = void 0; | ||
try { | ||
string = JSON.stringify(objectJson); | ||
} catch (e) { | ||
return e; | ||
} | ||
|
||
return string; | ||
} | ||
|
||
function getElectronFullPath(path) { | ||
return userData + '/' + path; | ||
} | ||
|
||
module.exports = { | ||
isFunction: isFunction, | ||
tryParseJson: tryParseJson, | ||
tryStringifyJson: tryStringifyJson, | ||
getElectronFullPath: getElectronFullPath | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const gulp = require('gulp'); | ||
const babel = require('gulp-babel'); | ||
|
||
gulp.task('default', () => | ||
gulp.src('./src/**/*.js') | ||
.pipe(babel({ | ||
presets: ['es2015'], | ||
})) | ||
.pipe(gulp.dest('dist')) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.