@adobe/aio-cli-plugin-api-mesh 1.0.1-beta → 1.0.4-beta

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/src/helpers.js CHANGED
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-console */
1
2
  /*
2
3
  Copyright 2021 Adobe. All rights reserved.
3
4
  This file is licensed to you under the Apache License, Version 2.0 (the "License");
@@ -9,24 +10,21 @@ OF ANY KIND, either express or implied. See the License for the specific languag
9
10
  governing permissions and limitations under the License.
10
11
  */
11
12
 
13
+ const fs = require('fs');
14
+ const inquirer = require('inquirer');
15
+
12
16
  const Config = require('@adobe/aio-lib-core-config');
13
17
  const { getToken, context } = require('@adobe/aio-lib-ims');
14
18
  const { CLI } = require('@adobe/aio-lib-ims/src/context');
15
- const fs = require('fs');
16
19
  const libConsoleCLI = require('@adobe/aio-cli-lib-console');
17
- const { SchemaServiceClient } = require('./classes/SchemaServiceClient');
18
20
  const { getCliEnv } = require('@adobe/aio-lib-env');
21
+
19
22
  const logger = require('../src/classes/logger');
20
23
  const { UUID } = require('./classes/UUID');
21
- const aioConsoleLogger = require('@adobe/aio-lib-core-logging')(
22
- '@adobe/aio-cli-plugin-api-mesh',
23
- { provider: 'debug' },
24
- );
25
-
26
- const CONSOLE_API_KEYS = {
27
- prod: 'aio-cli-console-auth',
28
- stage: 'aio-cli-console-auth-stage',
29
- };
24
+ const CONSTANTS = require('./constants');
25
+ const { objToString } = require('./utils');
26
+
27
+ const { DEV_CONSOLE_BASE_URL, DEV_CONSOLE_API_KEY, AIO_CLI_API_KEY } = CONSTANTS;
30
28
 
31
29
  /**
32
30
  * @returns {any} Returns a config object or null
@@ -36,9 +34,9 @@ async function getDevConsoleConfig() {
36
34
 
37
35
  if (!configFile) {
38
36
  return {
39
- baseUrl: 'https://developers.adobe.io/console',
37
+ baseUrl: DEV_CONSOLE_BASE_URL,
40
38
  accessToken: (await getLibConsoleCLI()).accessToken,
41
- apiKey: 'adobe-graph-prod',
39
+ apiKey: DEV_CONSOLE_API_KEY,
42
40
  };
43
41
  } else {
44
42
  try {
@@ -79,27 +77,37 @@ async function getDevConsoleConfig() {
79
77
  * @returns {string} Returns organizations the user belongs to
80
78
  */
81
79
  async function getAuthorizedOrganization() {
80
+ logger.info(`Initializing organization selection for`);
81
+
82
82
  const { consoleCLI } = await getLibConsoleCLI();
83
83
 
84
- aioConsoleLogger.debug('Get the selected organization');
84
+ logger.debug('Get the selected organization');
85
85
 
86
86
  const consoleConfigOrg = Config.get('console.org');
87
87
 
88
88
  if (!consoleConfigOrg) {
89
89
  const organizations = await consoleCLI.getOrganizations();
90
- const selectedOrg = await consoleCLI.promptForSelectOrganization(organizations);
91
90
 
92
- aioConsoleLogger.debug('Set the console config');
91
+ logger.info(`Retrieved organizations : ${objToString(organizations)}`);
92
+
93
+ if (organizations.length !== 0) {
94
+ const selectedOrg = await consoleCLI.promptForSelectOrganization(organizations);
95
+
96
+ logger.debug('Set the console org config');
93
97
 
94
- Config.set('console.org', {
95
- id: selectedOrg.id,
96
- code: selectedOrg.code,
97
- name: selectedOrg.name,
98
- });
98
+ Config.set('console.org', selectedOrg);
99
99
 
100
- return Object.assign({}, selectedOrg);
100
+ // remove selected project and workspace from config and let the user select a new one
101
+ Config.delete('console.project');
102
+ Config.delete('console.workspace');
103
+
104
+ return Object.assign({}, selectedOrg);
105
+ } else {
106
+ logger.error(`No organizations found`);
107
+ }
101
108
  } else {
102
- logger.info(`Selecting your organization as: ${consoleConfigOrg.name}`);
109
+ logger.debug(`Selected organization config ${objToString(consoleConfigOrg)}`);
110
+ console.log(`Selected organization: ${consoleConfigOrg.name}`);
103
111
 
104
112
  return Object.assign({}, consoleConfigOrg);
105
113
  }
@@ -110,35 +118,141 @@ async function getProject(imsOrgId, imsOrgTitle) {
110
118
 
111
119
  const { consoleCLI } = await getLibConsoleCLI();
112
120
 
113
- const projects = await consoleCLI.getProjects(imsOrgId);
114
- if (projects.length !== 0) {
115
- const selectedProject = await consoleCLI.promptForSelectProject(projects);
121
+ logger.debug('Get the selected project');
122
+
123
+ const consoleConfigProject = Config.get('console.project');
124
+
125
+ if (!consoleConfigProject) {
126
+ const projects = await consoleCLI.getProjects(imsOrgId);
127
+
128
+ logger.debug(`Retrieved projects for ${imsOrgId} : ${objToString(projects)}`);
129
+
130
+ if (projects.length !== 0) {
131
+ const selectedProject = await consoleCLI.promptForSelectProject(projects);
132
+
133
+ const shouldCacheProject = await promptConfirm(
134
+ `Do you want to use ${selectedProject.title} as selected project for future operations?`,
135
+ );
116
136
 
117
- return selectedProject;
137
+ if (shouldCacheProject) {
138
+ Config.set('console.project', selectedProject);
139
+ }
140
+
141
+ // remove selected workspace from config and let the user select a new one
142
+ Config.delete('console.workspace');
143
+
144
+ return Object.assign({}, selectedProject);
145
+ } else {
146
+ logger.error(`No projects found for the selected organization: ${imsOrgTitle}`);
147
+ }
118
148
  } else {
119
- aioConsoleLogger.error(`No projects found for the selected organization: ${imsOrgTitle}`);
149
+ logger.debug(`Selected project config ${objToString(consoleConfigProject)}`);
150
+ console.log(`Selected project: ${consoleConfigProject.title}`);
151
+
152
+ return consoleConfigProject;
120
153
  }
121
154
  }
122
155
 
123
156
  async function getWorkspace(orgId, projectId, imsOrgTitle, projectTitle) {
124
- logger.info(`Initializing workspace selection for ${orgId} / ${projectId}`);
157
+ logger.info(`Initializing workspace selection for ${orgId} -> ${projectId}`);
158
+
159
+ const { consoleCLI } = await getLibConsoleCLI();
160
+
161
+ logger.debug('Get the selected workspace');
162
+
163
+ const consoleConfigWorkspace = Config.get('console.workspace');
164
+
165
+ if (!consoleConfigWorkspace) {
166
+ const workspaces = await consoleCLI.getWorkspaces(orgId, projectId);
167
+
168
+ logger.debug(`Retrieved workspaces for ${orgId} -> ${projectId} : ${objToString(workspaces)}`);
169
+
170
+ if (workspaces.length !== 0) {
171
+ const selectedWorkspace = await consoleCLI.promptForSelectWorkspace(workspaces);
172
+
173
+ const shouldCacheWorkspace = await promptConfirm(
174
+ `Do you want to use ${selectedWorkspace.name} as selected workspace for future operations?`,
175
+ );
176
+
177
+ if (shouldCacheWorkspace) {
178
+ Config.set('console.workspace', selectedWorkspace);
179
+ }
180
+
181
+ return Object.assign({}, selectedWorkspace);
182
+ } else {
183
+ logger.error(
184
+ `No workspaces found for the selected organization: ${imsOrgTitle} and project: ${projectTitle}`,
185
+ );
186
+ }
187
+ } else {
188
+ logger.debug(`Selected workspace config ${objToString(consoleConfigWorkspace)}`);
189
+ console.log(`Select workspace: ${consoleConfigWorkspace.name}`);
190
+
191
+ return {
192
+ id: consoleConfigWorkspace.id,
193
+ title: consoleConfigWorkspace.name,
194
+ };
195
+ }
196
+ }
197
+
198
+ const selectAuthorizedOrganization = async () => {
199
+ const { consoleCLI } = await getLibConsoleCLI();
200
+ const organizations = await consoleCLI.getOrganizations();
201
+
202
+ if (organizations.length > 0) {
203
+ const selectedOrg = await consoleCLI.promptForSelectOrganization(organizations);
204
+
205
+ if (selectedOrg) {
206
+ return selectedOrg;
207
+ } else {
208
+ throw new Error('No org selected');
209
+ }
210
+ } else {
211
+ this.error('No organizations found');
212
+ }
213
+ };
125
214
 
215
+ const selectProject = async (imsOrgId, imsOrgTitle) => {
126
216
  const { consoleCLI } = await getLibConsoleCLI();
217
+ const projects = await consoleCLI.getProjects(imsOrgId);
127
218
 
219
+ if (projects.length > 0) {
220
+ const selectedProject = await consoleCLI.promptForSelectProject(projects);
221
+
222
+ if (selectedProject) {
223
+ return selectedProject;
224
+ } else {
225
+ throw new Error('No project selected');
226
+ }
227
+ } else {
228
+ this.error('No projects found for the selected organization: ' + imsOrgTitle);
229
+ }
230
+ };
231
+
232
+ const selectWorkspace = async (orgId, projectId, imsOrgTitle, projectTitle) => {
233
+ const { consoleCLI } = await getLibConsoleCLI();
128
234
  const workspaces = await consoleCLI.getWorkspaces(orgId, projectId);
129
- if (workspaces.length !== 0) {
235
+
236
+ if (workspaces.length > 0) {
130
237
  const selectedWorkspace = await consoleCLI.promptForSelectWorkspace(workspaces);
131
238
 
132
- return selectedWorkspace;
239
+ if (selectedWorkspace) {
240
+ return selectedWorkspace;
241
+ } else {
242
+ throw new Error('No workspace selected');
243
+ }
133
244
  } else {
134
- aioConsoleLogger.error(
135
- `No workspaces found for the selected organization: ${imsOrgTitle} and project: ${projectTitle}`,
245
+ this.error(
246
+ 'No workspaces found for the selected organization: ' +
247
+ imsOrgTitle +
248
+ ' and project: ' +
249
+ projectTitle,
136
250
  );
137
251
  }
138
- }
252
+ };
139
253
 
140
254
  /**
141
- * @private
255
+ * @returns {consoleCLI, accessToken}
142
256
  */
143
257
  async function getLibConsoleCLI() {
144
258
  await context.setCli({ 'cli.bare-output': true }, false);
@@ -149,7 +263,7 @@ async function getLibConsoleCLI() {
149
263
 
150
264
  const consoleCLI = await libConsoleCLI.init({
151
265
  accessToken: accessToken,
152
- apiKey: CONSOLE_API_KEYS[clientEnv],
266
+ apiKey: AIO_CLI_API_KEY,
153
267
  env: clientEnv,
154
268
  });
155
269
 
@@ -159,24 +273,30 @@ async function getLibConsoleCLI() {
159
273
  /**
160
274
  * @returns {any} Returns an object with properties ready for consumption
161
275
  */
162
- async function initSdk() {
163
- const org = await getAuthorizedOrganization();
164
- const project = await getProject(org.id, org.name);
165
- const workspace = await getWorkspace(org.id, project.id, org.name, project.title);
276
+ async function initSdk(options) {
277
+ const { ignoreCache = false } = options;
278
+
279
+ let org;
280
+ let project;
281
+ let workspace;
166
282
 
167
- aioConsoleLogger.log(
283
+ if (!ignoreCache) {
284
+ org = await getAuthorizedOrganization();
285
+ project = await getProject(org.id, org.name);
286
+ workspace = await getWorkspace(org.id, project.id, org.name, project.title);
287
+ } else {
288
+ org = await selectAuthorizedOrganization();
289
+ project = await selectProject(org.id, org.name);
290
+ workspace = await selectWorkspace(org.id, project.id, org.name, project.title);
291
+ }
292
+
293
+ logger.info(
168
294
  `Initializing SDK for org: ${org.name}, project: ${project.title} and workspace: ${workspace.title}`,
169
295
  );
170
296
 
171
297
  logger.info('Initialized user login and the selected organization');
172
298
 
173
- const { baseUrl, accessToken, apiKey } = await getDevConsoleConfig();
174
-
175
- const schemaServiceClient = new SchemaServiceClient();
176
- schemaServiceClient.init(baseUrl, accessToken, apiKey);
177
-
178
299
  return {
179
- schemaServiceClient: schemaServiceClient,
180
300
  imsOrgId: org.id,
181
301
  projectId: project.id,
182
302
  workspaceId: workspace.id,
@@ -190,7 +310,30 @@ async function initRequestId() {
190
310
  global.requestId = UUID.newUuid().toString();
191
311
  }
192
312
 
313
+ /**
314
+ * Function to run the CLI Y/N prompt to confirm the user's action
315
+ *
316
+ * @param {string} message
317
+ *
318
+ * @returns boolean
319
+ */
320
+ async function promptConfirm(message) {
321
+ const prompt = inquirer.createPromptModule({ output: process.stderr });
322
+
323
+ const confirm = await prompt([
324
+ {
325
+ type: 'confirm',
326
+ name: 'res',
327
+ message,
328
+ },
329
+ ]);
330
+
331
+ return confirm.res;
332
+ }
333
+
193
334
  module.exports = {
335
+ promptConfirm,
336
+ getLibConsoleCLI,
194
337
  getDevConsoleConfig,
195
338
  initSdk,
196
339
  initRequestId,