contensis-cli 1.0.0-beta.6 → 1.0.0-beta.60

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.
Files changed (71) hide show
  1. package/README.md +760 -75
  2. package/dist/commands/connect.js +3 -3
  3. package/dist/commands/connect.js.map +2 -2
  4. package/dist/commands/create.js +30 -10
  5. package/dist/commands/create.js.map +2 -2
  6. package/dist/commands/diff.js +57 -0
  7. package/dist/commands/diff.js.map +7 -0
  8. package/dist/commands/get.js +61 -12
  9. package/dist/commands/get.js.map +2 -2
  10. package/dist/commands/globalOptions.js +22 -17
  11. package/dist/commands/globalOptions.js.map +2 -2
  12. package/dist/commands/import.js +46 -11
  13. package/dist/commands/import.js.map +2 -2
  14. package/dist/commands/index.js +9 -1
  15. package/dist/commands/index.js.map +2 -2
  16. package/dist/commands/list.js +19 -8
  17. package/dist/commands/list.js.map +2 -2
  18. package/dist/commands/login.js +3 -3
  19. package/dist/commands/login.js.map +2 -2
  20. package/dist/commands/push.js +8 -4
  21. package/dist/commands/push.js.map +2 -2
  22. package/dist/commands/release.js +47 -0
  23. package/dist/commands/release.js.map +7 -0
  24. package/dist/commands/remove.js +40 -8
  25. package/dist/commands/remove.js.map +2 -2
  26. package/dist/commands/set.js +53 -12
  27. package/dist/commands/set.js.map +2 -2
  28. package/dist/localisation/en-GB.js +100 -48
  29. package/dist/localisation/en-GB.js.map +2 -2
  30. package/dist/providers/CredentialProvider.js +36 -7
  31. package/dist/providers/CredentialProvider.js.map +3 -3
  32. package/dist/providers/SessionCacheProvider.js +21 -1
  33. package/dist/providers/SessionCacheProvider.js.map +2 -2
  34. package/dist/providers/file-provider.js +8 -4
  35. package/dist/providers/file-provider.js.map +3 -3
  36. package/dist/services/ContensisCliService.js +640 -375
  37. package/dist/services/ContensisCliService.js.map +3 -3
  38. package/dist/shell.js +27 -10
  39. package/dist/shell.js.map +3 -3
  40. package/dist/util/console.printer.js +171 -55
  41. package/dist/util/console.printer.js.map +2 -2
  42. package/dist/util/index.js +5 -2
  43. package/dist/util/index.js.map +3 -3
  44. package/dist/util/logger.js +47 -16
  45. package/dist/util/logger.js.map +2 -2
  46. package/dist/version.js +1 -1
  47. package/dist/version.js.map +1 -1
  48. package/package.json +2 -2
  49. package/src/commands/connect.ts +3 -2
  50. package/src/commands/create.ts +37 -8
  51. package/src/commands/diff.ts +41 -0
  52. package/src/commands/get.ts +80 -5
  53. package/src/commands/globalOptions.ts +18 -17
  54. package/src/commands/import.ts +57 -7
  55. package/src/commands/index.ts +9 -1
  56. package/src/commands/list.ts +35 -9
  57. package/src/commands/login.ts +3 -2
  58. package/src/commands/push.ts +9 -2
  59. package/src/commands/release.ts +32 -0
  60. package/src/commands/remove.ts +50 -4
  61. package/src/commands/set.ts +65 -9
  62. package/src/localisation/en-GB.ts +146 -65
  63. package/src/providers/CredentialProvider.ts +39 -6
  64. package/src/providers/SessionCacheProvider.ts +29 -2
  65. package/src/providers/file-provider.ts +12 -4
  66. package/src/services/ContensisCliService.ts +789 -426
  67. package/src/shell.ts +31 -11
  68. package/src/util/console.printer.ts +240 -78
  69. package/src/util/index.ts +12 -6
  70. package/src/util/logger.ts +87 -18
  71. package/src/version.ts +1 -1
@@ -33,6 +33,7 @@ var import_path = __toESM(require("path"));
33
33
  var import_node_fetch = __toESM(require("node-fetch"));
34
34
  var import_inquirer = __toESM(require("inquirer"));
35
35
  var import_await_to_js = __toESM(require("await-to-js"));
36
+ var import_chalk = __toESM(require("chalk"));
36
37
  var import_util = require("../util");
37
38
  var import_SessionCacheProvider = __toESM(require("../providers/SessionCacheProvider"));
38
39
  var import_ContensisAuthService = __toESM(require("./ContensisAuthService"));
@@ -45,35 +46,53 @@ var import_xml = require("../util/xml.formatter");
45
46
  var import_json = require("../util/json.formatter");
46
47
  var import_console = require("../util/console.printer");
47
48
  var import_file_provider = require("../providers/file-provider");
49
+ let insecurePasswordWarningShown = false;
48
50
  class ContensisCli {
49
51
  static quit = (error) => {
50
52
  process.removeAllListeners("exit");
51
53
  const exitCode = error ? 1 : 0;
52
- console.info(`
53
- Exiting contensis-cli with exit code: ${exitCode}
54
- `);
55
54
  process.exit(exitCode);
56
55
  };
57
- cache;
56
+ command;
57
+ format;
58
+ output;
59
+ session;
58
60
  contensis;
59
61
  contensisOpts;
60
- contentTypes;
61
- components;
62
- currentEnv;
63
62
  currentProject;
64
- env;
65
- sourceEnv;
63
+ sourceAlias;
66
64
  targetEnv;
67
65
  urls;
68
- command;
69
- format;
70
- output;
71
66
  log = import_logger.Logger;
72
67
  messages = import_en_GB.LogMessages;
73
- session;
74
68
  verb;
75
69
  noun;
76
70
  thirdArg;
71
+ get cache() {
72
+ return this.session.Get();
73
+ }
74
+ get currentEnv() {
75
+ return this.cache.currentEnvironment || "";
76
+ }
77
+ set currentEnv(currentEnvironment) {
78
+ this.session.Update({ currentEnvironment });
79
+ }
80
+ get env() {
81
+ const currentEnvironment = this.currentEnv;
82
+ const environments = this.cache.environments || {};
83
+ if (!currentEnvironment)
84
+ return {};
85
+ else if (!!environments[currentEnvironment])
86
+ return environments[currentEnvironment];
87
+ else {
88
+ return {
89
+ history: [],
90
+ lastUserId: "",
91
+ projects: [],
92
+ versionStatus: "latest"
93
+ };
94
+ }
95
+ }
77
96
  constructor(args, outputOpts, contensisOpts = {}) {
78
97
  const [exe, script, verb = "", noun = "", ...restArgs] = args;
79
98
  this.verb = verb == null ? void 0 : verb.toLowerCase();
@@ -81,24 +100,12 @@ Exiting contensis-cli with exit code: ${exitCode}
81
100
  this.thirdArg = restArgs == null ? void 0 : restArgs[0];
82
101
  const commandText = `${this.verb} ${this.noun} ${restArgs ? restArgs.join(" ") : ""}`.trim();
83
102
  this.session = new import_SessionCacheProvider.default();
84
- this.cache = this.session.Get();
85
103
  this.contensisOpts = contensisOpts;
86
104
  this.format = outputOpts == null ? void 0 : outputOpts.format;
87
105
  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 || "";
106
+ const currentEnvironment = (outputOpts == null ? void 0 : outputOpts.alias) || this.currentEnv;
89
107
  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
- }
108
+ this.currentEnv = currentEnvironment;
102
109
  const env = this.env;
103
110
  if (outputOpts == null ? void 0 : outputOpts.projectId)
104
111
  env.currentProject = outputOpts.projectId;
@@ -110,9 +117,8 @@ Exiting contensis-cli with exit code: ${exitCode}
110
117
  env.lastUserId = outputOpts.clientId;
111
118
  if (outputOpts == null ? void 0 : outputOpts.sharedSecret)
112
119
  env.passwordFallback = outputOpts.sharedSecret;
113
- this.currentEnv = currentEnvironment;
114
120
  this.currentProject = (env == null ? void 0 : env.currentProject) || "null";
115
- this.sourceEnv = (outputOpts == null ? void 0 : outputOpts.sourceEnv) || currentEnvironment;
121
+ this.sourceAlias = (outputOpts == null ? void 0 : outputOpts.sourceAlias) || currentEnvironment;
116
122
  if (currentEnvironment) {
117
123
  this.urls = (0, import_util.url)(currentEnvironment, (env == null ? void 0 : env.currentProject) || "website");
118
124
  }
@@ -124,7 +130,7 @@ Exiting contensis-cli with exit code: ${exitCode}
124
130
  if (currentEnvironment) {
125
131
  env.history = [this.command];
126
132
  if (commandText) {
127
- environments[currentEnvironment] = this.env;
133
+ environments[currentEnvironment] = env;
128
134
  this.session.Update({
129
135
  currentEnvironment,
130
136
  environments,
@@ -149,34 +155,20 @@ Exiting contensis-cli with exit code: ${exitCode}
149
155
  };
150
156
  Connect = async (environment) => {
151
157
  var _a;
152
- const { cache, log, messages, session } = this;
158
+ const { log, messages, session } = this;
153
159
  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
160
  this.currentEnv = environment;
165
161
  this.urls = (0, import_util.url)(environment, "website");
166
162
  const [fetchErr, response] = await (0, import_await_to_js.default)((0, import_node_fetch.default)(this.urls.cms));
167
163
  if (response && (response == null ? void 0 : response.status) < 400) {
168
164
  log.success(messages.connect.connected(environment));
165
+ session.UpdateEnv(this.env, environment);
169
166
  if ((_a = this.env) == null ? void 0 : _a.lastUserId) {
170
- await this.ConnectContensis();
171
167
  await this.PrintProjects();
172
168
  } else {
173
169
  log.warning(messages.projects.noList());
174
170
  log.help(messages.connect.tip());
175
171
  }
176
- session.Update({
177
- currentEnvironment: environment,
178
- environments: cache.environments
179
- });
180
172
  } else {
181
173
  log.error(
182
174
  messages.connect.unreachable(this.urls.cms, (response == null ? void 0 : response.status) || 0)
@@ -188,93 +180,80 @@ Exiting contensis-cli with exit code: ${exitCode}
188
180
  };
189
181
  ConnectContensis = async ({ commit = false } = {}) => {
190
182
  var _a, _b, _c;
191
- const { contensisOpts, currentEnv, env, log, messages } = this;
192
- const userId = env == null ? void 0 : env.lastUserId;
193
- const isGuidId = userId && (0, import_util.isUuid)(userId);
194
- if (currentEnv && userId) {
195
- const [credentialError, credentials] = await new import_CredentialProvider.default(
196
- {
183
+ if (!this.contensis) {
184
+ const { contensisOpts, currentEnv, env, log, messages } = this;
185
+ const userId = env == null ? void 0 : env.lastUserId;
186
+ const isGuidId = userId && (0, import_util.isUuid)(userId);
187
+ if (currentEnv && userId) {
188
+ const credentials = await this.GetCredentials(
197
189
  userId,
198
- alias: currentEnv
199
- },
200
- env.passwordFallback
201
- ).Init();
202
- if (credentialError && !credentials.current) {
203
- log.error(credentialError);
204
- return;
205
- }
206
- const cachedPassword = (_a = credentials == null ? void 0 : credentials.current) == null ? void 0 : _a.password;
207
- if (cachedPassword) {
208
- this.contensis = new import_migratortron.ContensisMigrationService(
209
- {
210
- ...contensisOpts,
211
- source: {
212
- url: ((_b = this.urls) == null ? void 0 : _b.cms) || "",
213
- username: !isGuidId ? userId : void 0,
214
- password: !isGuidId ? cachedPassword : void 0,
215
- clientId: isGuidId ? userId : void 0,
216
- sharedSecret: isGuidId ? cachedPassword : void 0,
217
- project: (env == null ? void 0 : env.currentProject) || "",
218
- assetHostname: (_c = this.urls) == null ? void 0 : _c.previewWeb
219
- },
220
- concurrency: typeof contensisOpts.concurrency !== "undefined" ? contensisOpts.concurrency : 3,
221
- outputProgress: true
222
- },
223
- !commit
190
+ env.passwordFallback
224
191
  );
192
+ const cachedPassword = (_a = credentials == null ? void 0 : credentials.current) == null ? void 0 : _a.password;
193
+ if (cachedPassword) {
194
+ this.contensis = new import_migratortron.ContensisMigrationService(
195
+ {
196
+ ...contensisOpts,
197
+ source: {
198
+ url: ((_b = this.urls) == null ? void 0 : _b.cms) || "",
199
+ username: !isGuidId ? userId : void 0,
200
+ password: !isGuidId ? cachedPassword : void 0,
201
+ clientId: isGuidId ? userId : void 0,
202
+ sharedSecret: isGuidId ? cachedPassword : void 0,
203
+ project: (env == null ? void 0 : env.currentProject) || "",
204
+ assetHostname: (_c = this.urls) == null ? void 0 : _c.previewWeb
205
+ },
206
+ concurrency: typeof contensisOpts.concurrency !== "undefined" ? contensisOpts.concurrency : 3,
207
+ outputProgress: true
208
+ },
209
+ !commit
210
+ );
211
+ }
212
+ } else {
213
+ if (!currentEnv)
214
+ log.help(messages.connect.help());
215
+ if (!userId)
216
+ log.help(messages.connect.tip());
225
217
  }
226
- } else {
227
- if (!currentEnv)
228
- log.help(messages.connect.help());
229
- if (!userId)
230
- log.help(messages.connect.tip());
231
218
  }
219
+ return this.contensis;
232
220
  };
233
221
  ConnectContensisImport = async ({
234
- commit,
235
- source,
236
- fileData,
237
- fileDataType
222
+ commit = false,
223
+ fromFile,
224
+ importDataType
238
225
  }) => {
239
226
  var _a, _b, _c, _d, _e, _f;
240
- const { contensisOpts, currentEnv, env, log, messages, sourceEnv } = this;
227
+ const source = fromFile ? "file" : "contensis";
228
+ const fileData = fromFile ? (0, import_file_provider.readJsonFile)(fromFile) || [] : [];
229
+ if (typeof fileData === "string")
230
+ throw new Error(`Import file format must be of type JSON`);
231
+ const { contensisOpts, currentEnv, env, log, messages, sourceAlias } = this;
241
232
  const environments = this.cache.environments || {};
242
- const sourceEnvironment = environments[sourceEnv || ""] || {};
233
+ const sourceEnvironment = environments[sourceAlias || ""] || {};
243
234
  const sourceCms = "source" in contensisOpts && contensisOpts.source || {};
244
235
  const sourceUserId = sourceCms.clientId || sourceCms.username || sourceEnvironment.lastUserId;
245
236
  const sourceProjectId = sourceCms.project || sourceEnvironment.currentProject || "website";
246
237
  const isSourceGuidId = sourceUserId && (0, import_util.isUuid)(sourceUserId);
247
- const sourceUrls = (0, import_util.url)(sourceEnv || "", sourceProjectId);
238
+ const sourceUrls = (0, import_util.url)(sourceAlias || "", sourceProjectId);
248
239
  const sourcePassword = sourceCms.sharedSecret || sourceCms.password || sourceEnvironment.passwordFallback;
249
240
  const targetUserId = env == null ? void 0 : env.lastUserId;
250
241
  const isTargetGuidId = targetUserId && (0, import_util.isUuid)(targetUserId);
251
242
  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
- }
243
+ const sourceCredentials = await this.GetCredentials(
244
+ sourceUserId,
245
+ sourcePassword,
246
+ sourceAlias,
247
+ false
248
+ );
263
249
  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
- },
250
+ const targetCredentials = await this.GetCredentials(
251
+ targetUserId,
269
252
  env.passwordFallback
270
- ).Init();
271
- if (targetCredentialError && !targetCredentials.current) {
272
- log.error(targetCredentialError);
273
- return;
274
- }
253
+ );
275
254
  const cachedTargetPassword = (_b = targetCredentials == null ? void 0 : targetCredentials.current) == null ? void 0 : _b.password;
276
255
  if (cachedSourcePassword && cachedTargetPassword) {
277
- if (source === "file" || source === "input") {
256
+ if (source === "file" || importDataType === "user-input") {
278
257
  this.contensis = new import_migratortron.ContensisMigrationService(
279
258
  {
280
259
  concurrency: 3,
@@ -289,12 +268,11 @@ Exiting contensis-cli with exit code: ${exitCode}
289
268
  targetProjects: [env.currentProject || ""],
290
269
  assetHostname: (_d = this.urls) == null ? void 0 : _d.previewWeb
291
270
  },
292
- ...fileDataType ? { [fileDataType]: fileData } : {}
271
+ ...importDataType ? { [importDataType]: fileData } : {}
293
272
  },
294
273
  !commit
295
274
  );
296
- }
297
- if (source === "contensis") {
275
+ } else if (source === "contensis") {
298
276
  this.contensis = new import_migratortron.ContensisMigrationService(
299
277
  {
300
278
  concurrency: 3,
@@ -329,82 +307,104 @@ Exiting contensis-cli with exit code: ${exitCode}
329
307
  if (!targetUserId)
330
308
  log.help(messages.connect.tip());
331
309
  }
310
+ return this.contensis;
311
+ };
312
+ GetCredentials = async (userId, password, currentEnv = this.currentEnv, saveCurrentEnv = true) => {
313
+ const { log, messages } = this;
314
+ if (userId) {
315
+ const [credentialError, credentials] = await new import_CredentialProvider.default(
316
+ { userId, alias: currentEnv },
317
+ password
318
+ ).Init();
319
+ if (credentialError && !credentials.current) {
320
+ log.error(credentialError);
321
+ return;
322
+ }
323
+ if (credentials.remarks.secure !== true) {
324
+ if (!insecurePasswordWarningShown) {
325
+ log.warning(messages.login.insecurePassword());
326
+ insecurePasswordWarningShown = true;
327
+ }
328
+ } else {
329
+ const env = this.cache.environments[currentEnv];
330
+ env.passwordFallback = void 0;
331
+ this.session.UpdateEnv(env, currentEnv, saveCurrentEnv);
332
+ }
333
+ return credentials;
334
+ }
332
335
  };
333
336
  Login = async (userId, {
334
337
  password = (0, import_util.isPassword)(this.env.passwordFallback),
335
338
  promptPassword = true,
336
339
  sharedSecret = (0, import_util.isSharedSecret)(this.env.passwordFallback),
337
- silent = false
338
- }) => {
340
+ silent = false,
341
+ attempt = 1
342
+ } = {}) => {
339
343
  var _a, _b, _c, _d;
340
- let inputPassword = password;
344
+ let inputPassword = password || sharedSecret;
341
345
  const { log, messages } = this;
342
346
  if (userId) {
343
- const { cache, currentEnv, env } = this;
347
+ const { currentEnv, env } = this;
344
348
  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));
349
+ const credentials = await this.GetCredentials(userId, inputPassword);
350
+ if (credentials) {
351
+ const cachedPassword = (0, import_util.isPassword)((_a = credentials.current) == null ? void 0 : _a.password);
352
+ const cachedSecret = (0, import_util.isSharedSecret)((_b = credentials.current) == null ? void 0 : _b.password);
353
+ if (!cachedPassword && !cachedSecret && promptPassword) {
354
+ ({ inputPassword } = await import_inquirer.default.prompt([
355
+ {
356
+ type: "password",
357
+ message: messages.login.passwordPrompt(currentEnv, userId),
358
+ name: "inputPassword",
359
+ mask: "*",
360
+ prefix: void 0
361
+ }
362
+ ]));
363
+ }
364
+ if (inputPassword || cachedPassword || cachedSecret) {
365
+ const authService = new import_ContensisAuthService.default({
366
+ username: userId,
367
+ password: inputPassword || cachedPassword,
368
+ projectId: (env == null ? void 0 : env.currentProject) || "website",
369
+ rootUrl: ((_c = this.urls) == null ? void 0 : _c.cms) || "",
370
+ clientId: userId,
371
+ clientSecret: sharedSecret || cachedSecret
372
+ });
373
+ const [authError, bearerToken] = await (0, import_await_to_js.default)(
374
+ authService.BearerToken()
375
+ );
376
+ if (bearerToken) {
377
+ env.authToken = bearerToken;
378
+ env.lastUserId = userId;
379
+ env.passwordFallback = credentials.remarks.secure !== true ? (_d = credentials.current) == null ? void 0 : _d.password : void 0;
380
+ this.session.UpdateEnv(env);
381
+ if (inputPassword)
382
+ await credentials.Save(inputPassword);
383
+ if (sharedSecret)
384
+ await credentials.Save(sharedSecret);
385
+ if (!silent) {
386
+ import_logger.Logger.success(messages.login.success(currentEnv, userId));
387
+ await this.PrintProjects();
388
+ }
389
+ } else if (authError) {
390
+ import_logger.Logger.error(authError.toString());
391
+ env.authToken = "";
392
+ env.lastUserId = "";
393
+ env.passwordFallback = void 0;
394
+ this.session.UpdateEnv(env);
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));
400
+ }
400
401
  }
402
+ return env.authToken;
403
+ } else {
404
+ import_logger.Logger.error(messages.login.passwordPrompt());
405
+ if (attempt < 2)
406
+ return await this.Login(userId, { attempt: attempt + 1 });
401
407
  }
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
408
  }
409
409
  } else {
410
410
  import_logger.Logger.error(messages.login.noEnv());
@@ -413,37 +413,81 @@ Exiting contensis-cli with exit code: ${exitCode}
413
413
  import_logger.Logger.error(messages.login.noUserId());
414
414
  }
415
415
  };
416
+ PrintContensisVersion = async () => {
417
+ const { log, messages } = this;
418
+ const contensis = await this.ConnectContensis();
419
+ if (contensis) {
420
+ const [projectsErr, projects] = await (0, import_await_to_js.default)(
421
+ contensis.projects.GetSourceProjects()
422
+ );
423
+ if (Array.isArray(projects)) {
424
+ this.HandleFormattingAndOutput(
425
+ contensis.contensisVersion,
426
+ () => log.raw(log.highlightText(contensis.contensisVersion))
427
+ );
428
+ }
429
+ if (projectsErr) {
430
+ log.error(messages.projects.noList());
431
+ log.error(projectsErr.message);
432
+ }
433
+ }
434
+ };
416
435
  PrintProjects = async () => {
417
- const { cache, currentEnv, currentProject, log, messages, session } = this;
418
- if (!this.contensis)
419
- await this.ConnectContensis();
420
- if (this.contensis) {
436
+ const { currentProject, log, messages, session } = this;
437
+ const contensis = await this.ConnectContensis();
438
+ if (contensis) {
421
439
  const [projectsErr, projects] = await (0, import_await_to_js.default)(
422
- this.contensis.projects.GetSourceProjects()
440
+ contensis.projects.GetSourceProjects()
423
441
  );
424
442
  if (Array.isArray(projects)) {
425
- const currentVals = cache.environments[currentEnv] || {};
426
443
  const nextCurrentProject = currentProject && currentProject !== "null" ? currentProject : projects.some((p) => p.id === "website") ? "website" : void 0;
427
- cache.environments[currentEnv] = {
428
- ...currentVals,
444
+ session.UpdateEnv({
429
445
  projects: projects.map((p) => p.id),
430
446
  currentProject: nextCurrentProject
431
- };
447
+ });
432
448
  log.success(messages.projects.list());
449
+ log.raw("");
433
450
  this.HandleFormattingAndOutput(projects, () => {
434
- for (const project of projects) {
451
+ for (const project of projects.sort(
452
+ (a, b) => a.id.localeCompare(b.id)
453
+ )) {
454
+ let color;
455
+ try {
456
+ color = import_chalk.default.keyword(project.color);
457
+ } catch (ex) {
458
+ color = import_chalk.default.white;
459
+ }
435
460
  console.log(
436
- ` - ${nextCurrentProject === project.id ? "* " : ""}[${project.primaryLanguage}] ${project.id}`
461
+ `${nextCurrentProject === project.id ? `>> ${log.boldText(color(project.id))}` : ` ${color(project.id)}`} ${log.infoText(
462
+ `[${project.supportedLanguages.map(
463
+ (l) => l === project.primaryLanguage ? `*${log.boldText(l)}` : l
464
+ ).join(" ")}]`
465
+ )}`
437
466
  );
438
467
  }
439
468
  });
440
- session.Update({
441
- environments: cache.environments
442
- });
443
- if (nextCurrentProject) {
444
- this.env = cache.environments[currentEnv];
445
- this.SetProject(nextCurrentProject);
446
- }
469
+ if (!this.SetProject(nextCurrentProject))
470
+ log.warning(messages.projects.tip());
471
+ }
472
+ if (projectsErr) {
473
+ log.error(messages.projects.noList());
474
+ log.error(projectsErr.message);
475
+ }
476
+ }
477
+ };
478
+ PrintProject = async (projectId = this.currentProject) => {
479
+ const { log, messages, session } = this;
480
+ const contensis = await this.ConnectContensis();
481
+ if (contensis) {
482
+ const [projectsErr, projects] = await (0, import_await_to_js.default)(
483
+ contensis.projects.GetSourceProjects()
484
+ );
485
+ const foundProject = projects == null ? void 0 : projects.find(
486
+ (p) => p.id.toLowerCase() === projectId.toLowerCase()
487
+ );
488
+ if (foundProject) {
489
+ log.raw("");
490
+ this.HandleFormattingAndOutput(foundProject, log.object);
447
491
  }
448
492
  if (projectsErr) {
449
493
  log.error(messages.projects.noList());
@@ -451,8 +495,8 @@ Exiting contensis-cli with exit code: ${exitCode}
451
495
  }
452
496
  }
453
497
  };
454
- SetProject = async (projectId = "") => {
455
- const { cache, env, log, messages, session } = this;
498
+ SetProject = (projectId = "website") => {
499
+ const { env, log, messages, session } = this;
456
500
  let nextProjectId;
457
501
  if ((env == null ? void 0 : env.projects.length) > 0 && (env == null ? void 0 : env.lastUserId)) {
458
502
  nextProjectId = env.projects.find(
@@ -460,10 +504,9 @@ Exiting contensis-cli with exit code: ${exitCode}
460
504
  );
461
505
  if (nextProjectId) {
462
506
  env.currentProject = nextProjectId;
463
- session.Update({
464
- environments: cache.environments
465
- });
507
+ session.UpdateEnv(env);
466
508
  log.success(messages.projects.set(projectId));
509
+ log.raw("");
467
510
  } else {
468
511
  log.error(messages.projects.failedSet(projectId));
469
512
  }
@@ -473,8 +516,8 @@ Exiting contensis-cli with exit code: ${exitCode}
473
516
  }
474
517
  return nextProjectId;
475
518
  };
476
- SetVersion = async (versionStatus) => {
477
- const { cache, env, log, messages, session } = this;
519
+ SetVersion = (versionStatus) => {
520
+ const { env, log, messages, session } = this;
478
521
  if (!["latest", "published"].includes(versionStatus)) {
479
522
  log.error(messages.version.invalid(versionStatus));
480
523
  return false;
@@ -484,10 +527,7 @@ Exiting contensis-cli with exit code: ${exitCode}
484
527
  return false;
485
528
  }
486
529
  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
- });
530
+ session.UpdateEnv({ versionStatus });
491
531
  log.success(messages.version.set(this.currentEnv, versionStatus));
492
532
  return true;
493
533
  } else {
@@ -496,26 +536,11 @@ Exiting contensis-cli with exit code: ${exitCode}
496
536
  return false;
497
537
  }
498
538
  };
499
- HydrateContensis = async () => {
500
- const { log } = this;
501
- if (!this.contensis)
502
- await this.ConnectContensis();
503
- if (this.contensis) {
504
- const [contensisErr, models] = await (0, import_await_to_js.default)(
505
- this.contensis.models.HydrateContensisRepositories()
506
- );
507
- if (contensisErr) {
508
- log.error(contensisErr.message);
509
- return contensisErr;
510
- }
511
- }
512
- };
513
539
  PrintApiKeys = async () => {
514
540
  const { currentEnv, log, messages } = this;
515
- if (!this.contensis)
516
- await this.ConnectContensis();
517
- if (this.contensis) {
518
- const [keysErr, apiKeys] = await this.contensis.apiKeys.GetKeys();
541
+ const contensis = await this.ConnectContensis();
542
+ if (contensis) {
543
+ const [keysErr, apiKeys] = await contensis.apiKeys.GetKeys();
519
544
  if (Array.isArray(apiKeys)) {
520
545
  log.success(messages.keys.list(currentEnv));
521
546
  this.HandleFormattingAndOutput(apiKeys, () => {
@@ -543,13 +568,9 @@ Exiting contensis-cli with exit code: ${exitCode}
543
568
  };
544
569
  CreateApiKey = async (name, description) => {
545
570
  const { currentEnv, log, messages } = this;
546
- if (!this.contensis)
547
- await this.ConnectContensis();
548
- if (this.contensis) {
549
- const [err, key] = await this.contensis.apiKeys.CreateKey(
550
- name,
551
- description
552
- );
571
+ const contensis = await this.ConnectContensis();
572
+ if (contensis) {
573
+ const [err, key] = await contensis.apiKeys.CreateKey(name, description);
553
574
  if (key) {
554
575
  log.success(messages.keys.created(currentEnv, name));
555
576
  console.log(
@@ -566,10 +587,9 @@ Exiting contensis-cli with exit code: ${exitCode}
566
587
  };
567
588
  RemoveApiKey = async (id) => {
568
589
  const { currentEnv, log, messages } = this;
569
- if (!this.contensis)
570
- await this.ConnectContensis({ commit: true });
571
- if (this.contensis) {
572
- const [err, key] = await this.contensis.apiKeys.RemoveKey(id);
590
+ const contensis = await this.ConnectContensis({ commit: true });
591
+ if (contensis) {
592
+ const [err, key] = await contensis.apiKeys.RemoveKey(id);
573
593
  if (!err) {
574
594
  log.success(messages.keys.removed(currentEnv, id));
575
595
  console.log("");
@@ -578,25 +598,181 @@ Exiting contensis-cli with exit code: ${exitCode}
578
598
  }
579
599
  }
580
600
  };
581
- GetContentTypes = async () => {
601
+ CreateProject = async (project) => {
602
+ const { currentEnv, log, messages } = this;
603
+ const contensis = await this.ConnectContensis();
604
+ if (contensis) {
605
+ const [err, created] = await contensis.projects.CreateProject(project);
606
+ if (created) {
607
+ log.success(messages.projects.created(currentEnv, project.id));
608
+ this.HandleFormattingAndOutput(created, () => {
609
+ this.SetProject(project.id);
610
+ this.PrintProjects();
611
+ });
612
+ return project.id;
613
+ }
614
+ if (err) {
615
+ log.error(messages.projects.failedCreate(currentEnv, project.id), err);
616
+ }
617
+ }
618
+ };
619
+ UpdateProject = async (project) => {
620
+ const { currentEnv, currentProject, log, messages } = this;
621
+ const contensis = await this.ConnectContensis();
622
+ if (contensis) {
623
+ const [err, updated] = await contensis.projects.UpdateProject({
624
+ id: currentProject,
625
+ ...project
626
+ });
627
+ if (updated) {
628
+ log.success(messages.projects.updated(currentEnv, currentProject));
629
+ this.HandleFormattingAndOutput(updated, log.object);
630
+ return updated.id;
631
+ }
632
+ if (err) {
633
+ log.error(
634
+ messages.projects.failedUpdate(currentEnv, currentProject),
635
+ err
636
+ );
637
+ }
638
+ }
639
+ };
640
+ PrintContentModels = async (modelIds = []) => {
641
+ const { currentProject, log, messages } = this;
642
+ const contensis = await this.ConnectContensis();
643
+ if (contensis) {
644
+ const models = await contensis.models.contentModels();
645
+ const contentTypes = await contensis.models.contentTypes();
646
+ const components = await contensis.models.components();
647
+ const returnModels = (modelIds == null ? void 0 : modelIds.length) ? models == null ? void 0 : models.filter(
648
+ (m) => modelIds.some((id) => id.toLowerCase() === m.id.toLowerCase())
649
+ ) : void 0;
650
+ const contentTypeIds = Array.from(
651
+ /* @__PURE__ */ new Set([
652
+ ...(returnModels || models || []).map((m) => m.id),
653
+ ...(returnModels || models || []).map((m) => {
654
+ var _a, _b;
655
+ return ((_b = (_a = m.dependencies) == null ? void 0 : _a.contentTypes) == null ? void 0 : _b.map((c) => c[0])) || [];
656
+ }).flat()
657
+ ])
658
+ );
659
+ const componentIds = Array.from(
660
+ new Set(
661
+ (returnModels || models || []).map((m) => {
662
+ var _a, _b;
663
+ return ((_b = (_a = m.dependencies) == null ? void 0 : _a.components) == null ? void 0 : _b.map((c) => c[0])) || [];
664
+ }).flat()
665
+ )
666
+ );
667
+ const contentModelBackup = [
668
+ ...contentTypes.filter(
669
+ (c) => contentTypeIds.map((i) => i.toLowerCase()).includes(c.id.toLowerCase())
670
+ ),
671
+ ...components.filter(
672
+ (c) => componentIds.map((i) => i.toLowerCase()).includes(c.id.toLowerCase())
673
+ )
674
+ ];
675
+ if (Array.isArray(returnModels)) {
676
+ log.success(messages.models.list(currentProject));
677
+ this.HandleFormattingAndOutput(contentModelBackup, () => {
678
+ for (const model of returnModels) {
679
+ log.raw("");
680
+ log.object(model);
681
+ }
682
+ log.raw("");
683
+ });
684
+ } else {
685
+ log.success(
686
+ messages.models.get(currentProject, (models == null ? void 0 : models.length.toString()) || "0")
687
+ );
688
+ log.raw("");
689
+ if (models == null ? void 0 : models.length) {
690
+ this.HandleFormattingAndOutput(contentModelBackup, () => {
691
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
692
+ for (const model of models) {
693
+ const components2 = ((_a = model.components) == null ? void 0 : _a.length) || 0;
694
+ const contentTypes2 = ((_b = model.contentTypes) == null ? void 0 : _b.length) || 0;
695
+ const dependencies = (((_d = (_c = model.dependencies) == null ? void 0 : _c.components) == null ? void 0 : _d.length) || 0) + (((_f = (_e = model.dependencies) == null ? void 0 : _e.contentTypes) == null ? void 0 : _f.length) || 0);
696
+ const dependencyOf = (((_h = (_g = model.dependencyOf) == null ? void 0 : _g.components) == null ? void 0 : _h.length) || 0) + (((_j = (_i = model.dependencyOf) == null ? void 0 : _i.contentTypes) == null ? void 0 : _j.length) || 0);
697
+ const hasAny = components2 + contentTypes2 + dependencies + dependencyOf;
698
+ log.raw(
699
+ ` - ${log.highlightText(log.boldText(model.id))} ${hasAny ? log.infoText(
700
+ `{ ${components2 ? `components: ${components2}, ` : ""}${contentTypes2 ? `contentTypes: ${contentTypes2}, ` : ""}${dependencies ? `references: ${dependencies}, ` : ""}${dependencyOf ? `required by: ${dependencyOf}` : ""} }`
701
+ ) : ""}`
702
+ );
703
+ }
704
+ log.raw("");
705
+ });
706
+ }
707
+ }
708
+ }
709
+ };
710
+ ImportContentModels = async ({
711
+ commit,
712
+ fromFile
713
+ }) => {
582
714
  const { currentProject, log, messages } = this;
583
- let err;
584
- if (!this.contensis)
585
- err = await this.HydrateContensis();
586
- if (err)
587
- log.error(messages.contenttypes.noList(currentProject));
588
- if (this.contensis) {
589
- this.contentTypes = this.contensis.models.contentTypes();
590
- this.components = this.contensis.models.components();
715
+ const fileData = fromFile ? (0, import_file_provider.readJsonFile)(fromFile) || [] : [];
716
+ if (typeof fileData === "string")
717
+ throw new Error(`Import file format must be of type JSON`);
718
+ const contensis = await this.ConnectContensisImport({
719
+ commit,
720
+ fromFile,
721
+ importDataType: "models"
722
+ });
723
+ if (contensis) {
724
+ log.line();
725
+ if (contensis.isPreview) {
726
+ console.log(log.successText(` -- IMPORT PREVIEW -- `));
727
+ } else {
728
+ console.log(log.warningText(` *** COMMITTING IMPORT *** `));
729
+ }
730
+ const [migrateErr, result] = await contensis.MigrateContentModels();
731
+ if (migrateErr)
732
+ (0, import_logger.logError)(migrateErr);
733
+ else
734
+ this.HandleFormattingAndOutput(result, () => {
735
+ if (!commit) {
736
+ log.raw(log.boldText(`
737
+ Content types:`));
738
+ if (!result.contentTypes)
739
+ log.info(`- None returned
740
+ `);
741
+ else
742
+ (0, import_console.printModelMigrationAnalysis)(this, result.contentTypes);
743
+ log.raw(log.boldText(`
744
+ Components:`));
745
+ if (!result.components)
746
+ log.info(`- None returned
747
+ `);
748
+ else
749
+ (0, import_console.printModelMigrationAnalysis)(this, result.components);
750
+ } else {
751
+ const migrateResult = result;
752
+ log.raw(log.boldText(`
753
+ Content types:`));
754
+ (0, import_console.printModelMigrationResult)(
755
+ this,
756
+ migrateResult[currentProject].contentTypes
757
+ );
758
+ log.raw(log.boldText(`
759
+ Components:`));
760
+ (0, import_console.printModelMigrationResult)(
761
+ this,
762
+ migrateResult[currentProject].components
763
+ );
764
+ }
765
+ });
591
766
  } else {
592
- log.warning(messages.contenttypes.noList(currentProject));
767
+ log.warning(messages.models.noList(currentProject));
768
+ log.help(messages.connect.tip());
593
769
  }
594
770
  };
595
771
  PrintContentTypes = async () => {
596
772
  const { currentProject, log, messages } = this;
597
- await this.GetContentTypes();
598
- if (this.contensis) {
599
- const { contentTypes } = this;
773
+ const contensis = await this.ConnectContensis();
774
+ if (contensis) {
775
+ const contentTypes = await contensis.models.contentTypes();
600
776
  if (Array.isArray(contentTypes)) {
601
777
  log.success(messages.contenttypes.list(currentProject));
602
778
  this.HandleFormattingAndOutput(contentTypes, () => {
@@ -613,9 +789,9 @@ Exiting contensis-cli with exit code: ${exitCode}
613
789
  };
614
790
  PrintContentType = async (contentTypeId) => {
615
791
  const { currentProject, log, messages } = this;
616
- await this.GetContentTypes();
617
- if (this.contensis) {
618
- const { contentTypes } = this;
792
+ const contensis = await this.ConnectContensis();
793
+ if (contensis) {
794
+ const contentTypes = await contensis.models.contentTypes();
619
795
  if (Array.isArray(contentTypes)) {
620
796
  const contentType = contentTypes.find(
621
797
  (c) => c.id.toLowerCase() === contentTypeId.toLowerCase()
@@ -635,15 +811,12 @@ Exiting contensis-cli with exit code: ${exitCode}
635
811
  };
636
812
  RemoveContentTypes = async (contentTypeIds, commit = false) => {
637
813
  const { currentProject, log, messages } = this;
638
- if (!this.contensis)
639
- await this.ConnectContensisImport({
640
- source: "input",
641
- commit
642
- });
643
- if (this.contensis) {
644
- const [err, result] = await this.contensis.DeleteContentTypes(
645
- contentTypeIds
646
- );
814
+ const contensis = await this.ConnectContensisImport({
815
+ commit,
816
+ importDataType: "user-input"
817
+ });
818
+ if (contensis) {
819
+ const [err, result] = await contensis.DeleteContentTypes(contentTypeIds);
647
820
  if (err) {
648
821
  log.error(
649
822
  messages.contenttypes.failedRemove(
@@ -657,7 +830,7 @@ Exiting contensis-cli with exit code: ${exitCode}
657
830
  messages.contenttypes.removed(
658
831
  currentProject,
659
832
  contentTypeIds.join('", "'),
660
- !this.contensis.isPreview
833
+ !contensis.isPreview
661
834
  )
662
835
  );
663
836
  this.HandleFormattingAndOutput(
@@ -677,15 +850,15 @@ Exiting contensis-cli with exit code: ${exitCode}
677
850
  throw new Error(`Import file format must be of type JSON`);
678
851
  if (!Array.isArray(fileData))
679
852
  fileData = [fileData];
680
- await this.ConnectContensisImport({
853
+ const contensis = await this.ConnectContensisImport({
681
854
  commit,
682
- source: fromFile ? "file" : "contensis"
855
+ importDataType: fromFile ? "user-input" : void 0
683
856
  });
684
- if (this.contensis) {
857
+ if (contensis) {
685
858
  for (const contentType of fileData) {
686
859
  contentType.projectId = currentProject;
687
860
  delete contentType.uuid;
688
- const [err, created, createStatus] = await this.contensis.models.targetRepos[currentProject].repo.UpsertContentType(false, contentType);
861
+ const [err, created, createStatus] = await contensis.models.targetRepos[currentProject].repo.UpsertContentType(false, contentType);
689
862
  if (err)
690
863
  log.error(err.message, err);
691
864
  if (createStatus) {
@@ -702,11 +875,54 @@ Exiting contensis-cli with exit code: ${exitCode}
702
875
  }
703
876
  }
704
877
  };
878
+ DiffModels = async ({
879
+ fromFile
880
+ }, modelIds = []) => {
881
+ const { log } = this;
882
+ let fileData = fromFile ? (0, import_file_provider.readJsonFile)(fromFile) || [] : [];
883
+ if (typeof fileData === "string")
884
+ throw new Error(`Import file format must be of type JSON`);
885
+ if (!Array.isArray(fileData))
886
+ fileData = [fileData];
887
+ const contensis = await this.ConnectContensisImport({
888
+ fromFile,
889
+ importDataType: "models"
890
+ });
891
+ if (contensis) {
892
+ const [err, result] = await (0, import_await_to_js.default)(
893
+ contensis.models.Diff(fileData.length ? fileData : modelIds)
894
+ );
895
+ if (err)
896
+ log.error(err.message, err);
897
+ if (result)
898
+ this.HandleFormattingAndOutput(result, () => {
899
+ var _a;
900
+ log.success(
901
+ `Queried models ${log.infoText(
902
+ `"${(_a = result.query.modelIds) == null ? void 0 : _a.join(", ")}"`
903
+ )}
904
+ `
905
+ );
906
+ log.raw(log.boldText(`Content types:`));
907
+ if (!result.contentTypes)
908
+ log.info(`- None returned
909
+ `);
910
+ else
911
+ (0, import_console.printModelMigrationAnalysis)(this, result.contentTypes);
912
+ log.raw(log.boldText(`Components:`));
913
+ if (!result.components)
914
+ log.info(`- None returned
915
+ `);
916
+ else
917
+ (0, import_console.printModelMigrationAnalysis)(this, result.components);
918
+ });
919
+ }
920
+ };
705
921
  PrintComponents = async () => {
706
922
  const { currentProject, log, messages } = this;
707
- await this.GetContentTypes();
708
- if (this.contensis) {
709
- const { components } = this;
923
+ const contensis = await this.ConnectContensis();
924
+ if (contensis) {
925
+ const components = await contensis.models.components();
710
926
  if (Array.isArray(components)) {
711
927
  log.success(messages.components.list(currentProject));
712
928
  this.HandleFormattingAndOutput(components, () => {
@@ -723,9 +939,9 @@ Exiting contensis-cli with exit code: ${exitCode}
723
939
  };
724
940
  PrintComponent = async (componentId) => {
725
941
  const { currentProject, log, messages } = this;
726
- await this.GetContentTypes();
727
- if (this.contensis) {
728
- const { components } = this;
942
+ const contensis = await this.ConnectContensis();
943
+ if (contensis) {
944
+ const components = await contensis.models.components();
729
945
  if (Array.isArray(components)) {
730
946
  const component = components.find(
731
947
  (c) => c.id.toLowerCase() === componentId.toLowerCase()
@@ -741,13 +957,12 @@ Exiting contensis-cli with exit code: ${exitCode}
741
957
  };
742
958
  RemoveComponents = async (componentIds, commit = false) => {
743
959
  const { currentProject, log, messages } = this;
744
- if (!this.contensis)
745
- await this.ConnectContensisImport({
746
- source: "input",
747
- commit
748
- });
749
- if (this.contensis) {
750
- const [err, result] = await this.contensis.DeleteContentTypes(
960
+ const contensis = await this.ConnectContensisImport({
961
+ commit,
962
+ importDataType: "user-input"
963
+ });
964
+ if (contensis) {
965
+ const [err, result] = await contensis.DeleteContentTypes(
751
966
  void 0,
752
967
  componentIds
753
968
  );
@@ -764,7 +979,7 @@ Exiting contensis-cli with exit code: ${exitCode}
764
979
  messages.components.removed(
765
980
  currentProject,
766
981
  componentIds.join('", "'),
767
- !this.contensis.isPreview
982
+ !contensis.isPreview
768
983
  )
769
984
  );
770
985
  this.HandleFormattingAndOutput(
@@ -784,15 +999,15 @@ Exiting contensis-cli with exit code: ${exitCode}
784
999
  throw new Error(`Import file format must be of type JSON`);
785
1000
  if (!Array.isArray(fileData))
786
1001
  fileData = [fileData];
787
- await this.ConnectContensisImport({
1002
+ const contensis = await this.ConnectContensisImport({
788
1003
  commit,
789
- source: fromFile ? "file" : "contensis"
1004
+ importDataType: fromFile ? "user-input" : void 0
790
1005
  });
791
- if (this.contensis) {
1006
+ if (contensis) {
792
1007
  for (const component of fileData) {
793
1008
  component.projectId = currentProject;
794
1009
  delete component.uuid;
795
- const [err, created, createStatus] = await this.contensis.models.targetRepos[currentProject].repo.UpsertComponent(false, component);
1010
+ const [err, created, createStatus] = await contensis.models.targetRepos[currentProject].repo.UpsertComponent(false, component);
796
1011
  if (err)
797
1012
  log.error(err.message, err);
798
1013
  if (createStatus) {
@@ -809,33 +1024,37 @@ Exiting contensis-cli with exit code: ${exitCode}
809
1024
  }
810
1025
  }
811
1026
  };
812
- RemoveEntry = async (id, commit = false) => {
813
- var _a, _b, _c;
814
- const { currentEnv, log, messages } = this;
815
- if (!this.contensis)
816
- await this.ConnectContensisImport({
817
- source: "input",
818
- commit
819
- });
820
- if (this.contensis) {
821
- if (this.contensis.isPreview) {
1027
+ RemoveEntries = async (commit = false) => {
1028
+ var _a;
1029
+ const { currentEnv, currentProject, log, messages } = this;
1030
+ const contensis = await this.ConnectContensisImport({
1031
+ commit,
1032
+ importDataType: "user-input"
1033
+ });
1034
+ if (contensis) {
1035
+ if (contensis.isPreview) {
822
1036
  console.log(log.successText(` -- PREVIEW -- `));
823
1037
  } else {
824
1038
  console.log(log.warningText(` *** COMMITTING DELETE *** `));
825
1039
  }
826
- const [err, result] = await this.contensis.DeleteEntries();
1040
+ const [err, result] = await contensis.DeleteEntries();
827
1041
  if (result)
828
1042
  this.HandleFormattingAndOutput(result, () => {
829
- (0, import_console.printMigrateResult)(this, result, { action: "delete" });
1043
+ (0, import_console.printMigrateResult)(this, result, {
1044
+ action: "delete",
1045
+ showAllEntries: true
1046
+ });
830
1047
  });
831
- if (!err && (!commit && ((_a = Object.values(result.entriesToMigrate)) == null ? void 0 : _a[0].totalCount) > 0 || commit && ((_b = result.migrateResult) == null ? void 0 : _b.deleted))) {
832
- log.success(messages.entries.removed(currentEnv, id, commit));
833
- if (!commit)
1048
+ if (!err && (!commit && result.entriesToMigrate[currentProject].totalCount || commit && ((_a = result.migrateResult) == null ? void 0 : _a.deleted))) {
1049
+ log.success(messages.entries.removed(currentEnv, commit));
1050
+ if (!commit) {
1051
+ log.raw(``);
834
1052
  log.help(messages.entries.commitTip());
1053
+ }
835
1054
  } else {
836
- log.error(messages.entries.failedRemove(currentEnv, id), err);
837
- if (!((_c = Object.values(result.entriesToMigrate)) == null ? void 0 : _c[0].totalCount))
838
- log.help(messages.entries.notFound(id));
1055
+ log.error(messages.entries.failedRemove(currentEnv), err);
1056
+ if (!result.entriesToMigrate[currentProject].totalCount)
1057
+ log.help(messages.entries.notFound(currentEnv));
839
1058
  }
840
1059
  }
841
1060
  };
@@ -843,71 +1062,88 @@ Exiting contensis-cli with exit code: ${exitCode}
843
1062
  withDependents = false
844
1063
  }) => {
845
1064
  const { currentProject, log, messages } = this;
846
- await this.ConnectContensis();
847
- if (this.contensis) {
1065
+ const contensis = await this.ConnectContensis();
1066
+ if (contensis) {
848
1067
  log.line();
849
- const entries = await this.contensis.GetEntries({ withDependents });
1068
+ const entries = await contensis.GetEntries({ withDependents });
850
1069
  this.HandleFormattingAndOutput(
851
1070
  entries,
852
1071
  () => {
853
- var _a, _b;
1072
+ var _a;
854
1073
  return (0, import_migratortron.logEntriesTable)(
855
1074
  entries,
856
1075
  currentProject,
857
- (_b = (_a = this.contensis) == null ? void 0 : _a.payload.query) == null ? void 0 : _b.fields
1076
+ (_a = contensis.payload.query) == null ? void 0 : _a.fields
858
1077
  );
859
1078
  }
860
1079
  );
861
1080
  } else {
862
- log.warning(messages.contenttypes.noList(currentProject));
1081
+ log.warning(messages.models.noList(currentProject));
863
1082
  log.help(messages.connect.tip());
864
1083
  }
865
1084
  };
866
1085
  ImportEntries = async ({
867
1086
  commit,
868
- fromFile
1087
+ fromFile,
1088
+ logOutput
869
1089
  }) => {
870
- const { currentProject, log, messages } = this;
871
- const fileData = fromFile ? (0, import_file_provider.readJsonFile)(fromFile) || [] : [];
872
- if (typeof fileData === "string")
873
- throw new Error(`Import file format must be of type JSON`);
874
- await this.ConnectContensisImport({
1090
+ var _a, _b, _c, _d, _e;
1091
+ const { currentEnv, currentProject, log, messages } = this;
1092
+ const contensis = await this.ConnectContensisImport({
875
1093
  commit,
876
- source: fromFile ? "file" : "contensis",
877
- fileData,
878
- fileDataType: "entries"
1094
+ fromFile,
1095
+ importDataType: "entries"
879
1096
  });
880
- if (this.contensis) {
1097
+ if (contensis) {
881
1098
  log.line();
882
- if (this.contensis.isPreview) {
1099
+ if (contensis.isPreview) {
883
1100
  console.log(log.successText(` -- IMPORT PREVIEW -- `));
884
1101
  } else {
885
1102
  console.log(log.warningText(` *** COMMITTING IMPORT *** `));
886
1103
  }
887
- const [migrateErr, migrateResult] = await this.contensis.MigrateEntries();
888
- if (migrateErr)
889
- (0, import_logger.logError)(migrateErr);
1104
+ const [err, result] = await contensis.MigrateEntries();
1105
+ if (err)
1106
+ (0, import_logger.logError)(err);
890
1107
  else
891
- this.HandleFormattingAndOutput(migrateResult, () => {
892
- (0, import_console.printMigrateResult)(this, migrateResult);
1108
+ this.HandleFormattingAndOutput(result, () => {
1109
+ (0, import_console.printMigrateResult)(this, result, {
1110
+ showAllEntries: logOutput === "all",
1111
+ showChangedEntries: logOutput === "changes"
1112
+ });
893
1113
  });
1114
+ if (!err && !((_a = result.errors) == null ? void 0 : _a.length) && (!commit && result.entriesToMigrate[currentProject].totalCount || commit && (((_b = result.migrateResult) == null ? void 0 : _b.created) || ((_c = result.migrateResult) == null ? void 0 : _c.updated)))) {
1115
+ log.success(
1116
+ messages.entries.imported(
1117
+ currentEnv,
1118
+ commit,
1119
+ commit ? (((_d = result.migrateResult) == null ? void 0 : _d.created) || 0) + (((_e = result.migrateResult) == null ? void 0 : _e.updated) || 0) : result.entriesToMigrate[currentProject].totalCount
1120
+ )
1121
+ );
1122
+ if (!commit) {
1123
+ log.raw(``);
1124
+ log.help(messages.entries.commitTip());
1125
+ }
1126
+ } else {
1127
+ log.error(messages.entries.failedImport(currentEnv), err);
1128
+ if (!result.entriesToMigrate[currentProject].totalCount)
1129
+ log.help(messages.entries.notFound(currentEnv));
1130
+ }
894
1131
  } else {
895
- log.warning(messages.contenttypes.noList(currentProject));
1132
+ log.warning(messages.models.noList(currentProject));
896
1133
  log.help(messages.connect.tip());
897
1134
  }
898
1135
  };
899
1136
  PrintWebhookSubscriptions = async (subscriptionIds, name) => {
900
1137
  const { currentEnv, log, messages } = this;
901
- if (!this.contensis)
902
- await this.ConnectContensis();
903
- if (this.contensis) {
904
- const [webhooksErr, webhooks] = await this.contensis.subscriptions.webhooks.GetSubscriptions();
905
- const filteredResults = typeof name === "string" ? webhooks.filter(
1138
+ const contensis = await this.ConnectContensis();
1139
+ if (contensis) {
1140
+ const [webhooksErr, webhooks] = await contensis.subscriptions.webhooks.GetSubscriptions();
1141
+ const filteredResults = typeof name === "string" ? webhooks == null ? void 0 : webhooks.filter(
906
1142
  (w) => {
907
1143
  var _a;
908
1144
  return (_a = w.name) == null ? void 0 : _a.toLowerCase().includes(name.toLowerCase());
909
1145
  }
910
- ) : Array.isArray(subscriptionIds) ? webhooks.filter((w) => subscriptionIds == null ? void 0 : subscriptionIds.some((id) => id === w.id)) : webhooks;
1146
+ ) : Array.isArray(subscriptionIds) ? webhooks == null ? void 0 : webhooks.filter((w) => subscriptionIds == null ? void 0 : subscriptionIds.some((id) => id === w.id)) : webhooks;
911
1147
  if (Array.isArray(filteredResults)) {
912
1148
  this.HandleFormattingAndOutput(filteredResults, () => {
913
1149
  log.success(messages.webhooks.list(currentEnv));
@@ -935,14 +1171,13 @@ Exiting contensis-cli with exit code: ${exitCode}
935
1171
  }
936
1172
  };
937
1173
  PrintBlocks = async () => {
938
- const { currentEnv, log, messages } = this;
939
- if (!this.contensis)
940
- await this.ConnectContensis();
941
- if (this.contensis) {
942
- const [err, blocks] = await this.contensis.blocks.GetBlocks();
1174
+ const { currentEnv, env, log, messages } = this;
1175
+ const contensis = await this.ConnectContensis();
1176
+ if (contensis) {
1177
+ const [err, blocks] = await contensis.blocks.GetBlocks();
943
1178
  if (Array.isArray(blocks)) {
944
1179
  this.HandleFormattingAndOutput(blocks, () => {
945
- log.success(messages.blocks.list(currentEnv));
1180
+ log.success(messages.blocks.list(currentEnv, env.currentProject));
946
1181
  for (const {
947
1182
  id,
948
1183
  description,
@@ -969,10 +1204,9 @@ Exiting contensis-cli with exit code: ${exitCode}
969
1204
  };
970
1205
  PrintBlockVersions = async (blockId, branch, version) => {
971
1206
  const { currentEnv, env, log, messages } = this;
972
- if (!this.contensis)
973
- await this.ConnectContensis();
974
- if (this.contensis) {
975
- const [err, blocks] = await this.contensis.blocks.GetBlockVersions(
1207
+ const contensis = await this.ConnectContensis();
1208
+ if (contensis) {
1209
+ const [err, blocks] = await contensis.blocks.GetBlockVersions(
976
1210
  blockId,
977
1211
  branch,
978
1212
  version
@@ -980,7 +1214,7 @@ Exiting contensis-cli with exit code: ${exitCode}
980
1214
  if (blocks) {
981
1215
  this.HandleFormattingAndOutput(blocks, () => {
982
1216
  log.success(
983
- messages.blocks.get(`${currentEnv}:${env.currentProject}`)
1217
+ messages.blocks.get(blockId, currentEnv, env.currentProject)
984
1218
  );
985
1219
  for (const block of blocks)
986
1220
  (0, import_console.printBlockVersion)(
@@ -1003,17 +1237,18 @@ Exiting contensis-cli with exit code: ${exitCode}
1003
1237
  };
1004
1238
  PushBlock = async (block) => {
1005
1239
  const { currentEnv, env, log, messages } = this;
1006
- messages.blocks.tryPush(
1007
- block.id,
1008
- block.source.branch,
1009
- currentEnv,
1010
- env.currentProject
1240
+ log.info(
1241
+ messages.blocks.tryPush(
1242
+ block.id,
1243
+ block.source.branch,
1244
+ currentEnv,
1245
+ env.currentProject
1246
+ )
1011
1247
  );
1012
1248
  console.log((0, import_json.jsonFormatter)(block));
1013
- if (!this.contensis)
1014
- await this.ConnectContensis();
1015
- if (this.contensis) {
1016
- const [err, blockVersion] = await this.contensis.blocks.PushBlockVersion(
1249
+ const contensis = await this.ConnectContensis();
1250
+ if (contensis) {
1251
+ const [err, blockVersion] = await contensis.blocks.PushBlockVersion(
1017
1252
  block
1018
1253
  );
1019
1254
  if (!err) {
@@ -1025,7 +1260,6 @@ Exiting contensis-cli with exit code: ${exitCode}
1025
1260
  env.currentProject
1026
1261
  )
1027
1262
  );
1028
- console.log((0, import_json.jsonFormatter)(blockVersion));
1029
1263
  }
1030
1264
  if (blockVersion) {
1031
1265
  this.HandleFormattingAndOutput(blockVersion, () => {
@@ -1036,14 +1270,45 @@ Exiting contensis-cli with exit code: ${exitCode}
1036
1270
  throw new Error(
1037
1271
  messages.blocks.failedPush(block.id, currentEnv, env.currentProject)
1038
1272
  );
1273
+ } else {
1274
+ throw new Error(
1275
+ messages.blocks.failedPush(block.id, currentEnv, env.currentProject)
1276
+ );
1277
+ }
1278
+ };
1279
+ ReleaseBlock = async (blockId, version) => {
1280
+ const { currentEnv, env, log, messages } = this;
1281
+ const contensis = await this.ConnectContensis();
1282
+ if (contensis) {
1283
+ const [err, blockVersion] = await contensis.blocks.BlockAction(
1284
+ blockId,
1285
+ "release",
1286
+ version
1287
+ );
1288
+ if (blockVersion) {
1289
+ this.HandleFormattingAndOutput(blockVersion, () => {
1290
+ log.success(
1291
+ messages.blocks.released(blockId, currentEnv, env.currentProject)
1292
+ );
1293
+ (0, import_console.printBlockVersion)(this, blockVersion);
1294
+ });
1295
+ }
1296
+ if (err) {
1297
+ log.error(
1298
+ messages.blocks.failedRelease(blockId, currentEnv, env.currentProject)
1299
+ );
1300
+ log.error((0, import_json.jsonFormatter)(err));
1301
+ }
1039
1302
  }
1040
1303
  };
1041
1304
  PrintBlockLogs = async (blockId, branch, version, dataCenter) => {
1042
- const { currentEnv, log, messages } = this;
1043
- if (!this.contensis)
1044
- await this.ConnectContensis();
1045
- if (this.contensis) {
1046
- const [err, blockLogs] = await this.contensis.blocks.GetBlockLogs({
1305
+ const { currentEnv, env, log, messages } = this;
1306
+ const contensis = await this.ConnectContensis();
1307
+ if (contensis) {
1308
+ log.success(
1309
+ messages.blocks.getLogs(blockId, branch, currentEnv, env.currentProject)
1310
+ );
1311
+ const [err, blockLogs] = await contensis.blocks.GetBlockLogs({
1047
1312
  blockId,
1048
1313
  branchId: branch,
1049
1314
  version,
@@ -1051,7 +1316,6 @@ Exiting contensis-cli with exit code: ${exitCode}
1051
1316
  });
1052
1317
  if (blockLogs) {
1053
1318
  this.HandleFormattingAndOutput(blockLogs, () => {
1054
- log.success(messages.blocks.list(currentEnv));
1055
1319
  console.log(
1056
1320
  ` - ${blockId} ${branch} ${Number(version) ? `v${version}` : version} [${dataCenter}]`
1057
1321
  );
@@ -1061,13 +1325,28 @@ Exiting contensis-cli with exit code: ${exitCode}
1061
1325
  });
1062
1326
  }
1063
1327
  if (err) {
1064
- log.error(messages.blocks.noList(currentEnv));
1328
+ log.error(
1329
+ messages.blocks.failedGetLogs(blockId, currentEnv, env.currentProject)
1330
+ );
1065
1331
  log.error((0, import_json.jsonFormatter)(err));
1066
1332
  }
1067
1333
  }
1068
1334
  };
1069
1335
  HandleFormattingAndOutput = (obj, logFn) => {
1070
1336
  const { format, log, messages, output } = this;
1337
+ if (!format) {
1338
+ logFn(obj);
1339
+ } else if (format === "csv") {
1340
+ log.raw("");
1341
+ log.raw(log.infoText((0, import_csv.csvFormatter)(obj)));
1342
+ } else if (format === "xml") {
1343
+ log.raw("");
1344
+ log.raw(log.infoText((0, import_xml.xmlFormatter)(obj)));
1345
+ } else if (format === "json") {
1346
+ log.raw("");
1347
+ log.raw(log.infoText((0, import_json.jsonFormatter)(obj)));
1348
+ }
1349
+ log.raw("");
1071
1350
  if (output) {
1072
1351
  let writeString = "";
1073
1352
  if (format === "csv") {
@@ -1082,20 +1361,6 @@ Exiting contensis-cli with exit code: ${exitCode}
1082
1361
  } else {
1083
1362
  log.info(messages.app.noFileOutput());
1084
1363
  }
1085
- } else {
1086
- if (!format) {
1087
- logFn(obj);
1088
- } else if (format === "csv") {
1089
- log.raw("");
1090
- log.raw(log.infoText((0, import_csv.csvFormatter)(obj)));
1091
- } else if (format === "xml") {
1092
- log.raw("");
1093
- log.raw(log.infoText((0, import_xml.xmlFormatter)(obj)));
1094
- } else if (format === "json") {
1095
- log.raw("");
1096
- log.raw(log.infoText((0, import_json.jsonFormatter)(obj)));
1097
- }
1098
- log.raw("");
1099
1364
  }
1100
1365
  };
1101
1366
  }