@strapi/cloud-cli 0.0.0-next.ea6e3b80ab37f554da4f8bde08dbfe9b57400d31 → 0.0.0-next.ec9b1b708d4d319f2b8b39d9397bd752d250d541

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
@@ -35,6 +35,7 @@ const minimatch = require("minimatch");
35
35
  const fp = require("lodash/fp");
36
36
  const os = require("os");
37
37
  const XDGAppPaths = require("xdg-app-paths");
38
+ const lodash = require("lodash");
38
39
  const jwksClient = require("jwks-rsa");
39
40
  const jwt = require("jsonwebtoken");
40
41
  const stringify = require("fast-safe-stringify");
@@ -177,7 +178,7 @@ async function getConfigPath() {
177
178
  }
178
179
  return configPath;
179
180
  }
180
- async function getLocalConfig() {
181
+ async function getLocalConfig$1() {
181
182
  const configPath = await getConfigPath();
182
183
  const configFilePath = path__namespace.default.join(configPath, CONFIG_FILENAME);
183
184
  await fse__namespace.default.ensureFile(configFilePath);
@@ -193,7 +194,7 @@ async function saveLocalConfig(data) {
193
194
  await fse__namespace.default.writeJson(configFilePath, data, { encoding: "utf8", spaces: 2, mode: 384 });
194
195
  }
195
196
  const name = "@strapi/cloud-cli";
196
- const version = "5.3.0";
197
+ const version = "5.4.0";
197
198
  const description = "Commands to interact with the Strapi Cloud";
198
199
  const keywords = [
199
200
  "strapi",
@@ -238,7 +239,7 @@ const scripts = {
238
239
  watch: "pack-up watch"
239
240
  };
240
241
  const dependencies = {
241
- "@strapi/utils": "workspace:*",
242
+ "@strapi/utils": "5.4.0",
242
243
  axios: "1.7.4",
243
244
  chalk: "4.1.2",
244
245
  "cli-progress": "3.12.0",
@@ -263,13 +264,14 @@ const devDependencies = {
263
264
  "@types/cli-progress": "3.11.5",
264
265
  "@types/eventsource": "1.1.15",
265
266
  "@types/lodash": "^4.14.191",
266
- "eslint-config-custom": "workspace:*",
267
- tsconfig: "workspace:*"
267
+ "eslint-config-custom": "5.4.0",
268
+ tsconfig: "5.4.0"
268
269
  };
269
270
  const engines = {
270
271
  node: ">=18.0.0 <=22.x.x",
271
272
  npm: ">=6.0.0"
272
273
  };
274
+ const gitHead = "7d785703f52464577d077c4618cbe68b44f8a9cd";
273
275
  const packageJson = {
274
276
  name,
275
277
  version,
@@ -290,11 +292,12 @@ const packageJson = {
290
292
  scripts,
291
293
  dependencies,
292
294
  devDependencies,
293
- engines
295
+ engines,
296
+ gitHead
294
297
  };
295
298
  const VERSION = "v1";
296
299
  async function cloudApiFactory({ logger }, token) {
297
- const localConfig = await getLocalConfig();
300
+ const localConfig = await getLocalConfig$1();
298
301
  const customHeaders = {
299
302
  "x-device-id": localConfig.deviceId,
300
303
  "x-app-version": packageJson.version,
@@ -402,6 +405,20 @@ async function cloudApiFactory({ logger }, token) {
402
405
  throw error;
403
406
  }
404
407
  },
408
+ async listLinkEnvironments({ name: name2 }) {
409
+ try {
410
+ const response = await axiosCloudAPI.get(`/projects/${name2}/environments-linkable`);
411
+ if (response.status !== 200) {
412
+ throw new Error("Error fetching cloud environments from the server.");
413
+ }
414
+ return response;
415
+ } catch (error) {
416
+ logger.debug(
417
+ "🥲 Oops! Couldn't retrieve your project's environments from the server. Please try again."
418
+ );
419
+ throw error;
420
+ }
421
+ },
405
422
  async getProject({ name: name2 }) {
406
423
  try {
407
424
  const response = await axiosCloudAPI.get(`/projects/${name2}`);
@@ -425,26 +442,43 @@ async function cloudApiFactory({ logger }, token) {
425
442
  };
426
443
  }
427
444
  const LOCAL_SAVE_FILENAME = ".strapi-cloud.json";
445
+ const getFilePath = (directoryPath) => path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
428
446
  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);
447
+ const pathToFile = getFilePath(directoryPath);
432
448
  await fse__namespace.default.ensureDir(path__namespace.default.dirname(pathToFile));
433
- await fse__namespace.default.writeJson(pathToFile, storedData, { encoding: "utf8" });
449
+ await fse__namespace.default.writeJson(pathToFile, data, { encoding: "utf8" });
434
450
  }
435
451
  async function retrieve({
436
452
  directoryPath
437
453
  } = {}) {
438
- const pathToFile = path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
454
+ const pathToFile = getFilePath(directoryPath);
439
455
  const pathExists = await fse__namespace.default.pathExists(pathToFile);
440
456
  if (!pathExists) {
441
457
  return {};
442
458
  }
443
459
  return fse__namespace.default.readJSON(pathToFile, { encoding: "utf8" });
444
460
  }
461
+ async function patch(patchData, { directoryPath } = {}) {
462
+ const pathToFile = getFilePath(directoryPath);
463
+ const existingData = await retrieve({ directoryPath });
464
+ if (!existingData) {
465
+ throw new Error("No configuration data found to patch.");
466
+ }
467
+ const newData = lodash.merge(existingData, patchData);
468
+ await fse__namespace.default.writeJson(pathToFile, newData, { encoding: "utf8" });
469
+ }
470
+ async function deleteConfig({ directoryPath } = {}) {
471
+ const pathToFile = getFilePath(directoryPath);
472
+ const pathExists = await fse__namespace.default.pathExists(pathToFile);
473
+ if (pathExists) {
474
+ await fse__namespace.default.remove(pathToFile);
475
+ }
476
+ }
445
477
  const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
446
478
  __proto__: null,
447
479
  LOCAL_SAVE_FILENAME,
480
+ deleteConfig,
481
+ patch,
448
482
  retrieve,
449
483
  save
450
484
  }, Symbol.toStringTag, { value: "Module" }));
@@ -452,7 +486,7 @@ let cliConfig;
452
486
  async function tokenServiceFactory({ logger }) {
453
487
  const cloudApiService = await cloudApiFactory({ logger });
454
488
  async function saveToken(str) {
455
- const appConfig = await getLocalConfig();
489
+ const appConfig = await getLocalConfig$1();
456
490
  if (!appConfig) {
457
491
  logger.error("There was a problem saving your token. Please try again.");
458
492
  return;
@@ -466,7 +500,7 @@ async function tokenServiceFactory({ logger }) {
466
500
  }
467
501
  }
468
502
  async function retrieveToken() {
469
- const appConfig = await getLocalConfig();
503
+ const appConfig = await getLocalConfig$1();
470
504
  if (appConfig.token) {
471
505
  if (await isTokenValid(appConfig.token)) {
472
506
  return appConfig.token;
@@ -528,7 +562,7 @@ async function tokenServiceFactory({ logger }) {
528
562
  }
529
563
  }
530
564
  async function eraseToken() {
531
- const appConfig = await getLocalConfig();
565
+ const appConfig = await getLocalConfig$1();
532
566
  if (!appConfig) {
533
567
  return;
534
568
  }
@@ -974,7 +1008,7 @@ async function createProject$1(ctx, cloudApi, projectInput) {
974
1008
  throw e;
975
1009
  }
976
1010
  }
977
- const action$5 = async (ctx) => {
1011
+ const action$6 = async (ctx) => {
978
1012
  const { logger } = ctx;
979
1013
  const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
980
1014
  const token = await getValidToken(ctx, promptLogin);
@@ -1103,7 +1137,7 @@ const buildLogsServiceFactory = ({ logger }) => {
1103
1137
  });
1104
1138
  };
1105
1139
  };
1106
- const QUIT_OPTION$1 = "Quit";
1140
+ const QUIT_OPTION$2 = "Quit";
1107
1141
  async function promptForEnvironment(environments) {
1108
1142
  const choices = environments.map((env) => ({ name: env, value: env }));
1109
1143
  const { selectedEnvironment } = await inquirer__default.default.prompt([
@@ -1111,7 +1145,7 @@ async function promptForEnvironment(environments) {
1111
1145
  type: "list",
1112
1146
  name: "selectedEnvironment",
1113
1147
  message: "Select the environment to deploy:",
1114
- choices: [...choices, { name: chalk__default.default.grey(`(${QUIT_OPTION$1})`), value: null }]
1148
+ choices: [...choices, { name: chalk__default.default.grey(`(${QUIT_OPTION$2})`), value: null }]
1115
1149
  }
1116
1150
  ]);
1117
1151
  if (selectedEnvironment === null) {
@@ -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,13 +1266,32 @@ 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
+ const action$5 = async (ctx, opts) => {
1236
1289
  const { getValidToken } = await tokenServiceFactory(ctx);
1237
1290
  const token = await getValidToken(ctx, promptLogin);
1238
1291
  if (!token) {
1239
1292
  return;
1240
1293
  }
1241
- const project = await getProject$1(ctx);
1294
+ const project = await getProject(ctx);
1242
1295
  if (!project) {
1243
1296
  return;
1244
1297
  }
@@ -1296,22 +1349,15 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1296
1349
  );
1297
1350
  maxSize = 1e8;
1298
1351
  }
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;
1304
- }
1305
- targetEnvironment = opts.environment;
1306
- } else {
1307
- targetEnvironment = environments.length > 1 ? await promptForEnvironment(environments) : environments[0];
1308
- }
1309
- project.targetEnvironment = targetEnvironment;
1352
+ project.targetEnvironment = await getTargetEnvironment(ctx, opts, project, environments);
1310
1353
  const buildId = await upload(ctx, project, token, maxSize);
1311
1354
  if (!buildId) {
1312
1355
  return;
1313
1356
  }
1314
1357
  try {
1358
+ ctx.logger.log(
1359
+ `🚀 Deploying project to ${chalk__default.default.cyan(project.targetEnvironment ?? `production`)} environment...`
1360
+ );
1315
1361
  notificationService(`${apiConfig.apiBaseUrl}/notifications`, token, cliConfig2);
1316
1362
  await buildLogsService(`${apiConfig.apiBaseUrl}/v1/logs/${buildId}`, token, cliConfig2);
1317
1363
  ctx.logger.log(
@@ -1356,17 +1402,16 @@ const runAction = (name2, action2) => (...args) => {
1356
1402
  process.exit(1);
1357
1403
  });
1358
1404
  };
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));
1405
+ const command$7 = ({ ctx }) => {
1406
+ 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, --env <name>", "Specify the environment to deploy").action((opts) => runAction("deploy", action$5)(ctx, opts));
1361
1407
  };
1362
1408
  const deployProject = {
1363
1409
  name: "deploy-project",
1364
1410
  description: "Deploy a Strapi Cloud project",
1365
- action: action$4,
1366
- command: command$6
1411
+ action: action$5,
1412
+ command: command$7
1367
1413
  };
1368
- const QUIT_OPTION = "Quit";
1369
- async function getExistingConfig(ctx) {
1414
+ async function getLocalConfig(ctx) {
1370
1415
  try {
1371
1416
  return await retrieve();
1372
1417
  } catch (e) {
@@ -1375,6 +1420,21 @@ async function getExistingConfig(ctx) {
1375
1420
  return null;
1376
1421
  }
1377
1422
  }
1423
+ async function getLocalProject(ctx) {
1424
+ const localConfig = await getLocalConfig(ctx);
1425
+ if (!localConfig || !localConfig.project) {
1426
+ ctx.logger.warn(
1427
+ `
1428
+ We couldn't find a valid local project config.
1429
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1430
+ "link"
1431
+ )} command.`
1432
+ );
1433
+ process.exit(1);
1434
+ }
1435
+ return localConfig.project;
1436
+ }
1437
+ const QUIT_OPTION$1 = "Quit";
1378
1438
  async function promptForRelink(ctx, cloudApiService, existingConfig) {
1379
1439
  if (existingConfig && existingConfig.project) {
1380
1440
  const { shouldRelink } = await inquirer__default.default.prompt([
@@ -1404,7 +1464,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1404
1464
  } = await cloudApiService.listLinkProjects();
1405
1465
  spinner.succeed();
1406
1466
  if (!Array.isArray(projectList)) {
1407
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1467
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1408
1468
  return null;
1409
1469
  }
1410
1470
  const projects = projectList.filter(
@@ -1416,7 +1476,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1416
1476
  };
1417
1477
  });
1418
1478
  if (projects.length === 0) {
1419
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1479
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1420
1480
  return null;
1421
1481
  }
1422
1482
  return projects;
@@ -1434,7 +1494,7 @@ async function getUserSelection(ctx, projects) {
1434
1494
  type: "list",
1435
1495
  name: "linkProject",
1436
1496
  message: "Which project do you want to link?",
1437
- choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION})`), value: null }]
1497
+ choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION$1})`), value: null }]
1438
1498
  }
1439
1499
  ]);
1440
1500
  if (!answer.linkProject) {
@@ -1447,7 +1507,7 @@ async function getUserSelection(ctx, projects) {
1447
1507
  return null;
1448
1508
  }
1449
1509
  }
1450
- const action$3 = async (ctx) => {
1510
+ const action$4 = async (ctx) => {
1451
1511
  const { getValidToken } = await tokenServiceFactory(ctx);
1452
1512
  const token = await getValidToken(ctx, promptLogin);
1453
1513
  const { logger } = ctx;
@@ -1455,7 +1515,7 @@ const action$3 = async (ctx) => {
1455
1515
  return;
1456
1516
  }
1457
1517
  const cloudApiService = await cloudApiFactory(ctx, token);
1458
- const existingConfig = await getExistingConfig(ctx);
1518
+ const existingConfig = await getLocalConfig(ctx);
1459
1519
  const shouldRelink = await promptForRelink(ctx, cloudApiService, existingConfig);
1460
1520
  if (!shouldRelink) {
1461
1521
  return;
@@ -1490,7 +1550,9 @@ const action$3 = async (ctx) => {
1490
1550
  return;
1491
1551
  }
1492
1552
  await save({ project: answer.linkProject });
1493
- logger.log(`Project ${chalk__default.default.cyan(answer.linkProject.displayName)} linked successfully.`);
1553
+ logger.log(
1554
+ ` You have successfully linked your project to ${chalk__default.default.cyan(answer.linkProject.displayName)}. You are now able to deploy your project.`
1555
+ );
1494
1556
  await trackEvent(ctx, cloudApiService, "didLinkProject", {
1495
1557
  projectInternalName: answer.linkProject
1496
1558
  });
@@ -1502,16 +1564,16 @@ const action$3 = async (ctx) => {
1502
1564
  });
1503
1565
  }
1504
1566
  };
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));
1567
+ const command$6 = ({ command: command2, ctx }) => {
1568
+ 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
1569
  };
1508
1570
  const link = {
1509
1571
  name: "link-project",
1510
1572
  description: "Link a local directory to a Strapi Cloud project",
1511
- action: action$3,
1512
- command: command$5
1573
+ action: action$4,
1574
+ command: command$6
1513
1575
  };
1514
- const command$4 = ({ ctx }) => {
1576
+ const command$5 = ({ ctx }) => {
1515
1577
  return commander.createCommand("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1516
1578
  "after",
1517
1579
  "\nAfter running this command, you will be prompted to enter your authentication information."
@@ -1521,10 +1583,10 @@ const login = {
1521
1583
  name: "login",
1522
1584
  description: "Strapi Cloud Login",
1523
1585
  action: loginAction,
1524
- command: command$4
1586
+ command: command$5
1525
1587
  };
1526
1588
  const openModule = import("open");
1527
- const action$2 = async (ctx) => {
1589
+ const action$3 = async (ctx) => {
1528
1590
  const { logger } = ctx;
1529
1591
  const { retrieveToken, eraseToken } = await tokenServiceFactory(ctx);
1530
1592
  const token = await retrieveToken();
@@ -1556,25 +1618,25 @@ const action$2 = async (ctx) => {
1556
1618
  }
1557
1619
  await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1558
1620
  };
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));
1621
+ const command$4 = ({ ctx }) => {
1622
+ 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
1623
  };
1562
1624
  const logout = {
1563
1625
  name: "logout",
1564
1626
  description: "Strapi Cloud Logout",
1565
- action: action$2,
1566
- command: command$3
1627
+ action: action$3,
1628
+ command: command$4
1567
1629
  };
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));
1630
+ const command$3 = ({ ctx }) => {
1631
+ 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
1632
  };
1571
1633
  const createProject = {
1572
1634
  name: "create-project",
1573
1635
  description: "Create a new project",
1574
- action: action$5,
1575
- command: command$2
1636
+ action: action$6,
1637
+ command: command$3
1576
1638
  };
1577
- const action$1 = async (ctx) => {
1639
+ const action$2 = async (ctx) => {
1578
1640
  const { getValidToken } = await tokenServiceFactory(ctx);
1579
1641
  const token = await getValidToken(ctx, promptLogin);
1580
1642
  const { logger } = ctx;
@@ -1594,37 +1656,23 @@ const action$1 = async (ctx) => {
1594
1656
  spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1595
1657
  }
1596
1658
  };
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));
1659
+ const command$2 = ({ command: command2, ctx }) => {
1660
+ 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
1661
  };
1600
1662
  const listProjects = {
1601
1663
  name: "list-projects",
1602
1664
  description: "List Strapi Cloud projects",
1603
- action: action$1,
1604
- command: command$1
1665
+ action: action$2,
1666
+ command: command$2
1605
1667
  };
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) => {
1668
+ const action$1 = async (ctx) => {
1621
1669
  const { getValidToken } = await tokenServiceFactory(ctx);
1622
1670
  const token = await getValidToken(ctx, promptLogin);
1623
1671
  const { logger } = ctx;
1624
1672
  if (!token) {
1625
1673
  return;
1626
1674
  }
1627
- const project = await getProject(ctx);
1675
+ const project = await getLocalProject(ctx);
1628
1676
  if (!project) {
1629
1677
  ctx.logger.debug(`No valid local project configuration was found.`);
1630
1678
  return;
@@ -1664,7 +1712,7 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1664
1712
  };
1665
1713
  function defineCloudNamespace(command2, ctx) {
1666
1714
  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));
1715
+ cloud.command("environments").description("Alias for cloud environment list").action(() => runAction("list", action$1)(ctx));
1668
1716
  return cloud;
1669
1717
  }
1670
1718
  let environmentCmd = null;
@@ -1675,13 +1723,127 @@ const initializeEnvironmentCommand = (command2, ctx) => {
1675
1723
  }
1676
1724
  return environmentCmd;
1677
1725
  };
1678
- const command = ({ command: command2, ctx }) => {
1726
+ const command$1 = ({ command: command2, ctx }) => {
1679
1727
  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));
1728
+ 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
1729
  };
1682
1730
  const listEnvironments = {
1683
1731
  name: "list-environments",
1684
1732
  description: "List Strapi Cloud environments",
1733
+ action: action$1,
1734
+ command: command$1
1735
+ };
1736
+ const QUIT_OPTION = "Quit";
1737
+ const action = async (ctx) => {
1738
+ const { getValidToken } = await tokenServiceFactory(ctx);
1739
+ const token = await getValidToken(ctx, promptLogin);
1740
+ const { logger } = ctx;
1741
+ if (!token) {
1742
+ return;
1743
+ }
1744
+ const project = await getLocalProject(ctx);
1745
+ if (!project) {
1746
+ logger.debug(`No valid local project configuration was found.`);
1747
+ return;
1748
+ }
1749
+ const cloudApiService = await cloudApiFactory(ctx, token);
1750
+ const environments = await getEnvironmentsList(ctx, cloudApiService, project);
1751
+ if (!environments) {
1752
+ logger.debug(`Fetching environments failed.`);
1753
+ return;
1754
+ }
1755
+ if (environments.length === 0) {
1756
+ logger.log(
1757
+ `The only available environment is already linked. You can add a new one from your project settings on the Strapi Cloud dashboard.`
1758
+ );
1759
+ return;
1760
+ }
1761
+ const answer = await promptUserForEnvironment(ctx, environments);
1762
+ if (!answer) {
1763
+ return;
1764
+ }
1765
+ await trackEvent(ctx, cloudApiService, "willLinkEnvironment", {
1766
+ projectName: project.name,
1767
+ environmentName: answer.targetEnvironment
1768
+ });
1769
+ try {
1770
+ await patch({ project: { targetEnvironment: answer.targetEnvironment } });
1771
+ } catch (e) {
1772
+ await trackEvent(ctx, cloudApiService, "didNotLinkEnvironment", {
1773
+ projectName: project.name,
1774
+ environmentName: answer.targetEnvironment
1775
+ });
1776
+ logger.debug("Failed to link environment", e);
1777
+ logger.error(
1778
+ "Failed to link the environment. If this issue persists, try re-linking your project or contact support."
1779
+ );
1780
+ process.exit(1);
1781
+ }
1782
+ logger.log(
1783
+ ` 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.`
1784
+ );
1785
+ await trackEvent(ctx, cloudApiService, "didLinkEnvironment", {
1786
+ projectName: project.name,
1787
+ environmentName: answer.targetEnvironment
1788
+ });
1789
+ };
1790
+ async function promptUserForEnvironment(ctx, environments) {
1791
+ const { logger } = ctx;
1792
+ try {
1793
+ const answer = await inquirer__default.default.prompt([
1794
+ {
1795
+ type: "list",
1796
+ name: "targetEnvironment",
1797
+ message: "Which environment do you want to link?",
1798
+ choices: [...environments, { name: chalk__default.default.grey(`(${QUIT_OPTION})`), value: null }]
1799
+ }
1800
+ ]);
1801
+ if (!answer.targetEnvironment) {
1802
+ return null;
1803
+ }
1804
+ return answer;
1805
+ } catch (e) {
1806
+ logger.debug("Failed to get user input", e);
1807
+ logger.error("An error occurred while trying to get your environment selection.");
1808
+ return null;
1809
+ }
1810
+ }
1811
+ async function getEnvironmentsList(ctx, cloudApiService, project) {
1812
+ const spinner = ctx.logger.spinner("Fetching environments...\n").start();
1813
+ try {
1814
+ const {
1815
+ data: { data: environmentsList }
1816
+ } = await cloudApiService.listLinkEnvironments({ name: project.name });
1817
+ if (!Array.isArray(environmentsList) || environmentsList.length === 0) {
1818
+ throw new Error("Environments not found in server response");
1819
+ }
1820
+ spinner.succeed();
1821
+ return environmentsList.filter(
1822
+ (environment) => environment.name !== project.targetEnvironment
1823
+ );
1824
+ } catch (e) {
1825
+ if (e.response && e.response.status === 404) {
1826
+ spinner.succeed();
1827
+ ctx.logger.warn(
1828
+ `
1829
+ The project associated with this folder does not exist in Strapi Cloud.
1830
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1831
+ "link"
1832
+ )} command.`
1833
+ );
1834
+ } else {
1835
+ spinner.fail("An error occurred while fetching environments data from Strapi Cloud.");
1836
+ ctx.logger.debug("Failed to list environments", e);
1837
+ }
1838
+ }
1839
+ }
1840
+ const command = ({ command: command2, ctx }) => {
1841
+ const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1842
+ 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));
1843
+ };
1844
+ const linkEnvironment = {
1845
+ name: "link-environment",
1846
+ description: "Link Strapi Cloud environment to a local project",
1685
1847
  action,
1686
1848
  command
1687
1849
  };
@@ -1691,12 +1853,21 @@ const cli = {
1691
1853
  login,
1692
1854
  logout,
1693
1855
  createProject,
1856
+ linkEnvironment,
1694
1857
  listProjects,
1695
1858
  listEnvironments
1696
1859
  };
1697
- const cloudCommands = [deployProject, link, login, logout, listProjects, listEnvironments];
1860
+ const cloudCommands = [
1861
+ deployProject,
1862
+ link,
1863
+ login,
1864
+ logout,
1865
+ linkEnvironment,
1866
+ listProjects,
1867
+ listEnvironments
1868
+ ];
1698
1869
  async function initCloudCLIConfig() {
1699
- const localConfig = await getLocalConfig();
1870
+ const localConfig = await getLocalConfig$1();
1700
1871
  if (!localConfig.deviceId) {
1701
1872
  localConfig.deviceId = crypto__default.default.randomUUID();
1702
1873
  }