@strapi/cloud-cli 4.25.3 → 4.25.5

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.
package/dist/index.mjs CHANGED
@@ -1,12 +1,12 @@
1
1
  import crypto$1 from "crypto";
2
- import fse from "fs-extra";
2
+ import * as fse from "fs-extra";
3
+ import fse__default from "fs-extra";
3
4
  import * as path from "path";
4
5
  import path__default from "path";
5
6
  import chalk from "chalk";
6
7
  import axios, { AxiosError } from "axios";
7
8
  import * as crypto from "node:crypto";
8
9
  import { env } from "@strapi/utils";
9
- import * as fs from "fs";
10
10
  import * as tar from "tar";
11
11
  import { minimatch } from "minimatch";
12
12
  import inquirer from "inquirer";
@@ -18,7 +18,6 @@ import jwt from "jsonwebtoken";
18
18
  import stringify from "fast-safe-stringify";
19
19
  import ora from "ora";
20
20
  import * as cliProgress from "cli-progress";
21
- import fs$1 from "fs/promises";
22
21
  import pkgUp from "pkg-up";
23
22
  import * as yup from "yup";
24
23
  import _ from "lodash";
@@ -41,23 +40,6 @@ const IGNORED_PATTERNS = [
41
40
  "**/.idea/**",
42
41
  "**/.vscode/**"
43
42
  ];
44
- const getFiles = (dirPath, ignorePatterns = [], arrayOfFiles = [], subfolder = "") => {
45
- const entries = fs.readdirSync(path.join(dirPath, subfolder));
46
- entries.forEach((entry) => {
47
- const entryPathFromRoot = path.join(subfolder, entry);
48
- const entryPath = path.relative(dirPath, entryPathFromRoot);
49
- const isIgnored = isIgnoredFile(dirPath, entryPathFromRoot, ignorePatterns);
50
- if (isIgnored) {
51
- return;
52
- }
53
- if (fs.statSync(entryPath).isDirectory()) {
54
- getFiles(dirPath, ignorePatterns, arrayOfFiles, entryPathFromRoot);
55
- } else {
56
- arrayOfFiles.push(entryPath);
57
- }
58
- });
59
- return arrayOfFiles;
60
- };
61
43
  const isIgnoredFile = (folderPath, file, ignorePatterns) => {
62
44
  ignorePatterns.push(...IGNORED_PATTERNS);
63
45
  const relativeFilePath = path.join(folderPath, file);
@@ -75,16 +57,35 @@ const isIgnoredFile = (folderPath, file, ignorePatterns) => {
75
57
  }
76
58
  return isIgnored;
77
59
  };
78
- const readGitignore = (folderPath) => {
60
+ const getFiles = async (dirPath, ignorePatterns = [], subfolder = "") => {
61
+ const arrayOfFiles = [];
62
+ const entries = await fse.readdir(path.join(dirPath, subfolder));
63
+ for (const entry of entries) {
64
+ const entryPathFromRoot = path.join(subfolder, entry);
65
+ const entryPath = path.relative(dirPath, entryPathFromRoot);
66
+ const isIgnored = isIgnoredFile(dirPath, entryPathFromRoot, ignorePatterns);
67
+ if (!isIgnored) {
68
+ if (fse.statSync(entryPath).isDirectory()) {
69
+ const subFiles = await getFiles(dirPath, ignorePatterns, entryPathFromRoot);
70
+ arrayOfFiles.push(...subFiles);
71
+ } else {
72
+ arrayOfFiles.push(entryPath);
73
+ }
74
+ }
75
+ }
76
+ return arrayOfFiles;
77
+ };
78
+ const readGitignore = async (folderPath) => {
79
79
  const gitignorePath = path.resolve(folderPath, ".gitignore");
80
- if (!fs.existsSync(gitignorePath))
80
+ const pathExist = await fse.pathExists(gitignorePath);
81
+ if (!pathExist)
81
82
  return [];
82
- const gitignoreContent = fs.readFileSync(gitignorePath, "utf8");
83
+ const gitignoreContent = await fse.readFile(gitignorePath, "utf8");
83
84
  return gitignoreContent.split(/\r?\n/).filter((line) => Boolean(line.trim()) && !line.startsWith("#"));
84
85
  };
85
86
  const compressFilesToTar = async (storagePath, folderToCompress, filename) => {
86
- const ignorePatterns = readGitignore(folderToCompress);
87
- const filesToCompress = getFiles(folderToCompress, ignorePatterns);
87
+ const ignorePatterns = await readGitignore(folderToCompress);
88
+ const filesToCompress = await getFiles(folderToCompress, ignorePatterns);
88
89
  return tar.c(
89
90
  {
90
91
  gzip: true,
@@ -97,7 +98,7 @@ const APP_FOLDER_NAME = "com.strapi.cli";
97
98
  const CONFIG_FILENAME = "config.json";
98
99
  async function checkDirectoryExists(directoryPath) {
99
100
  try {
100
- const fsStat = await fse.lstat(directoryPath);
101
+ const fsStat = await fse__default.lstat(directoryPath);
101
102
  return fsStat.isDirectory();
102
103
  } catch (e) {
103
104
  return false;
@@ -105,14 +106,14 @@ async function checkDirectoryExists(directoryPath) {
105
106
  }
106
107
  async function getTmpStoragePath() {
107
108
  const storagePath = path__default.join(os.tmpdir(), APP_FOLDER_NAME);
108
- await fse.ensureDir(storagePath);
109
+ await fse__default.ensureDir(storagePath);
109
110
  return storagePath;
110
111
  }
111
112
  async function getConfigPath() {
112
113
  const configDirs = XDGAppPaths(APP_FOLDER_NAME).configDirs();
113
114
  const configPath = configDirs.find(checkDirectoryExists);
114
115
  if (!configPath) {
115
- await fse.ensureDir(configDirs[0]);
116
+ await fse__default.ensureDir(configDirs[0]);
116
117
  return configDirs[0];
117
118
  }
118
119
  return configPath;
@@ -120,9 +121,9 @@ async function getConfigPath() {
120
121
  async function getLocalConfig() {
121
122
  const configPath = await getConfigPath();
122
123
  const configFilePath = path__default.join(configPath, CONFIG_FILENAME);
123
- await fse.ensureFile(configFilePath);
124
+ await fse__default.ensureFile(configFilePath);
124
125
  try {
125
- return await fse.readJSON(configFilePath, { encoding: "utf8", throws: true });
126
+ return await fse__default.readJSON(configFilePath, { encoding: "utf8", throws: true });
126
127
  } catch (e) {
127
128
  return {};
128
129
  }
@@ -130,10 +131,10 @@ async function getLocalConfig() {
130
131
  async function saveLocalConfig(data) {
131
132
  const configPath = await getConfigPath();
132
133
  const configFilePath = path__default.join(configPath, CONFIG_FILENAME);
133
- await fse.writeJson(configFilePath, data, { encoding: "utf8", spaces: 2, mode: 384 });
134
+ await fse__default.writeJson(configFilePath, data, { encoding: "utf8", spaces: 2, mode: 384 });
134
135
  }
135
136
  const name = "@strapi/cloud-cli";
136
- const version = "4.25.2";
137
+ const version = "4.25.4";
137
138
  const description = "Commands to interact with the Strapi Cloud";
138
139
  const keywords = [
139
140
  "strapi",
@@ -178,7 +179,7 @@ const scripts = {
178
179
  watch: "pack-up watch"
179
180
  };
180
181
  const dependencies = {
181
- "@strapi/utils": "4.25.2",
182
+ "@strapi/utils": "4.25.4",
182
183
  axios: "1.6.0",
183
184
  chalk: "4.1.2",
184
185
  "cli-progress": "3.12.0",
@@ -203,8 +204,8 @@ const devDependencies = {
203
204
  "@types/cli-progress": "3.11.5",
204
205
  "@types/eventsource": "1.1.15",
205
206
  "@types/lodash": "^4.14.191",
206
- "eslint-config-custom": "4.25.2",
207
- tsconfig: "4.25.2"
207
+ "eslint-config-custom": "4.25.4",
208
+ tsconfig: "4.25.4"
208
209
  };
209
210
  const engines = {
210
211
  node: ">=18.0.0 <=20.x.x",
@@ -257,7 +258,7 @@ async function cloudApiFactory({ logger }, token) {
257
258
  deploy({ filePath, project }, { onUploadProgress }) {
258
259
  return axiosCloudAPI.post(
259
260
  `/deploy/${project.name}`,
260
- { file: fse.createReadStream(filePath) },
261
+ { file: fse__default.createReadStream(filePath) },
261
262
  {
262
263
  headers: {
263
264
  "Content-Type": "multipart/form-data"
@@ -300,8 +301,33 @@ async function cloudApiFactory({ logger }, token) {
300
301
  throw error;
301
302
  }
302
303
  },
303
- listProjects() {
304
- return axiosCloudAPI.get("/projects");
304
+ async listProjects() {
305
+ try {
306
+ const response = await axiosCloudAPI.get("/projects");
307
+ if (response.status !== 200) {
308
+ throw new Error("Error fetching cloud projects from the server.");
309
+ }
310
+ return response;
311
+ } catch (error) {
312
+ logger.debug(
313
+ "🥲 Oops! Couldn't retrieve your project's list from the server. Please try again."
314
+ );
315
+ throw error;
316
+ }
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
+ }
305
331
  },
306
332
  track(event, payload = {}) {
307
333
  return axiosCloudAPI.post("/track", {
@@ -316,18 +342,18 @@ async function save(data, { directoryPath } = {}) {
316
342
  const alreadyInFileData = await retrieve({ directoryPath });
317
343
  const storedData = { ...alreadyInFileData, ...data };
318
344
  const pathToFile = path__default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
319
- await fse.ensureDir(path__default.dirname(pathToFile));
320
- await fse.writeJson(pathToFile, storedData, { encoding: "utf8" });
345
+ await fse__default.ensureDir(path__default.dirname(pathToFile));
346
+ await fse__default.writeJson(pathToFile, storedData, { encoding: "utf8" });
321
347
  }
322
348
  async function retrieve({
323
349
  directoryPath
324
350
  } = {}) {
325
351
  const pathToFile = path__default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
326
- const pathExists = await fse.pathExists(pathToFile);
352
+ const pathExists = await fse__default.pathExists(pathToFile);
327
353
  if (!pathExists) {
328
354
  return {};
329
355
  }
330
- return fse.readJSON(pathToFile, { encoding: "utf8" });
356
+ return fse__default.readJSON(pathToFile, { encoding: "utf8" });
331
357
  }
332
358
  const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
333
359
  __proto__: null,
@@ -599,7 +625,7 @@ const loadPkg = async ({ cwd, logger }) => {
599
625
  if (!pkgPath) {
600
626
  throw new Error("Could not find a package.json in the current directory");
601
627
  }
602
- const buffer = await fs$1.readFile(pkgPath);
628
+ const buffer = await fse.readFile(pkgPath);
603
629
  const pkg = JSON.parse(buffer.toString());
604
630
  logger.debug("Loaded package.json:", os.EOL, pkg);
605
631
  return pkg;
@@ -624,6 +650,13 @@ function applyDefaultName(newDefaultName, questions, defaultValues) {
624
650
  });
625
651
  return { newQuestions, newDefaultValues };
626
652
  }
653
+ const trackEvent = async (ctx, cloudApiService, eventName, eventData) => {
654
+ try {
655
+ await cloudApiService.track(eventName, eventData);
656
+ } catch (e) {
657
+ ctx.logger.debug(`Failed to track ${eventName}`, e);
658
+ }
659
+ };
627
660
  const openModule$1 = import("open");
628
661
  async function promptLogin(ctx) {
629
662
  const response = await inquirer.prompt([
@@ -644,13 +677,6 @@ async function loginAction(ctx) {
644
677
  const tokenService = await tokenServiceFactory(ctx);
645
678
  const existingToken = await tokenService.retrieveToken();
646
679
  const cloudApiService = await cloudApiFactory(ctx, existingToken || void 0);
647
- const trackFailedLogin = async () => {
648
- try {
649
- await cloudApiService.track("didNotLogin", { loginMethod: "cli" });
650
- } catch (e) {
651
- logger.debug("Failed to track failed login", e);
652
- }
653
- };
654
680
  if (existingToken) {
655
681
  const isTokenValid = await tokenService.isTokenValid(existingToken);
656
682
  if (isTokenValid) {
@@ -682,11 +708,7 @@ async function loginAction(ctx) {
682
708
  logger.debug(e);
683
709
  return false;
684
710
  }
685
- try {
686
- await cloudApiService.track("willLoginAttempt", {});
687
- } catch (e) {
688
- logger.debug("Failed to track login attempt", e);
689
- }
711
+ await trackEvent(ctx, cloudApiService, "willLoginAttempt", {});
690
712
  logger.debug("🔐 Creating device authentication request...", {
691
713
  client_id: cliConfig2.clientId,
692
714
  scope: cliConfig2.scope,
@@ -766,13 +788,13 @@ async function loginAction(ctx) {
766
788
  "There seems to be a problem with your login information. Please try logging in again."
767
789
  );
768
790
  spinnerFail();
769
- await trackFailedLogin();
791
+ await trackEvent(ctx, cloudApiService, "didNotLogin", { loginMethod: "cli" });
770
792
  return false;
771
793
  }
772
794
  if (e.response?.data.error && !["authorization_pending", "slow_down"].includes(e.response.data.error)) {
773
795
  logger.debug(e);
774
796
  spinnerFail();
775
- await trackFailedLogin();
797
+ await trackEvent(ctx, cloudApiService, "didNotLogin", { loginMethod: "cli" });
776
798
  return false;
777
799
  }
778
800
  await new Promise((resolve) => {
@@ -786,11 +808,7 @@ async function loginAction(ctx) {
786
808
  "To access your dashboard, please copy and paste the following URL into your web browser:"
787
809
  );
788
810
  logger.log(chalk.underline(`${apiConfig.dashboardBaseUrl}/projects`));
789
- try {
790
- await cloudApiService.track("didLogin", { loginMethod: "cli" });
791
- } catch (e) {
792
- logger.debug("Failed to track login", e);
793
- }
811
+ await trackEvent(ctx, cloudApiService, "didLogin", { loginMethod: "cli" });
794
812
  };
795
813
  await authenticate();
796
814
  return isAuthenticated;
@@ -839,7 +857,7 @@ async function createProject$1(ctx, cloudApi, projectInput) {
839
857
  throw e;
840
858
  }
841
859
  }
842
- const action$2 = async (ctx) => {
860
+ const action$4 = async (ctx) => {
843
861
  const { logger } = ctx;
844
862
  const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
845
863
  const token = await getValidToken(ctx, promptLogin);
@@ -954,6 +972,7 @@ const buildLogsServiceFactory = ({ logger }) => {
954
972
  if (retries > MAX_RETRIES) {
955
973
  spinner.fail("We were unable to connect to the server to get build logs at this time.");
956
974
  es.close();
975
+ clearExistingTimeout();
957
976
  reject(new Error("Max retries reached"));
958
977
  }
959
978
  };
@@ -996,13 +1015,13 @@ async function upload(ctx, project, token, maxProjectFileSize) {
996
1015
  process.exit(1);
997
1016
  }
998
1017
  const tarFilePath = path__default.resolve(storagePath, compressedFilename);
999
- const fileStats = await fse.stat(tarFilePath);
1018
+ const fileStats = await fse__default.stat(tarFilePath);
1000
1019
  if (fileStats.size > maxProjectFileSize) {
1001
1020
  ctx.logger.log(
1002
1021
  "Unable to proceed: Your project is too big to be transferred, please use a git repo instead."
1003
1022
  );
1004
1023
  try {
1005
- await fse.remove(tarFilePath);
1024
+ await fse__default.remove(tarFilePath);
1006
1025
  } catch (e) {
1007
1026
  ctx.logger.log("Unable to remove file: ", tarFilePath);
1008
1027
  ctx.logger.debug(e);
@@ -1041,7 +1060,7 @@ async function upload(ctx, project, token, maxProjectFileSize) {
1041
1060
  }
1042
1061
  ctx.logger.debug(e);
1043
1062
  } finally {
1044
- await fse.remove(tarFilePath);
1063
+ await fse__default.remove(tarFilePath);
1045
1064
  }
1046
1065
  process.exit(0);
1047
1066
  } catch (e) {
@@ -1054,7 +1073,7 @@ async function getProject(ctx) {
1054
1073
  const { project } = await retrieve();
1055
1074
  if (!project) {
1056
1075
  try {
1057
- return await action$2(ctx);
1076
+ return await action$4(ctx);
1058
1077
  } catch (e) {
1059
1078
  ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1060
1079
  ctx.logger.debug(e);
@@ -1063,7 +1082,19 @@ async function getProject(ctx) {
1063
1082
  }
1064
1083
  return project;
1065
1084
  }
1066
- const action$1 = async (ctx) => {
1085
+ async function getConfig({
1086
+ ctx,
1087
+ cloudApiService
1088
+ }) {
1089
+ try {
1090
+ const { data: cliConfig2 } = await cloudApiService.config();
1091
+ return cliConfig2;
1092
+ } catch (e) {
1093
+ ctx.logger.debug("Failed to get cli config", e);
1094
+ return null;
1095
+ }
1096
+ }
1097
+ const action$3 = async (ctx) => {
1067
1098
  const { getValidToken } = await tokenServiceFactory(ctx);
1068
1099
  const token = await getValidToken(ctx, promptLogin);
1069
1100
  if (!token) {
@@ -1074,14 +1105,18 @@ const action$1 = async (ctx) => {
1074
1105
  return;
1075
1106
  }
1076
1107
  const cloudApiService = await cloudApiFactory(ctx);
1077
- try {
1078
- await cloudApiService.track("willDeployWithCLI", { projectInternalName: project.name });
1079
- } catch (e) {
1080
- ctx.logger.debug("Failed to track willDeploy", e);
1081
- }
1108
+ await trackEvent(ctx, cloudApiService, "willDeployWithCLI", {
1109
+ projectInternalName: project.name
1110
+ });
1082
1111
  const notificationService = notificationServiceFactory(ctx);
1083
1112
  const buildLogsService = buildLogsServiceFactory(ctx);
1084
- const { data: cliConfig2 } = await cloudApiService.config();
1113
+ const cliConfig2 = await getConfig({ ctx, cloudApiService });
1114
+ if (!cliConfig2) {
1115
+ ctx.logger.error(
1116
+ "An error occurred while retrieving data from Strapi Cloud. Please try check your network or again later."
1117
+ );
1118
+ return;
1119
+ }
1085
1120
  let maxSize = parseInt(cliConfig2.maxProjectFileSize, 10);
1086
1121
  if (Number.isNaN(maxSize)) {
1087
1122
  ctx.logger.debug(
@@ -1103,10 +1138,11 @@ const action$1 = async (ctx) => {
1103
1138
  chalk.underline(`${apiConfig.dashboardBaseUrl}/projects/${project.name}/deployments`)
1104
1139
  );
1105
1140
  } catch (e) {
1141
+ ctx.logger.debug(e);
1106
1142
  if (e instanceof Error) {
1107
1143
  ctx.logger.error(e.message);
1108
1144
  } else {
1109
- throw e;
1145
+ ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1110
1146
  }
1111
1147
  }
1112
1148
  };
@@ -1137,16 +1173,162 @@ const runAction = (name2, action2) => (...args) => {
1137
1173
  process.exit(1);
1138
1174
  });
1139
1175
  };
1140
- const command$3 = ({ command: command2, ctx }) => {
1141
- 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$1)(ctx));
1176
+ const command$5 = ({ command: command2, ctx }) => {
1177
+ 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));
1142
1178
  };
1143
1179
  const deployProject = {
1144
1180
  name: "deploy-project",
1145
1181
  description: "Deploy a Strapi Cloud project",
1146
- action: action$1,
1147
- command: command$3
1182
+ action: action$3,
1183
+ command: command$5
1148
1184
  };
1149
- const command$2 = ({ command: command2, ctx }) => {
1185
+ const QUIT_OPTION = "Quit";
1186
+ async function getExistingConfig(ctx) {
1187
+ try {
1188
+ return await retrieve();
1189
+ } catch (e) {
1190
+ ctx.logger.debug("Failed to get project config", e);
1191
+ ctx.logger.error("An error occurred while retrieving config data from your local project.");
1192
+ return null;
1193
+ }
1194
+ }
1195
+ async function promptForRelink(ctx, cloudApiService, existingConfig) {
1196
+ if (existingConfig && existingConfig.project) {
1197
+ const { shouldRelink } = await inquirer.prompt([
1198
+ {
1199
+ type: "confirm",
1200
+ name: "shouldRelink",
1201
+ message: `A project named ${chalk.cyan(
1202
+ existingConfig.project.displayName ? existingConfig.project.displayName : existingConfig.project.name
1203
+ )} is already linked to this local folder. Do you want to update the link?`,
1204
+ default: false
1205
+ }
1206
+ ]);
1207
+ if (!shouldRelink) {
1208
+ await trackEvent(ctx, cloudApiService, "didNotLinkProject", {
1209
+ currentProjectName: existingConfig.project?.name
1210
+ });
1211
+ return false;
1212
+ }
1213
+ }
1214
+ return true;
1215
+ }
1216
+ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1217
+ const spinner = ctx.logger.spinner("Fetching your projects...\n").start();
1218
+ try {
1219
+ const {
1220
+ data: { data: projectList }
1221
+ } = await cloudApiService.listLinkProjects();
1222
+ spinner.succeed();
1223
+ if (!Array.isArray(projectList)) {
1224
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1225
+ return null;
1226
+ }
1227
+ const projects = projectList.filter(
1228
+ (project) => !(project.isMaintainer || project.name === existingConfig?.project?.name)
1229
+ ).map((project) => {
1230
+ return {
1231
+ name: project.displayName,
1232
+ value: { name: project.name, displayName: project.displayName }
1233
+ };
1234
+ });
1235
+ if (projects.length === 0) {
1236
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1237
+ return null;
1238
+ }
1239
+ return projects;
1240
+ } catch (e) {
1241
+ spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1242
+ ctx.logger.debug("Failed to list projects", e);
1243
+ return null;
1244
+ }
1245
+ }
1246
+ async function getUserSelection(ctx, projects) {
1247
+ const { logger } = ctx;
1248
+ try {
1249
+ const answer = await inquirer.prompt([
1250
+ {
1251
+ type: "list",
1252
+ name: "linkProject",
1253
+ message: "Which project do you want to link?",
1254
+ choices: [...projects, { name: chalk.grey(`(${QUIT_OPTION})`), value: null }]
1255
+ }
1256
+ ]);
1257
+ if (!answer.linkProject) {
1258
+ return null;
1259
+ }
1260
+ return answer;
1261
+ } catch (e) {
1262
+ logger.debug("Failed to get user input", e);
1263
+ logger.error("An error occurred while trying to get your input.");
1264
+ return null;
1265
+ }
1266
+ }
1267
+ const action$2 = async (ctx) => {
1268
+ const { getValidToken } = await tokenServiceFactory(ctx);
1269
+ const token = await getValidToken(ctx, promptLogin);
1270
+ const { logger } = ctx;
1271
+ if (!token) {
1272
+ return;
1273
+ }
1274
+ const cloudApiService = await cloudApiFactory(ctx, token);
1275
+ const existingConfig = await getExistingConfig(ctx);
1276
+ const shouldRelink = await promptForRelink(ctx, cloudApiService, existingConfig);
1277
+ if (!shouldRelink) {
1278
+ return;
1279
+ }
1280
+ await trackEvent(ctx, cloudApiService, "willLinkProject", {});
1281
+ const projects = await getProjectsList(
1282
+ ctx,
1283
+ cloudApiService,
1284
+ existingConfig
1285
+ );
1286
+ if (!projects) {
1287
+ return;
1288
+ }
1289
+ const answer = await getUserSelection(ctx, projects);
1290
+ if (!answer) {
1291
+ return;
1292
+ }
1293
+ try {
1294
+ const { confirmAction } = await inquirer.prompt([
1295
+ {
1296
+ type: "confirm",
1297
+ name: "confirmAction",
1298
+ message: "Warning: Once linked, deploying from CLI will replace the existing project and its data. Confirm to proceed:",
1299
+ default: false
1300
+ }
1301
+ ]);
1302
+ if (!confirmAction) {
1303
+ await trackEvent(ctx, cloudApiService, "didNotLinkProject", {
1304
+ cancelledProjectName: answer.linkProject.name,
1305
+ currentProjectName: existingConfig ? existingConfig.project?.name : null
1306
+ });
1307
+ return;
1308
+ }
1309
+ await save({ project: answer.linkProject });
1310
+ logger.log(`Project ${chalk.cyan(answer.linkProject.displayName)} linked successfully.`);
1311
+ await trackEvent(ctx, cloudApiService, "didLinkProject", {
1312
+ projectInternalName: answer.linkProject
1313
+ });
1314
+ } catch (e) {
1315
+ logger.debug("Failed to link project", e);
1316
+ logger.error("An error occurred while linking the project.");
1317
+ await trackEvent(ctx, cloudApiService, "didNotLinkProject", {
1318
+ projectInternalName: answer.linkProject
1319
+ });
1320
+ }
1321
+ };
1322
+ const command$4 = ({ command: command2, ctx }) => {
1323
+ 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));
1324
+ };
1325
+ const link = {
1326
+ name: "link-project",
1327
+ description: "Link a local directory to a Strapi Cloud project",
1328
+ action: action$2,
1329
+ command: command$4
1330
+ };
1331
+ const command$3 = ({ command: command2, ctx }) => {
1150
1332
  command2.command("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1151
1333
  "after",
1152
1334
  "\nAfter running this command, you will be prompted to enter your authentication information."
@@ -1156,10 +1338,10 @@ const login = {
1156
1338
  name: "login",
1157
1339
  description: "Strapi Cloud Login",
1158
1340
  action: loginAction,
1159
- command: command$2
1341
+ command: command$3
1160
1342
  };
1161
1343
  const openModule = import("open");
1162
- const action = async (ctx) => {
1344
+ const action$1 = async (ctx) => {
1163
1345
  const { logger } = ctx;
1164
1346
  const { retrieveToken, eraseToken } = await tokenServiceFactory(ctx);
1165
1347
  const token = await retrieveToken();
@@ -1189,37 +1371,64 @@ const action = async (ctx) => {
1189
1371
  logger.error("🥲 Oops! Something went wrong while logging you out. Please try again.");
1190
1372
  logger.debug(e);
1191
1373
  }
1192
- try {
1193
- await cloudApiService.track("didLogout", { loginMethod: "cli" });
1194
- } catch (e) {
1195
- logger.debug("Failed to track logout event", e);
1196
- }
1374
+ await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1197
1375
  };
1198
- const command$1 = ({ command: command2, ctx }) => {
1199
- 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)(ctx));
1376
+ const command$2 = ({ command: command2, ctx }) => {
1377
+ 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));
1200
1378
  };
1201
1379
  const logout = {
1202
1380
  name: "logout",
1203
1381
  description: "Strapi Cloud Logout",
1204
- action,
1205
- command: command$1
1382
+ action: action$1,
1383
+ command: command$2
1206
1384
  };
1207
- const command = ({ command: command2, ctx }) => {
1208
- 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$2)(ctx));
1385
+ const command$1 = ({ command: command2, ctx }) => {
1386
+ 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));
1209
1387
  };
1210
1388
  const createProject = {
1211
1389
  name: "create-project",
1212
1390
  description: "Create a new project",
1213
- action: action$2,
1391
+ action: action$4,
1392
+ command: command$1
1393
+ };
1394
+ const action = async (ctx) => {
1395
+ const { getValidToken } = await tokenServiceFactory(ctx);
1396
+ const token = await getValidToken(ctx, promptLogin);
1397
+ const { logger } = ctx;
1398
+ if (!token) {
1399
+ return;
1400
+ }
1401
+ const cloudApiService = await cloudApiFactory(ctx, token);
1402
+ const spinner = logger.spinner("Fetching your projects...").start();
1403
+ try {
1404
+ const {
1405
+ data: { data: projectList }
1406
+ } = await cloudApiService.listProjects();
1407
+ spinner.succeed();
1408
+ logger.log(projectList);
1409
+ } catch (e) {
1410
+ ctx.logger.debug("Failed to list projects", e);
1411
+ spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1412
+ }
1413
+ };
1414
+ const command = ({ command: command2, ctx }) => {
1415
+ 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));
1416
+ };
1417
+ const listProjects = {
1418
+ name: "list-projects",
1419
+ description: "List Strapi Cloud projects",
1420
+ action,
1214
1421
  command
1215
1422
  };
1216
1423
  const cli = {
1217
1424
  deployProject,
1425
+ link,
1218
1426
  login,
1219
1427
  logout,
1220
- createProject
1428
+ createProject,
1429
+ listProjects
1221
1430
  };
1222
- const cloudCommands = [deployProject, login, logout];
1431
+ const cloudCommands = [deployProject, link, login, logout, listProjects];
1223
1432
  async function initCloudCLIConfig() {
1224
1433
  const localConfig = await getLocalConfig();
1225
1434
  if (!localConfig.deviceId) {