1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const cloneDeep = require('lodash.clonedeep');
|
17 | const { ActiveItemTypes, Errors } = require('./Constants');
|
18 | const KinveyError = require('./KinveyError');
|
19 | const { isEmpty, isNullOrUndefined, readJSONSync, validateActiveItemType, writeJSON } = require('./Utils');
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | class Setup {
|
25 | constructor(path) {
|
26 | this._path = path;
|
27 | this._profiles = {};
|
28 | this._activeItemsFirstLevel = {};
|
29 | }
|
30 |
|
31 | load() {
|
32 | try {
|
33 | const globalSetup = readJSONSync(this._path);
|
34 | if (!isEmpty(globalSetup)) {
|
35 | this._setActiveItems(globalSetup.active);
|
36 | this._setProfiles(globalSetup.profiles);
|
37 | }
|
38 | } catch (ex) {
|
39 | return ex;
|
40 | }
|
41 | }
|
42 |
|
43 | save(done) {
|
44 | const data = {
|
45 | active: this._activeItemsFirstLevel,
|
46 | profiles: this._profiles
|
47 | };
|
48 |
|
49 | writeJSON({ file: this._path, data }, done);
|
50 | }
|
51 |
|
52 | getProfiles() {
|
53 | return cloneDeep(this._profiles);
|
54 | }
|
55 |
|
56 | addProfile(name, email, token, host) {
|
57 | this._profiles[name] = {
|
58 | email,
|
59 | token,
|
60 | host
|
61 | };
|
62 | }
|
63 |
|
64 | setProfileToken(name, token) {
|
65 | if (isNullOrUndefined(this.findProfileByName(name))) {
|
66 | throw new KinveyError(Errors.ProfileNotFound);
|
67 | }
|
68 |
|
69 | this._profiles[name].token = token;
|
70 | }
|
71 |
|
72 | deleteProfile(name) {
|
73 | const activeProfile = this.getActiveProfile();
|
74 | if (activeProfile && activeProfile.name === name) {
|
75 | delete this._activeItemsFirstLevel.profile;
|
76 | }
|
77 |
|
78 | delete this._profiles[name];
|
79 | }
|
80 |
|
81 | _setProfiles(profiles = {}) {
|
82 | this._profiles = profiles;
|
83 | }
|
84 |
|
85 | hasProfiles() {
|
86 | return !isEmpty(this._profiles);
|
87 | }
|
88 |
|
89 | findProfileByName(name) {
|
90 | if (!this.hasProfiles()) {
|
91 | return null;
|
92 | }
|
93 |
|
94 | const profile = this._profiles[name];
|
95 | if (isEmpty(profile)) {
|
96 | return null;
|
97 | }
|
98 |
|
99 | return Object.assign({ name }, profile);
|
100 | }
|
101 |
|
102 | _setActiveItems(activeItems = {}) {
|
103 | this._activeItemsFirstLevel = activeItems;
|
104 | }
|
105 |
|
106 | hasActiveProfile() {
|
107 | return !isEmpty(this._activeItemsFirstLevel) && this._activeItemsFirstLevel.profile;
|
108 | }
|
109 |
|
110 | getActiveProfile() {
|
111 | if (!this.hasActiveProfile()) {
|
112 | return null;
|
113 | }
|
114 |
|
115 | const activeProfileName = this._activeItemsFirstLevel.profile;
|
116 | const activeProfile = this.findProfileByName(activeProfileName);
|
117 | if (!isNullOrUndefined(activeProfile)) {
|
118 | activeProfile.name = activeProfileName;
|
119 | }
|
120 |
|
121 | return activeProfile;
|
122 | }
|
123 |
|
124 | setActiveProfile(profile) {
|
125 | this._activeItemsFirstLevel.profile = profile;
|
126 | }
|
127 |
|
128 | |
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | getActiveItemProfileLevel(itemType, profileName) {
|
136 | validateActiveItemType(itemType);
|
137 |
|
138 | const profile = this.findProfileByName(profileName);
|
139 | if (isNullOrUndefined(profile)) {
|
140 | return null;
|
141 | }
|
142 |
|
143 | if (!profile.active) {
|
144 | return null;
|
145 | }
|
146 |
|
147 | return profile.active[itemType];
|
148 | }
|
149 |
|
150 | setActiveItemProfileLevel(itemType, item, profileName) {
|
151 | const profile = this.findProfileByName(profileName);
|
152 | if (isNullOrUndefined(profile)) {
|
153 | throw new KinveyError(Errors.ProfileNotFound);
|
154 | }
|
155 |
|
156 | validateActiveItemType(itemType);
|
157 |
|
158 | if (!item || !Object.prototype.hasOwnProperty.call(item, 'id')) {
|
159 | throw new KinveyError('Item must have an id.');
|
160 | }
|
161 |
|
162 | if (isNullOrUndefined(this._profiles[profileName].active) || isEmpty(this._profiles[profileName].active)) {
|
163 | this._profiles[profileName].active = {};
|
164 | }
|
165 |
|
166 | this._profiles[profileName].active[itemType] = {
|
167 | id: item.id
|
168 | };
|
169 | }
|
170 | }
|
171 |
|
172 | module.exports = Setup;
|