UNPKG

4.68 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2018, Kinvey, Inc. All rights reserved.
3 *
4 * This software is licensed to you under the Kinvey terms of service located at
5 * http://www.kinvey.com/terms-of-use. By downloading, accessing and/or using this
6 * software, you hereby accept such terms of service (and any agreement referenced
7 * therein) and agree that you have read, understand and agree to be bound by such
8 * terms of service and are of legal age to agree to such terms with Kinvey.
9 *
10 * This software contains valuable confidential and proprietary information of
11 * KINVEY, INC and is subject to applicable licensing agreements.
12 * Unauthorized reproduction, transmission or distribution of this file and its
13 * contents is a violation of applicable laws.
14 */
15
16const cloneDeep = require('lodash.clonedeep');
17const { ActiveItemTypes, Errors } = require('./Constants');
18const KinveyError = require('./KinveyError');
19const { isEmpty, isNullOrUndefined, readJSONSync, validateActiveItemType, writeJSON } = require('./Utils');
20
21/**
22 * Keeps information about CLI configuration (e.g. saved profiles).
23 */
24class 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 * Gets specific active item at the profile name. If profile cannot be found or it does not contain any active items,
130 * returns null. Throws if itemType is not supported.
131 * @param {Constants.ActiveItemType} itemType
132 * @param {String} profileName
133 * @returns {*}
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
172module.exports = Setup;