index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. 'use strict';
  2. var _fs = require('fs');
  3. var _fs2 = _interopRequireDefault(_fs);
  4. var _mkdirp = require('mkdirp');
  5. var _mkdirp2 = _interopRequireDefault(_mkdirp);
  6. var _path = require('path');
  7. var _path2 = _interopRequireDefault(_path);
  8. var _rimraf = require('rimraf');
  9. var _rimraf2 = _interopRequireDefault(_rimraf);
  10. var _utils = require('./utils');
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * _get - function the gets filePath and a callback and returns a parse json
  14. * from the file in the specefied path
  15. *
  16. * @param {string} filePath - path to the file relative to the `userData` folder
  17. * @param {function} cb - a callback function
  18. */
  19. function _get(filePath, cb) {
  20. var fullPath = (0, _utils.processPath)(filePath);
  21. return _fs2.default.readFile(fullPath, { encoding: 'utf8' }, function (err, json) {
  22. if (!err) {
  23. var object = (0, _utils.tryParseJson)(json);
  24. if (object instanceof Error) {
  25. return cb(new Error('The file in path ' + fullPath + ' is not a valid json file'), null);
  26. }
  27. return cb(null, object);
  28. }
  29. if (err.code === 'ENOENT') {
  30. err.message = 'The file in path ' + fullPath + ' doesn\'t exist'; /* eslint no-param-reassign: 0 */
  31. }
  32. return cb(err);
  33. });
  34. }
  35. /**
  36. * get - function the gets filePath and a callback and returns a parse json
  37. * from the file in the specefied path
  38. *
  39. * @param {string} filePath - path to the file relative to the `userData` folder
  40. * @param {undefined | function} cb - an optional callback function
  41. * @return {undefined | Promise} if there is only the first argument, the function
  42. * will return a thenable Promise object
  43. */
  44. /* eslint no-underscore-dangle: 0 */
  45. function get(filePath, cb) {
  46. if (cb && (0, _utils.isFunction)(cb)) {
  47. return _get(filePath, cb);
  48. }
  49. return new Promise(function (resolve, reject) {
  50. return _get(filePath, function (err, data) {
  51. if (err) return reject(err);
  52. return resolve(data);
  53. });
  54. });
  55. }
  56. /**
  57. * _set - sets a json file into the storage at a path (relative to the `userData` folder)
  58. * also stringify the json if needed and create any folder that is required in the path
  59. *
  60. * @param {string} filePath - path to the file relative to the `userData` folder
  61. * @param {string | object} data - the data to save in the file path, can be parseJson or an object
  62. * @param {type} cb - a callback function
  63. */
  64. function _set(filePath, data, cb) {
  65. var fullPath = (0, _utils.processPath)(filePath);
  66. var json = (0, _utils.tryStringifyJson)(data);
  67. if (json instanceof Error) {
  68. return cb(new Error('The file you trying to save at path ' + fullPath + ' is not valid json'));
  69. }
  70. var dir = _path2.default.dirname(fullPath);
  71. return _fs2.default.access(dir, _fs2.default.F_OK, function (notExists) {
  72. if (!notExists) return _fs2.default.writeFile(fullPath, json, cb);
  73. return (0, _mkdirp2.default)(dir, function (err) {
  74. if (err) return cb(err);
  75. return _fs2.default.writeFile(fullPath, json, cb);
  76. });
  77. });
  78. }
  79. /**
  80. * set - sets a json file into the storage at a path (relative to the `userData` folder)
  81. * also stringify the json if needed and create any folder that is required in the path
  82. *
  83. * @param {string} filePath - path to the file relative to the `userData` folder
  84. * @param {string | object} data - the data to save in the file path, can be parseJson or an object
  85. * @param {undefined | function} cb - an optional callback function
  86. * @return {undefined | Promise} if there are only two first arguments, the function
  87. * will return a thenable Promise object
  88. */
  89. function set(filePath, data, cb) {
  90. if (cb && (0, _utils.isFunction)(cb)) {
  91. return _set(filePath, data, cb);
  92. }
  93. return new Promise(function (resolve, reject) {
  94. return _set(filePath, data, function (err) {
  95. if (err) return reject(err);
  96. return resolve();
  97. });
  98. });
  99. }
  100. /**
  101. * _isPathExists - check whether the path inserted is exist in the userData directory
  102. *
  103. * @param {string} fileOrDirPath - a path inside of the userData directory
  104. * @param {func} cb - a callback function
  105. */
  106. function _isPathExists(fileOrDirPath, cb) {
  107. var fullPath = (0, _utils.processPathNoJson)(fileOrDirPath);
  108. return _fs2.default.exists(fullPath, function (exists) {
  109. if (exists) return cb(true);
  110. return cb(false);
  111. });
  112. }
  113. /**
  114. * isPathExists - check whether the path inserted is exist in the userData directory
  115. *
  116. * @param {string} fileOrDirPath - a path inside of the userData directory
  117. * @param {undefined|func} cb - an optional callback function
  118. * @return {undefined | Promise} if there are only two first arguments, the function
  119. * will return a thenable Promise object
  120. */
  121. function isPathExists(fileOrDirPath, cb) {
  122. if (cb && (0, _utils.isFunction)(cb)) {
  123. return _isPathExists(fileOrDirPath, cb);
  124. }
  125. return new Promise(function (resolve) {
  126. return _isPathExists(fileOrDirPath, function (result) {
  127. return resolve(result);
  128. });
  129. });
  130. }
  131. /**
  132. * _remove - remove the file/folder from the path inserted
  133. *
  134. * @param {string} fileOrDirPath - a path inside of the userData directory
  135. * @param {func} cb - a callback function
  136. */
  137. function _remove(fileOrDirPath, cb) {
  138. var fullPath = (0, _utils.processPathNoJson)(fileOrDirPath);
  139. return (0, _rimraf2.default)(fullPath, cb);
  140. }
  141. /**
  142. * remove - remove the file/folder from the path inserted
  143. *
  144. * @param {string} fileOrDirPath - a path inside of the userData directory
  145. * @param {undefined|func} cb - an optional callback function
  146. * @return {undefined | Promise} if there are only two first arguments, the function
  147. * will return a thenable Promise object
  148. */
  149. function remove(fileOrDirPath, cb) {
  150. if (cb && (0, _utils.isFunction)(cb)) {
  151. return _remove(fileOrDirPath, cb);
  152. }
  153. return new Promise(function (resolve, reject) {
  154. return _remove(fileOrDirPath, function (error) {
  155. if (error) return reject(error);
  156. return resolve();
  157. });
  158. });
  159. }
  160. module.exports = {
  161. get: get,
  162. set: set,
  163. isPathExists: isPathExists,
  164. remove: remove
  165. };