Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict';
  2. const path = require('path');
  3. const os = require('os');
  4. const fs = require('graceful-fs');
  5. const makeDir = require('make-dir');
  6. const xdgBasedir = require('xdg-basedir');
  7. const writeFileAtomic = require('write-file-atomic');
  8. const dotProp = require('dot-prop');
  9. const uniqueString = require('unique-string');
  10. const configDirectory = xdgBasedir.config || path.join(os.tmpdir(), uniqueString());
  11. const permissionError = 'You don\'t have access to this file.';
  12. const makeDirOptions = {mode: 0o0700};
  13. const writeFileOptions = {mode: 0o0600};
  14. class Configstore {
  15. constructor(id, defaults, options = {}) {
  16. const pathPrefix = options.globalConfigPath ?
  17. path.join(id, 'config.json') :
  18. path.join('configstore', `${id}.json`);
  19. this.path = options.configPath || path.join(configDirectory, pathPrefix);
  20. if (defaults) {
  21. this.all = {
  22. ...defaults,
  23. ...this.all
  24. };
  25. }
  26. }
  27. get all() {
  28. try {
  29. return JSON.parse(fs.readFileSync(this.path, 'utf8'));
  30. } catch (error) {
  31. // Create directory if it doesn't exist
  32. if (error.code === 'ENOENT') {
  33. return {};
  34. }
  35. // Improve the message of permission errors
  36. if (error.code === 'EACCES') {
  37. error.message = `${error.message}\n${permissionError}\n`;
  38. }
  39. // Empty the file if it encounters invalid JSON
  40. if (error.name === 'SyntaxError') {
  41. writeFileAtomic.sync(this.path, '', writeFileOptions);
  42. return {};
  43. }
  44. throw error;
  45. }
  46. }
  47. set all(value) {
  48. try {
  49. // Make sure the folder exists as it could have been deleted in the meantime
  50. makeDir.sync(path.dirname(this.path), makeDirOptions);
  51. writeFileAtomic.sync(this.path, JSON.stringify(value, undefined, '\t'), writeFileOptions);
  52. } catch (error) {
  53. // Improve the message of permission errors
  54. if (error.code === 'EACCES') {
  55. error.message = `${error.message}\n${permissionError}\n`;
  56. }
  57. throw error;
  58. }
  59. }
  60. get size() {
  61. return Object.keys(this.all || {}).length;
  62. }
  63. get(key) {
  64. return dotProp.get(this.all, key);
  65. }
  66. set(key, value) {
  67. const config = this.all;
  68. if (arguments.length === 1) {
  69. for (const k of Object.keys(key)) {
  70. dotProp.set(config, k, key[k]);
  71. }
  72. } else {
  73. dotProp.set(config, key, value);
  74. }
  75. this.all = config;
  76. }
  77. has(key) {
  78. return dotProp.has(this.all, key);
  79. }
  80. delete(key) {
  81. const config = this.all;
  82. dotProp.delete(config, key);
  83. this.all = config;
  84. }
  85. clear() {
  86. this.all = {};
  87. }
  88. }
  89. module.exports = Configstore;