@strapi/cloud-cli 5.3.0 → 5.4.1

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.js CHANGED
@@ -25,6 +25,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
25
25
  const crypto$1 = require("crypto");
26
26
  const fse = require("fs-extra");
27
27
  const inquirer = require("inquirer");
28
+ const boxen = require("boxen");
28
29
  const path = require("path");
29
30
  const chalk = require("chalk");
30
31
  const axios = require("axios");
@@ -35,6 +36,7 @@ const minimatch = require("minimatch");
35
36
  const fp = require("lodash/fp");
36
37
  const os = require("os");
37
38
  const XDGAppPaths = require("xdg-app-paths");
39
+ const lodash = require("lodash");
38
40
  const jwksClient = require("jwks-rsa");
39
41
  const jwt = require("jsonwebtoken");
40
42
  const stringify = require("fast-safe-stringify");
@@ -66,6 +68,7 @@ function _interopNamespace(e) {
66
68
  const crypto__default = /* @__PURE__ */ _interopDefault(crypto$1);
67
69
  const fse__namespace = /* @__PURE__ */ _interopNamespace(fse);
68
70
  const inquirer__default = /* @__PURE__ */ _interopDefault(inquirer);
71
+ const boxen__default = /* @__PURE__ */ _interopDefault(boxen);
69
72
  const path__namespace = /* @__PURE__ */ _interopNamespace(path);
70
73
  const chalk__default = /* @__PURE__ */ _interopDefault(chalk);
71
74
  const axios__default = /* @__PURE__ */ _interopDefault(axios);
@@ -177,7 +180,7 @@ async function getConfigPath() {
177
180
  }
178
181
  return configPath;
179
182
  }
180
- async function getLocalConfig() {
183
+ async function getLocalConfig$1() {
181
184
  const configPath = await getConfigPath();
182
185
  const configFilePath = path__namespace.default.join(configPath, CONFIG_FILENAME);
183
186
  await fse__namespace.default.ensureFile(configFilePath);
@@ -193,7 +196,7 @@ async function saveLocalConfig(data) {
193
196
  await fse__namespace.default.writeJson(configFilePath, data, { encoding: "utf8", spaces: 2, mode: 384 });
194
197
  }
195
198
  const name = "@strapi/cloud-cli";
196
- const version = "5.2.0";
199
+ const version = "5.4.0";
197
200
  const description = "Commands to interact with the Strapi Cloud";
198
201
  const keywords = [
199
202
  "strapi",
@@ -238,8 +241,9 @@ const scripts = {
238
241
  watch: "pack-up watch"
239
242
  };
240
243
  const dependencies = {
241
- "@strapi/utils": "workspace:*",
244
+ "@strapi/utils": "5.4.0",
242
245
  axios: "1.7.4",
246
+ boxen: "5.1.2",
243
247
  chalk: "4.1.2",
244
248
  "cli-progress": "3.12.0",
245
249
  commander: "8.3.0",
@@ -263,13 +267,14 @@ const devDependencies = {
263
267
  "@types/cli-progress": "3.11.5",
264
268
  "@types/eventsource": "1.1.15",
265
269
  "@types/lodash": "^4.14.191",
266
- "eslint-config-custom": "workspace:*",
267
- tsconfig: "workspace:*"
270
+ "eslint-config-custom": "5.4.0",
271
+ tsconfig: "5.4.0"
268
272
  };
269
273
  const engines = {
270
274
  node: ">=18.0.0 <=22.x.x",
271
275
  npm: ">=6.0.0"
272
276
  };
277
+ const gitHead = "7d785703f52464577d077c4618cbe68b44f8a9cd";
273
278
  const packageJson = {
274
279
  name,
275
280
  version,
@@ -290,11 +295,12 @@ const packageJson = {
290
295
  scripts,
291
296
  dependencies,
292
297
  devDependencies,
293
- engines
298
+ engines,
299
+ gitHead
294
300
  };
295
301
  const VERSION = "v1";
296
302
  async function cloudApiFactory({ logger }, token) {
297
- const localConfig = await getLocalConfig();
303
+ const localConfig = await getLocalConfig$1();
298
304
  const customHeaders = {
299
305
  "x-device-id": localConfig.deviceId,
300
306
  "x-app-version": packageJson.version,
@@ -402,6 +408,20 @@ async function cloudApiFactory({ logger }, token) {
402
408
  throw error;
403
409
  }
404
410
  },
411
+ async listLinkEnvironments({ name: name2 }) {
412
+ try {
413
+ const response = await axiosCloudAPI.get(`/projects/${name2}/environments-linkable`);
414
+ if (response.status !== 200) {
415
+ throw new Error("Error fetching cloud environments from the server.");
416
+ }
417
+ return response;
418
+ } catch (error) {
419
+ logger.debug(
420
+ "🥲 Oops! Couldn't retrieve your project's environments from the server. Please try again."
421
+ );
422
+ throw error;
423
+ }
424
+ },
405
425
  async getProject({ name: name2 }) {
406
426
  try {
407
427
  const response = await axiosCloudAPI.get(`/projects/${name2}`);
@@ -425,26 +445,43 @@ async function cloudApiFactory({ logger }, token) {
425
445
  };
426
446
  }
427
447
  const LOCAL_SAVE_FILENAME = ".strapi-cloud.json";
448
+ const getFilePath = (directoryPath) => path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
428
449
  async function save(data, { directoryPath } = {}) {
429
- const alreadyInFileData = await retrieve({ directoryPath });
430
- const storedData = { ...alreadyInFileData, ...data };
431
- const pathToFile = path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
450
+ const pathToFile = getFilePath(directoryPath);
432
451
  await fse__namespace.default.ensureDir(path__namespace.default.dirname(pathToFile));
433
- await fse__namespace.default.writeJson(pathToFile, storedData, { encoding: "utf8" });
452
+ await fse__namespace.default.writeJson(pathToFile, data, { encoding: "utf8" });
434
453
  }
435
454
  async function retrieve({
436
455
  directoryPath
437
456
  } = {}) {
438
- const pathToFile = path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
457
+ const pathToFile = getFilePath(directoryPath);
439
458
  const pathExists = await fse__namespace.default.pathExists(pathToFile);
440
459
  if (!pathExists) {
441
460
  return {};
442
461
  }
443
462
  return fse__namespace.default.readJSON(pathToFile, { encoding: "utf8" });
444
463
  }
464
+ async function patch(patchData, { directoryPath } = {}) {
465
+ const pathToFile = getFilePath(directoryPath);
466
+ const existingData = await retrieve({ directoryPath });
467
+ if (!existingData) {
468
+ throw new Error("No configuration data found to patch.");
469
+ }
470
+ const newData = lodash.merge(existingData, patchData);
471
+ await fse__namespace.default.writeJson(pathToFile, newData, { encoding: "utf8" });
472
+ }
473
+ async function deleteConfig({ directoryPath } = {}) {
474
+ const pathToFile = getFilePath(directoryPath);
475
+ const pathExists = await fse__namespace.default.pathExists(pathToFile);
476
+ if (pathExists) {
477
+ await fse__namespace.default.remove(pathToFile);
478
+ }
479
+ }
445
480
  const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
446
481
  __proto__: null,
447
482
  LOCAL_SAVE_FILENAME,
483
+ deleteConfig,
484
+ patch,
448
485
  retrieve,
449
486
  save
450
487
  }, Symbol.toStringTag, { value: "Module" }));
@@ -452,7 +489,7 @@ let cliConfig;
452
489
  async function tokenServiceFactory({ logger }) {
453
490
  const cloudApiService = await cloudApiFactory({ logger });
454
491
  async function saveToken(str) {
455
- const appConfig = await getLocalConfig();
492
+ const appConfig = await getLocalConfig$1();
456
493
  if (!appConfig) {
457
494
  logger.error("There was a problem saving your token. Please try again.");
458
495
  return;
@@ -466,7 +503,7 @@ async function tokenServiceFactory({ logger }) {
466
503
  }
467
504
  }
468
505
  async function retrieveToken() {
469
- const appConfig = await getLocalConfig();
506
+ const appConfig = await getLocalConfig$1();
470
507
  if (appConfig.token) {
471
508
  if (await isTokenValid(appConfig.token)) {
472
509
  return appConfig.token;
@@ -528,7 +565,7 @@ async function tokenServiceFactory({ logger }) {
528
565
  }
529
566
  }
530
567
  async function eraseToken() {
531
- const appConfig = await getLocalConfig();
568
+ const appConfig = await getLocalConfig$1();
532
569
  if (!appConfig) {
533
570
  return;
534
571
  }
@@ -974,7 +1011,7 @@ async function createProject$1(ctx, cloudApi, projectInput) {
974
1011
  throw e;
975
1012
  }
976
1013
  }
977
- const action$5 = async (ctx) => {
1014
+ const action$6 = async (ctx) => {
978
1015
  const { logger } = ctx;
979
1016
  const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
980
1017
  const token = await getValidToken(ctx, promptLogin);
@@ -1103,7 +1140,14 @@ const buildLogsServiceFactory = ({ logger }) => {
1103
1140
  });
1104
1141
  };
1105
1142
  };
1106
- const QUIT_OPTION$1 = "Quit";
1143
+ const boxenOptions = {
1144
+ padding: 1,
1145
+ margin: 1,
1146
+ align: "center",
1147
+ borderColor: "yellow",
1148
+ borderStyle: "round"
1149
+ };
1150
+ const QUIT_OPTION$2 = "Quit";
1107
1151
  async function promptForEnvironment(environments) {
1108
1152
  const choices = environments.map((env) => ({ name: env, value: env }));
1109
1153
  const { selectedEnvironment } = await inquirer__default.default.prompt([
@@ -1111,22 +1155,12 @@ async function promptForEnvironment(environments) {
1111
1155
  type: "list",
1112
1156
  name: "selectedEnvironment",
1113
1157
  message: "Select the environment to deploy:",
1114
- choices: [...choices, { name: chalk__default.default.grey(`(${QUIT_OPTION$1})`), value: null }]
1158
+ choices: [...choices, { name: chalk__default.default.grey(`(${QUIT_OPTION$2})`), value: null }]
1115
1159
  }
1116
1160
  ]);
1117
1161
  if (selectedEnvironment === null) {
1118
1162
  process.exit(1);
1119
1163
  }
1120
- const { confirm } = await inquirer__default.default.prompt([
1121
- {
1122
- type: "confirm",
1123
- name: "confirm",
1124
- message: `Do you want to proceed with deployment to ${chalk__default.default.cyan(selectedEnvironment)}?`
1125
- }
1126
- ]);
1127
- if (!confirm) {
1128
- process.exit(1);
1129
- }
1130
1164
  return selectedEnvironment;
1131
1165
  }
1132
1166
  async function upload(ctx, project, token, maxProjectFileSize) {
@@ -1207,11 +1241,11 @@ async function upload(ctx, project, token, maxProjectFileSize) {
1207
1241
  process.exit(1);
1208
1242
  }
1209
1243
  }
1210
- async function getProject$1(ctx) {
1244
+ async function getProject(ctx) {
1211
1245
  const { project } = await retrieve();
1212
1246
  if (!project) {
1213
1247
  try {
1214
- return await action$5(ctx);
1248
+ return await action$6(ctx);
1215
1249
  } catch (e) {
1216
1250
  ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1217
1251
  ctx.logger.debug(e);
@@ -1232,24 +1266,54 @@ async function getConfig({
1232
1266
  return null;
1233
1267
  }
1234
1268
  }
1235
- const action$4 = async (ctx, opts) => {
1269
+ function validateEnvironment(ctx, environment, environments) {
1270
+ if (!environments.includes(environment)) {
1271
+ ctx.logger.error(`Environment ${environment} does not exist.`);
1272
+ process.exit(1);
1273
+ }
1274
+ }
1275
+ async function getTargetEnvironment(ctx, opts, project, environments) {
1276
+ if (opts.env) {
1277
+ validateEnvironment(ctx, opts.env, environments);
1278
+ return opts.env;
1279
+ }
1280
+ if (project.targetEnvironment) {
1281
+ return project.targetEnvironment;
1282
+ }
1283
+ if (environments.length > 1) {
1284
+ return promptForEnvironment(environments);
1285
+ }
1286
+ return environments[0];
1287
+ }
1288
+ function hasPendingOrLiveDeployment(environments, targetEnvironment) {
1289
+ const environment = environments.find((env) => env.name === targetEnvironment);
1290
+ if (!environment) {
1291
+ throw new Error(`Environment details ${targetEnvironment} not found.`);
1292
+ }
1293
+ return environment.hasPendingDeployment || environment.hasLiveDeployment || false;
1294
+ }
1295
+ const action$5 = async (ctx, opts) => {
1236
1296
  const { getValidToken } = await tokenServiceFactory(ctx);
1237
1297
  const token = await getValidToken(ctx, promptLogin);
1238
1298
  if (!token) {
1239
1299
  return;
1240
1300
  }
1241
- const project = await getProject$1(ctx);
1301
+ const project = await getProject(ctx);
1242
1302
  if (!project) {
1243
1303
  return;
1244
1304
  }
1245
1305
  const cloudApiService = await cloudApiFactory(ctx, token);
1306
+ let projectData;
1246
1307
  let environments;
1308
+ let environmentsDetails;
1247
1309
  try {
1248
1310
  const {
1249
- data: { data: projectData, metadata }
1311
+ data: { data, metadata }
1250
1312
  } = await cloudApiService.getProject({ name: project.name });
1251
- const isProjectSuspended = projectData.suspendedAt;
1313
+ projectData = data;
1252
1314
  environments = projectData.environments;
1315
+ environmentsDetails = projectData.environmentsDetails;
1316
+ const isProjectSuspended = projectData.suspendedAt;
1253
1317
  if (isProjectSuspended) {
1254
1318
  ctx.logger.log(
1255
1319
  "\n Oops! This project has been suspended. \n\n Please reactivate it from the dashboard to continue deploying: "
@@ -1296,22 +1360,34 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1296
1360
  );
1297
1361
  maxSize = 1e8;
1298
1362
  }
1299
- let targetEnvironment;
1300
- if (opts.environment) {
1301
- if (!environments.includes(opts.environment)) {
1302
- ctx.logger.error(`Environment ${opts.environment} does not exist.`);
1303
- return;
1363
+ project.targetEnvironment = await getTargetEnvironment(ctx, opts, project, environments);
1364
+ if (!opts.force) {
1365
+ const shouldDisplayWarning = hasPendingOrLiveDeployment(
1366
+ environmentsDetails,
1367
+ project.targetEnvironment
1368
+ );
1369
+ if (shouldDisplayWarning) {
1370
+ ctx.logger.log(boxen__default.default(cliConfig2.projectDeployment.confirmationText, boxenOptions));
1371
+ const { confirm } = await inquirer__default.default.prompt([
1372
+ {
1373
+ type: "confirm",
1374
+ name: "confirm",
1375
+ message: `Do you want to proceed with deployment to ${chalk__default.default.cyan(projectData.displayName)} on ${chalk__default.default.cyan(project.targetEnvironment)} environment?`
1376
+ }
1377
+ ]);
1378
+ if (!confirm) {
1379
+ process.exit(1);
1380
+ }
1304
1381
  }
1305
- targetEnvironment = opts.environment;
1306
- } else {
1307
- targetEnvironment = environments.length > 1 ? await promptForEnvironment(environments) : environments[0];
1308
1382
  }
1309
- project.targetEnvironment = targetEnvironment;
1310
1383
  const buildId = await upload(ctx, project, token, maxSize);
1311
1384
  if (!buildId) {
1312
1385
  return;
1313
1386
  }
1314
1387
  try {
1388
+ ctx.logger.log(
1389
+ `🚀 Deploying project to ${chalk__default.default.cyan(project.targetEnvironment ?? `production`)} environment...`
1390
+ );
1315
1391
  notificationService(`${apiConfig.apiBaseUrl}/notifications`, token, cliConfig2);
1316
1392
  await buildLogsService(`${apiConfig.apiBaseUrl}/v1/logs/${buildId}`, token, cliConfig2);
1317
1393
  ctx.logger.log(
@@ -1356,17 +1432,16 @@ const runAction = (name2, action2) => (...args) => {
1356
1432
  process.exit(1);
1357
1433
  });
1358
1434
  };
1359
- const command$6 = ({ ctx }) => {
1360
- return commander.createCommand("cloud:deploy").alias("deploy").description("Deploy a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").option("-e, --environment <name>", "Specify the environment to deploy").action((opts) => runAction("deploy", action$4)(ctx, opts));
1435
+ const command$7 = ({ ctx }) => {
1436
+ return commander.createCommand("cloud:deploy").alias("deploy").description("Deploy a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").option("-f, --force", "Skip confirmation to deploy").option("-e, --env <name>", "Specify the environment to deploy").action((opts) => runAction("deploy", action$5)(ctx, opts));
1361
1437
  };
1362
1438
  const deployProject = {
1363
1439
  name: "deploy-project",
1364
1440
  description: "Deploy a Strapi Cloud project",
1365
- action: action$4,
1366
- command: command$6
1441
+ action: action$5,
1442
+ command: command$7
1367
1443
  };
1368
- const QUIT_OPTION = "Quit";
1369
- async function getExistingConfig(ctx) {
1444
+ async function getLocalConfig(ctx) {
1370
1445
  try {
1371
1446
  return await retrieve();
1372
1447
  } catch (e) {
@@ -1375,6 +1450,21 @@ async function getExistingConfig(ctx) {
1375
1450
  return null;
1376
1451
  }
1377
1452
  }
1453
+ async function getLocalProject(ctx) {
1454
+ const localConfig = await getLocalConfig(ctx);
1455
+ if (!localConfig || !localConfig.project) {
1456
+ ctx.logger.warn(
1457
+ `
1458
+ We couldn't find a valid local project config.
1459
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1460
+ "link"
1461
+ )} command.`
1462
+ );
1463
+ process.exit(1);
1464
+ }
1465
+ return localConfig.project;
1466
+ }
1467
+ const QUIT_OPTION$1 = "Quit";
1378
1468
  async function promptForRelink(ctx, cloudApiService, existingConfig) {
1379
1469
  if (existingConfig && existingConfig.project) {
1380
1470
  const { shouldRelink } = await inquirer__default.default.prompt([
@@ -1404,7 +1494,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1404
1494
  } = await cloudApiService.listLinkProjects();
1405
1495
  spinner.succeed();
1406
1496
  if (!Array.isArray(projectList)) {
1407
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1497
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1408
1498
  return null;
1409
1499
  }
1410
1500
  const projects = projectList.filter(
@@ -1416,7 +1506,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1416
1506
  };
1417
1507
  });
1418
1508
  if (projects.length === 0) {
1419
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1509
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1420
1510
  return null;
1421
1511
  }
1422
1512
  return projects;
@@ -1434,7 +1524,7 @@ async function getUserSelection(ctx, projects) {
1434
1524
  type: "list",
1435
1525
  name: "linkProject",
1436
1526
  message: "Which project do you want to link?",
1437
- choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION})`), value: null }]
1527
+ choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION$1})`), value: null }]
1438
1528
  }
1439
1529
  ]);
1440
1530
  if (!answer.linkProject) {
@@ -1447,7 +1537,7 @@ async function getUserSelection(ctx, projects) {
1447
1537
  return null;
1448
1538
  }
1449
1539
  }
1450
- const action$3 = async (ctx) => {
1540
+ const action$4 = async (ctx) => {
1451
1541
  const { getValidToken } = await tokenServiceFactory(ctx);
1452
1542
  const token = await getValidToken(ctx, promptLogin);
1453
1543
  const { logger } = ctx;
@@ -1455,7 +1545,7 @@ const action$3 = async (ctx) => {
1455
1545
  return;
1456
1546
  }
1457
1547
  const cloudApiService = await cloudApiFactory(ctx, token);
1458
- const existingConfig = await getExistingConfig(ctx);
1548
+ const existingConfig = await getLocalConfig(ctx);
1459
1549
  const shouldRelink = await promptForRelink(ctx, cloudApiService, existingConfig);
1460
1550
  if (!shouldRelink) {
1461
1551
  return;
@@ -1490,7 +1580,9 @@ const action$3 = async (ctx) => {
1490
1580
  return;
1491
1581
  }
1492
1582
  await save({ project: answer.linkProject });
1493
- logger.log(`Project ${chalk__default.default.cyan(answer.linkProject.displayName)} linked successfully.`);
1583
+ logger.log(
1584
+ ` You have successfully linked your project to ${chalk__default.default.cyan(answer.linkProject.displayName)}. You are now able to deploy your project.`
1585
+ );
1494
1586
  await trackEvent(ctx, cloudApiService, "didLinkProject", {
1495
1587
  projectInternalName: answer.linkProject
1496
1588
  });
@@ -1502,16 +1594,16 @@ const action$3 = async (ctx) => {
1502
1594
  });
1503
1595
  }
1504
1596
  };
1505
- const command$5 = ({ command: command2, ctx }) => {
1506
- 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));
1597
+ const command$6 = ({ command: command2, ctx }) => {
1598
+ 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));
1507
1599
  };
1508
1600
  const link = {
1509
1601
  name: "link-project",
1510
1602
  description: "Link a local directory to a Strapi Cloud project",
1511
- action: action$3,
1512
- command: command$5
1603
+ action: action$4,
1604
+ command: command$6
1513
1605
  };
1514
- const command$4 = ({ ctx }) => {
1606
+ const command$5 = ({ ctx }) => {
1515
1607
  return commander.createCommand("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1516
1608
  "after",
1517
1609
  "\nAfter running this command, you will be prompted to enter your authentication information."
@@ -1521,10 +1613,10 @@ const login = {
1521
1613
  name: "login",
1522
1614
  description: "Strapi Cloud Login",
1523
1615
  action: loginAction,
1524
- command: command$4
1616
+ command: command$5
1525
1617
  };
1526
1618
  const openModule = import("open");
1527
- const action$2 = async (ctx) => {
1619
+ const action$3 = async (ctx) => {
1528
1620
  const { logger } = ctx;
1529
1621
  const { retrieveToken, eraseToken } = await tokenServiceFactory(ctx);
1530
1622
  const token = await retrieveToken();
@@ -1556,25 +1648,25 @@ const action$2 = async (ctx) => {
1556
1648
  }
1557
1649
  await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1558
1650
  };
1559
- const command$3 = ({ ctx }) => {
1560
- return commander.createCommand("cloud:logout").alias("logout").description("Strapi Cloud Logout").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("logout", action$2)(ctx));
1651
+ const command$4 = ({ ctx }) => {
1652
+ return commander.createCommand("cloud:logout").alias("logout").description("Strapi Cloud Logout").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("logout", action$3)(ctx));
1561
1653
  };
1562
1654
  const logout = {
1563
1655
  name: "logout",
1564
1656
  description: "Strapi Cloud Logout",
1565
- action: action$2,
1566
- command: command$3
1657
+ action: action$3,
1658
+ command: command$4
1567
1659
  };
1568
- const command$2 = ({ ctx }) => {
1569
- return commander.createCommand("cloud:create-project").description("Create a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("cloud:create-project", action$5)(ctx));
1660
+ const command$3 = ({ ctx }) => {
1661
+ return commander.createCommand("cloud:create-project").description("Create a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("cloud:create-project", action$6)(ctx));
1570
1662
  };
1571
1663
  const createProject = {
1572
1664
  name: "create-project",
1573
1665
  description: "Create a new project",
1574
- action: action$5,
1575
- command: command$2
1666
+ action: action$6,
1667
+ command: command$3
1576
1668
  };
1577
- const action$1 = async (ctx) => {
1669
+ const action$2 = async (ctx) => {
1578
1670
  const { getValidToken } = await tokenServiceFactory(ctx);
1579
1671
  const token = await getValidToken(ctx, promptLogin);
1580
1672
  const { logger } = ctx;
@@ -1594,37 +1686,23 @@ const action$1 = async (ctx) => {
1594
1686
  spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1595
1687
  }
1596
1688
  };
1597
- const command$1 = ({ command: command2, ctx }) => {
1598
- 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));
1689
+ const command$2 = ({ command: command2, ctx }) => {
1690
+ 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));
1599
1691
  };
1600
1692
  const listProjects = {
1601
1693
  name: "list-projects",
1602
1694
  description: "List Strapi Cloud projects",
1603
- action: action$1,
1604
- command: command$1
1695
+ action: action$2,
1696
+ command: command$2
1605
1697
  };
1606
- async function getProject(ctx) {
1607
- const { project } = await retrieve();
1608
- if (!project) {
1609
- ctx.logger.warn(
1610
- `
1611
- We couldn't find a valid local project config.
1612
- Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1613
- "link"
1614
- )} command`
1615
- );
1616
- process.exit(1);
1617
- }
1618
- return project;
1619
- }
1620
- const action = async (ctx) => {
1698
+ const action$1 = async (ctx) => {
1621
1699
  const { getValidToken } = await tokenServiceFactory(ctx);
1622
1700
  const token = await getValidToken(ctx, promptLogin);
1623
1701
  const { logger } = ctx;
1624
1702
  if (!token) {
1625
1703
  return;
1626
1704
  }
1627
- const project = await getProject(ctx);
1705
+ const project = await getLocalProject(ctx);
1628
1706
  if (!project) {
1629
1707
  ctx.logger.debug(`No valid local project configuration was found.`);
1630
1708
  return;
@@ -1664,7 +1742,7 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1664
1742
  };
1665
1743
  function defineCloudNamespace(command2, ctx) {
1666
1744
  const cloud = command2.command("cloud").description("Manage Strapi Cloud projects");
1667
- cloud.command("environments").description("Alias for cloud environment list").action(() => runAction("list", action)(ctx));
1745
+ cloud.command("environments").description("Alias for cloud environment list").action(() => runAction("list", action$1)(ctx));
1668
1746
  return cloud;
1669
1747
  }
1670
1748
  let environmentCmd = null;
@@ -1675,13 +1753,127 @@ const initializeEnvironmentCommand = (command2, ctx) => {
1675
1753
  }
1676
1754
  return environmentCmd;
1677
1755
  };
1678
- const command = ({ command: command2, ctx }) => {
1756
+ const command$1 = ({ command: command2, ctx }) => {
1679
1757
  const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1680
- 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));
1758
+ 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));
1681
1759
  };
1682
1760
  const listEnvironments = {
1683
1761
  name: "list-environments",
1684
1762
  description: "List Strapi Cloud environments",
1763
+ action: action$1,
1764
+ command: command$1
1765
+ };
1766
+ const QUIT_OPTION = "Quit";
1767
+ const action = async (ctx) => {
1768
+ const { getValidToken } = await tokenServiceFactory(ctx);
1769
+ const token = await getValidToken(ctx, promptLogin);
1770
+ const { logger } = ctx;
1771
+ if (!token) {
1772
+ return;
1773
+ }
1774
+ const project = await getLocalProject(ctx);
1775
+ if (!project) {
1776
+ logger.debug(`No valid local project configuration was found.`);
1777
+ return;
1778
+ }
1779
+ const cloudApiService = await cloudApiFactory(ctx, token);
1780
+ const environments = await getEnvironmentsList(ctx, cloudApiService, project);
1781
+ if (!environments) {
1782
+ logger.debug(`Fetching environments failed.`);
1783
+ return;
1784
+ }
1785
+ if (environments.length === 0) {
1786
+ logger.log(
1787
+ `The only available environment is already linked. You can add a new one from your project settings on the Strapi Cloud dashboard.`
1788
+ );
1789
+ return;
1790
+ }
1791
+ const answer = await promptUserForEnvironment(ctx, environments);
1792
+ if (!answer) {
1793
+ return;
1794
+ }
1795
+ await trackEvent(ctx, cloudApiService, "willLinkEnvironment", {
1796
+ projectName: project.name,
1797
+ environmentName: answer.targetEnvironment
1798
+ });
1799
+ try {
1800
+ await patch({ project: { targetEnvironment: answer.targetEnvironment } });
1801
+ } catch (e) {
1802
+ await trackEvent(ctx, cloudApiService, "didNotLinkEnvironment", {
1803
+ projectName: project.name,
1804
+ environmentName: answer.targetEnvironment
1805
+ });
1806
+ logger.debug("Failed to link environment", e);
1807
+ logger.error(
1808
+ "Failed to link the environment. If this issue persists, try re-linking your project or contact support."
1809
+ );
1810
+ process.exit(1);
1811
+ }
1812
+ logger.log(
1813
+ ` You have successfully linked your project to ${chalk__default.default.cyan(answer.targetEnvironment)}, on ${chalk__default.default.cyan(project.displayName)}. You are now able to deploy your project.`
1814
+ );
1815
+ await trackEvent(ctx, cloudApiService, "didLinkEnvironment", {
1816
+ projectName: project.name,
1817
+ environmentName: answer.targetEnvironment
1818
+ });
1819
+ };
1820
+ async function promptUserForEnvironment(ctx, environments) {
1821
+ const { logger } = ctx;
1822
+ try {
1823
+ const answer = await inquirer__default.default.prompt([
1824
+ {
1825
+ type: "list",
1826
+ name: "targetEnvironment",
1827
+ message: "Which environment do you want to link?",
1828
+ choices: [...environments, { name: chalk__default.default.grey(`(${QUIT_OPTION})`), value: null }]
1829
+ }
1830
+ ]);
1831
+ if (!answer.targetEnvironment) {
1832
+ return null;
1833
+ }
1834
+ return answer;
1835
+ } catch (e) {
1836
+ logger.debug("Failed to get user input", e);
1837
+ logger.error("An error occurred while trying to get your environment selection.");
1838
+ return null;
1839
+ }
1840
+ }
1841
+ async function getEnvironmentsList(ctx, cloudApiService, project) {
1842
+ const spinner = ctx.logger.spinner("Fetching environments...\n").start();
1843
+ try {
1844
+ const {
1845
+ data: { data: environmentsList }
1846
+ } = await cloudApiService.listLinkEnvironments({ name: project.name });
1847
+ if (!Array.isArray(environmentsList) || environmentsList.length === 0) {
1848
+ throw new Error("Environments not found in server response");
1849
+ }
1850
+ spinner.succeed();
1851
+ return environmentsList.filter(
1852
+ (environment) => environment.name !== project.targetEnvironment
1853
+ );
1854
+ } catch (e) {
1855
+ if (e.response && e.response.status === 404) {
1856
+ spinner.succeed();
1857
+ ctx.logger.warn(
1858
+ `
1859
+ The project associated with this folder does not exist in Strapi Cloud.
1860
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1861
+ "link"
1862
+ )} command.`
1863
+ );
1864
+ } else {
1865
+ spinner.fail("An error occurred while fetching environments data from Strapi Cloud.");
1866
+ ctx.logger.debug("Failed to list environments", e);
1867
+ }
1868
+ }
1869
+ }
1870
+ const command = ({ command: command2, ctx }) => {
1871
+ const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1872
+ 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));
1873
+ };
1874
+ const linkEnvironment = {
1875
+ name: "link-environment",
1876
+ description: "Link Strapi Cloud environment to a local project",
1685
1877
  action,
1686
1878
  command
1687
1879
  };
@@ -1691,12 +1883,21 @@ const cli = {
1691
1883
  login,
1692
1884
  logout,
1693
1885
  createProject,
1886
+ linkEnvironment,
1694
1887
  listProjects,
1695
1888
  listEnvironments
1696
1889
  };
1697
- const cloudCommands = [deployProject, link, login, logout, listProjects, listEnvironments];
1890
+ const cloudCommands = [
1891
+ deployProject,
1892
+ link,
1893
+ login,
1894
+ logout,
1895
+ linkEnvironment,
1896
+ listProjects,
1897
+ listEnvironments
1898
+ ];
1698
1899
  async function initCloudCLIConfig() {
1699
- const localConfig = await getLocalConfig();
1900
+ const localConfig = await getLocalConfig$1();
1700
1901
  if (!localConfig.deviceId) {
1701
1902
  localConfig.deviceId = crypto__default.default.randomUUID();
1702
1903
  }