contensis-cli 1.0.0-beta.11 → 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.
@@ -54,26 +54,48 @@ Exiting contensis-cli with exit code: ${exitCode}
54
54
  `);
55
55
  process.exit(exitCode);
56
56
  };
57
- cache;
57
+ command;
58
+ format;
59
+ output;
60
+ session;
58
61
  contensis;
59
62
  contensisOpts;
60
63
  contentTypes;
61
64
  components;
62
- currentEnv;
63
65
  currentProject;
64
- env;
65
66
  sourceEnv;
66
67
  targetEnv;
67
68
  urls;
68
- command;
69
- format;
70
- output;
71
69
  log = import_logger.Logger;
72
70
  messages = import_en_GB.LogMessages;
73
- session;
74
71
  verb;
75
72
  noun;
76
73
  thirdArg;
74
+ get cache() {
75
+ return this.session.Get();
76
+ }
77
+ get currentEnv() {
78
+ return this.cache.currentEnvironment || "";
79
+ }
80
+ set currentEnv(currentEnvironment) {
81
+ this.session.Update({ currentEnvironment });
82
+ }
83
+ get env() {
84
+ const currentEnvironment = this.currentEnv;
85
+ const environments = this.cache.environments || {};
86
+ if (!currentEnvironment)
87
+ return {};
88
+ else if (!!environments[currentEnvironment])
89
+ return environments[currentEnvironment];
90
+ else {
91
+ return {
92
+ history: [],
93
+ lastUserId: "",
94
+ projects: [],
95
+ versionStatus: "latest"
96
+ };
97
+ }
98
+ }
77
99
  constructor(args, outputOpts, contensisOpts = {}) {
78
100
  const [exe, script, verb = "", noun = "", ...restArgs] = args;
79
101
  this.verb = verb == null ? void 0 : verb.toLowerCase();
@@ -81,24 +103,12 @@ Exiting contensis-cli with exit code: ${exitCode}
81
103
  this.thirdArg = restArgs == null ? void 0 : restArgs[0];
82
104
  const commandText = `${this.verb} ${this.noun} ${restArgs ? restArgs.join(" ") : ""}`.trim();
83
105
  this.session = new import_SessionCacheProvider.default();
84
- this.cache = this.session.Get();
85
106
  this.contensisOpts = contensisOpts;
86
107
  this.format = outputOpts == null ? void 0 : outputOpts.format;
87
108
  this.output = (outputOpts == null ? void 0 : outputOpts.output) && import_path.default.join(process.cwd(), outputOpts.output);
88
- const currentEnvironment = (outputOpts == null ? void 0 : outputOpts.alias) || this.cache.currentEnvironment || "";
109
+ const currentEnvironment = (outputOpts == null ? void 0 : outputOpts.alias) || this.currentEnv;
89
110
  const environments = this.cache.environments || {};
90
- if (!currentEnvironment)
91
- this.env = {};
92
- else if (!!environments[currentEnvironment])
93
- this.env = environments[currentEnvironment];
94
- else {
95
- this.env = {
96
- history: [],
97
- lastUserId: "",
98
- projects: [],
99
- versionStatus: "latest"
100
- };
101
- }
111
+ this.currentEnv = currentEnvironment;
102
112
  const env = this.env;
103
113
  if (outputOpts == null ? void 0 : outputOpts.projectId)
104
114
  env.currentProject = outputOpts.projectId;
@@ -110,7 +120,6 @@ Exiting contensis-cli with exit code: ${exitCode}
110
120
  env.lastUserId = outputOpts.clientId;
111
121
  if (outputOpts == null ? void 0 : outputOpts.sharedSecret)
112
122
  env.passwordFallback = outputOpts.sharedSecret;
113
- this.currentEnv = currentEnvironment;
114
123
  this.currentProject = (env == null ? void 0 : env.currentProject) || "null";
115
124
  this.sourceEnv = (outputOpts == null ? void 0 : outputOpts.sourceEnv) || currentEnvironment;
116
125
  if (currentEnvironment) {
@@ -124,7 +133,7 @@ Exiting contensis-cli with exit code: ${exitCode}
124
133
  if (currentEnvironment) {
125
134
  env.history = [this.command];
126
135
  if (commandText) {
127
- environments[currentEnvironment] = this.env;
136
+ environments[currentEnvironment] = env;
128
137
  this.session.Update({
129
138
  currentEnvironment,
130
139
  environments,
@@ -149,34 +158,20 @@ Exiting contensis-cli with exit code: ${exitCode}
149
158
  };
150
159
  Connect = async (environment) => {
151
160
  var _a;
152
- const { cache, log, messages, session } = this;
161
+ const { log, messages, session } = this;
153
162
  if (environment) {
154
- const envCache = cache.environments[environment];
155
- if (!envCache)
156
- cache.environments[environment] = {
157
- versionStatus: "published",
158
- history: [],
159
- lastUserId: "",
160
- projects: [],
161
- ...!this.currentEnv ? this.env : {}
162
- };
163
- this.env = cache.environments[environment];
164
163
  this.currentEnv = environment;
165
164
  this.urls = (0, import_util.url)(environment, "website");
166
165
  const [fetchErr, response] = await (0, import_await_to_js.default)((0, import_node_fetch.default)(this.urls.cms));
167
166
  if (response && (response == null ? void 0 : response.status) < 400) {
168
167
  log.success(messages.connect.connected(environment));
168
+ session.UpdateEnv(this.env, environment);
169
169
  if ((_a = this.env) == null ? void 0 : _a.lastUserId) {
170
- await this.ConnectContensis();
171
170
  await this.PrintProjects();
172
171
  } else {
173
172
  log.warning(messages.projects.noList());
174
173
  log.help(messages.connect.tip());
175
174
  }
176
- session.Update({
177
- currentEnvironment: environment,
178
- environments: cache.environments
179
- });
180
175
  } else {
181
176
  log.error(
182
177
  messages.connect.unreachable(this.urls.cms, (response == null ? void 0 : response.status) || 0)
@@ -192,17 +187,10 @@ Exiting contensis-cli with exit code: ${exitCode}
192
187
  const userId = env == null ? void 0 : env.lastUserId;
193
188
  const isGuidId = userId && (0, import_util.isUuid)(userId);
194
189
  if (currentEnv && userId) {
195
- const [credentialError, credentials] = await new import_CredentialProvider.default(
196
- {
197
- userId,
198
- alias: currentEnv
199
- },
190
+ const credentials = await this.GetCredentials(
191
+ userId,
200
192
  env.passwordFallback
201
- ).Init();
202
- if (credentialError && !credentials.current) {
203
- log.error(credentialError);
204
- return;
205
- }
193
+ );
206
194
  const cachedPassword = (_a = credentials == null ? void 0 : credentials.current) == null ? void 0 : _a.password;
207
195
  if (cachedPassword) {
208
196
  this.contensis = new import_migratortron.ContensisMigrationService(
@@ -249,29 +237,16 @@ Exiting contensis-cli with exit code: ${exitCode}
249
237
  const targetUserId = env == null ? void 0 : env.lastUserId;
250
238
  const isTargetGuidId = targetUserId && (0, import_util.isUuid)(targetUserId);
251
239
  if (sourceUserId && currentEnv && targetUserId) {
252
- const [sourceCredentialError, sourceCredentials] = await new import_CredentialProvider.default(
253
- {
254
- userId: sourceUserId,
255
- alias: currentEnv
256
- },
257
- sourcePassword
258
- ).Init();
259
- if (sourceCredentialError && !sourceCredentials.current) {
260
- (0, import_logger.logError)(sourceCredentialError);
261
- return;
262
- }
240
+ const sourceCredentials = await this.GetCredentials(
241
+ sourceUserId,
242
+ sourcePassword,
243
+ sourceEnv
244
+ );
263
245
  const cachedSourcePassword = (_a = sourceCredentials == null ? void 0 : sourceCredentials.current) == null ? void 0 : _a.password;
264
- const [targetCredentialError, targetCredentials] = await new import_CredentialProvider.default(
265
- {
266
- userId: targetUserId,
267
- alias: currentEnv
268
- },
246
+ const targetCredentials = await this.GetCredentials(
247
+ targetUserId,
269
248
  env.passwordFallback
270
- ).Init();
271
- if (targetCredentialError && !targetCredentials.current) {
272
- log.error(targetCredentialError);
273
- return;
274
- }
249
+ );
275
250
  const cachedTargetPassword = (_b = targetCredentials == null ? void 0 : targetCredentials.current) == null ? void 0 : _b.password;
276
251
  if (cachedSourcePassword && cachedTargetPassword) {
277
252
  if (source === "file" || source === "input") {
@@ -330,81 +305,98 @@ Exiting contensis-cli with exit code: ${exitCode}
330
305
  log.help(messages.connect.tip());
331
306
  }
332
307
  };
308
+ GetCredentials = async (userId, password, currentEnv = this.currentEnv) => {
309
+ const { env, log, messages } = this;
310
+ if (userId) {
311
+ const [credentialError, credentials] = await new import_CredentialProvider.default(
312
+ { userId, alias: currentEnv },
313
+ password
314
+ ).Init();
315
+ if (credentialError && !credentials.current) {
316
+ log.error(credentialError);
317
+ return;
318
+ }
319
+ if (credentials.remarks.secure !== true) {
320
+ log.warning(messages.login.insecurePassword());
321
+ } else {
322
+ env.passwordFallback = void 0;
323
+ this.session.UpdateEnv(env, currentEnv);
324
+ }
325
+ return credentials;
326
+ }
327
+ };
333
328
  Login = async (userId, {
334
329
  password = (0, import_util.isPassword)(this.env.passwordFallback),
335
330
  promptPassword = true,
336
331
  sharedSecret = (0, import_util.isSharedSecret)(this.env.passwordFallback),
337
- silent = false
338
- }) => {
332
+ silent = false,
333
+ attempt = 1
334
+ } = {}) => {
339
335
  var _a, _b, _c, _d;
340
- let inputPassword = password;
336
+ let inputPassword = password || sharedSecret;
341
337
  const { log, messages } = this;
342
338
  if (userId) {
343
- const { cache, currentEnv, env } = this;
339
+ const { currentEnv, env } = this;
344
340
  if (currentEnv) {
345
- const [credentialError, credentials] = await new import_CredentialProvider.default(
346
- { userId, alias: currentEnv },
347
- inputPassword || sharedSecret
348
- ).Init();
349
- if (credentialError && !credentials.current) {
350
- log.error(credentialError);
351
- return;
352
- }
353
- if (credentials.remarks.secure !== true)
354
- log.warning(messages.login.insecurePassword());
355
- const cachedPassword = (0, import_util.isPassword)((_a = credentials == null ? void 0 : credentials.current) == null ? void 0 : _a.password);
356
- const cachedSecret = (0, import_util.isSharedSecret)((_b = credentials == null ? void 0 : credentials.current) == null ? void 0 : _b.password);
357
- if (!sharedSecret && !inputPassword && !cachedPassword && !cachedSecret && promptPassword) {
358
- ({ inputPassword } = await import_inquirer.default.prompt([
359
- {
360
- type: "password",
361
- message: messages.login.passwordPrompt(currentEnv, userId),
362
- name: "inputPassword",
363
- mask: "*",
364
- prefix: void 0
365
- }
366
- ]));
367
- }
368
- if (sharedSecret || cachedSecret || inputPassword || cachedPassword) {
369
- const authService = new import_ContensisAuthService.default({
370
- username: userId,
371
- password: inputPassword || cachedPassword,
372
- projectId: (env == null ? void 0 : env.currentProject) || "website",
373
- rootUrl: ((_c = this.urls) == null ? void 0 : _c.cms) || "",
374
- clientId: userId,
375
- clientSecret: sharedSecret || cachedSecret
376
- });
377
- const [authError, bearerToken] = await (0, import_await_to_js.default)(authService.BearerToken());
378
- if (bearerToken) {
379
- env.authToken = bearerToken;
380
- env.lastUserId = userId;
381
- env.passwordFallback = credentials.remarks.secure !== true ? (_d = credentials.current) == null ? void 0 : _d.password : void 0;
382
- if (!silent) {
383
- import_logger.Logger.success(messages.login.success(currentEnv, userId));
384
- await this.PrintProjects();
385
- }
386
- if (inputPassword)
387
- await credentials.Save(inputPassword);
388
- if (sharedSecret)
389
- await credentials.Save(sharedSecret);
390
- } else if (authError) {
391
- import_logger.Logger.error(authError.toString());
392
- env.authToken = "";
393
- env.lastUserId = "";
394
- env.passwordFallback = void 0;
395
- if ((cachedPassword || cachedSecret) && credentials.remarks.secure) {
396
- await credentials.Delete();
397
- return await this.Login(userId, { password, sharedSecret });
398
- } else {
399
- throw new Error(messages.login.failed(currentEnv, userId));
341
+ const credentials = await this.GetCredentials(userId, inputPassword);
342
+ if (credentials) {
343
+ const cachedPassword = (0, import_util.isPassword)((_a = credentials.current) == null ? void 0 : _a.password);
344
+ const cachedSecret = (0, import_util.isSharedSecret)((_b = credentials.current) == null ? void 0 : _b.password);
345
+ if (!cachedPassword && !cachedSecret && promptPassword) {
346
+ ({ inputPassword } = await import_inquirer.default.prompt([
347
+ {
348
+ type: "password",
349
+ message: messages.login.passwordPrompt(currentEnv, userId),
350
+ name: "inputPassword",
351
+ mask: "*",
352
+ prefix: void 0
353
+ }
354
+ ]));
355
+ }
356
+ if (inputPassword || cachedPassword || cachedSecret) {
357
+ const authService = new import_ContensisAuthService.default({
358
+ username: userId,
359
+ password: inputPassword || cachedPassword,
360
+ projectId: (env == null ? void 0 : env.currentProject) || "website",
361
+ rootUrl: ((_c = this.urls) == null ? void 0 : _c.cms) || "",
362
+ clientId: userId,
363
+ clientSecret: sharedSecret || cachedSecret
364
+ });
365
+ const [authError, bearerToken] = await (0, import_await_to_js.default)(
366
+ authService.BearerToken()
367
+ );
368
+ if (bearerToken) {
369
+ env.authToken = bearerToken;
370
+ env.lastUserId = userId;
371
+ env.passwordFallback = credentials.remarks.secure !== true ? (_d = credentials.current) == null ? void 0 : _d.password : void 0;
372
+ this.session.UpdateEnv(env);
373
+ if (!silent) {
374
+ import_logger.Logger.success(messages.login.success(currentEnv, userId));
375
+ await this.PrintProjects();
376
+ }
377
+ if (inputPassword)
378
+ await credentials.Save(inputPassword);
379
+ if (sharedSecret)
380
+ await credentials.Save(sharedSecret);
381
+ } else if (authError) {
382
+ import_logger.Logger.error(authError.toString());
383
+ env.authToken = "";
384
+ env.lastUserId = "";
385
+ env.passwordFallback = void 0;
386
+ this.session.UpdateEnv(env);
387
+ if ((cachedPassword || cachedSecret) && credentials.remarks.secure) {
388
+ await credentials.Delete();
389
+ return await this.Login(userId, { password, sharedSecret });
390
+ } else {
391
+ throw new Error(messages.login.failed(currentEnv, userId));
392
+ }
400
393
  }
394
+ return env.authToken;
395
+ } else {
396
+ import_logger.Logger.error(messages.login.passwordPrompt());
397
+ if (attempt < 2)
398
+ return await this.Login(userId, { attempt: attempt + 1 });
401
399
  }
402
- this.session.Update({
403
- environments: cache.environments
404
- });
405
- return env.authToken;
406
- } else {
407
- import_logger.Logger.error(messages.login.passwordPrompt(currentEnv, userId));
408
400
  }
409
401
  } else {
410
402
  import_logger.Logger.error(messages.login.noEnv());
@@ -414,7 +406,7 @@ Exiting contensis-cli with exit code: ${exitCode}
414
406
  }
415
407
  };
416
408
  PrintProjects = async () => {
417
- const { cache, currentEnv, currentProject, log, messages, session } = this;
409
+ const { currentProject, log, messages, session } = this;
418
410
  if (!this.contensis)
419
411
  await this.ConnectContensis();
420
412
  if (this.contensis) {
@@ -422,13 +414,11 @@ Exiting contensis-cli with exit code: ${exitCode}
422
414
  this.contensis.projects.GetSourceProjects()
423
415
  );
424
416
  if (Array.isArray(projects)) {
425
- const currentVals = cache.environments[currentEnv] || {};
426
417
  const nextCurrentProject = currentProject && currentProject !== "null" ? currentProject : projects.some((p) => p.id === "website") ? "website" : void 0;
427
- cache.environments[currentEnv] = {
428
- ...currentVals,
418
+ session.UpdateEnv({
429
419
  projects: projects.map((p) => p.id),
430
420
  currentProject: nextCurrentProject
431
- };
421
+ });
432
422
  log.success(messages.projects.list());
433
423
  this.HandleFormattingAndOutput(projects, () => {
434
424
  for (const project of projects) {
@@ -437,13 +427,8 @@ Exiting contensis-cli with exit code: ${exitCode}
437
427
  );
438
428
  }
439
429
  });
440
- session.Update({
441
- environments: cache.environments
442
- });
443
- if (nextCurrentProject) {
444
- this.env = cache.environments[currentEnv];
445
- this.SetProject(nextCurrentProject);
446
- }
430
+ if (!this.SetProject(nextCurrentProject))
431
+ log.warning(messages.projects.tip());
447
432
  }
448
433
  if (projectsErr) {
449
434
  log.error(messages.projects.noList());
@@ -451,8 +436,8 @@ Exiting contensis-cli with exit code: ${exitCode}
451
436
  }
452
437
  }
453
438
  };
454
- SetProject = async (projectId = "") => {
455
- const { cache, env, log, messages, session } = this;
439
+ SetProject = (projectId = "website") => {
440
+ const { env, log, messages, session } = this;
456
441
  let nextProjectId;
457
442
  if ((env == null ? void 0 : env.projects.length) > 0 && (env == null ? void 0 : env.lastUserId)) {
458
443
  nextProjectId = env.projects.find(
@@ -460,9 +445,7 @@ Exiting contensis-cli with exit code: ${exitCode}
460
445
  );
461
446
  if (nextProjectId) {
462
447
  env.currentProject = nextProjectId;
463
- session.Update({
464
- environments: cache.environments
465
- });
448
+ session.UpdateEnv(env);
466
449
  log.success(messages.projects.set(projectId));
467
450
  } else {
468
451
  log.error(messages.projects.failedSet(projectId));
@@ -473,8 +456,8 @@ Exiting contensis-cli with exit code: ${exitCode}
473
456
  }
474
457
  return nextProjectId;
475
458
  };
476
- SetVersion = async (versionStatus) => {
477
- const { cache, env, log, messages, session } = this;
459
+ SetVersion = (versionStatus) => {
460
+ const { env, log, messages, session } = this;
478
461
  if (!["latest", "published"].includes(versionStatus)) {
479
462
  log.error(messages.version.invalid(versionStatus));
480
463
  return false;
@@ -484,10 +467,7 @@ Exiting contensis-cli with exit code: ${exitCode}
484
467
  return false;
485
468
  }
486
469
  if ((env == null ? void 0 : env.projects.length) > 0 && (env == null ? void 0 : env.lastUserId)) {
487
- env.versionStatus = versionStatus;
488
- session.Update({
489
- environments: cache.environments
490
- });
470
+ session.UpdateEnv({ versionStatus });
491
471
  log.success(messages.version.set(this.currentEnv, versionStatus));
492
472
  return true;
493
473
  } else {