@strapi/cloud-cli 0.0.0-next.f4ec69568d980c6fee91ce2ee0f41c138347aa81 → 0.0.0-next.f698d55751345c4ca87477ef683475c1a68f310a

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
@@ -2,6 +2,7 @@ import crypto$1 from "crypto";
2
2
  import * as fse from "fs-extra";
3
3
  import fse__default from "fs-extra";
4
4
  import inquirer from "inquirer";
5
+ import boxen from "boxen";
5
6
  import * as path from "path";
6
7
  import path__default from "path";
7
8
  import chalk from "chalk";
@@ -13,6 +14,7 @@ import { minimatch } from "minimatch";
13
14
  import { defaults, has } from "lodash/fp";
14
15
  import os from "os";
15
16
  import XDGAppPaths from "xdg-app-paths";
17
+ import { merge } from "lodash";
16
18
  import jwksClient from "jwks-rsa";
17
19
  import jwt from "jsonwebtoken";
18
20
  import stringify from "fast-safe-stringify";
@@ -78,8 +80,7 @@ const getFiles = async (dirPath, ignorePatterns = [], subfolder = "") => {
78
80
  const readGitignore = async (folderPath) => {
79
81
  const gitignorePath = path.resolve(folderPath, ".gitignore");
80
82
  const pathExist = await fse.pathExists(gitignorePath);
81
- if (!pathExist)
82
- return [];
83
+ if (!pathExist) return [];
83
84
  const gitignoreContent = await fse.readFile(gitignorePath, "utf8");
84
85
  return gitignoreContent.split(/\r?\n/).filter((line) => Boolean(line.trim()) && !line.startsWith("#"));
85
86
  };
@@ -118,7 +119,7 @@ async function getConfigPath() {
118
119
  }
119
120
  return configPath;
120
121
  }
121
- async function getLocalConfig() {
122
+ async function getLocalConfig$1() {
122
123
  const configPath = await getConfigPath();
123
124
  const configFilePath = path__default.join(configPath, CONFIG_FILENAME);
124
125
  await fse__default.ensureFile(configFilePath);
@@ -134,7 +135,7 @@ async function saveLocalConfig(data) {
134
135
  await fse__default.writeJson(configFilePath, data, { encoding: "utf8", spaces: 2, mode: 384 });
135
136
  }
136
137
  const name = "@strapi/cloud-cli";
137
- const version = "5.3.0";
138
+ const version = "5.5.1";
138
139
  const description = "Commands to interact with the Strapi Cloud";
139
140
  const keywords = [
140
141
  "strapi",
@@ -179,8 +180,9 @@ const scripts = {
179
180
  watch: "pack-up watch"
180
181
  };
181
182
  const dependencies = {
182
- "@strapi/utils": "workspace:*",
183
+ "@strapi/utils": "5.5.1",
183
184
  axios: "1.7.4",
185
+ boxen: "5.1.2",
184
186
  chalk: "4.1.2",
185
187
  "cli-progress": "3.12.0",
186
188
  commander: "8.3.0",
@@ -200,12 +202,12 @@ const dependencies = {
200
202
  yup: "0.32.9"
201
203
  };
202
204
  const devDependencies = {
203
- "@strapi/pack-up": "4.23.0",
205
+ "@strapi/pack-up": "5.0.2",
204
206
  "@types/cli-progress": "3.11.5",
205
207
  "@types/eventsource": "1.1.15",
206
208
  "@types/lodash": "^4.14.191",
207
- "eslint-config-custom": "workspace:*",
208
- tsconfig: "workspace:*"
209
+ "eslint-config-custom": "5.5.1",
210
+ tsconfig: "5.5.1"
209
211
  };
210
212
  const engines = {
211
213
  node: ">=18.0.0 <=22.x.x",
@@ -235,7 +237,7 @@ const packageJson = {
235
237
  };
236
238
  const VERSION = "v1";
237
239
  async function cloudApiFactory({ logger }, token) {
238
- const localConfig = await getLocalConfig();
240
+ const localConfig = await getLocalConfig$1();
239
241
  const customHeaders = {
240
242
  "x-device-id": localConfig.deviceId,
241
243
  "x-app-version": packageJson.version,
@@ -343,6 +345,20 @@ async function cloudApiFactory({ logger }, token) {
343
345
  throw error;
344
346
  }
345
347
  },
348
+ async listLinkEnvironments({ name: name2 }) {
349
+ try {
350
+ const response = await axiosCloudAPI.get(`/projects/${name2}/environments-linkable`);
351
+ if (response.status !== 200) {
352
+ throw new Error("Error fetching cloud environments from the server.");
353
+ }
354
+ return response;
355
+ } catch (error) {
356
+ logger.debug(
357
+ "🥲 Oops! Couldn't retrieve your project's environments from the server. Please try again."
358
+ );
359
+ throw error;
360
+ }
361
+ },
346
362
  async getProject({ name: name2 }) {
347
363
  try {
348
364
  const response = await axiosCloudAPI.get(`/projects/${name2}`);
@@ -366,26 +382,43 @@ async function cloudApiFactory({ logger }, token) {
366
382
  };
367
383
  }
368
384
  const LOCAL_SAVE_FILENAME = ".strapi-cloud.json";
385
+ const getFilePath = (directoryPath) => path__default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
369
386
  async function save(data, { directoryPath } = {}) {
370
- const alreadyInFileData = await retrieve({ directoryPath });
371
- const storedData = { ...alreadyInFileData, ...data };
372
- const pathToFile = path__default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
387
+ const pathToFile = getFilePath(directoryPath);
373
388
  await fse__default.ensureDir(path__default.dirname(pathToFile));
374
- await fse__default.writeJson(pathToFile, storedData, { encoding: "utf8" });
389
+ await fse__default.writeJson(pathToFile, data, { encoding: "utf8" });
375
390
  }
376
391
  async function retrieve({
377
392
  directoryPath
378
393
  } = {}) {
379
- const pathToFile = path__default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
394
+ const pathToFile = getFilePath(directoryPath);
380
395
  const pathExists = await fse__default.pathExists(pathToFile);
381
396
  if (!pathExists) {
382
397
  return {};
383
398
  }
384
399
  return fse__default.readJSON(pathToFile, { encoding: "utf8" });
385
400
  }
401
+ async function patch(patchData, { directoryPath } = {}) {
402
+ const pathToFile = getFilePath(directoryPath);
403
+ const existingData = await retrieve({ directoryPath });
404
+ if (!existingData) {
405
+ throw new Error("No configuration data found to patch.");
406
+ }
407
+ const newData = merge(existingData, patchData);
408
+ await fse__default.writeJson(pathToFile, newData, { encoding: "utf8" });
409
+ }
410
+ async function deleteConfig({ directoryPath } = {}) {
411
+ const pathToFile = getFilePath(directoryPath);
412
+ const pathExists = await fse__default.pathExists(pathToFile);
413
+ if (pathExists) {
414
+ await fse__default.remove(pathToFile);
415
+ }
416
+ }
386
417
  const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
387
418
  __proto__: null,
388
419
  LOCAL_SAVE_FILENAME,
420
+ deleteConfig,
421
+ patch,
389
422
  retrieve,
390
423
  save
391
424
  }, Symbol.toStringTag, { value: "Module" }));
@@ -393,7 +426,7 @@ let cliConfig;
393
426
  async function tokenServiceFactory({ logger }) {
394
427
  const cloudApiService = await cloudApiFactory({ logger });
395
428
  async function saveToken(str) {
396
- const appConfig = await getLocalConfig();
429
+ const appConfig = await getLocalConfig$1();
397
430
  if (!appConfig) {
398
431
  logger.error("There was a problem saving your token. Please try again.");
399
432
  return;
@@ -407,7 +440,7 @@ async function tokenServiceFactory({ logger }) {
407
440
  }
408
441
  }
409
442
  async function retrieveToken() {
410
- const appConfig = await getLocalConfig();
443
+ const appConfig = await getLocalConfig$1();
411
444
  if (appConfig.token) {
412
445
  if (await isTokenValid(appConfig.token)) {
413
446
  return appConfig.token;
@@ -469,7 +502,7 @@ async function tokenServiceFactory({ logger }) {
469
502
  }
470
503
  }
471
504
  async function eraseToken() {
472
- const appConfig = await getLocalConfig();
505
+ const appConfig = await getLocalConfig$1();
473
506
  if (!appConfig) {
474
507
  return;
475
508
  }
@@ -490,8 +523,7 @@ async function tokenServiceFactory({ logger }) {
490
523
  logger.log(
491
524
  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."
492
525
  );
493
- if (!await loginAction2(ctx))
494
- return null;
526
+ if (!await loginAction2(ctx)) return null;
495
527
  token = await retrieveToken();
496
528
  }
497
529
  return token;
@@ -915,7 +947,7 @@ async function createProject$1(ctx, cloudApi, projectInput) {
915
947
  throw e;
916
948
  }
917
949
  }
918
- const action$5 = async (ctx) => {
950
+ const action$6 = async (ctx) => {
919
951
  const { logger } = ctx;
920
952
  const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
921
953
  const token = await getValidToken(ctx, promptLogin);
@@ -1044,7 +1076,14 @@ const buildLogsServiceFactory = ({ logger }) => {
1044
1076
  });
1045
1077
  };
1046
1078
  };
1047
- const QUIT_OPTION$1 = "Quit";
1079
+ const boxenOptions = {
1080
+ padding: 1,
1081
+ margin: 1,
1082
+ align: "center",
1083
+ borderColor: "yellow",
1084
+ borderStyle: "round"
1085
+ };
1086
+ const QUIT_OPTION$2 = "Quit";
1048
1087
  async function promptForEnvironment(environments) {
1049
1088
  const choices = environments.map((env2) => ({ name: env2, value: env2 }));
1050
1089
  const { selectedEnvironment } = await inquirer.prompt([
@@ -1052,22 +1091,12 @@ async function promptForEnvironment(environments) {
1052
1091
  type: "list",
1053
1092
  name: "selectedEnvironment",
1054
1093
  message: "Select the environment to deploy:",
1055
- choices: [...choices, { name: chalk.grey(`(${QUIT_OPTION$1})`), value: null }]
1094
+ choices: [...choices, { name: chalk.grey(`(${QUIT_OPTION$2})`), value: null }]
1056
1095
  }
1057
1096
  ]);
1058
1097
  if (selectedEnvironment === null) {
1059
1098
  process.exit(1);
1060
1099
  }
1061
- const { confirm } = await inquirer.prompt([
1062
- {
1063
- type: "confirm",
1064
- name: "confirm",
1065
- message: `Do you want to proceed with deployment to ${chalk.cyan(selectedEnvironment)}?`
1066
- }
1067
- ]);
1068
- if (!confirm) {
1069
- process.exit(1);
1070
- }
1071
1100
  return selectedEnvironment;
1072
1101
  }
1073
1102
  async function upload(ctx, project, token, maxProjectFileSize) {
@@ -1148,11 +1177,11 @@ async function upload(ctx, project, token, maxProjectFileSize) {
1148
1177
  process.exit(1);
1149
1178
  }
1150
1179
  }
1151
- async function getProject$1(ctx) {
1180
+ async function getProject(ctx) {
1152
1181
  const { project } = await retrieve();
1153
1182
  if (!project) {
1154
1183
  try {
1155
- return await action$5(ctx);
1184
+ return await action$6(ctx);
1156
1185
  } catch (e) {
1157
1186
  ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1158
1187
  ctx.logger.debug(e);
@@ -1173,24 +1202,54 @@ async function getConfig({
1173
1202
  return null;
1174
1203
  }
1175
1204
  }
1176
- const action$4 = async (ctx, opts) => {
1205
+ function validateEnvironment(ctx, environment, environments) {
1206
+ if (!environments.includes(environment)) {
1207
+ ctx.logger.error(`Environment ${environment} does not exist.`);
1208
+ process.exit(1);
1209
+ }
1210
+ }
1211
+ async function getTargetEnvironment(ctx, opts, project, environments) {
1212
+ if (opts.env) {
1213
+ validateEnvironment(ctx, opts.env, environments);
1214
+ return opts.env;
1215
+ }
1216
+ if (project.targetEnvironment) {
1217
+ return project.targetEnvironment;
1218
+ }
1219
+ if (environments.length > 1) {
1220
+ return promptForEnvironment(environments);
1221
+ }
1222
+ return environments[0];
1223
+ }
1224
+ function hasPendingOrLiveDeployment(environments, targetEnvironment) {
1225
+ const environment = environments.find((env2) => env2.name === targetEnvironment);
1226
+ if (!environment) {
1227
+ throw new Error(`Environment details ${targetEnvironment} not found.`);
1228
+ }
1229
+ return environment.hasPendingDeployment || environment.hasLiveDeployment || false;
1230
+ }
1231
+ const action$5 = async (ctx, opts) => {
1177
1232
  const { getValidToken } = await tokenServiceFactory(ctx);
1178
1233
  const token = await getValidToken(ctx, promptLogin);
1179
1234
  if (!token) {
1180
1235
  return;
1181
1236
  }
1182
- const project = await getProject$1(ctx);
1237
+ const project = await getProject(ctx);
1183
1238
  if (!project) {
1184
1239
  return;
1185
1240
  }
1186
1241
  const cloudApiService = await cloudApiFactory(ctx, token);
1242
+ let projectData;
1187
1243
  let environments;
1244
+ let environmentsDetails;
1188
1245
  try {
1189
1246
  const {
1190
- data: { data: projectData, metadata }
1247
+ data: { data, metadata }
1191
1248
  } = await cloudApiService.getProject({ name: project.name });
1192
- const isProjectSuspended = projectData.suspendedAt;
1249
+ projectData = data;
1193
1250
  environments = projectData.environments;
1251
+ environmentsDetails = projectData.environmentsDetails;
1252
+ const isProjectSuspended = projectData.suspendedAt;
1194
1253
  if (isProjectSuspended) {
1195
1254
  ctx.logger.log(
1196
1255
  "\n Oops! This project has been suspended. \n\n Please reactivate it from the dashboard to continue deploying: "
@@ -1237,22 +1296,34 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1237
1296
  );
1238
1297
  maxSize = 1e8;
1239
1298
  }
1240
- let targetEnvironment;
1241
- if (opts.environment) {
1242
- if (!environments.includes(opts.environment)) {
1243
- ctx.logger.error(`Environment ${opts.environment} does not exist.`);
1244
- return;
1299
+ project.targetEnvironment = await getTargetEnvironment(ctx, opts, project, environments);
1300
+ if (!opts.force) {
1301
+ const shouldDisplayWarning = hasPendingOrLiveDeployment(
1302
+ environmentsDetails,
1303
+ project.targetEnvironment
1304
+ );
1305
+ if (shouldDisplayWarning) {
1306
+ ctx.logger.log(boxen(cliConfig2.projectDeployment.confirmationText, boxenOptions));
1307
+ const { confirm } = await inquirer.prompt([
1308
+ {
1309
+ type: "confirm",
1310
+ name: "confirm",
1311
+ message: `Do you want to proceed with deployment to ${chalk.cyan(projectData.displayName)} on ${chalk.cyan(project.targetEnvironment)} environment?`
1312
+ }
1313
+ ]);
1314
+ if (!confirm) {
1315
+ process.exit(1);
1316
+ }
1245
1317
  }
1246
- targetEnvironment = opts.environment;
1247
- } else {
1248
- targetEnvironment = environments.length > 1 ? await promptForEnvironment(environments) : environments[0];
1249
1318
  }
1250
- project.targetEnvironment = targetEnvironment;
1251
1319
  const buildId = await upload(ctx, project, token, maxSize);
1252
1320
  if (!buildId) {
1253
1321
  return;
1254
1322
  }
1255
1323
  try {
1324
+ ctx.logger.log(
1325
+ `🚀 Deploying project to ${chalk.cyan(project.targetEnvironment ?? `production`)} environment...`
1326
+ );
1256
1327
  notificationService(`${apiConfig.apiBaseUrl}/notifications`, token, cliConfig2);
1257
1328
  await buildLogsService(`${apiConfig.apiBaseUrl}/v1/logs/${buildId}`, token, cliConfig2);
1258
1329
  ctx.logger.log(
@@ -1297,17 +1368,16 @@ const runAction = (name2, action2) => (...args) => {
1297
1368
  process.exit(1);
1298
1369
  });
1299
1370
  };
1300
- const command$6 = ({ ctx }) => {
1301
- 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").option("-e, --environment <name>", "Specify the environment to deploy").action((opts) => runAction("deploy", action$4)(ctx, opts));
1371
+ const command$7 = ({ ctx }) => {
1372
+ 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").option("-f, --force", "Skip confirmation to deploy").option("-e, --env <name>", "Specify the environment to deploy").action((opts) => runAction("deploy", action$5)(ctx, opts));
1302
1373
  };
1303
1374
  const deployProject = {
1304
1375
  name: "deploy-project",
1305
1376
  description: "Deploy a Strapi Cloud project",
1306
- action: action$4,
1307
- command: command$6
1377
+ action: action$5,
1378
+ command: command$7
1308
1379
  };
1309
- const QUIT_OPTION = "Quit";
1310
- async function getExistingConfig(ctx) {
1380
+ async function getLocalConfig(ctx) {
1311
1381
  try {
1312
1382
  return await retrieve();
1313
1383
  } catch (e) {
@@ -1316,6 +1386,21 @@ async function getExistingConfig(ctx) {
1316
1386
  return null;
1317
1387
  }
1318
1388
  }
1389
+ async function getLocalProject(ctx) {
1390
+ const localConfig = await getLocalConfig(ctx);
1391
+ if (!localConfig || !localConfig.project) {
1392
+ ctx.logger.warn(
1393
+ `
1394
+ We couldn't find a valid local project config.
1395
+ Please link your local project to an existing Strapi Cloud project using the ${chalk.cyan(
1396
+ "link"
1397
+ )} command.`
1398
+ );
1399
+ process.exit(1);
1400
+ }
1401
+ return localConfig.project;
1402
+ }
1403
+ const QUIT_OPTION$1 = "Quit";
1319
1404
  async function promptForRelink(ctx, cloudApiService, existingConfig) {
1320
1405
  if (existingConfig && existingConfig.project) {
1321
1406
  const { shouldRelink } = await inquirer.prompt([
@@ -1345,7 +1430,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1345
1430
  } = await cloudApiService.listLinkProjects();
1346
1431
  spinner.succeed();
1347
1432
  if (!Array.isArray(projectList)) {
1348
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1433
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1349
1434
  return null;
1350
1435
  }
1351
1436
  const projects = projectList.filter(
@@ -1357,7 +1442,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1357
1442
  };
1358
1443
  });
1359
1444
  if (projects.length === 0) {
1360
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1445
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1361
1446
  return null;
1362
1447
  }
1363
1448
  return projects;
@@ -1375,7 +1460,7 @@ async function getUserSelection(ctx, projects) {
1375
1460
  type: "list",
1376
1461
  name: "linkProject",
1377
1462
  message: "Which project do you want to link?",
1378
- choices: [...projects, { name: chalk.grey(`(${QUIT_OPTION})`), value: null }]
1463
+ choices: [...projects, { name: chalk.grey(`(${QUIT_OPTION$1})`), value: null }]
1379
1464
  }
1380
1465
  ]);
1381
1466
  if (!answer.linkProject) {
@@ -1388,7 +1473,7 @@ async function getUserSelection(ctx, projects) {
1388
1473
  return null;
1389
1474
  }
1390
1475
  }
1391
- const action$3 = async (ctx) => {
1476
+ const action$4 = async (ctx) => {
1392
1477
  const { getValidToken } = await tokenServiceFactory(ctx);
1393
1478
  const token = await getValidToken(ctx, promptLogin);
1394
1479
  const { logger } = ctx;
@@ -1396,7 +1481,7 @@ const action$3 = async (ctx) => {
1396
1481
  return;
1397
1482
  }
1398
1483
  const cloudApiService = await cloudApiFactory(ctx, token);
1399
- const existingConfig = await getExistingConfig(ctx);
1484
+ const existingConfig = await getLocalConfig(ctx);
1400
1485
  const shouldRelink = await promptForRelink(ctx, cloudApiService, existingConfig);
1401
1486
  if (!shouldRelink) {
1402
1487
  return;
@@ -1431,7 +1516,9 @@ const action$3 = async (ctx) => {
1431
1516
  return;
1432
1517
  }
1433
1518
  await save({ project: answer.linkProject });
1434
- logger.log(`Project ${chalk.cyan(answer.linkProject.displayName)} linked successfully.`);
1519
+ logger.log(
1520
+ ` You have successfully linked your project to ${chalk.cyan(answer.linkProject.displayName)}. You are now able to deploy your project.`
1521
+ );
1435
1522
  await trackEvent(ctx, cloudApiService, "didLinkProject", {
1436
1523
  projectInternalName: answer.linkProject
1437
1524
  });
@@ -1443,16 +1530,16 @@ const action$3 = async (ctx) => {
1443
1530
  });
1444
1531
  }
1445
1532
  };
1446
- const command$5 = ({ command: command2, ctx }) => {
1447
- 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$3)(ctx));
1533
+ const command$6 = ({ command: command2, ctx }) => {
1534
+ 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));
1448
1535
  };
1449
1536
  const link = {
1450
1537
  name: "link-project",
1451
1538
  description: "Link a local directory to a Strapi Cloud project",
1452
- action: action$3,
1453
- command: command$5
1539
+ action: action$4,
1540
+ command: command$6
1454
1541
  };
1455
- const command$4 = ({ ctx }) => {
1542
+ const command$5 = ({ ctx }) => {
1456
1543
  return createCommand("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1457
1544
  "after",
1458
1545
  "\nAfter running this command, you will be prompted to enter your authentication information."
@@ -1462,10 +1549,10 @@ const login = {
1462
1549
  name: "login",
1463
1550
  description: "Strapi Cloud Login",
1464
1551
  action: loginAction,
1465
- command: command$4
1552
+ command: command$5
1466
1553
  };
1467
1554
  const openModule = import("open");
1468
- const action$2 = async (ctx) => {
1555
+ const action$3 = async (ctx) => {
1469
1556
  const { logger } = ctx;
1470
1557
  const { retrieveToken, eraseToken } = await tokenServiceFactory(ctx);
1471
1558
  const token = await retrieveToken();
@@ -1497,25 +1584,25 @@ const action$2 = async (ctx) => {
1497
1584
  }
1498
1585
  await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1499
1586
  };
1500
- const command$3 = ({ ctx }) => {
1501
- 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$2)(ctx));
1587
+ const command$4 = ({ ctx }) => {
1588
+ 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$3)(ctx));
1502
1589
  };
1503
1590
  const logout = {
1504
1591
  name: "logout",
1505
1592
  description: "Strapi Cloud Logout",
1506
- action: action$2,
1507
- command: command$3
1593
+ action: action$3,
1594
+ command: command$4
1508
1595
  };
1509
- const command$2 = ({ ctx }) => {
1510
- 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$5)(ctx));
1596
+ const command$3 = ({ ctx }) => {
1597
+ 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$6)(ctx));
1511
1598
  };
1512
1599
  const createProject = {
1513
1600
  name: "create-project",
1514
1601
  description: "Create a new project",
1515
- action: action$5,
1516
- command: command$2
1602
+ action: action$6,
1603
+ command: command$3
1517
1604
  };
1518
- const action$1 = async (ctx) => {
1605
+ const action$2 = async (ctx) => {
1519
1606
  const { getValidToken } = await tokenServiceFactory(ctx);
1520
1607
  const token = await getValidToken(ctx, promptLogin);
1521
1608
  const { logger } = ctx;
@@ -1535,37 +1622,23 @@ const action$1 = async (ctx) => {
1535
1622
  spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1536
1623
  }
1537
1624
  };
1538
- const command$1 = ({ command: command2, ctx }) => {
1539
- 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$1)(ctx));
1625
+ const command$2 = ({ command: command2, ctx }) => {
1626
+ 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));
1540
1627
  };
1541
1628
  const listProjects = {
1542
1629
  name: "list-projects",
1543
1630
  description: "List Strapi Cloud projects",
1544
- action: action$1,
1545
- command: command$1
1631
+ action: action$2,
1632
+ command: command$2
1546
1633
  };
1547
- async function getProject(ctx) {
1548
- const { project } = await retrieve();
1549
- if (!project) {
1550
- ctx.logger.warn(
1551
- `
1552
- We couldn't find a valid local project config.
1553
- Please link your local project to an existing Strapi Cloud project using the ${chalk.cyan(
1554
- "link"
1555
- )} command`
1556
- );
1557
- process.exit(1);
1558
- }
1559
- return project;
1560
- }
1561
- const action = async (ctx) => {
1634
+ const action$1 = async (ctx) => {
1562
1635
  const { getValidToken } = await tokenServiceFactory(ctx);
1563
1636
  const token = await getValidToken(ctx, promptLogin);
1564
1637
  const { logger } = ctx;
1565
1638
  if (!token) {
1566
1639
  return;
1567
1640
  }
1568
- const project = await getProject(ctx);
1641
+ const project = await getLocalProject(ctx);
1569
1642
  if (!project) {
1570
1643
  ctx.logger.debug(`No valid local project configuration was found.`);
1571
1644
  return;
@@ -1605,7 +1678,7 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1605
1678
  };
1606
1679
  function defineCloudNamespace(command2, ctx) {
1607
1680
  const cloud = command2.command("cloud").description("Manage Strapi Cloud projects");
1608
- cloud.command("environments").description("Alias for cloud environment list").action(() => runAction("list", action)(ctx));
1681
+ cloud.command("environments").description("Alias for cloud environment list").action(() => runAction("list", action$1)(ctx));
1609
1682
  return cloud;
1610
1683
  }
1611
1684
  let environmentCmd = null;
@@ -1616,13 +1689,127 @@ const initializeEnvironmentCommand = (command2, ctx) => {
1616
1689
  }
1617
1690
  return environmentCmd;
1618
1691
  };
1619
- const command = ({ command: command2, ctx }) => {
1692
+ const command$1 = ({ command: command2, ctx }) => {
1620
1693
  const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1621
- 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)(ctx));
1694
+ 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));
1622
1695
  };
1623
1696
  const listEnvironments = {
1624
1697
  name: "list-environments",
1625
1698
  description: "List Strapi Cloud environments",
1699
+ action: action$1,
1700
+ command: command$1
1701
+ };
1702
+ const QUIT_OPTION = "Quit";
1703
+ const action = async (ctx) => {
1704
+ const { getValidToken } = await tokenServiceFactory(ctx);
1705
+ const token = await getValidToken(ctx, promptLogin);
1706
+ const { logger } = ctx;
1707
+ if (!token) {
1708
+ return;
1709
+ }
1710
+ const project = await getLocalProject(ctx);
1711
+ if (!project) {
1712
+ logger.debug(`No valid local project configuration was found.`);
1713
+ return;
1714
+ }
1715
+ const cloudApiService = await cloudApiFactory(ctx, token);
1716
+ const environments = await getEnvironmentsList(ctx, cloudApiService, project);
1717
+ if (!environments) {
1718
+ logger.debug(`Fetching environments failed.`);
1719
+ return;
1720
+ }
1721
+ if (environments.length === 0) {
1722
+ logger.log(
1723
+ `The only available environment is already linked. You can add a new one from your project settings on the Strapi Cloud dashboard.`
1724
+ );
1725
+ return;
1726
+ }
1727
+ const answer = await promptUserForEnvironment(ctx, environments);
1728
+ if (!answer) {
1729
+ return;
1730
+ }
1731
+ await trackEvent(ctx, cloudApiService, "willLinkEnvironment", {
1732
+ projectName: project.name,
1733
+ environmentName: answer.targetEnvironment
1734
+ });
1735
+ try {
1736
+ await patch({ project: { targetEnvironment: answer.targetEnvironment } });
1737
+ } catch (e) {
1738
+ await trackEvent(ctx, cloudApiService, "didNotLinkEnvironment", {
1739
+ projectName: project.name,
1740
+ environmentName: answer.targetEnvironment
1741
+ });
1742
+ logger.debug("Failed to link environment", e);
1743
+ logger.error(
1744
+ "Failed to link the environment. If this issue persists, try re-linking your project or contact support."
1745
+ );
1746
+ process.exit(1);
1747
+ }
1748
+ logger.log(
1749
+ ` You have successfully linked your project to ${chalk.cyan(answer.targetEnvironment)}, on ${chalk.cyan(project.displayName)}. You are now able to deploy your project.`
1750
+ );
1751
+ await trackEvent(ctx, cloudApiService, "didLinkEnvironment", {
1752
+ projectName: project.name,
1753
+ environmentName: answer.targetEnvironment
1754
+ });
1755
+ };
1756
+ async function promptUserForEnvironment(ctx, environments) {
1757
+ const { logger } = ctx;
1758
+ try {
1759
+ const answer = await inquirer.prompt([
1760
+ {
1761
+ type: "list",
1762
+ name: "targetEnvironment",
1763
+ message: "Which environment do you want to link?",
1764
+ choices: [...environments, { name: chalk.grey(`(${QUIT_OPTION})`), value: null }]
1765
+ }
1766
+ ]);
1767
+ if (!answer.targetEnvironment) {
1768
+ return null;
1769
+ }
1770
+ return answer;
1771
+ } catch (e) {
1772
+ logger.debug("Failed to get user input", e);
1773
+ logger.error("An error occurred while trying to get your environment selection.");
1774
+ return null;
1775
+ }
1776
+ }
1777
+ async function getEnvironmentsList(ctx, cloudApiService, project) {
1778
+ const spinner = ctx.logger.spinner("Fetching environments...\n").start();
1779
+ try {
1780
+ const {
1781
+ data: { data: environmentsList }
1782
+ } = await cloudApiService.listLinkEnvironments({ name: project.name });
1783
+ if (!Array.isArray(environmentsList) || environmentsList.length === 0) {
1784
+ throw new Error("Environments not found in server response");
1785
+ }
1786
+ spinner.succeed();
1787
+ return environmentsList.filter(
1788
+ (environment) => environment.name !== project.targetEnvironment
1789
+ );
1790
+ } catch (e) {
1791
+ if (e.response && e.response.status === 404) {
1792
+ spinner.succeed();
1793
+ ctx.logger.warn(
1794
+ `
1795
+ The project associated with this folder does not exist in Strapi Cloud.
1796
+ Please link your local project to an existing Strapi Cloud project using the ${chalk.cyan(
1797
+ "link"
1798
+ )} command.`
1799
+ );
1800
+ } else {
1801
+ spinner.fail("An error occurred while fetching environments data from Strapi Cloud.");
1802
+ ctx.logger.debug("Failed to list environments", e);
1803
+ }
1804
+ }
1805
+ }
1806
+ const command = ({ command: command2, ctx }) => {
1807
+ const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1808
+ 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));
1809
+ };
1810
+ const linkEnvironment = {
1811
+ name: "link-environment",
1812
+ description: "Link Strapi Cloud environment to a local project",
1626
1813
  action,
1627
1814
  command
1628
1815
  };
@@ -1632,12 +1819,21 @@ const cli = {
1632
1819
  login,
1633
1820
  logout,
1634
1821
  createProject,
1822
+ linkEnvironment,
1635
1823
  listProjects,
1636
1824
  listEnvironments
1637
1825
  };
1638
- const cloudCommands = [deployProject, link, login, logout, listProjects, listEnvironments];
1826
+ const cloudCommands = [
1827
+ deployProject,
1828
+ link,
1829
+ login,
1830
+ logout,
1831
+ linkEnvironment,
1832
+ listProjects,
1833
+ listEnvironments
1834
+ ];
1639
1835
  async function initCloudCLIConfig() {
1640
- const localConfig = await getLocalConfig();
1836
+ const localConfig = await getLocalConfig$1();
1641
1837
  if (!localConfig.deviceId) {
1642
1838
  localConfig.deviceId = crypto$1.randomUUID();
1643
1839
  }