@strapi/cloud-cli 0.0.0-next.d54a672641d83234230d3d80d92aaf87f60f8c75 → 0.0.0-next.d6ed4fcf2c833656b8c91a90909c1e77c79379e5

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 (50) hide show
  1. package/dist/bin.js +2 -2
  2. package/dist/bin.js.map +1 -1
  3. package/dist/index.js +522 -115
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +519 -111
  6. package/dist/index.mjs.map +1 -1
  7. package/dist/src/cloud/command.d.ts +3 -0
  8. package/dist/src/cloud/command.d.ts.map +1 -0
  9. package/dist/src/create-project/action.d.ts.map +1 -1
  10. package/dist/src/create-project/command.d.ts.map +1 -1
  11. package/dist/src/create-project/utils/project-questions.utils.d.ts +20 -0
  12. package/dist/src/create-project/utils/project-questions.utils.d.ts.map +1 -0
  13. package/dist/src/deploy-project/action.d.ts +5 -1
  14. package/dist/src/deploy-project/action.d.ts.map +1 -1
  15. package/dist/src/deploy-project/command.d.ts.map +1 -1
  16. package/dist/src/environment/command.d.ts +3 -0
  17. package/dist/src/environment/command.d.ts.map +1 -0
  18. package/dist/src/environment/link/action.d.ts +4 -0
  19. package/dist/src/environment/link/action.d.ts.map +1 -0
  20. package/dist/src/environment/link/command.d.ts +4 -0
  21. package/dist/src/environment/link/command.d.ts.map +1 -0
  22. package/dist/src/environment/link/index.d.ts +7 -0
  23. package/dist/src/environment/link/index.d.ts.map +1 -0
  24. package/dist/src/environment/list/action.d.ts +4 -0
  25. package/dist/src/environment/list/action.d.ts.map +1 -0
  26. package/dist/src/environment/list/command.d.ts +4 -0
  27. package/dist/src/environment/list/command.d.ts.map +1 -0
  28. package/dist/src/environment/list/index.d.ts +7 -0
  29. package/dist/src/environment/list/index.d.ts.map +1 -0
  30. package/dist/src/index.d.ts +2 -0
  31. package/dist/src/index.d.ts.map +1 -1
  32. package/dist/src/link/action.d.ts.map +1 -1
  33. package/dist/src/login/command.d.ts.map +1 -1
  34. package/dist/src/logout/command.d.ts.map +1 -1
  35. package/dist/src/services/cli-api.d.ts +46 -3
  36. package/dist/src/services/cli-api.d.ts.map +1 -1
  37. package/dist/src/services/strapi-info-save.d.ts +15 -2
  38. package/dist/src/services/strapi-info-save.d.ts.map +1 -1
  39. package/dist/src/services/token.d.ts.map +1 -1
  40. package/dist/src/types.d.ts +7 -1
  41. package/dist/src/types.d.ts.map +1 -1
  42. package/dist/src/utils/get-local-config.d.ts +6 -0
  43. package/dist/src/utils/get-local-config.d.ts.map +1 -0
  44. package/dist/src/utils/helpers.d.ts.map +1 -1
  45. package/dist/src/utils/pkg.d.ts.map +1 -1
  46. package/dist/src/utils/tests/compress-files.test.d.ts +2 -0
  47. package/dist/src/utils/tests/compress-files.test.d.ts.map +1 -0
  48. package/package.json +11 -10
  49. package/dist/src/create-project/utils/apply-default-name.d.ts +0 -7
  50. package/dist/src/create-project/utils/apply-default-name.d.ts.map +0 -1
package/dist/index.js CHANGED
@@ -24,6 +24,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
24
24
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
25
25
  const crypto$1 = require("crypto");
26
26
  const fse = require("fs-extra");
27
+ const inquirer = require("inquirer");
28
+ const boxen = require("boxen");
27
29
  const path = require("path");
28
30
  const chalk = require("chalk");
29
31
  const axios = require("axios");
@@ -31,10 +33,10 @@ const crypto = require("node:crypto");
31
33
  const utils = require("@strapi/utils");
32
34
  const tar = require("tar");
33
35
  const minimatch = require("minimatch");
34
- const inquirer = require("inquirer");
35
36
  const fp = require("lodash/fp");
36
37
  const os = require("os");
37
38
  const XDGAppPaths = require("xdg-app-paths");
39
+ const lodash = require("lodash");
38
40
  const jwksClient = require("jwks-rsa");
39
41
  const jwt = require("jsonwebtoken");
40
42
  const stringify = require("fast-safe-stringify");
@@ -42,12 +44,11 @@ const ora = require("ora");
42
44
  const cliProgress = require("cli-progress");
43
45
  const pkgUp = require("pkg-up");
44
46
  const yup = require("yup");
45
- const _ = require("lodash");
46
47
  const EventSource = require("eventsource");
48
+ const commander = require("commander");
47
49
  const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
48
50
  function _interopNamespace(e) {
49
- if (e && e.__esModule)
50
- return e;
51
+ if (e && e.__esModule) return e;
51
52
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
52
53
  if (e) {
53
54
  for (const k in e) {
@@ -65,12 +66,13 @@ function _interopNamespace(e) {
65
66
  }
66
67
  const crypto__default = /* @__PURE__ */ _interopDefault(crypto$1);
67
68
  const fse__namespace = /* @__PURE__ */ _interopNamespace(fse);
69
+ const inquirer__default = /* @__PURE__ */ _interopDefault(inquirer);
70
+ const boxen__default = /* @__PURE__ */ _interopDefault(boxen);
68
71
  const path__namespace = /* @__PURE__ */ _interopNamespace(path);
69
72
  const chalk__default = /* @__PURE__ */ _interopDefault(chalk);
70
73
  const axios__default = /* @__PURE__ */ _interopDefault(axios);
71
74
  const crypto__namespace = /* @__PURE__ */ _interopNamespace(crypto);
72
75
  const tar__namespace = /* @__PURE__ */ _interopNamespace(tar);
73
- const inquirer__default = /* @__PURE__ */ _interopDefault(inquirer);
74
76
  const os__default = /* @__PURE__ */ _interopDefault(os);
75
77
  const XDGAppPaths__default = /* @__PURE__ */ _interopDefault(XDGAppPaths);
76
78
  const jwksClient__default = /* @__PURE__ */ _interopDefault(jwksClient);
@@ -80,7 +82,6 @@ const ora__default = /* @__PURE__ */ _interopDefault(ora);
80
82
  const cliProgress__namespace = /* @__PURE__ */ _interopNamespace(cliProgress);
81
83
  const pkgUp__default = /* @__PURE__ */ _interopDefault(pkgUp);
82
84
  const yup__namespace = /* @__PURE__ */ _interopNamespace(yup);
83
- const ___default = /* @__PURE__ */ _interopDefault(_);
84
85
  const EventSource__default = /* @__PURE__ */ _interopDefault(EventSource);
85
86
  const apiConfig = {
86
87
  apiBaseUrl: utils.env("STRAPI_CLI_CLOUD_API", "https://cloud-cli-api.strapi.io"),
@@ -138,8 +139,7 @@ const getFiles = async (dirPath, ignorePatterns = [], subfolder = "") => {
138
139
  const readGitignore = async (folderPath) => {
139
140
  const gitignorePath = path__namespace.resolve(folderPath, ".gitignore");
140
141
  const pathExist = await fse__namespace.pathExists(gitignorePath);
141
- if (!pathExist)
142
- return [];
142
+ if (!pathExist) return [];
143
143
  const gitignoreContent = await fse__namespace.readFile(gitignorePath, "utf8");
144
144
  return gitignoreContent.split(/\r?\n/).filter((line) => Boolean(line.trim()) && !line.startsWith("#"));
145
145
  };
@@ -178,7 +178,7 @@ async function getConfigPath() {
178
178
  }
179
179
  return configPath;
180
180
  }
181
- async function getLocalConfig() {
181
+ async function getLocalConfig$1() {
182
182
  const configPath = await getConfigPath();
183
183
  const configFilePath = path__namespace.default.join(configPath, CONFIG_FILENAME);
184
184
  await fse__namespace.default.ensureFile(configFilePath);
@@ -194,7 +194,7 @@ async function saveLocalConfig(data) {
194
194
  await fse__namespace.default.writeJson(configFilePath, data, { encoding: "utf8", spaces: 2, mode: 384 });
195
195
  }
196
196
  const name = "@strapi/cloud-cli";
197
- const version = "4.25.5";
197
+ const version = "5.4.2";
198
198
  const description = "Commands to interact with the Strapi Cloud";
199
199
  const keywords = [
200
200
  "strapi",
@@ -239,14 +239,15 @@ const scripts = {
239
239
  watch: "pack-up watch"
240
240
  };
241
241
  const dependencies = {
242
- "@strapi/utils": "4.25.5",
243
- axios: "1.6.0",
242
+ "@strapi/utils": "5.4.2",
243
+ axios: "1.7.4",
244
+ boxen: "5.1.2",
244
245
  chalk: "4.1.2",
245
246
  "cli-progress": "3.12.0",
246
247
  commander: "8.3.0",
247
248
  eventsource: "2.0.2",
248
249
  "fast-safe-stringify": "2.1.1",
249
- "fs-extra": "10.0.0",
250
+ "fs-extra": "11.2.0",
250
251
  inquirer: "8.2.5",
251
252
  jsonwebtoken: "9.0.0",
252
253
  "jwks-rsa": "3.1.0",
@@ -255,22 +256,23 @@ const dependencies = {
255
256
  open: "8.4.0",
256
257
  ora: "5.4.1",
257
258
  "pkg-up": "3.1.0",
258
- tar: "6.1.13",
259
+ tar: "6.2.1",
259
260
  "xdg-app-paths": "8.3.0",
260
261
  yup: "0.32.9"
261
262
  };
262
263
  const devDependencies = {
263
- "@strapi/pack-up": "4.23.0",
264
+ "@strapi/pack-up": "5.0.2",
264
265
  "@types/cli-progress": "3.11.5",
265
266
  "@types/eventsource": "1.1.15",
266
267
  "@types/lodash": "^4.14.191",
267
- "eslint-config-custom": "4.25.5",
268
- tsconfig: "4.25.5"
268
+ "eslint-config-custom": "5.4.2",
269
+ tsconfig: "5.4.2"
269
270
  };
270
271
  const engines = {
271
- node: ">=18.0.0 <=20.x.x",
272
+ node: ">=18.0.0 <=22.x.x",
272
273
  npm: ">=6.0.0"
273
274
  };
275
+ const gitHead = "7d785703f52464577d077c4618cbe68b44f8a9cd";
274
276
  const packageJson = {
275
277
  name,
276
278
  version,
@@ -291,11 +293,12 @@ const packageJson = {
291
293
  scripts,
292
294
  dependencies,
293
295
  devDependencies,
294
- engines
296
+ engines,
297
+ gitHead
295
298
  };
296
299
  const VERSION = "v1";
297
300
  async function cloudApiFactory({ logger }, token) {
298
- const localConfig = await getLocalConfig();
301
+ const localConfig = await getLocalConfig$1();
299
302
  const customHeaders = {
300
303
  "x-device-id": localConfig.deviceId,
301
304
  "x-app-version": packageJson.version,
@@ -318,7 +321,7 @@ async function cloudApiFactory({ logger }, token) {
318
321
  deploy({ filePath, project }, { onUploadProgress }) {
319
322
  return axiosCloudAPI.post(
320
323
  `/deploy/${project.name}`,
321
- { file: fse__namespace.default.createReadStream(filePath) },
324
+ { file: fse__namespace.default.createReadStream(filePath), targetEnvironment: project.targetEnvironment },
322
325
  {
323
326
  headers: {
324
327
  "Content-Type": "multipart/form-data"
@@ -377,7 +380,7 @@ async function cloudApiFactory({ logger }, token) {
377
380
  },
378
381
  async listLinkProjects() {
379
382
  try {
380
- const response = await axiosCloudAPI.get("/projects/linkable");
383
+ const response = await axiosCloudAPI.get("/projects-linkable");
381
384
  if (response.status !== 200) {
382
385
  throw new Error("Error fetching cloud projects from the server.");
383
386
  }
@@ -389,6 +392,48 @@ async function cloudApiFactory({ logger }, token) {
389
392
  throw error;
390
393
  }
391
394
  },
395
+ async listEnvironments({ name: name2 }) {
396
+ try {
397
+ const response = await axiosCloudAPI.get(`/projects/${name2}/environments`);
398
+ if (response.status !== 200) {
399
+ throw new Error("Error fetching cloud environments from the server.");
400
+ }
401
+ return response;
402
+ } catch (error) {
403
+ logger.debug(
404
+ "🥲 Oops! Couldn't retrieve your project's environments from the server. Please try again."
405
+ );
406
+ throw error;
407
+ }
408
+ },
409
+ async listLinkEnvironments({ name: name2 }) {
410
+ try {
411
+ const response = await axiosCloudAPI.get(`/projects/${name2}/environments-linkable`);
412
+ if (response.status !== 200) {
413
+ throw new Error("Error fetching cloud environments from the server.");
414
+ }
415
+ return response;
416
+ } catch (error) {
417
+ logger.debug(
418
+ "🥲 Oops! Couldn't retrieve your project's environments from the server. Please try again."
419
+ );
420
+ throw error;
421
+ }
422
+ },
423
+ async getProject({ name: name2 }) {
424
+ try {
425
+ const response = await axiosCloudAPI.get(`/projects/${name2}`);
426
+ if (response.status !== 200) {
427
+ throw new Error("Error fetching project's details.");
428
+ }
429
+ return response;
430
+ } catch (error) {
431
+ logger.debug(
432
+ "🥲 Oops! There was a problem retrieving your project's details. Please try again."
433
+ );
434
+ throw error;
435
+ }
436
+ },
392
437
  track(event, payload = {}) {
393
438
  return axiosCloudAPI.post("/track", {
394
439
  event,
@@ -398,26 +443,43 @@ async function cloudApiFactory({ logger }, token) {
398
443
  };
399
444
  }
400
445
  const LOCAL_SAVE_FILENAME = ".strapi-cloud.json";
446
+ const getFilePath = (directoryPath) => path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
401
447
  async function save(data, { directoryPath } = {}) {
402
- const alreadyInFileData = await retrieve({ directoryPath });
403
- const storedData = { ...alreadyInFileData, ...data };
404
- const pathToFile = path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
448
+ const pathToFile = getFilePath(directoryPath);
405
449
  await fse__namespace.default.ensureDir(path__namespace.default.dirname(pathToFile));
406
- await fse__namespace.default.writeJson(pathToFile, storedData, { encoding: "utf8" });
450
+ await fse__namespace.default.writeJson(pathToFile, data, { encoding: "utf8" });
407
451
  }
408
452
  async function retrieve({
409
453
  directoryPath
410
454
  } = {}) {
411
- const pathToFile = path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
455
+ const pathToFile = getFilePath(directoryPath);
412
456
  const pathExists = await fse__namespace.default.pathExists(pathToFile);
413
457
  if (!pathExists) {
414
458
  return {};
415
459
  }
416
460
  return fse__namespace.default.readJSON(pathToFile, { encoding: "utf8" });
417
461
  }
462
+ async function patch(patchData, { directoryPath } = {}) {
463
+ const pathToFile = getFilePath(directoryPath);
464
+ const existingData = await retrieve({ directoryPath });
465
+ if (!existingData) {
466
+ throw new Error("No configuration data found to patch.");
467
+ }
468
+ const newData = lodash.merge(existingData, patchData);
469
+ await fse__namespace.default.writeJson(pathToFile, newData, { encoding: "utf8" });
470
+ }
471
+ async function deleteConfig({ directoryPath } = {}) {
472
+ const pathToFile = getFilePath(directoryPath);
473
+ const pathExists = await fse__namespace.default.pathExists(pathToFile);
474
+ if (pathExists) {
475
+ await fse__namespace.default.remove(pathToFile);
476
+ }
477
+ }
418
478
  const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
419
479
  __proto__: null,
420
480
  LOCAL_SAVE_FILENAME,
481
+ deleteConfig,
482
+ patch,
421
483
  retrieve,
422
484
  save
423
485
  }, Symbol.toStringTag, { value: "Module" }));
@@ -425,7 +487,7 @@ let cliConfig;
425
487
  async function tokenServiceFactory({ logger }) {
426
488
  const cloudApiService = await cloudApiFactory({ logger });
427
489
  async function saveToken(str) {
428
- const appConfig = await getLocalConfig();
490
+ const appConfig = await getLocalConfig$1();
429
491
  if (!appConfig) {
430
492
  logger.error("There was a problem saving your token. Please try again.");
431
493
  return;
@@ -439,7 +501,7 @@ async function tokenServiceFactory({ logger }) {
439
501
  }
440
502
  }
441
503
  async function retrieveToken() {
442
- const appConfig = await getLocalConfig();
504
+ const appConfig = await getLocalConfig$1();
443
505
  if (appConfig.token) {
444
506
  if (await isTokenValid(appConfig.token)) {
445
507
  return appConfig.token;
@@ -501,7 +563,7 @@ async function tokenServiceFactory({ logger }) {
501
563
  }
502
564
  }
503
565
  async function eraseToken() {
504
- const appConfig = await getLocalConfig();
566
+ const appConfig = await getLocalConfig$1();
505
567
  if (!appConfig) {
506
568
  return;
507
569
  }
@@ -522,8 +584,7 @@ async function tokenServiceFactory({ logger }) {
522
584
  logger.log(
523
585
  token ? "Oops! Your token seems expired or invalid. Please login again." : "We couldn't find a valid token. You need to be logged in to use this feature."
524
586
  );
525
- if (!await loginAction2(ctx))
526
- return null;
587
+ if (!await loginAction2(ctx)) return null;
527
588
  token = await retrieveToken();
528
589
  }
529
590
  return token;
@@ -662,21 +723,24 @@ yup__namespace.object({
662
723
  name: yup__namespace.string().required(),
663
724
  exports: yup__namespace.lazy(
664
725
  (value) => yup__namespace.object(
665
- typeof value === "object" ? Object.entries(value).reduce((acc, [key, value2]) => {
666
- if (typeof value2 === "object") {
667
- acc[key] = yup__namespace.object({
668
- types: yup__namespace.string().optional(),
669
- source: yup__namespace.string().required(),
670
- module: yup__namespace.string().optional(),
671
- import: yup__namespace.string().required(),
672
- require: yup__namespace.string().required(),
673
- default: yup__namespace.string().required()
674
- }).noUnknown(true);
675
- } else {
676
- acc[key] = yup__namespace.string().matches(/^\.\/.*\.json$/).required();
677
- }
678
- return acc;
679
- }, {}) : void 0
726
+ typeof value === "object" ? Object.entries(value).reduce(
727
+ (acc, [key, value2]) => {
728
+ if (typeof value2 === "object") {
729
+ acc[key] = yup__namespace.object({
730
+ types: yup__namespace.string().optional(),
731
+ source: yup__namespace.string().required(),
732
+ module: yup__namespace.string().optional(),
733
+ import: yup__namespace.string().required(),
734
+ require: yup__namespace.string().required(),
735
+ default: yup__namespace.string().required()
736
+ }).noUnknown(true);
737
+ } else {
738
+ acc[key] = yup__namespace.string().matches(/^\.\/.*\.json$/).required();
739
+ }
740
+ return acc;
741
+ },
742
+ {}
743
+ ) : void 0
680
744
  ).optional()
681
745
  )
682
746
  });
@@ -698,18 +762,6 @@ async function getProjectNameFromPackageJson(ctx) {
698
762
  return "my-strapi-project";
699
763
  }
700
764
  }
701
- function applyDefaultName(newDefaultName, questions, defaultValues) {
702
- const newDefaultValues = ___default.default.cloneDeep(defaultValues);
703
- newDefaultValues.name = newDefaultName;
704
- const newQuestions = questions.map((question) => {
705
- const questionCopy = ___default.default.cloneDeep(question);
706
- if (questionCopy.name === "name") {
707
- questionCopy.default = newDefaultName;
708
- }
709
- return questionCopy;
710
- });
711
- return { newQuestions, newDefaultValues };
712
- }
713
765
  const trackEvent = async (ctx, cloudApiService, eventName, eventData) => {
714
766
  try {
715
767
  await cloudApiService.track(eventName, eventData);
@@ -873,6 +925,45 @@ async function loginAction(ctx) {
873
925
  await authenticate();
874
926
  return isAuthenticated;
875
927
  }
928
+ function questionDefaultValuesMapper(questionsMap) {
929
+ return (questions) => {
930
+ return questions.map((question) => {
931
+ const questionName = question.name;
932
+ if (questionName in questionsMap) {
933
+ const questionDefault = questionsMap[questionName];
934
+ if (typeof questionDefault === "function") {
935
+ return {
936
+ ...question,
937
+ default: questionDefault(question)
938
+ };
939
+ }
940
+ return {
941
+ ...question,
942
+ default: questionDefault
943
+ };
944
+ }
945
+ return question;
946
+ });
947
+ };
948
+ }
949
+ function getDefaultsFromQuestions(questions) {
950
+ return questions.reduce((acc, question) => {
951
+ if (question.default && question.name) {
952
+ return { ...acc, [question.name]: question.default };
953
+ }
954
+ return acc;
955
+ }, {});
956
+ }
957
+ function getProjectNodeVersionDefault(question) {
958
+ const currentNodeVersion = process.versions.node.split(".")[0];
959
+ if (question.type === "list" && Array.isArray(question.choices)) {
960
+ const choice = question.choices.find((choice2) => choice2.value === currentNodeVersion);
961
+ if (choice) {
962
+ return choice.value;
963
+ }
964
+ }
965
+ return question.default;
966
+ }
876
967
  async function handleError(ctx, error) {
877
968
  const { logger } = ctx;
878
969
  logger.debug(error);
@@ -917,7 +1008,7 @@ async function createProject$1(ctx, cloudApi, projectInput) {
917
1008
  throw e;
918
1009
  }
919
1010
  }
920
- const action$4 = async (ctx) => {
1011
+ const action$6 = async (ctx) => {
921
1012
  const { logger } = ctx;
922
1013
  const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
923
1014
  const token = await getValidToken(ctx, promptLogin);
@@ -926,11 +1017,16 @@ const action$4 = async (ctx) => {
926
1017
  }
927
1018
  const cloudApi = await cloudApiFactory(ctx, token);
928
1019
  const { data: config } = await cloudApi.config();
929
- const { newQuestions: questions, newDefaultValues: defaultValues } = applyDefaultName(
930
- await getProjectNameFromPackageJson(ctx),
931
- config.projectCreation.questions,
932
- config.projectCreation.defaults
933
- );
1020
+ const projectName = await getProjectNameFromPackageJson(ctx);
1021
+ const defaultAnswersMapper = questionDefaultValuesMapper({
1022
+ name: projectName,
1023
+ nodeVersion: getProjectNodeVersionDefault
1024
+ });
1025
+ const questions = defaultAnswersMapper(config.projectCreation.questions);
1026
+ const defaultValues = {
1027
+ ...config.projectCreation.defaults,
1028
+ ...getDefaultsFromQuestions(questions)
1029
+ };
934
1030
  const projectAnswersDefaulted = fp.defaults(defaultValues);
935
1031
  const projectAnswers = await inquirer__default.default.prompt(questions);
936
1032
  const projectInput = projectAnswersDefaulted(projectAnswers);
@@ -1041,6 +1137,29 @@ const buildLogsServiceFactory = ({ logger }) => {
1041
1137
  });
1042
1138
  };
1043
1139
  };
1140
+ const boxenOptions = {
1141
+ padding: 1,
1142
+ margin: 1,
1143
+ align: "center",
1144
+ borderColor: "yellow",
1145
+ borderStyle: "round"
1146
+ };
1147
+ const QUIT_OPTION$2 = "Quit";
1148
+ async function promptForEnvironment(environments) {
1149
+ const choices = environments.map((env) => ({ name: env, value: env }));
1150
+ const { selectedEnvironment } = await inquirer__default.default.prompt([
1151
+ {
1152
+ type: "list",
1153
+ name: "selectedEnvironment",
1154
+ message: "Select the environment to deploy:",
1155
+ choices: [...choices, { name: chalk__default.default.grey(`(${QUIT_OPTION$2})`), value: null }]
1156
+ }
1157
+ ]);
1158
+ if (selectedEnvironment === null) {
1159
+ process.exit(1);
1160
+ }
1161
+ return selectedEnvironment;
1162
+ }
1044
1163
  async function upload(ctx, project, token, maxProjectFileSize) {
1045
1164
  const cloudApi = await cloudApiFactory(ctx, token);
1046
1165
  try {
@@ -1107,17 +1226,7 @@ async function upload(ctx, project, token, maxProjectFileSize) {
1107
1226
  return data.build_id;
1108
1227
  } catch (e) {
1109
1228
  progressBar.stop();
1110
- if (e instanceof axios.AxiosError && e.response?.data) {
1111
- if (e.response.status === 404) {
1112
- ctx.logger.error(
1113
- `The project does not exist. Remove the ${LOCAL_SAVE_FILENAME} file and try again.`
1114
- );
1115
- } else {
1116
- ctx.logger.error(e.response.data);
1117
- }
1118
- } else {
1119
- ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1120
- }
1229
+ ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1121
1230
  ctx.logger.debug(e);
1122
1231
  } finally {
1123
1232
  await fse__namespace.default.remove(tarFilePath);
@@ -1133,7 +1242,7 @@ async function getProject(ctx) {
1133
1242
  const { project } = await retrieve();
1134
1243
  if (!project) {
1135
1244
  try {
1136
- return await action$4(ctx);
1245
+ return await action$6(ctx);
1137
1246
  } catch (e) {
1138
1247
  ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1139
1248
  ctx.logger.debug(e);
@@ -1154,7 +1263,33 @@ async function getConfig({
1154
1263
  return null;
1155
1264
  }
1156
1265
  }
1157
- const action$3 = async (ctx) => {
1266
+ function validateEnvironment(ctx, environment, environments) {
1267
+ if (!environments.includes(environment)) {
1268
+ ctx.logger.error(`Environment ${environment} does not exist.`);
1269
+ process.exit(1);
1270
+ }
1271
+ }
1272
+ async function getTargetEnvironment(ctx, opts, project, environments) {
1273
+ if (opts.env) {
1274
+ validateEnvironment(ctx, opts.env, environments);
1275
+ return opts.env;
1276
+ }
1277
+ if (project.targetEnvironment) {
1278
+ return project.targetEnvironment;
1279
+ }
1280
+ if (environments.length > 1) {
1281
+ return promptForEnvironment(environments);
1282
+ }
1283
+ return environments[0];
1284
+ }
1285
+ function hasPendingOrLiveDeployment(environments, targetEnvironment) {
1286
+ const environment = environments.find((env) => env.name === targetEnvironment);
1287
+ if (!environment) {
1288
+ throw new Error(`Environment details ${targetEnvironment} not found.`);
1289
+ }
1290
+ return environment.hasPendingDeployment || environment.hasLiveDeployment || false;
1291
+ }
1292
+ const action$5 = async (ctx, opts) => {
1158
1293
  const { getValidToken } = await tokenServiceFactory(ctx);
1159
1294
  const token = await getValidToken(ctx, promptLogin);
1160
1295
  if (!token) {
@@ -1164,7 +1299,45 @@ const action$3 = async (ctx) => {
1164
1299
  if (!project) {
1165
1300
  return;
1166
1301
  }
1167
- const cloudApiService = await cloudApiFactory(ctx);
1302
+ const cloudApiService = await cloudApiFactory(ctx, token);
1303
+ let projectData;
1304
+ let environments;
1305
+ let environmentsDetails;
1306
+ try {
1307
+ const {
1308
+ data: { data, metadata }
1309
+ } = await cloudApiService.getProject({ name: project.name });
1310
+ projectData = data;
1311
+ environments = projectData.environments;
1312
+ environmentsDetails = projectData.environmentsDetails;
1313
+ const isProjectSuspended = projectData.suspendedAt;
1314
+ if (isProjectSuspended) {
1315
+ ctx.logger.log(
1316
+ "\n Oops! This project has been suspended. \n\n Please reactivate it from the dashboard to continue deploying: "
1317
+ );
1318
+ ctx.logger.log(chalk__default.default.underline(`${metadata.dashboardUrls.project}`));
1319
+ return;
1320
+ }
1321
+ } catch (e) {
1322
+ if (e instanceof axios.AxiosError && e.response?.data) {
1323
+ if (e.response.status === 404) {
1324
+ ctx.logger.warn(
1325
+ `The project associated with this folder does not exist in Strapi Cloud.
1326
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1327
+ "link"
1328
+ )} command before deploying.`
1329
+ );
1330
+ } else {
1331
+ ctx.logger.error(e.response.data);
1332
+ }
1333
+ } else {
1334
+ ctx.logger.error(
1335
+ "An error occurred while retrieving the project's information. Please try again later."
1336
+ );
1337
+ }
1338
+ ctx.logger.debug(e);
1339
+ return;
1340
+ }
1168
1341
  await trackEvent(ctx, cloudApiService, "willDeployWithCLI", {
1169
1342
  projectInternalName: project.name
1170
1343
  });
@@ -1173,7 +1346,7 @@ const action$3 = async (ctx) => {
1173
1346
  const cliConfig2 = await getConfig({ ctx, cloudApiService });
1174
1347
  if (!cliConfig2) {
1175
1348
  ctx.logger.error(
1176
- "An error occurred while retrieving data from Strapi Cloud. Please try check your network or again later."
1349
+ "An error occurred while retrieving data from Strapi Cloud. Please check your network or try again later."
1177
1350
  );
1178
1351
  return;
1179
1352
  }
@@ -1184,11 +1357,34 @@ const action$3 = async (ctx) => {
1184
1357
  );
1185
1358
  maxSize = 1e8;
1186
1359
  }
1360
+ project.targetEnvironment = await getTargetEnvironment(ctx, opts, project, environments);
1361
+ if (!opts.force) {
1362
+ const shouldDisplayWarning = hasPendingOrLiveDeployment(
1363
+ environmentsDetails,
1364
+ project.targetEnvironment
1365
+ );
1366
+ if (shouldDisplayWarning) {
1367
+ ctx.logger.log(boxen__default.default(cliConfig2.projectDeployment.confirmationText, boxenOptions));
1368
+ const { confirm } = await inquirer__default.default.prompt([
1369
+ {
1370
+ type: "confirm",
1371
+ name: "confirm",
1372
+ message: `Do you want to proceed with deployment to ${chalk__default.default.cyan(projectData.displayName)} on ${chalk__default.default.cyan(project.targetEnvironment)} environment?`
1373
+ }
1374
+ ]);
1375
+ if (!confirm) {
1376
+ process.exit(1);
1377
+ }
1378
+ }
1379
+ }
1187
1380
  const buildId = await upload(ctx, project, token, maxSize);
1188
1381
  if (!buildId) {
1189
1382
  return;
1190
1383
  }
1191
1384
  try {
1385
+ ctx.logger.log(
1386
+ `🚀 Deploying project to ${chalk__default.default.cyan(project.targetEnvironment ?? `production`)} environment...`
1387
+ );
1192
1388
  notificationService(`${apiConfig.apiBaseUrl}/notifications`, token, cliConfig2);
1193
1389
  await buildLogsService(`${apiConfig.apiBaseUrl}/v1/logs/${buildId}`, token, cliConfig2);
1194
1390
  ctx.logger.log(
@@ -1233,17 +1429,16 @@ const runAction = (name2, action2) => (...args) => {
1233
1429
  process.exit(1);
1234
1430
  });
1235
1431
  };
1236
- const command$5 = ({ command: command2, ctx }) => {
1237
- command2.command("cloud:deploy").alias("deploy").description("Deploy a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("deploy", action$3)(ctx));
1432
+ const command$7 = ({ ctx }) => {
1433
+ return commander.createCommand("cloud:deploy").alias("deploy").description("Deploy a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").option("-f, --force", "Skip confirmation to deploy").option("-e, --env <name>", "Specify the environment to deploy").action((opts) => runAction("deploy", action$5)(ctx, opts));
1238
1434
  };
1239
1435
  const deployProject = {
1240
1436
  name: "deploy-project",
1241
1437
  description: "Deploy a Strapi Cloud project",
1242
- action: action$3,
1243
- command: command$5
1438
+ action: action$5,
1439
+ command: command$7
1244
1440
  };
1245
- const QUIT_OPTION = "Quit";
1246
- async function getExistingConfig(ctx) {
1441
+ async function getLocalConfig(ctx) {
1247
1442
  try {
1248
1443
  return await retrieve();
1249
1444
  } catch (e) {
@@ -1252,6 +1447,21 @@ async function getExistingConfig(ctx) {
1252
1447
  return null;
1253
1448
  }
1254
1449
  }
1450
+ async function getLocalProject(ctx) {
1451
+ const localConfig = await getLocalConfig(ctx);
1452
+ if (!localConfig || !localConfig.project) {
1453
+ ctx.logger.warn(
1454
+ `
1455
+ We couldn't find a valid local project config.
1456
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1457
+ "link"
1458
+ )} command.`
1459
+ );
1460
+ process.exit(1);
1461
+ }
1462
+ return localConfig.project;
1463
+ }
1464
+ const QUIT_OPTION$1 = "Quit";
1255
1465
  async function promptForRelink(ctx, cloudApiService, existingConfig) {
1256
1466
  if (existingConfig && existingConfig.project) {
1257
1467
  const { shouldRelink } = await inquirer__default.default.prompt([
@@ -1281,7 +1491,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1281
1491
  } = await cloudApiService.listLinkProjects();
1282
1492
  spinner.succeed();
1283
1493
  if (!Array.isArray(projectList)) {
1284
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1494
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1285
1495
  return null;
1286
1496
  }
1287
1497
  const projects = projectList.filter(
@@ -1293,7 +1503,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1293
1503
  };
1294
1504
  });
1295
1505
  if (projects.length === 0) {
1296
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1506
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1297
1507
  return null;
1298
1508
  }
1299
1509
  return projects;
@@ -1311,7 +1521,7 @@ async function getUserSelection(ctx, projects) {
1311
1521
  type: "list",
1312
1522
  name: "linkProject",
1313
1523
  message: "Which project do you want to link?",
1314
- choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION})`), value: null }]
1524
+ choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION$1})`), value: null }]
1315
1525
  }
1316
1526
  ]);
1317
1527
  if (!answer.linkProject) {
@@ -1324,7 +1534,7 @@ async function getUserSelection(ctx, projects) {
1324
1534
  return null;
1325
1535
  }
1326
1536
  }
1327
- const action$2 = async (ctx) => {
1537
+ const action$4 = async (ctx) => {
1328
1538
  const { getValidToken } = await tokenServiceFactory(ctx);
1329
1539
  const token = await getValidToken(ctx, promptLogin);
1330
1540
  const { logger } = ctx;
@@ -1332,7 +1542,7 @@ const action$2 = async (ctx) => {
1332
1542
  return;
1333
1543
  }
1334
1544
  const cloudApiService = await cloudApiFactory(ctx, token);
1335
- const existingConfig = await getExistingConfig(ctx);
1545
+ const existingConfig = await getLocalConfig(ctx);
1336
1546
  const shouldRelink = await promptForRelink(ctx, cloudApiService, existingConfig);
1337
1547
  if (!shouldRelink) {
1338
1548
  return;
@@ -1367,7 +1577,9 @@ const action$2 = async (ctx) => {
1367
1577
  return;
1368
1578
  }
1369
1579
  await save({ project: answer.linkProject });
1370
- logger.log(`Project ${chalk__default.default.cyan(answer.linkProject.displayName)} linked successfully.`);
1580
+ logger.log(
1581
+ ` You have successfully linked your project to ${chalk__default.default.cyan(answer.linkProject.displayName)}. You are now able to deploy your project.`
1582
+ );
1371
1583
  await trackEvent(ctx, cloudApiService, "didLinkProject", {
1372
1584
  projectInternalName: answer.linkProject
1373
1585
  });
@@ -1379,17 +1591,17 @@ const action$2 = async (ctx) => {
1379
1591
  });
1380
1592
  }
1381
1593
  };
1382
- const command$4 = ({ command: command2, ctx }) => {
1383
- command2.command("cloud:link").alias("link").description("Link a local directory to a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("link", action$2)(ctx));
1594
+ const command$6 = ({ command: command2, ctx }) => {
1595
+ command2.command("cloud:link").alias("link").description("Link a local directory to a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("link", action$4)(ctx));
1384
1596
  };
1385
1597
  const link = {
1386
1598
  name: "link-project",
1387
1599
  description: "Link a local directory to a Strapi Cloud project",
1388
- action: action$2,
1389
- command: command$4
1600
+ action: action$4,
1601
+ command: command$6
1390
1602
  };
1391
- const command$3 = ({ command: command2, ctx }) => {
1392
- command2.command("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1603
+ const command$5 = ({ ctx }) => {
1604
+ return commander.createCommand("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1393
1605
  "after",
1394
1606
  "\nAfter running this command, you will be prompted to enter your authentication information."
1395
1607
  ).option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("login", loginAction)(ctx));
@@ -1398,10 +1610,10 @@ const login = {
1398
1610
  name: "login",
1399
1611
  description: "Strapi Cloud Login",
1400
1612
  action: loginAction,
1401
- command: command$3
1613
+ command: command$5
1402
1614
  };
1403
1615
  const openModule = import("open");
1404
- const action$1 = async (ctx) => {
1616
+ const action$3 = async (ctx) => {
1405
1617
  const { logger } = ctx;
1406
1618
  const { retrieveToken, eraseToken } = await tokenServiceFactory(ctx);
1407
1619
  const token = await retrieveToken();
@@ -1433,25 +1645,25 @@ const action$1 = async (ctx) => {
1433
1645
  }
1434
1646
  await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1435
1647
  };
1436
- const command$2 = ({ command: command2, ctx }) => {
1437
- command2.command("cloud:logout").alias("logout").description("Strapi Cloud Logout").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("logout", action$1)(ctx));
1648
+ const command$4 = ({ ctx }) => {
1649
+ return commander.createCommand("cloud:logout").alias("logout").description("Strapi Cloud Logout").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("logout", action$3)(ctx));
1438
1650
  };
1439
1651
  const logout = {
1440
1652
  name: "logout",
1441
1653
  description: "Strapi Cloud Logout",
1442
- action: action$1,
1443
- command: command$2
1654
+ action: action$3,
1655
+ command: command$4
1444
1656
  };
1445
- const command$1 = ({ command: command2, ctx }) => {
1446
- command2.command("cloud:create-project").description("Create a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("cloud:create-project", action$4)(ctx));
1657
+ const command$3 = ({ ctx }) => {
1658
+ return commander.createCommand("cloud:create-project").description("Create a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("cloud:create-project", action$6)(ctx));
1447
1659
  };
1448
1660
  const createProject = {
1449
1661
  name: "create-project",
1450
1662
  description: "Create a new project",
1451
- action: action$4,
1452
- command: command$1
1663
+ action: action$6,
1664
+ command: command$3
1453
1665
  };
1454
- const action = async (ctx) => {
1666
+ const action$2 = async (ctx) => {
1455
1667
  const { getValidToken } = await tokenServiceFactory(ctx);
1456
1668
  const token = await getValidToken(ctx, promptLogin);
1457
1669
  const { logger } = ctx;
@@ -1471,12 +1683,194 @@ const action = async (ctx) => {
1471
1683
  spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1472
1684
  }
1473
1685
  };
1474
- const command = ({ command: command2, ctx }) => {
1475
- command2.command("cloud:projects").alias("projects").description("List Strapi Cloud projects").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("projects", action)(ctx));
1686
+ const command$2 = ({ command: command2, ctx }) => {
1687
+ command2.command("cloud:projects").alias("projects").description("List Strapi Cloud projects").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("projects", action$2)(ctx));
1476
1688
  };
1477
1689
  const listProjects = {
1478
1690
  name: "list-projects",
1479
1691
  description: "List Strapi Cloud projects",
1692
+ action: action$2,
1693
+ command: command$2
1694
+ };
1695
+ const action$1 = async (ctx) => {
1696
+ const { getValidToken } = await tokenServiceFactory(ctx);
1697
+ const token = await getValidToken(ctx, promptLogin);
1698
+ const { logger } = ctx;
1699
+ if (!token) {
1700
+ return;
1701
+ }
1702
+ const project = await getLocalProject(ctx);
1703
+ if (!project) {
1704
+ ctx.logger.debug(`No valid local project configuration was found.`);
1705
+ return;
1706
+ }
1707
+ const cloudApiService = await cloudApiFactory(ctx, token);
1708
+ const spinner = logger.spinner("Fetching environments...").start();
1709
+ await trackEvent(ctx, cloudApiService, "willListEnvironment", {
1710
+ projectInternalName: project.name
1711
+ });
1712
+ try {
1713
+ const {
1714
+ data: { data: environmentsList }
1715
+ } = await cloudApiService.listEnvironments({ name: project.name });
1716
+ spinner.succeed();
1717
+ logger.log(environmentsList);
1718
+ await trackEvent(ctx, cloudApiService, "didListEnvironment", {
1719
+ projectInternalName: project.name
1720
+ });
1721
+ } catch (e) {
1722
+ if (e.response && e.response.status === 404) {
1723
+ spinner.succeed();
1724
+ logger.warn(
1725
+ `
1726
+ The project associated with this folder does not exist in Strapi Cloud.
1727
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1728
+ "link"
1729
+ )} command`
1730
+ );
1731
+ } else {
1732
+ spinner.fail("An error occurred while fetching environments data from Strapi Cloud.");
1733
+ logger.debug("Failed to list environments", e);
1734
+ }
1735
+ await trackEvent(ctx, cloudApiService, "didNotListEnvironment", {
1736
+ projectInternalName: project.name
1737
+ });
1738
+ }
1739
+ };
1740
+ function defineCloudNamespace(command2, ctx) {
1741
+ const cloud = command2.command("cloud").description("Manage Strapi Cloud projects");
1742
+ cloud.command("environments").description("Alias for cloud environment list").action(() => runAction("list", action$1)(ctx));
1743
+ return cloud;
1744
+ }
1745
+ let environmentCmd = null;
1746
+ const initializeEnvironmentCommand = (command2, ctx) => {
1747
+ if (!environmentCmd) {
1748
+ const cloud = defineCloudNamespace(command2, ctx);
1749
+ environmentCmd = cloud.command("environment").description("Manage environments");
1750
+ }
1751
+ return environmentCmd;
1752
+ };
1753
+ const command$1 = ({ command: command2, ctx }) => {
1754
+ const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1755
+ environmentCmd2.command("list").description("List Strapi Cloud project environments").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("list", action$1)(ctx));
1756
+ };
1757
+ const listEnvironments = {
1758
+ name: "list-environments",
1759
+ description: "List Strapi Cloud environments",
1760
+ action: action$1,
1761
+ command: command$1
1762
+ };
1763
+ const QUIT_OPTION = "Quit";
1764
+ const action = async (ctx) => {
1765
+ const { getValidToken } = await tokenServiceFactory(ctx);
1766
+ const token = await getValidToken(ctx, promptLogin);
1767
+ const { logger } = ctx;
1768
+ if (!token) {
1769
+ return;
1770
+ }
1771
+ const project = await getLocalProject(ctx);
1772
+ if (!project) {
1773
+ logger.debug(`No valid local project configuration was found.`);
1774
+ return;
1775
+ }
1776
+ const cloudApiService = await cloudApiFactory(ctx, token);
1777
+ const environments = await getEnvironmentsList(ctx, cloudApiService, project);
1778
+ if (!environments) {
1779
+ logger.debug(`Fetching environments failed.`);
1780
+ return;
1781
+ }
1782
+ if (environments.length === 0) {
1783
+ logger.log(
1784
+ `The only available environment is already linked. You can add a new one from your project settings on the Strapi Cloud dashboard.`
1785
+ );
1786
+ return;
1787
+ }
1788
+ const answer = await promptUserForEnvironment(ctx, environments);
1789
+ if (!answer) {
1790
+ return;
1791
+ }
1792
+ await trackEvent(ctx, cloudApiService, "willLinkEnvironment", {
1793
+ projectName: project.name,
1794
+ environmentName: answer.targetEnvironment
1795
+ });
1796
+ try {
1797
+ await patch({ project: { targetEnvironment: answer.targetEnvironment } });
1798
+ } catch (e) {
1799
+ await trackEvent(ctx, cloudApiService, "didNotLinkEnvironment", {
1800
+ projectName: project.name,
1801
+ environmentName: answer.targetEnvironment
1802
+ });
1803
+ logger.debug("Failed to link environment", e);
1804
+ logger.error(
1805
+ "Failed to link the environment. If this issue persists, try re-linking your project or contact support."
1806
+ );
1807
+ process.exit(1);
1808
+ }
1809
+ logger.log(
1810
+ ` You have successfully linked your project to ${chalk__default.default.cyan(answer.targetEnvironment)}, on ${chalk__default.default.cyan(project.displayName)}. You are now able to deploy your project.`
1811
+ );
1812
+ await trackEvent(ctx, cloudApiService, "didLinkEnvironment", {
1813
+ projectName: project.name,
1814
+ environmentName: answer.targetEnvironment
1815
+ });
1816
+ };
1817
+ async function promptUserForEnvironment(ctx, environments) {
1818
+ const { logger } = ctx;
1819
+ try {
1820
+ const answer = await inquirer__default.default.prompt([
1821
+ {
1822
+ type: "list",
1823
+ name: "targetEnvironment",
1824
+ message: "Which environment do you want to link?",
1825
+ choices: [...environments, { name: chalk__default.default.grey(`(${QUIT_OPTION})`), value: null }]
1826
+ }
1827
+ ]);
1828
+ if (!answer.targetEnvironment) {
1829
+ return null;
1830
+ }
1831
+ return answer;
1832
+ } catch (e) {
1833
+ logger.debug("Failed to get user input", e);
1834
+ logger.error("An error occurred while trying to get your environment selection.");
1835
+ return null;
1836
+ }
1837
+ }
1838
+ async function getEnvironmentsList(ctx, cloudApiService, project) {
1839
+ const spinner = ctx.logger.spinner("Fetching environments...\n").start();
1840
+ try {
1841
+ const {
1842
+ data: { data: environmentsList }
1843
+ } = await cloudApiService.listLinkEnvironments({ name: project.name });
1844
+ if (!Array.isArray(environmentsList) || environmentsList.length === 0) {
1845
+ throw new Error("Environments not found in server response");
1846
+ }
1847
+ spinner.succeed();
1848
+ return environmentsList.filter(
1849
+ (environment) => environment.name !== project.targetEnvironment
1850
+ );
1851
+ } catch (e) {
1852
+ if (e.response && e.response.status === 404) {
1853
+ spinner.succeed();
1854
+ ctx.logger.warn(
1855
+ `
1856
+ The project associated with this folder does not exist in Strapi Cloud.
1857
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1858
+ "link"
1859
+ )} command.`
1860
+ );
1861
+ } else {
1862
+ spinner.fail("An error occurred while fetching environments data from Strapi Cloud.");
1863
+ ctx.logger.debug("Failed to list environments", e);
1864
+ }
1865
+ }
1866
+ }
1867
+ const command = ({ command: command2, ctx }) => {
1868
+ const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1869
+ environmentCmd2.command("link").description("Link project to a specific Strapi Cloud project environment").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("link", action)(ctx));
1870
+ };
1871
+ const linkEnvironment = {
1872
+ name: "link-environment",
1873
+ description: "Link Strapi Cloud environment to a local project",
1480
1874
  action,
1481
1875
  command
1482
1876
  };
@@ -1486,11 +1880,21 @@ const cli = {
1486
1880
  login,
1487
1881
  logout,
1488
1882
  createProject,
1489
- listProjects
1883
+ linkEnvironment,
1884
+ listProjects,
1885
+ listEnvironments
1490
1886
  };
1491
- const cloudCommands = [deployProject, link, login, logout, listProjects];
1887
+ const cloudCommands = [
1888
+ deployProject,
1889
+ link,
1890
+ login,
1891
+ logout,
1892
+ linkEnvironment,
1893
+ listProjects,
1894
+ listEnvironments
1895
+ ];
1492
1896
  async function initCloudCLIConfig() {
1493
- const localConfig = await getLocalConfig();
1897
+ const localConfig = await getLocalConfig$1();
1494
1898
  if (!localConfig.deviceId) {
1495
1899
  localConfig.deviceId = crypto__default.default.randomUUID();
1496
1900
  }
@@ -1504,7 +1908,10 @@ async function buildStrapiCloudCommands({
1504
1908
  await initCloudCLIConfig();
1505
1909
  for (const cloudCommand of cloudCommands) {
1506
1910
  try {
1507
- await cloudCommand.command({ command: command2, ctx, argv });
1911
+ const subCommand = await cloudCommand.command({ command: command2, ctx, argv });
1912
+ if (subCommand) {
1913
+ command2.addCommand(subCommand);
1914
+ }
1508
1915
  } catch (e) {
1509
1916
  console.error(`Failed to load command ${cloudCommand.name}`, e);
1510
1917
  }