Skip to content

Commit

Permalink
enable es2015, + build
Browse files Browse the repository at this point in the history
  • Loading branch information
Ran Yitzhaki committed Jul 5, 2016
1 parent 2886e29 commit b141df6
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 29 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "airbnb-base",
"env": {
"mocha": "true"
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ storage.isPathExists(path)
}
});
```
## Development
``` npm run build ```
for creating es5 files in dist folder

## License
The MIT License (MIT)
Expand Down
170 changes: 170 additions & 0 deletions dist/index.js
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
};
51 changes: 51 additions & 0 deletions dist/utils.js
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
};
10 changes: 10 additions & 0 deletions gulpfile.js
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'))
);
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"name": "electron-storage",
"version": "1.0.0",
"description": "module for managing storage in electron applications",
"main": "src/index.js",
"main": "dist/index.js",
"scripts": {
"test": "electron-mocha --recursive tests -R spec && electron-mocha --renderer --recursive tests -R spec",
"lint": "eslint ./src/**/*.js; exit 0"
"test": "gulp; electron-mocha --recursive tests -R spec && electron-mocha --renderer --recursive tests -R spec",
"lint": "eslint ./src/**/*.js; exit 0",
"build": "gulp"
},
"homepage": "https://github.com/ran-y/electron-storage",
"repository": {
Expand All @@ -23,12 +24,15 @@
"author": "Ran yitzhaki",
"license": "MIT",
"devDependencies": {
"babel-preset-es2015": "^6.9.0",
"chai": "^3.5.0",
"electron-mocha": "^2.2.1",
"electron-prebuilt": "^1.2.5",
"eslint": "^3.0.0",
"eslint-config-airbnb-base": "^3.0.1",
"eslint-plugin-import": "^1.10.2",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"proxyquire": "^1.7.10"
},
"dependencies": {
Expand Down
23 changes: 12 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* eslint object-shorthand: 0 */
/* eslint no-underscore-dangle: 0 */

const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const isFunction = require('./utils').isFunction;
const tryParseJson = require('./utils').tryParseJson;
const tryStringifyJson = require('./utils').tryStringifyJson;
const getElectronFullPath = require('./utils').getElectronFullPath;
import fs from 'fs';
import mkdirp from 'mkdirp';
import path from 'path';
import {
isFunction,
tryParseJson,
tryStringifyJson,
getElectronFullPath,
} from './utils';

/**
* _get - function the gets filePath and a callback and returns a parse json
Expand Down Expand Up @@ -155,7 +156,7 @@ function isPathExists(fileOrDirPath, cb) {
}

module.exports = {
get: get,
set: set,
isPathExists: isPathExists,
get,
set,
isPathExists,
};
Loading

0 comments on commit b141df6

Please sign in to comment.