UNPKG

2.89 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 { isEmpty, isNullOrUndefined, readJSONSync, writeJSON } = require('./Utils');
17
18/**
19 * Keeps information about a project's configuration (e.g. service id).
20 */
21class ProjectSetup {
22 constructor(projectPath) {
23 this._path = projectPath;
24 this._setup = {};
25 }
26
27 save(done) {
28 const data = this._setup;
29 writeJSON({ file: this._path, data }, done);
30 }
31
32 load() {
33 try {
34 const projectSetup = readJSONSync(this._path);
35 if (!isEmpty(projectSetup)) {
36 this._setup = projectSetup;
37 }
38 } catch (err) {
39 return err;
40 }
41 }
42
43 getFlexNamespace(key) {
44 if (isNullOrUndefined(this._setup[key])) {
45 return {};
46 }
47
48 return Object.assign({}, this._setup[key].flex);
49 }
50
51 setFlexNamespace(key, { domain, domainEntityId, serviceId, serviceName, svcEnvId, schemaVersion }) {
52 this._setup[key] = {
53 flex: {
54 domain,
55 domainEntityId,
56 serviceId,
57 serviceName,
58 svcEnvId,
59 schemaVersion
60 }
61 };
62 }
63
64 clear() {
65 this._setup = '';
66 }
67
68 setJobId(key, id) {
69 if (isNullOrUndefined(this._setup[key]) || isNullOrUndefined(this._setup[key].flex)) {
70 this._setup[key] = { flex: {} };
71 }
72
73 this._setup[key].flex.jobId = id;
74 }
75
76 _isPropertySetInFlexNamespace(firstLevelKey, propName) {
77 return this._setup && !isEmpty(this._setup[firstLevelKey]) && !isEmpty(this._setup[firstLevelKey].flex)
78 && !isNullOrUndefined(this._setup[firstLevelKey].flex[propName]);
79 }
80
81 _clearPropertyInFlexNamespace(firstLevelKey, propName) {
82 delete this._setup[firstLevelKey].flex[propName];
83 }
84
85 isServiceSetInFlexNamespace(firstLevelKey, serviceId) {
86 return this._isPropertySetInFlexNamespace(firstLevelKey, 'serviceId') && this.getFlexNamespace(firstLevelKey).serviceId === serviceId;
87 }
88
89 clearServiceInFlexNamespace(firstLevelKey) {
90 const keysToRemove = ['serviceId', 'serviceName'];
91 keysToRemove.forEach(k => this._clearPropertyInFlexNamespace(firstLevelKey, k));
92 }
93}
94
95module.exports = ProjectSetup;