contensis-cli 1.0.0-beta.12 → 1.0.0-beta.13

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.
@@ -208,7 +208,7 @@ class ContensisCli {
208
208
  session.UpdateEnv(this.env, environment);
209
209
 
210
210
  if (this.env?.lastUserId) {
211
- await this.ConnectContensis();
211
+ // await this.ConnectContensis();
212
212
  await this.PrintProjects();
213
213
  } else {
214
214
  log.warning(messages.projects.noList());
@@ -232,19 +232,11 @@ class ContensisCli {
232
232
  const isGuidId = userId && isUuid(userId);
233
233
 
234
234
  if (currentEnv && userId) {
235
- const [credentialError, credentials] = await new CredentialProvider(
236
- {
237
- userId,
238
- alias: currentEnv,
239
- },
235
+ const credentials = await this.GetCredentials(
236
+ userId,
240
237
  env.passwordFallback
241
- ).Init();
238
+ );
242
239
 
243
- if (credentialError && !credentials.current) {
244
- // Log problem with Credential Provider
245
- log.error(credentialError as any);
246
- return;
247
- }
248
240
  const cachedPassword = credentials?.current?.password;
249
241
 
250
242
  if (cachedPassword) {
@@ -308,36 +300,19 @@ class ContensisCli {
308
300
  const isTargetGuidId = targetUserId && isUuid(targetUserId);
309
301
 
310
302
  if (sourceUserId && currentEnv && targetUserId) {
311
- const [sourceCredentialError, sourceCredentials] =
312
- await new CredentialProvider(
313
- {
314
- userId: sourceUserId,
315
- alias: currentEnv,
316
- },
317
- sourcePassword
318
- ).Init();
303
+ const sourceCredentials = await this.GetCredentials(
304
+ sourceUserId,
305
+ sourcePassword,
306
+ sourceEnv
307
+ );
319
308
 
320
- if (sourceCredentialError && !sourceCredentials.current) {
321
- // Log problem with Credential Provider
322
- logError(sourceCredentialError);
323
- return;
324
- }
325
309
  const cachedSourcePassword = sourceCredentials?.current?.password;
326
310
 
327
- const [targetCredentialError, targetCredentials] =
328
- await new CredentialProvider(
329
- {
330
- userId: targetUserId,
331
- alias: currentEnv,
332
- },
333
- env.passwordFallback
334
- ).Init();
311
+ const targetCredentials = await this.GetCredentials(
312
+ targetUserId,
313
+ env.passwordFallback
314
+ );
335
315
 
336
- if (targetCredentialError && !targetCredentials.current) {
337
- // Log problem with Credential Provider
338
- log.error(targetCredentialError as any);
339
- return;
340
- }
341
316
  const cachedTargetPassword = targetCredentials?.current?.password;
342
317
 
343
318
  if (cachedSourcePassword && cachedTargetPassword) {
@@ -396,6 +371,34 @@ class ContensisCli {
396
371
  }
397
372
  };
398
373
 
374
+ GetCredentials = async (
375
+ userId: string,
376
+ password?: string,
377
+ currentEnv = this.currentEnv
378
+ ): Promise<CredentialProvider | undefined> => {
379
+ const { env, log, messages } = this;
380
+ if (userId) {
381
+ const [credentialError, credentials] = await new CredentialProvider(
382
+ { userId, alias: currentEnv },
383
+ password
384
+ ).Init();
385
+
386
+ if (credentialError && !credentials.current) {
387
+ // Log problem with Credential Provider
388
+ log.error(credentialError as any);
389
+ return;
390
+ }
391
+
392
+ if (credentials.remarks.secure !== true) {
393
+ log.warning(messages.login.insecurePassword());
394
+ } else {
395
+ env.passwordFallback = undefined;
396
+ this.session.UpdateEnv(env, currentEnv);
397
+ }
398
+ return credentials;
399
+ }
400
+ };
401
+
399
402
  Login = async (
400
403
  userId: string,
401
404
  {
@@ -403,115 +406,101 @@ class ContensisCli {
403
406
  promptPassword = true,
404
407
  sharedSecret = isSharedSecret(this.env.passwordFallback),
405
408
  silent = false,
409
+ attempt = 1,
406
410
  }: {
407
411
  password?: string;
408
412
  promptPassword?: boolean;
409
413
  sharedSecret?: string;
410
414
  silent?: boolean;
411
- }
415
+ attempt?: number;
416
+ } = {}
412
417
  ): Promise<string | undefined> => {
413
- let inputPassword = password;
418
+ let inputPassword = password || sharedSecret;
414
419
  const { log, messages } = this;
415
420
 
416
421
  if (userId) {
417
422
  const { currentEnv, env } = this;
418
423
 
419
424
  if (currentEnv) {
420
- const [credentialError, credentials] = await new CredentialProvider(
421
- { userId, alias: currentEnv },
422
- inputPassword || sharedSecret
423
- ).Init();
424
-
425
- if (credentialError && !credentials.current) {
426
- // Log problem with Credential Provider
427
- log.error(credentialError as any);
428
- return;
429
- }
430
-
431
- if (credentials.remarks.secure !== true) {
432
- log.warning(messages.login.insecurePassword());
433
- } else {
434
- delete env.passwordFallback;
435
- this.session.UpdateEnv(env);
436
- }
425
+ const credentials = await this.GetCredentials(userId, inputPassword);
426
+
427
+ if (credentials) {
428
+ const cachedPassword = isPassword(credentials.current?.password);
429
+ const cachedSecret = isSharedSecret(credentials.current?.password);
430
+
431
+ if (!cachedPassword && !cachedSecret && promptPassword) {
432
+ // Password prompt
433
+ ({ inputPassword } = await inquirer.prompt([
434
+ {
435
+ type: 'password',
436
+ message: messages.login.passwordPrompt(currentEnv, userId),
437
+ name: 'inputPassword',
438
+ mask: '*',
439
+ prefix: undefined,
440
+ },
441
+ ]));
442
+ }
437
443
 
438
- const cachedPassword = isPassword(credentials?.current?.password);
439
- const cachedSecret = isSharedSecret(credentials?.current?.password);
440
-
441
- if (
442
- !sharedSecret &&
443
- !inputPassword &&
444
- !cachedPassword &&
445
- !cachedSecret &&
446
- promptPassword
447
- ) {
448
- // Password prompt
449
- ({ inputPassword } = await inquirer.prompt([
450
- {
451
- type: 'password',
452
- message: messages.login.passwordPrompt(currentEnv, userId),
453
- name: 'inputPassword',
454
- mask: '*',
455
- prefix: undefined,
456
- },
457
- ]));
458
- }
444
+ if (inputPassword || cachedPassword || cachedSecret) {
445
+ const authService = new ContensisAuthService({
446
+ username: userId,
447
+ password: inputPassword || cachedPassword,
448
+ projectId: env?.currentProject || 'website',
449
+ rootUrl: this.urls?.cms || '',
450
+ clientId: userId,
451
+ clientSecret: sharedSecret || cachedSecret,
452
+ });
453
+
454
+ const [authError, bearerToken] = await to(
455
+ authService.BearerToken()
456
+ );
459
457
 
460
- if (sharedSecret || cachedSecret || inputPassword || cachedPassword) {
461
- const authService = new ContensisAuthService({
462
- username: userId,
463
- password: inputPassword || cachedPassword,
464
- projectId: env?.currentProject || 'website',
465
- rootUrl: this.urls?.cms || '',
466
- clientId: userId,
467
- clientSecret: sharedSecret || cachedSecret,
468
- });
469
-
470
- const [authError, bearerToken] = await to(authService.BearerToken());
471
-
472
- // Login successful
473
- if (bearerToken) {
474
- // Set env vars
475
- env.authToken = bearerToken;
476
- env.lastUserId = userId;
477
- env.passwordFallback =
478
- credentials.remarks.secure !== true
479
- ? credentials.current?.password
480
- : undefined;
481
- // Persist env before finding projects or doing anything else
482
- this.session.UpdateEnv(env);
483
-
484
- if (!silent) {
485
- Logger.success(messages.login.success(currentEnv, userId));
486
- await this.PrintProjects();
487
- }
488
- if (inputPassword) await credentials.Save(inputPassword);
489
- if (sharedSecret) await credentials.Save(sharedSecret);
490
- } else if (authError) {
491
- Logger.error(authError.toString());
492
- // Clear env vars
493
- env.authToken = '';
494
- env.lastUserId = '';
495
- env.passwordFallback = undefined;
496
- // Persist env to remove cleared values
497
- this.session.UpdateEnv(env);
498
-
499
- // If the auth error was raised using a cached password
500
- if (
501
- (cachedPassword || cachedSecret) &&
502
- credentials.remarks.secure
503
- ) {
504
- // Remove any bad stored credential and trigger login prompt again
505
- await credentials.Delete();
506
- return await this.Login(userId, { password, sharedSecret });
507
- } else {
508
- throw new Error(messages.login.failed(currentEnv, userId));
458
+ // Login successful
459
+ if (bearerToken) {
460
+ // Set env vars
461
+ env.authToken = bearerToken;
462
+ env.lastUserId = userId;
463
+ env.passwordFallback =
464
+ credentials.remarks.secure !== true
465
+ ? credentials.current?.password
466
+ : undefined;
467
+ // Persist env before finding projects or doing anything else
468
+ this.session.UpdateEnv(env);
469
+
470
+ if (!silent) {
471
+ Logger.success(messages.login.success(currentEnv, userId));
472
+ await this.PrintProjects();
473
+ }
474
+ if (inputPassword) await credentials.Save(inputPassword);
475
+ if (sharedSecret) await credentials.Save(sharedSecret);
476
+ } else if (authError) {
477
+ Logger.error(authError.toString());
478
+ // Clear env vars
479
+ env.authToken = '';
480
+ env.lastUserId = '';
481
+ env.passwordFallback = undefined;
482
+ // Persist env to remove cleared values
483
+ this.session.UpdateEnv(env);
484
+
485
+ // If the auth error was raised using a cached password
486
+ if (
487
+ (cachedPassword || cachedSecret) &&
488
+ credentials.remarks.secure
489
+ ) {
490
+ // Remove any bad stored credential and trigger login prompt again
491
+ await credentials.Delete();
492
+ return await this.Login(userId, { password, sharedSecret });
493
+ } else {
494
+ throw new Error(messages.login.failed(currentEnv, userId));
495
+ }
509
496
  }
510
- }
511
497
 
512
- return env.authToken;
513
- } else {
514
- Logger.error(messages.login.passwordPrompt(currentEnv, userId));
498
+ return env.authToken;
499
+ } else {
500
+ Logger.error(messages.login.passwordPrompt());
501
+ if (attempt < 2)
502
+ return await this.Login(userId, { attempt: attempt + 1 });
503
+ }
515
504
  }
516
505
  } else {
517
506
  // No environment set, use `contensis connect {alias}` first
package/src/shell.ts CHANGED
@@ -72,8 +72,13 @@ class ContensisShell {
72
72
  );
73
73
  }
74
74
 
75
+ restart = async () => {
76
+ this.firstStart = false;
77
+ this.log.line(); // add a line so we can see where the shell has been restarted
78
+ await this.start();
79
+ };
80
+
75
81
  start = async () => {
76
- this.log.line();
77
82
  this.refreshEnvironment();
78
83
  this.userId = '';
79
84
  const { currentEnvironment, env, log, messages } = this;
@@ -98,7 +103,10 @@ class ContensisShell {
98
103
  silent: true,
99
104
  }
100
105
  );
101
- if (token) this.userId = env.lastUserId;
106
+ if (token) {
107
+ this.userId = env.lastUserId;
108
+ if (!env.currentProject) log.warning(messages.projects.tip());
109
+ }
102
110
  this.firstStart = false;
103
111
  this.refreshEnvironment();
104
112
  } else {
@@ -221,7 +229,10 @@ class ContensisShell {
221
229
  let globalShell: ContensisShell;
222
230
 
223
231
  export const shell = () => {
224
- if (typeof process.argv?.[2] !== 'undefined') return { start() {} } as any;
232
+ // Return a benign function for shell().restart() when used in cli context
233
+ // as some commands need to restart the shell to show an updated prompt
234
+ // after successful connect / login / set project
235
+ if (typeof process.argv?.[2] !== 'undefined') return { restart() {} } as any;
225
236
  if (!globalShell) globalShell = new ContensisShell();
226
237
  return globalShell;
227
238
  };
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const LIB_VERSION = "1.0.0-beta.12";
1
+ export const LIB_VERSION = "1.0.0-beta.13";