@strapi/cloud-cli 0.0.0-next.6785f47fadddb80404c824be0a0732c652850b45 → 0.0.0-next.7f1333f1967e625c57ab16648c057aea08c9dddb

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 (37) hide show
  1. package/dist/index.js +336 -96
  2. package/dist/index.js.map +1 -1
  3. package/dist/index.mjs +336 -95
  4. package/dist/index.mjs.map +1 -1
  5. package/dist/src/create-project/action.d.ts.map +1 -1
  6. package/dist/src/create-project/command.d.ts.map +1 -1
  7. package/dist/src/create-project/utils/project-questions.utils.d.ts +20 -0
  8. package/dist/src/create-project/utils/project-questions.utils.d.ts.map +1 -0
  9. package/dist/src/deploy-project/action.d.ts.map +1 -1
  10. package/dist/src/deploy-project/command.d.ts.map +1 -1
  11. package/dist/src/index.d.ts +1 -0
  12. package/dist/src/index.d.ts.map +1 -1
  13. package/dist/src/link/action.d.ts +4 -0
  14. package/dist/src/link/action.d.ts.map +1 -0
  15. package/dist/src/link/command.d.ts +7 -0
  16. package/dist/src/link/command.d.ts.map +1 -0
  17. package/dist/src/link/index.d.ts +7 -0
  18. package/dist/src/link/index.d.ts.map +1 -0
  19. package/dist/src/login/action.d.ts.map +1 -1
  20. package/dist/src/login/command.d.ts.map +1 -1
  21. package/dist/src/logout/action.d.ts.map +1 -1
  22. package/dist/src/logout/command.d.ts.map +1 -1
  23. package/dist/src/services/build-logs.d.ts.map +1 -1
  24. package/dist/src/services/cli-api.d.ts +29 -6
  25. package/dist/src/services/cli-api.d.ts.map +1 -1
  26. package/dist/src/services/strapi-info-save.d.ts +1 -1
  27. package/dist/src/services/strapi-info-save.d.ts.map +1 -1
  28. package/dist/src/types.d.ts +2 -1
  29. package/dist/src/types.d.ts.map +1 -1
  30. package/dist/src/utils/analytics.d.ts +4 -0
  31. package/dist/src/utils/analytics.d.ts.map +1 -0
  32. package/dist/src/utils/pkg.d.ts.map +1 -1
  33. package/dist/src/utils/tests/compress-files.test.d.ts +2 -0
  34. package/dist/src/utils/tests/compress-files.test.d.ts.map +1 -0
  35. package/package.json +8 -8
  36. package/dist/src/create-project/utils/apply-default-name.d.ts +0 -7
  37. package/dist/src/create-project/utils/apply-default-name.d.ts.map +0 -1
package/dist/index.mjs CHANGED
@@ -20,8 +20,8 @@ import ora from "ora";
20
20
  import * as cliProgress from "cli-progress";
21
21
  import pkgUp from "pkg-up";
22
22
  import * as yup from "yup";
23
- import _ from "lodash";
24
23
  import EventSource from "eventsource";
24
+ import { createCommand } from "commander";
25
25
  const apiConfig = {
26
26
  apiBaseUrl: env("STRAPI_CLI_CLOUD_API", "https://cloud-cli-api.strapi.io"),
27
27
  dashboardBaseUrl: env("STRAPI_CLI_CLOUD_DASHBOARD", "https://cloud.strapi.io")
@@ -134,7 +134,7 @@ async function saveLocalConfig(data) {
134
134
  await fse__default.writeJson(configFilePath, data, { encoding: "utf8", spaces: 2, mode: 384 });
135
135
  }
136
136
  const name = "@strapi/cloud-cli";
137
- const version = "4.25.3";
137
+ const version = "5.0.2-beta.0";
138
138
  const description = "Commands to interact with the Strapi Cloud";
139
139
  const keywords = [
140
140
  "strapi",
@@ -179,14 +179,14 @@ const scripts = {
179
179
  watch: "pack-up watch"
180
180
  };
181
181
  const dependencies = {
182
- "@strapi/utils": "4.25.3",
183
- axios: "1.6.0",
182
+ "@strapi/utils": "workspace:*",
183
+ axios: "1.7.4",
184
184
  chalk: "4.1.2",
185
185
  "cli-progress": "3.12.0",
186
186
  commander: "8.3.0",
187
187
  eventsource: "2.0.2",
188
188
  "fast-safe-stringify": "2.1.1",
189
- "fs-extra": "10.0.0",
189
+ "fs-extra": "11.2.0",
190
190
  inquirer: "8.2.5",
191
191
  jsonwebtoken: "9.0.0",
192
192
  "jwks-rsa": "3.1.0",
@@ -195,7 +195,7 @@ const dependencies = {
195
195
  open: "8.4.0",
196
196
  ora: "5.4.1",
197
197
  "pkg-up": "3.1.0",
198
- tar: "6.1.13",
198
+ tar: "6.2.1",
199
199
  "xdg-app-paths": "8.3.0",
200
200
  yup: "0.32.9"
201
201
  };
@@ -204,8 +204,8 @@ const devDependencies = {
204
204
  "@types/cli-progress": "3.11.5",
205
205
  "@types/eventsource": "1.1.15",
206
206
  "@types/lodash": "^4.14.191",
207
- "eslint-config-custom": "4.25.3",
208
- tsconfig: "4.25.3"
207
+ "eslint-config-custom": "workspace:*",
208
+ tsconfig: "workspace:*"
209
209
  };
210
210
  const engines = {
211
211
  node: ">=18.0.0 <=20.x.x",
@@ -315,6 +315,34 @@ async function cloudApiFactory({ logger }, token) {
315
315
  throw error;
316
316
  }
317
317
  },
318
+ async listLinkProjects() {
319
+ try {
320
+ const response = await axiosCloudAPI.get("/projects-linkable");
321
+ if (response.status !== 200) {
322
+ throw new Error("Error fetching cloud projects from the server.");
323
+ }
324
+ return response;
325
+ } catch (error) {
326
+ logger.debug(
327
+ "🥲 Oops! Couldn't retrieve your project's list from the server. Please try again."
328
+ );
329
+ throw error;
330
+ }
331
+ },
332
+ async getProject({ name: name2 }) {
333
+ try {
334
+ const response = await axiosCloudAPI.get(`/projects/${name2}`);
335
+ if (response.status !== 200) {
336
+ throw new Error("Error fetching project's details.");
337
+ }
338
+ return response;
339
+ } catch (error) {
340
+ logger.debug(
341
+ "🥲 Oops! There was a problem retrieving your project's details. Please try again."
342
+ );
343
+ throw error;
344
+ }
345
+ },
318
346
  track(event, payload = {}) {
319
347
  return axiosCloudAPI.post("/track", {
320
348
  event,
@@ -588,21 +616,24 @@ yup.object({
588
616
  name: yup.string().required(),
589
617
  exports: yup.lazy(
590
618
  (value) => yup.object(
591
- typeof value === "object" ? Object.entries(value).reduce((acc, [key, value2]) => {
592
- if (typeof value2 === "object") {
593
- acc[key] = yup.object({
594
- types: yup.string().optional(),
595
- source: yup.string().required(),
596
- module: yup.string().optional(),
597
- import: yup.string().required(),
598
- require: yup.string().required(),
599
- default: yup.string().required()
600
- }).noUnknown(true);
601
- } else {
602
- acc[key] = yup.string().matches(/^\.\/.*\.json$/).required();
603
- }
604
- return acc;
605
- }, {}) : void 0
619
+ typeof value === "object" ? Object.entries(value).reduce(
620
+ (acc, [key, value2]) => {
621
+ if (typeof value2 === "object") {
622
+ acc[key] = yup.object({
623
+ types: yup.string().optional(),
624
+ source: yup.string().required(),
625
+ module: yup.string().optional(),
626
+ import: yup.string().required(),
627
+ require: yup.string().required(),
628
+ default: yup.string().required()
629
+ }).noUnknown(true);
630
+ } else {
631
+ acc[key] = yup.string().matches(/^\.\/.*\.json$/).required();
632
+ }
633
+ return acc;
634
+ },
635
+ {}
636
+ ) : void 0
606
637
  ).optional()
607
638
  )
608
639
  });
@@ -624,18 +655,13 @@ async function getProjectNameFromPackageJson(ctx) {
624
655
  return "my-strapi-project";
625
656
  }
626
657
  }
627
- function applyDefaultName(newDefaultName, questions, defaultValues) {
628
- const newDefaultValues = _.cloneDeep(defaultValues);
629
- newDefaultValues.name = newDefaultName;
630
- const newQuestions = questions.map((question) => {
631
- const questionCopy = _.cloneDeep(question);
632
- if (questionCopy.name === "name") {
633
- questionCopy.default = newDefaultName;
634
- }
635
- return questionCopy;
636
- });
637
- return { newQuestions, newDefaultValues };
638
- }
658
+ const trackEvent = async (ctx, cloudApiService, eventName, eventData) => {
659
+ try {
660
+ await cloudApiService.track(eventName, eventData);
661
+ } catch (e) {
662
+ ctx.logger.debug(`Failed to track ${eventName}`, e);
663
+ }
664
+ };
639
665
  const openModule$1 = import("open");
640
666
  async function promptLogin(ctx) {
641
667
  const response = await inquirer.prompt([
@@ -656,13 +682,6 @@ async function loginAction(ctx) {
656
682
  const tokenService = await tokenServiceFactory(ctx);
657
683
  const existingToken = await tokenService.retrieveToken();
658
684
  const cloudApiService = await cloudApiFactory(ctx, existingToken || void 0);
659
- const trackFailedLogin = async () => {
660
- try {
661
- await cloudApiService.track("didNotLogin", { loginMethod: "cli" });
662
- } catch (e) {
663
- logger.debug("Failed to track failed login", e);
664
- }
665
- };
666
685
  if (existingToken) {
667
686
  const isTokenValid = await tokenService.isTokenValid(existingToken);
668
687
  if (isTokenValid) {
@@ -694,11 +713,7 @@ async function loginAction(ctx) {
694
713
  logger.debug(e);
695
714
  return false;
696
715
  }
697
- try {
698
- await cloudApiService.track("willLoginAttempt", {});
699
- } catch (e) {
700
- logger.debug("Failed to track login attempt", e);
701
- }
716
+ await trackEvent(ctx, cloudApiService, "willLoginAttempt", {});
702
717
  logger.debug("🔐 Creating device authentication request...", {
703
718
  client_id: cliConfig2.clientId,
704
719
  scope: cliConfig2.scope,
@@ -778,13 +793,13 @@ async function loginAction(ctx) {
778
793
  "There seems to be a problem with your login information. Please try logging in again."
779
794
  );
780
795
  spinnerFail();
781
- await trackFailedLogin();
796
+ await trackEvent(ctx, cloudApiService, "didNotLogin", { loginMethod: "cli" });
782
797
  return false;
783
798
  }
784
799
  if (e.response?.data.error && !["authorization_pending", "slow_down"].includes(e.response.data.error)) {
785
800
  logger.debug(e);
786
801
  spinnerFail();
787
- await trackFailedLogin();
802
+ await trackEvent(ctx, cloudApiService, "didNotLogin", { loginMethod: "cli" });
788
803
  return false;
789
804
  }
790
805
  await new Promise((resolve) => {
@@ -798,15 +813,50 @@ async function loginAction(ctx) {
798
813
  "To access your dashboard, please copy and paste the following URL into your web browser:"
799
814
  );
800
815
  logger.log(chalk.underline(`${apiConfig.dashboardBaseUrl}/projects`));
801
- try {
802
- await cloudApiService.track("didLogin", { loginMethod: "cli" });
803
- } catch (e) {
804
- logger.debug("Failed to track login", e);
805
- }
816
+ await trackEvent(ctx, cloudApiService, "didLogin", { loginMethod: "cli" });
806
817
  };
807
818
  await authenticate();
808
819
  return isAuthenticated;
809
820
  }
821
+ function questionDefaultValuesMapper(questionsMap) {
822
+ return (questions) => {
823
+ return questions.map((question) => {
824
+ const questionName = question.name;
825
+ if (questionName in questionsMap) {
826
+ const questionDefault = questionsMap[questionName];
827
+ if (typeof questionDefault === "function") {
828
+ return {
829
+ ...question,
830
+ default: questionDefault(question)
831
+ };
832
+ }
833
+ return {
834
+ ...question,
835
+ default: questionDefault
836
+ };
837
+ }
838
+ return question;
839
+ });
840
+ };
841
+ }
842
+ function getDefaultsFromQuestions(questions) {
843
+ return questions.reduce((acc, question) => {
844
+ if (question.default && question.name) {
845
+ return { ...acc, [question.name]: question.default };
846
+ }
847
+ return acc;
848
+ }, {});
849
+ }
850
+ function getProjectNodeVersionDefault(question) {
851
+ const currentNodeVersion = process.versions.node.split(".")[0];
852
+ if (question.type === "list" && Array.isArray(question.choices)) {
853
+ const choice = question.choices.find((choice2) => choice2.value === currentNodeVersion);
854
+ if (choice) {
855
+ return choice.value;
856
+ }
857
+ }
858
+ return question.default;
859
+ }
810
860
  async function handleError(ctx, error) {
811
861
  const { logger } = ctx;
812
862
  logger.debug(error);
@@ -851,7 +901,7 @@ async function createProject$1(ctx, cloudApi, projectInput) {
851
901
  throw e;
852
902
  }
853
903
  }
854
- const action$3 = async (ctx) => {
904
+ const action$4 = async (ctx) => {
855
905
  const { logger } = ctx;
856
906
  const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
857
907
  const token = await getValidToken(ctx, promptLogin);
@@ -860,11 +910,16 @@ const action$3 = async (ctx) => {
860
910
  }
861
911
  const cloudApi = await cloudApiFactory(ctx, token);
862
912
  const { data: config } = await cloudApi.config();
863
- const { newQuestions: questions, newDefaultValues: defaultValues } = applyDefaultName(
864
- await getProjectNameFromPackageJson(ctx),
865
- config.projectCreation.questions,
866
- config.projectCreation.defaults
867
- );
913
+ const projectName = await getProjectNameFromPackageJson(ctx);
914
+ const defaultAnswersMapper = questionDefaultValuesMapper({
915
+ name: projectName,
916
+ nodeVersion: getProjectNodeVersionDefault
917
+ });
918
+ const questions = defaultAnswersMapper(config.projectCreation.questions);
919
+ const defaultValues = {
920
+ ...config.projectCreation.defaults,
921
+ ...getDefaultsFromQuestions(questions)
922
+ };
868
923
  const projectAnswersDefaulted = defaults(defaultValues);
869
924
  const projectAnswers = await inquirer.prompt(questions);
870
925
  const projectInput = projectAnswersDefaulted(projectAnswers);
@@ -966,6 +1021,7 @@ const buildLogsServiceFactory = ({ logger }) => {
966
1021
  if (retries > MAX_RETRIES) {
967
1022
  spinner.fail("We were unable to connect to the server to get build logs at this time.");
968
1023
  es.close();
1024
+ clearExistingTimeout();
969
1025
  reject(new Error("Max retries reached"));
970
1026
  }
971
1027
  };
@@ -1040,17 +1096,7 @@ async function upload(ctx, project, token, maxProjectFileSize) {
1040
1096
  return data.build_id;
1041
1097
  } catch (e) {
1042
1098
  progressBar.stop();
1043
- if (e instanceof AxiosError && e.response?.data) {
1044
- if (e.response.status === 404) {
1045
- ctx.logger.error(
1046
- `The project does not exist. Remove the ${LOCAL_SAVE_FILENAME} file and try again.`
1047
- );
1048
- } else {
1049
- ctx.logger.error(e.response.data);
1050
- }
1051
- } else {
1052
- ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1053
- }
1099
+ ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1054
1100
  ctx.logger.debug(e);
1055
1101
  } finally {
1056
1102
  await fse__default.remove(tarFilePath);
@@ -1066,7 +1112,7 @@ async function getProject(ctx) {
1066
1112
  const { project } = await retrieve();
1067
1113
  if (!project) {
1068
1114
  try {
1069
- return await action$3(ctx);
1115
+ return await action$4(ctx);
1070
1116
  } catch (e) {
1071
1117
  ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1072
1118
  ctx.logger.debug(e);
@@ -1075,7 +1121,19 @@ async function getProject(ctx) {
1075
1121
  }
1076
1122
  return project;
1077
1123
  }
1078
- const action$2 = async (ctx) => {
1124
+ async function getConfig({
1125
+ ctx,
1126
+ cloudApiService
1127
+ }) {
1128
+ try {
1129
+ const { data: cliConfig2 } = await cloudApiService.config();
1130
+ return cliConfig2;
1131
+ } catch (e) {
1132
+ ctx.logger.debug("Failed to get cli config", e);
1133
+ return null;
1134
+ }
1135
+ }
1136
+ const action$3 = async (ctx) => {
1079
1137
  const { getValidToken } = await tokenServiceFactory(ctx);
1080
1138
  const token = await getValidToken(ctx, promptLogin);
1081
1139
  if (!token) {
@@ -1085,15 +1143,51 @@ const action$2 = async (ctx) => {
1085
1143
  if (!project) {
1086
1144
  return;
1087
1145
  }
1088
- const cloudApiService = await cloudApiFactory(ctx);
1146
+ const cloudApiService = await cloudApiFactory(ctx, token);
1089
1147
  try {
1090
- await cloudApiService.track("willDeployWithCLI", { projectInternalName: project.name });
1148
+ const {
1149
+ data: { data: projectData, metadata }
1150
+ } = await cloudApiService.getProject({ name: project.name });
1151
+ const isProjectSuspended = projectData.suspendedAt;
1152
+ if (isProjectSuspended) {
1153
+ ctx.logger.log(
1154
+ "\n Oops! This project has been suspended. \n\n Please reactivate it from the dashboard to continue deploying: "
1155
+ );
1156
+ ctx.logger.log(chalk.underline(`${metadata.dashboardUrls.project}`));
1157
+ return;
1158
+ }
1091
1159
  } catch (e) {
1092
- ctx.logger.debug("Failed to track willDeploy", e);
1160
+ if (e instanceof AxiosError && e.response?.data) {
1161
+ if (e.response.status === 404) {
1162
+ ctx.logger.warn(
1163
+ `The project associated with this folder does not exist in Strapi Cloud.
1164
+ Please link your local project to an existing Strapi Cloud project using the ${chalk.cyan(
1165
+ "link"
1166
+ )} command before deploying.`
1167
+ );
1168
+ } else {
1169
+ ctx.logger.error(e.response.data);
1170
+ }
1171
+ } else {
1172
+ ctx.logger.error(
1173
+ "An error occurred while retrieving the project's information. Please try again later."
1174
+ );
1175
+ }
1176
+ ctx.logger.debug(e);
1177
+ return;
1093
1178
  }
1179
+ await trackEvent(ctx, cloudApiService, "willDeployWithCLI", {
1180
+ projectInternalName: project.name
1181
+ });
1094
1182
  const notificationService = notificationServiceFactory(ctx);
1095
1183
  const buildLogsService = buildLogsServiceFactory(ctx);
1096
- const { data: cliConfig2 } = await cloudApiService.config();
1184
+ const cliConfig2 = await getConfig({ ctx, cloudApiService });
1185
+ if (!cliConfig2) {
1186
+ ctx.logger.error(
1187
+ "An error occurred while retrieving data from Strapi Cloud. Please check your network or try again later."
1188
+ );
1189
+ return;
1190
+ }
1097
1191
  let maxSize = parseInt(cliConfig2.maxProjectFileSize, 10);
1098
1192
  if (Number.isNaN(maxSize)) {
1099
1193
  ctx.logger.debug(
@@ -1115,10 +1209,11 @@ const action$2 = async (ctx) => {
1115
1209
  chalk.underline(`${apiConfig.dashboardBaseUrl}/projects/${project.name}/deployments`)
1116
1210
  );
1117
1211
  } catch (e) {
1212
+ ctx.logger.debug(e);
1118
1213
  if (e instanceof Error) {
1119
1214
  ctx.logger.error(e.message);
1120
1215
  } else {
1121
- throw e;
1216
+ ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1122
1217
  }
1123
1218
  }
1124
1219
  };
@@ -1149,17 +1244,163 @@ const runAction = (name2, action2) => (...args) => {
1149
1244
  process.exit(1);
1150
1245
  });
1151
1246
  };
1152
- const command$4 = ({ command: command2, ctx }) => {
1153
- 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$2)(ctx));
1247
+ const command$5 = ({ ctx }) => {
1248
+ return 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").action(() => runAction("deploy", action$3)(ctx));
1154
1249
  };
1155
1250
  const deployProject = {
1156
1251
  name: "deploy-project",
1157
1252
  description: "Deploy a Strapi Cloud project",
1253
+ action: action$3,
1254
+ command: command$5
1255
+ };
1256
+ const QUIT_OPTION = "Quit";
1257
+ async function getExistingConfig(ctx) {
1258
+ try {
1259
+ return await retrieve();
1260
+ } catch (e) {
1261
+ ctx.logger.debug("Failed to get project config", e);
1262
+ ctx.logger.error("An error occurred while retrieving config data from your local project.");
1263
+ return null;
1264
+ }
1265
+ }
1266
+ async function promptForRelink(ctx, cloudApiService, existingConfig) {
1267
+ if (existingConfig && existingConfig.project) {
1268
+ const { shouldRelink } = await inquirer.prompt([
1269
+ {
1270
+ type: "confirm",
1271
+ name: "shouldRelink",
1272
+ message: `A project named ${chalk.cyan(
1273
+ existingConfig.project.displayName ? existingConfig.project.displayName : existingConfig.project.name
1274
+ )} is already linked to this local folder. Do you want to update the link?`,
1275
+ default: false
1276
+ }
1277
+ ]);
1278
+ if (!shouldRelink) {
1279
+ await trackEvent(ctx, cloudApiService, "didNotLinkProject", {
1280
+ currentProjectName: existingConfig.project?.name
1281
+ });
1282
+ return false;
1283
+ }
1284
+ }
1285
+ return true;
1286
+ }
1287
+ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1288
+ const spinner = ctx.logger.spinner("Fetching your projects...\n").start();
1289
+ try {
1290
+ const {
1291
+ data: { data: projectList }
1292
+ } = await cloudApiService.listLinkProjects();
1293
+ spinner.succeed();
1294
+ if (!Array.isArray(projectList)) {
1295
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1296
+ return null;
1297
+ }
1298
+ const projects = projectList.filter(
1299
+ (project) => !(project.isMaintainer || project.name === existingConfig?.project?.name)
1300
+ ).map((project) => {
1301
+ return {
1302
+ name: project.displayName,
1303
+ value: { name: project.name, displayName: project.displayName }
1304
+ };
1305
+ });
1306
+ if (projects.length === 0) {
1307
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1308
+ return null;
1309
+ }
1310
+ return projects;
1311
+ } catch (e) {
1312
+ spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1313
+ ctx.logger.debug("Failed to list projects", e);
1314
+ return null;
1315
+ }
1316
+ }
1317
+ async function getUserSelection(ctx, projects) {
1318
+ const { logger } = ctx;
1319
+ try {
1320
+ const answer = await inquirer.prompt([
1321
+ {
1322
+ type: "list",
1323
+ name: "linkProject",
1324
+ message: "Which project do you want to link?",
1325
+ choices: [...projects, { name: chalk.grey(`(${QUIT_OPTION})`), value: null }]
1326
+ }
1327
+ ]);
1328
+ if (!answer.linkProject) {
1329
+ return null;
1330
+ }
1331
+ return answer;
1332
+ } catch (e) {
1333
+ logger.debug("Failed to get user input", e);
1334
+ logger.error("An error occurred while trying to get your input.");
1335
+ return null;
1336
+ }
1337
+ }
1338
+ const action$2 = async (ctx) => {
1339
+ const { getValidToken } = await tokenServiceFactory(ctx);
1340
+ const token = await getValidToken(ctx, promptLogin);
1341
+ const { logger } = ctx;
1342
+ if (!token) {
1343
+ return;
1344
+ }
1345
+ const cloudApiService = await cloudApiFactory(ctx, token);
1346
+ const existingConfig = await getExistingConfig(ctx);
1347
+ const shouldRelink = await promptForRelink(ctx, cloudApiService, existingConfig);
1348
+ if (!shouldRelink) {
1349
+ return;
1350
+ }
1351
+ await trackEvent(ctx, cloudApiService, "willLinkProject", {});
1352
+ const projects = await getProjectsList(
1353
+ ctx,
1354
+ cloudApiService,
1355
+ existingConfig
1356
+ );
1357
+ if (!projects) {
1358
+ return;
1359
+ }
1360
+ const answer = await getUserSelection(ctx, projects);
1361
+ if (!answer) {
1362
+ return;
1363
+ }
1364
+ try {
1365
+ const { confirmAction } = await inquirer.prompt([
1366
+ {
1367
+ type: "confirm",
1368
+ name: "confirmAction",
1369
+ message: "Warning: Once linked, deploying from CLI will replace the existing project and its data. Confirm to proceed:",
1370
+ default: false
1371
+ }
1372
+ ]);
1373
+ if (!confirmAction) {
1374
+ await trackEvent(ctx, cloudApiService, "didNotLinkProject", {
1375
+ cancelledProjectName: answer.linkProject.name,
1376
+ currentProjectName: existingConfig ? existingConfig.project?.name : null
1377
+ });
1378
+ return;
1379
+ }
1380
+ await save({ project: answer.linkProject });
1381
+ logger.log(`Project ${chalk.cyan(answer.linkProject.displayName)} linked successfully.`);
1382
+ await trackEvent(ctx, cloudApiService, "didLinkProject", {
1383
+ projectInternalName: answer.linkProject
1384
+ });
1385
+ } catch (e) {
1386
+ logger.debug("Failed to link project", e);
1387
+ logger.error("An error occurred while linking the project.");
1388
+ await trackEvent(ctx, cloudApiService, "didNotLinkProject", {
1389
+ projectInternalName: answer.linkProject
1390
+ });
1391
+ }
1392
+ };
1393
+ const command$4 = ({ command: command2, ctx }) => {
1394
+ 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));
1395
+ };
1396
+ const link = {
1397
+ name: "link-project",
1398
+ description: "Link a local directory to a Strapi Cloud project",
1158
1399
  action: action$2,
1159
1400
  command: command$4
1160
1401
  };
1161
- const command$3 = ({ command: command2, ctx }) => {
1162
- command2.command("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1402
+ const command$3 = ({ ctx }) => {
1403
+ return createCommand("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1163
1404
  "after",
1164
1405
  "\nAfter running this command, you will be prompted to enter your authentication information."
1165
1406
  ).option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("login", loginAction)(ctx));
@@ -1201,14 +1442,10 @@ const action$1 = async (ctx) => {
1201
1442
  logger.error("🥲 Oops! Something went wrong while logging you out. Please try again.");
1202
1443
  logger.debug(e);
1203
1444
  }
1204
- try {
1205
- await cloudApiService.track("didLogout", { loginMethod: "cli" });
1206
- } catch (e) {
1207
- logger.debug("Failed to track logout event", e);
1208
- }
1445
+ await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1209
1446
  };
1210
- const command$2 = ({ command: command2, ctx }) => {
1211
- 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));
1447
+ const command$2 = ({ ctx }) => {
1448
+ return 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$1)(ctx));
1212
1449
  };
1213
1450
  const logout = {
1214
1451
  name: "logout",
@@ -1216,13 +1453,13 @@ const logout = {
1216
1453
  action: action$1,
1217
1454
  command: command$2
1218
1455
  };
1219
- const command$1 = ({ command: command2, ctx }) => {
1220
- 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$3)(ctx));
1456
+ const command$1 = ({ ctx }) => {
1457
+ return 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$4)(ctx));
1221
1458
  };
1222
1459
  const createProject = {
1223
1460
  name: "create-project",
1224
1461
  description: "Create a new project",
1225
- action: action$3,
1462
+ action: action$4,
1226
1463
  command: command$1
1227
1464
  };
1228
1465
  const action = async (ctx) => {
@@ -1246,7 +1483,7 @@ const action = async (ctx) => {
1246
1483
  }
1247
1484
  };
1248
1485
  const command = ({ command: command2, ctx }) => {
1249
- 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("listProjects", action)(ctx));
1486
+ 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));
1250
1487
  };
1251
1488
  const listProjects = {
1252
1489
  name: "list-projects",
@@ -1256,12 +1493,13 @@ const listProjects = {
1256
1493
  };
1257
1494
  const cli = {
1258
1495
  deployProject,
1496
+ link,
1259
1497
  login,
1260
1498
  logout,
1261
1499
  createProject,
1262
1500
  listProjects
1263
1501
  };
1264
- const cloudCommands = [deployProject, login, logout, listProjects];
1502
+ const cloudCommands = [deployProject, link, login, logout, listProjects];
1265
1503
  async function initCloudCLIConfig() {
1266
1504
  const localConfig = await getLocalConfig();
1267
1505
  if (!localConfig.deviceId) {
@@ -1277,7 +1515,10 @@ async function buildStrapiCloudCommands({
1277
1515
  await initCloudCLIConfig();
1278
1516
  for (const cloudCommand of cloudCommands) {
1279
1517
  try {
1280
- await cloudCommand.command({ command: command2, ctx, argv });
1518
+ const subCommand = await cloudCommand.command({ command: command2, ctx, argv });
1519
+ if (subCommand) {
1520
+ command2.addCommand(subCommand);
1521
+ }
1281
1522
  } catch (e) {
1282
1523
  console.error(`Failed to load command ${cloudCommand.name}`, e);
1283
1524
  }