@strapi/cloud-cli 4.25.4 → 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
@@ -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 = "4.25.4";
138
138
  const description = "Commands to interact with the Strapi Cloud";
139
139
  const keywords = [
140
140
  "strapi",
@@ -179,7 +179,7 @@ const scripts = {
179
179
  watch: "pack-up watch"
180
180
  };
181
181
  const dependencies = {
182
- "@strapi/utils": "4.25.3",
182
+ "@strapi/utils": "4.25.4",
183
183
  axios: "1.6.0",
184
184
  chalk: "4.1.2",
185
185
  "cli-progress": "3.12.0",
@@ -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": "4.25.4",
208
+ tsconfig: "4.25.4"
209
209
  };
210
210
  const engines = {
211
211
  node: ">=18.0.0 <=20.x.x",
@@ -315,6 +315,20 @@ 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
+ },
318
332
  track(event, payload = {}) {
319
333
  return axiosCloudAPI.post("/track", {
320
334
  event,
@@ -636,6 +650,13 @@ function applyDefaultName(newDefaultName, questions, defaultValues) {
636
650
  });
637
651
  return { newQuestions, newDefaultValues };
638
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
+ };
639
660
  const openModule$1 = import("open");
640
661
  async function promptLogin(ctx) {
641
662
  const response = await inquirer.prompt([
@@ -656,13 +677,6 @@ async function loginAction(ctx) {
656
677
  const tokenService = await tokenServiceFactory(ctx);
657
678
  const existingToken = await tokenService.retrieveToken();
658
679
  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
680
  if (existingToken) {
667
681
  const isTokenValid = await tokenService.isTokenValid(existingToken);
668
682
  if (isTokenValid) {
@@ -694,11 +708,7 @@ async function loginAction(ctx) {
694
708
  logger.debug(e);
695
709
  return false;
696
710
  }
697
- try {
698
- await cloudApiService.track("willLoginAttempt", {});
699
- } catch (e) {
700
- logger.debug("Failed to track login attempt", e);
701
- }
711
+ await trackEvent(ctx, cloudApiService, "willLoginAttempt", {});
702
712
  logger.debug("🔐 Creating device authentication request...", {
703
713
  client_id: cliConfig2.clientId,
704
714
  scope: cliConfig2.scope,
@@ -778,13 +788,13 @@ async function loginAction(ctx) {
778
788
  "There seems to be a problem with your login information. Please try logging in again."
779
789
  );
780
790
  spinnerFail();
781
- await trackFailedLogin();
791
+ await trackEvent(ctx, cloudApiService, "didNotLogin", { loginMethod: "cli" });
782
792
  return false;
783
793
  }
784
794
  if (e.response?.data.error && !["authorization_pending", "slow_down"].includes(e.response.data.error)) {
785
795
  logger.debug(e);
786
796
  spinnerFail();
787
- await trackFailedLogin();
797
+ await trackEvent(ctx, cloudApiService, "didNotLogin", { loginMethod: "cli" });
788
798
  return false;
789
799
  }
790
800
  await new Promise((resolve) => {
@@ -798,11 +808,7 @@ async function loginAction(ctx) {
798
808
  "To access your dashboard, please copy and paste the following URL into your web browser:"
799
809
  );
800
810
  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
- }
811
+ await trackEvent(ctx, cloudApiService, "didLogin", { loginMethod: "cli" });
806
812
  };
807
813
  await authenticate();
808
814
  return isAuthenticated;
@@ -851,7 +857,7 @@ async function createProject$1(ctx, cloudApi, projectInput) {
851
857
  throw e;
852
858
  }
853
859
  }
854
- const action$3 = async (ctx) => {
860
+ const action$4 = async (ctx) => {
855
861
  const { logger } = ctx;
856
862
  const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
857
863
  const token = await getValidToken(ctx, promptLogin);
@@ -966,6 +972,7 @@ const buildLogsServiceFactory = ({ logger }) => {
966
972
  if (retries > MAX_RETRIES) {
967
973
  spinner.fail("We were unable to connect to the server to get build logs at this time.");
968
974
  es.close();
975
+ clearExistingTimeout();
969
976
  reject(new Error("Max retries reached"));
970
977
  }
971
978
  };
@@ -1066,7 +1073,7 @@ async function getProject(ctx) {
1066
1073
  const { project } = await retrieve();
1067
1074
  if (!project) {
1068
1075
  try {
1069
- return await action$3(ctx);
1076
+ return await action$4(ctx);
1070
1077
  } catch (e) {
1071
1078
  ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1072
1079
  ctx.logger.debug(e);
@@ -1075,7 +1082,19 @@ async function getProject(ctx) {
1075
1082
  }
1076
1083
  return project;
1077
1084
  }
1078
- const action$2 = 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) => {
1079
1098
  const { getValidToken } = await tokenServiceFactory(ctx);
1080
1099
  const token = await getValidToken(ctx, promptLogin);
1081
1100
  if (!token) {
@@ -1086,14 +1105,18 @@ const action$2 = async (ctx) => {
1086
1105
  return;
1087
1106
  }
1088
1107
  const cloudApiService = await cloudApiFactory(ctx);
1089
- try {
1090
- await cloudApiService.track("willDeployWithCLI", { projectInternalName: project.name });
1091
- } catch (e) {
1092
- ctx.logger.debug("Failed to track willDeploy", e);
1093
- }
1108
+ await trackEvent(ctx, cloudApiService, "willDeployWithCLI", {
1109
+ projectInternalName: project.name
1110
+ });
1094
1111
  const notificationService = notificationServiceFactory(ctx);
1095
1112
  const buildLogsService = buildLogsServiceFactory(ctx);
1096
- 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
+ }
1097
1120
  let maxSize = parseInt(cliConfig2.maxProjectFileSize, 10);
1098
1121
  if (Number.isNaN(maxSize)) {
1099
1122
  ctx.logger.debug(
@@ -1115,10 +1138,11 @@ const action$2 = async (ctx) => {
1115
1138
  chalk.underline(`${apiConfig.dashboardBaseUrl}/projects/${project.name}/deployments`)
1116
1139
  );
1117
1140
  } catch (e) {
1141
+ ctx.logger.debug(e);
1118
1142
  if (e instanceof Error) {
1119
1143
  ctx.logger.error(e.message);
1120
1144
  } else {
1121
- throw e;
1145
+ ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1122
1146
  }
1123
1147
  }
1124
1148
  };
@@ -1149,12 +1173,158 @@ const runAction = (name2, action2) => (...args) => {
1149
1173
  process.exit(1);
1150
1174
  });
1151
1175
  };
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));
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));
1154
1178
  };
1155
1179
  const deployProject = {
1156
1180
  name: "deploy-project",
1157
1181
  description: "Deploy a Strapi Cloud project",
1182
+ action: action$3,
1183
+ command: command$5
1184
+ };
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",
1158
1328
  action: action$2,
1159
1329
  command: command$4
1160
1330
  };
@@ -1201,11 +1371,7 @@ const action$1 = async (ctx) => {
1201
1371
  logger.error("🥲 Oops! Something went wrong while logging you out. Please try again.");
1202
1372
  logger.debug(e);
1203
1373
  }
1204
- try {
1205
- await cloudApiService.track("didLogout", { loginMethod: "cli" });
1206
- } catch (e) {
1207
- logger.debug("Failed to track logout event", e);
1208
- }
1374
+ await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1209
1375
  };
1210
1376
  const command$2 = ({ command: command2, ctx }) => {
1211
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));
@@ -1217,12 +1383,12 @@ const logout = {
1217
1383
  command: command$2
1218
1384
  };
1219
1385
  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));
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));
1221
1387
  };
1222
1388
  const createProject = {
1223
1389
  name: "create-project",
1224
1390
  description: "Create a new project",
1225
- action: action$3,
1391
+ action: action$4,
1226
1392
  command: command$1
1227
1393
  };
1228
1394
  const action = async (ctx) => {
@@ -1246,7 +1412,7 @@ const action = async (ctx) => {
1246
1412
  }
1247
1413
  };
1248
1414
  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));
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));
1250
1416
  };
1251
1417
  const listProjects = {
1252
1418
  name: "list-projects",
@@ -1256,12 +1422,13 @@ const listProjects = {
1256
1422
  };
1257
1423
  const cli = {
1258
1424
  deployProject,
1425
+ link,
1259
1426
  login,
1260
1427
  logout,
1261
1428
  createProject,
1262
1429
  listProjects
1263
1430
  };
1264
- const cloudCommands = [deployProject, login, logout, listProjects];
1431
+ const cloudCommands = [deployProject, link, login, logout, listProjects];
1265
1432
  async function initCloudCLIConfig() {
1266
1433
  const localConfig = await getLocalConfig();
1267
1434
  if (!localConfig.deviceId) {