settings.js 885 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * Settings management methods
  3. *
  4. *
  5. */
  6. 'use strict';
  7. var appSettingsPath = 'app-settings.json';
  8. module.exports = function(fs, path) {
  9. function get() {
  10. return fs.exists(appSettingsPath)
  11. .then(function() { return fs.readFile(appSettingsPath); })
  12. .then(JSON.parse)
  13. .then(validateFormat);
  14. }
  15. function create() {
  16. var basePath = path.resolve(__dirname, '../../' + appSettingsPath);
  17. return fs.copy(basePath, appSettingsPath);
  18. }
  19. function remove() {
  20. return fs.unlink(appSettingsPath)
  21. .catch(function(err) {
  22. if (err && err.code !== 'ENOENT') {
  23. throw err;
  24. }
  25. });
  26. }
  27. function validateFormat(config) {
  28. if(Array.isArray(config) && Object.keys(config[0]).length) {
  29. return config;
  30. }
  31. var err = "app-settings.json not valid: is empty or is not an array";
  32. throw err;
  33. }
  34. return {
  35. get: get,
  36. create: create,
  37. remove: remove
  38. };
  39. };