@squiz/dxp-cli-next 5.4.0-develop.2 → 5.4.0-develop.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/auth/login.js +112 -3
- package/package.json +3 -1
package/lib/auth/login.js
CHANGED
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
26
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
27
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -40,7 +63,16 @@ const loginCommand = new commander_1.Command('login')
|
|
|
40
63
|
console.log(''); // needed to not screw up line endings in terminal
|
|
41
64
|
return;
|
|
42
65
|
}
|
|
43
|
-
|
|
66
|
+
yield handleLoginRequest(credentials, options);
|
|
67
|
+
const availableTenants = yield handleGetTenantsRequest(options);
|
|
68
|
+
if (options.tenant == undefined) {
|
|
69
|
+
const selectedTenant = yield promptForTenant(availableTenants);
|
|
70
|
+
options.tenant = selectedTenant;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
validateTenant(options.tenant, availableTenants);
|
|
74
|
+
}
|
|
75
|
+
return yield writeTenantToSessionConfig(options);
|
|
44
76
|
}
|
|
45
77
|
catch (error) {
|
|
46
78
|
if (!!process.env.DEBUG && error.stack) {
|
|
@@ -54,7 +86,6 @@ const loginCommand = new commander_1.Command('login')
|
|
|
54
86
|
}
|
|
55
87
|
}
|
|
56
88
|
}));
|
|
57
|
-
exports.default = loginCommand;
|
|
58
89
|
function getLoginCredentials(optionalUsername, optionalPassword) {
|
|
59
90
|
return __awaiter(this, void 0, void 0, function* () {
|
|
60
91
|
const prompts = [];
|
|
@@ -117,7 +148,6 @@ function handleLoginRequest(login, options) {
|
|
|
117
148
|
console.log('Login successful');
|
|
118
149
|
yield (0, ApplicationStore_1.saveApplicationFile)(ApplicationStore_1.STORE_FILES.sessionConfig, JSON.stringify({
|
|
119
150
|
baseUrl,
|
|
120
|
-
tenant: options.tenant,
|
|
121
151
|
region: options.region,
|
|
122
152
|
}));
|
|
123
153
|
return;
|
|
@@ -126,3 +156,82 @@ function handleLoginRequest(login, options) {
|
|
|
126
156
|
}
|
|
127
157
|
});
|
|
128
158
|
}
|
|
159
|
+
function handleGetTenantsRequest(options) {
|
|
160
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
161
|
+
const apiService = new ApiService_1.ApiService();
|
|
162
|
+
const baseUrl = options.dxpBaseUrl.replace(/\/$/, '');
|
|
163
|
+
const tenantsUrl = `${baseUrl}/__dxp/${options.region}/dxp/access/tenants`;
|
|
164
|
+
const tenantsResponse = yield apiService.client.get(tenantsUrl);
|
|
165
|
+
switch (tenantsResponse.status) {
|
|
166
|
+
case 200:
|
|
167
|
+
return tenantsResponse.data;
|
|
168
|
+
default:
|
|
169
|
+
throw new Error(`An error occurred fetching tenants ${tenantsResponse.data.message
|
|
170
|
+
? `"${tenantsResponse.data.message}"`
|
|
171
|
+
: ''} (code: ${tenantsResponse.status})`.trim());
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
function promptForTenant(tenants) {
|
|
176
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
177
|
+
//Our tsconfig module is commonjs so we need to use require here and only version ^8.0.0 of inquirer as latest is default esm
|
|
178
|
+
const inquirer = yield Promise.resolve().then(() => __importStar(require('inquirer')));
|
|
179
|
+
const tenantChoices = formatTenantResponseForInquirer(tenants);
|
|
180
|
+
const selectedTenant = yield inquirer.default.prompt([
|
|
181
|
+
{
|
|
182
|
+
type: 'list',
|
|
183
|
+
name: 'selectedTenantId',
|
|
184
|
+
message: 'Select a tenant: ',
|
|
185
|
+
choices: tenantChoices,
|
|
186
|
+
},
|
|
187
|
+
]);
|
|
188
|
+
//return the tenant ID
|
|
189
|
+
return selectedTenant.selectedTenantId;
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
function formatTenantResponseForInquirer(tenants) {
|
|
193
|
+
return tenants.organisations.map(tenant => {
|
|
194
|
+
return {
|
|
195
|
+
name: `${tenant.name} : (${tenant.id})`,
|
|
196
|
+
value: tenant.id,
|
|
197
|
+
};
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
function validateTenant(tenant, tenants) {
|
|
201
|
+
if (tenants.organisations.some(t => t.id === tenant)) {
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
let tenantListErrorString = undefined;
|
|
206
|
+
if (tenants.organisations.length > 5) {
|
|
207
|
+
const initialTenants = tenants.organisations.slice(0, 5).map(t => t.name);
|
|
208
|
+
tenantListErrorString = `${initialTenants} ... and ${tenants.organisations.length - 5} more}`;
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
tenantListErrorString = tenants.organisations.map(t => t.name).join(', ');
|
|
212
|
+
}
|
|
213
|
+
throw new Error(`
|
|
214
|
+
The tenant ID you provided : ${tenant} does not correspond to a tenant that you have access to.\n
|
|
215
|
+
Please ensure that you are using the correct ID and that you have the appropriate permissions to access this tenant.
|
|
216
|
+
Here is a list of the tenants you have access to: ${tenantListErrorString || 'None'}
|
|
217
|
+
`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
function writeTenantToSessionConfig(options) {
|
|
221
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
222
|
+
const baseUrl = options.dxpBaseUrl.replace(/\/$/, '');
|
|
223
|
+
try {
|
|
224
|
+
yield (0, ApplicationStore_1.saveApplicationFile)(ApplicationStore_1.STORE_FILES.sessionConfig, JSON.stringify({
|
|
225
|
+
baseUrl,
|
|
226
|
+
tenant: options.tenant,
|
|
227
|
+
region: options.region,
|
|
228
|
+
}));
|
|
229
|
+
console.log('Tenant saved to session');
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
throw new Error(`An error occurred during saving of tenant: ${error}`);
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
exports.default = loginCommand;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@squiz/dxp-cli-next",
|
|
3
|
-
"version": "5.4.0-develop.
|
|
3
|
+
"version": "5.4.0-develop.3",
|
|
4
4
|
"repository": {
|
|
5
5
|
"url": "https://gitlab.squiz.net/developer-experience/dxp-cli-next"
|
|
6
6
|
},
|
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
"cli-color": "2.0.3",
|
|
45
45
|
"commander": "9.4.0",
|
|
46
46
|
"env-paths": "2.2.1",
|
|
47
|
+
"inquirer": "8.2.5",
|
|
47
48
|
"prompt": "^1.3.0",
|
|
48
49
|
"tough-cookie": "4.1.2",
|
|
49
50
|
"update-notifier": "5.1.0"
|
|
@@ -53,6 +54,7 @@
|
|
|
53
54
|
"@semantic-release/gitlab": "9.4.2",
|
|
54
55
|
"@semantic-release/npm": "9.0.1",
|
|
55
56
|
"@types/cli-color": "2.0.2",
|
|
57
|
+
"@types/inquirer": "8.2.6",
|
|
56
58
|
"@types/jest": "28.1.6",
|
|
57
59
|
"@types/node": "17.0.45",
|
|
58
60
|
"@types/prompt": "^1.1.4",
|