Source: registry/registry.js

  1. /*global process */
  2. /**
  3. * @file Registry for connection information, cached in local file system
  4. * @author Shinichi Tomita <shinichi.tomita@gmail.com>
  5. */
  6. 'use strict';
  7. var _ = require('lodash/core');
  8. var Connection = require('../connection');
  9. /* */
  10. var Registry = function(configFilePath) {
  11. this._registryConfig = {};
  12. };
  13. /**
  14. * @private
  15. * @override
  16. */
  17. Registry.prototype._saveConfig = function() {
  18. throw new Error('_saveConfig must be implemented in subclass');
  19. };
  20. Registry.prototype._getClients = function() {
  21. return this._registryConfig.clients || (this._registryConfig.clients = {});
  22. };
  23. Registry.prototype._getConnections = function() {
  24. return this._registryConfig.connections || (this._registryConfig.connections = {});
  25. };
  26. Registry.prototype.getConnectionNames = function() {
  27. return Object.keys(this._getConnections());
  28. };
  29. Registry.prototype.getConnection = function(name) {
  30. return new Connection(this.getConnectionConfig(name));
  31. };
  32. Registry.prototype.getConnectionConfig = function(name) {
  33. if (!name) { name = this._registryConfig["default"]; }
  34. var connections = this._getConnections();
  35. var connConfig = connections[name];
  36. if (connConfig) {
  37. connConfig = _.clone(connConfig);
  38. if (connConfig.client) {
  39. connConfig.oauth2 = _.clone(this.getClient(connConfig.client));
  40. }
  41. delete connConfig.client;
  42. }
  43. return connConfig;
  44. };
  45. Registry.prototype.saveConnectionConfig = function(name, connConfig) {
  46. var connections = this._getConnections();
  47. connConfig = _.clone(connConfig);
  48. if (connConfig.oauth2) {
  49. var clientName = this._findClientName(connConfig.oauth2);
  50. if (clientName) {
  51. connConfig.client = clientName;
  52. }
  53. delete connConfig.oauth2;
  54. }
  55. connections[name] = connConfig;
  56. this._saveConfig();
  57. };
  58. Registry.prototype._findClientName = function(clientConfig) {
  59. var clients = this._getClients();
  60. for (var name in clients) {
  61. var client = clients[name];
  62. if (client.clientId === clientConfig.clientId &&
  63. (client.loginUrl || 'https://login.salesforce.com') === clientConfig.loginUrl) {
  64. return name;
  65. }
  66. }
  67. return null;
  68. };
  69. Registry.prototype.setDefaultConnection = function(name) {
  70. this._registryConfig["default"] = name;
  71. this._saveConfig();
  72. };
  73. Registry.prototype.removeConnectionConfig = function(name) {
  74. var connections = this._getConnections();
  75. delete connections[name];
  76. this._saveConfig();
  77. };
  78. Registry.prototype.getClient = function(name) {
  79. var clientConfig = this._getClients()[name];
  80. return clientConfig && _.clone(clientConfig);
  81. };
  82. Registry.prototype.getClientNames = function() {
  83. return Object.keys(this._getClients());
  84. };
  85. Registry.prototype.registerClient = function(name, clientConfig) {
  86. var clients = this._getClients();
  87. clients[name] = clientConfig;
  88. this._saveConfig();
  89. };
  90. /* ------------------------------------------------------------------------- */
  91. module.exports = Registry;