@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.mjs CHANGED
@@ -13,6 +13,7 @@ import { minimatch } from "minimatch";
13
13
  import { defaults, has } from "lodash/fp";
14
14
  import os from "os";
15
15
  import XDGAppPaths from "xdg-app-paths";
16
+ import { merge } from "lodash";
16
17
  import jwksClient from "jwks-rsa";
17
18
  import jwt from "jsonwebtoken";
18
19
  import stringify from "fast-safe-stringify";
@@ -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.4.0";
138
139
  const description = "Commands to interact with the Strapi Cloud";
139
140
  const keywords = [
140
141
  "strapi",
@@ -179,7 +180,7 @@ const scripts = {
179
180
  watch: "pack-up watch"
180
181
  };
181
182
  const dependencies = {
182
- "@strapi/utils": "workspace:*",
183
+ "@strapi/utils": "5.4.0",
183
184
  axios: "1.7.4",
184
185
  chalk: "4.1.2",
185
186
  "cli-progress": "3.12.0",
@@ -204,13 +205,14 @@ const devDependencies = {
204
205
  "@types/cli-progress": "3.11.5",
205
206
  "@types/eventsource": "1.1.15",
206
207
  "@types/lodash": "^4.14.191",
207
- "eslint-config-custom": "workspace:*",
208
- tsconfig: "workspace:*"
208
+ "eslint-config-custom": "5.4.0",
209
+ tsconfig: "5.4.0"
209
210
  };
210
211
  const engines = {
211
212
  node: ">=18.0.0 <=22.x.x",
212
213
  npm: ">=6.0.0"
213
214
  };
215
+ const gitHead = "7d785703f52464577d077c4618cbe68b44f8a9cd";
214
216
  const packageJson = {
215
217
  name,
216
218
  version,
@@ -231,11 +233,12 @@ const packageJson = {
231
233
  scripts,
232
234
  dependencies,
233
235
  devDependencies,
234
- engines
236
+ engines,
237
+ gitHead
235
238
  };
236
239
  const VERSION = "v1";
237
240
  async function cloudApiFactory({ logger }, token) {
238
- const localConfig = await getLocalConfig();
241
+ const localConfig = await getLocalConfig$1();
239
242
  const customHeaders = {
240
243
  "x-device-id": localConfig.deviceId,
241
244
  "x-app-version": packageJson.version,
@@ -343,6 +346,20 @@ async function cloudApiFactory({ logger }, token) {
343
346
  throw error;
344
347
  }
345
348
  },
349
+ async listLinkEnvironments({ name: name2 }) {
350
+ try {
351
+ const response = await axiosCloudAPI.get(`/projects/${name2}/environments-linkable`);
352
+ if (response.status !== 200) {
353
+ throw new Error("Error fetching cloud environments from the server.");
354
+ }
355
+ return response;
356
+ } catch (error) {
357
+ logger.debug(
358
+ "🥲 Oops! Couldn't retrieve your project's environments from the server. Please try again."
359
+ );
360
+ throw error;
361
+ }
362
+ },
346
363
  async getProject({ name: name2 }) {
347
364
  try {
348
365
  const response = await axiosCloudAPI.get(`/projects/${name2}`);
@@ -366,26 +383,43 @@ async function cloudApiFactory({ logger }, token) {
366
383
  };
367
384
  }
368
385
  const LOCAL_SAVE_FILENAME = ".strapi-cloud.json";
386
+ const getFilePath = (directoryPath) => path__default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
369
387
  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);
388
+ const pathToFile = getFilePath(directoryPath);
373
389
  await fse__default.ensureDir(path__default.dirname(pathToFile));
374
- await fse__default.writeJson(pathToFile, storedData, { encoding: "utf8" });
390
+ await fse__default.writeJson(pathToFile, data, { encoding: "utf8" });
375
391
  }
376
392
  async function retrieve({
377
393
  directoryPath
378
394
  } = {}) {
379
- const pathToFile = path__default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
395
+ const pathToFile = getFilePath(directoryPath);
380
396
  const pathExists = await fse__default.pathExists(pathToFile);
381
397
  if (!pathExists) {
382
398
  return {};
383
399
  }
384
400
  return fse__default.readJSON(pathToFile, { encoding: "utf8" });
385
401
  }
402
+ async function patch(patchData, { directoryPath } = {}) {
403
+ const pathToFile = getFilePath(directoryPath);
404
+ const existingData = await retrieve({ directoryPath });
405
+ if (!existingData) {
406
+ throw new Error("No configuration data found to patch.");
407
+ }
408
+ const newData = merge(existingData, patchData);
409
+ await fse__default.writeJson(pathToFile, newData, { encoding: "utf8" });
410
+ }
411
+ async function deleteConfig({ directoryPath } = {}) {
412
+ const pathToFile = getFilePath(directoryPath);
413
+ const pathExists = await fse__default.pathExists(pathToFile);
414
+ if (pathExists) {
415
+ await fse__default.remove(pathToFile);
416
+ }
417
+ }
386
418
  const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
387
419
  __proto__: null,
388
420
  LOCAL_SAVE_FILENAME,
421
+ deleteConfig,
422
+ patch,
389
423
  retrieve,
390
424
  save
391
425
  }, Symbol.toStringTag, { value: "Module" }));
@@ -393,7 +427,7 @@ let cliConfig;
393
427
  async function tokenServiceFactory({ logger }) {
394
428
  const cloudApiService = await cloudApiFactory({ logger });
395
429
  async function saveToken(str) {
396
- const appConfig = await getLocalConfig();
430
+ const appConfig = await getLocalConfig$1();
397
431
  if (!appConfig) {
398
432
  logger.error("There was a problem saving your token. Please try again.");
399
433
  return;
@@ -407,7 +441,7 @@ async function tokenServiceFactory({ logger }) {
407
441
  }
408
442
  }
409
443
  async function retrieveToken() {
410
- const appConfig = await getLocalConfig();
444
+ const appConfig = await getLocalConfig$1();
411
445
  if (appConfig.token) {
412
446
  if (await isTokenValid(appConfig.token)) {
413
447
  return appConfig.token;
@@ -469,7 +503,7 @@ async function tokenServiceFactory({ logger }) {
469
503
  }
470
504
  }
471
505
  async function eraseToken() {
472
- const appConfig = await getLocalConfig();
506
+ const appConfig = await getLocalConfig$1();
473
507
  if (!appConfig) {
474
508
  return;
475
509
  }
@@ -915,7 +949,7 @@ async function createProject$1(ctx, cloudApi, projectInput) {
915
949
  throw e;
916
950
  }
917
951
  }
918
- const action$5 = async (ctx) => {
952
+ const action$6 = async (ctx) => {
919
953
  const { logger } = ctx;
920
954
  const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
921
955
  const token = await getValidToken(ctx, promptLogin);
@@ -1044,7 +1078,7 @@ const buildLogsServiceFactory = ({ logger }) => {
1044
1078
  });
1045
1079
  };
1046
1080
  };
1047
- const QUIT_OPTION$1 = "Quit";
1081
+ const QUIT_OPTION$2 = "Quit";
1048
1082
  async function promptForEnvironment(environments) {
1049
1083
  const choices = environments.map((env2) => ({ name: env2, value: env2 }));
1050
1084
  const { selectedEnvironment } = await inquirer.prompt([
@@ -1052,7 +1086,7 @@ async function promptForEnvironment(environments) {
1052
1086
  type: "list",
1053
1087
  name: "selectedEnvironment",
1054
1088
  message: "Select the environment to deploy:",
1055
- choices: [...choices, { name: chalk.grey(`(${QUIT_OPTION$1})`), value: null }]
1089
+ choices: [...choices, { name: chalk.grey(`(${QUIT_OPTION$2})`), value: null }]
1056
1090
  }
1057
1091
  ]);
1058
1092
  if (selectedEnvironment === null) {
@@ -1148,11 +1182,11 @@ async function upload(ctx, project, token, maxProjectFileSize) {
1148
1182
  process.exit(1);
1149
1183
  }
1150
1184
  }
1151
- async function getProject$1(ctx) {
1185
+ async function getProject(ctx) {
1152
1186
  const { project } = await retrieve();
1153
1187
  if (!project) {
1154
1188
  try {
1155
- return await action$5(ctx);
1189
+ return await action$6(ctx);
1156
1190
  } catch (e) {
1157
1191
  ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1158
1192
  ctx.logger.debug(e);
@@ -1173,13 +1207,32 @@ async function getConfig({
1173
1207
  return null;
1174
1208
  }
1175
1209
  }
1176
- const action$4 = async (ctx, opts) => {
1210
+ function validateEnvironment(ctx, environment, environments) {
1211
+ if (!environments.includes(environment)) {
1212
+ ctx.logger.error(`Environment ${environment} does not exist.`);
1213
+ process.exit(1);
1214
+ }
1215
+ }
1216
+ async function getTargetEnvironment(ctx, opts, project, environments) {
1217
+ if (opts.env) {
1218
+ validateEnvironment(ctx, opts.env, environments);
1219
+ return opts.env;
1220
+ }
1221
+ if (project.targetEnvironment) {
1222
+ return project.targetEnvironment;
1223
+ }
1224
+ if (environments.length > 1) {
1225
+ return promptForEnvironment(environments);
1226
+ }
1227
+ return environments[0];
1228
+ }
1229
+ const action$5 = async (ctx, opts) => {
1177
1230
  const { getValidToken } = await tokenServiceFactory(ctx);
1178
1231
  const token = await getValidToken(ctx, promptLogin);
1179
1232
  if (!token) {
1180
1233
  return;
1181
1234
  }
1182
- const project = await getProject$1(ctx);
1235
+ const project = await getProject(ctx);
1183
1236
  if (!project) {
1184
1237
  return;
1185
1238
  }
@@ -1237,22 +1290,15 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1237
1290
  );
1238
1291
  maxSize = 1e8;
1239
1292
  }
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;
1245
- }
1246
- targetEnvironment = opts.environment;
1247
- } else {
1248
- targetEnvironment = environments.length > 1 ? await promptForEnvironment(environments) : environments[0];
1249
- }
1250
- project.targetEnvironment = targetEnvironment;
1293
+ project.targetEnvironment = await getTargetEnvironment(ctx, opts, project, environments);
1251
1294
  const buildId = await upload(ctx, project, token, maxSize);
1252
1295
  if (!buildId) {
1253
1296
  return;
1254
1297
  }
1255
1298
  try {
1299
+ ctx.logger.log(
1300
+ `🚀 Deploying project to ${chalk.cyan(project.targetEnvironment ?? `production`)} environment...`
1301
+ );
1256
1302
  notificationService(`${apiConfig.apiBaseUrl}/notifications`, token, cliConfig2);
1257
1303
  await buildLogsService(`${apiConfig.apiBaseUrl}/v1/logs/${buildId}`, token, cliConfig2);
1258
1304
  ctx.logger.log(
@@ -1297,17 +1343,16 @@ const runAction = (name2, action2) => (...args) => {
1297
1343
  process.exit(1);
1298
1344
  });
1299
1345
  };
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));
1346
+ const command$7 = ({ ctx }) => {
1347
+ 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, --env <name>", "Specify the environment to deploy").action((opts) => runAction("deploy", action$5)(ctx, opts));
1302
1348
  };
1303
1349
  const deployProject = {
1304
1350
  name: "deploy-project",
1305
1351
  description: "Deploy a Strapi Cloud project",
1306
- action: action$4,
1307
- command: command$6
1352
+ action: action$5,
1353
+ command: command$7
1308
1354
  };
1309
- const QUIT_OPTION = "Quit";
1310
- async function getExistingConfig(ctx) {
1355
+ async function getLocalConfig(ctx) {
1311
1356
  try {
1312
1357
  return await retrieve();
1313
1358
  } catch (e) {
@@ -1316,6 +1361,21 @@ async function getExistingConfig(ctx) {
1316
1361
  return null;
1317
1362
  }
1318
1363
  }
1364
+ async function getLocalProject(ctx) {
1365
+ const localConfig = await getLocalConfig(ctx);
1366
+ if (!localConfig || !localConfig.project) {
1367
+ ctx.logger.warn(
1368
+ `
1369
+ We couldn't find a valid local project config.
1370
+ Please link your local project to an existing Strapi Cloud project using the ${chalk.cyan(
1371
+ "link"
1372
+ )} command.`
1373
+ );
1374
+ process.exit(1);
1375
+ }
1376
+ return localConfig.project;
1377
+ }
1378
+ const QUIT_OPTION$1 = "Quit";
1319
1379
  async function promptForRelink(ctx, cloudApiService, existingConfig) {
1320
1380
  if (existingConfig && existingConfig.project) {
1321
1381
  const { shouldRelink } = await inquirer.prompt([
@@ -1345,7 +1405,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1345
1405
  } = await cloudApiService.listLinkProjects();
1346
1406
  spinner.succeed();
1347
1407
  if (!Array.isArray(projectList)) {
1348
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1408
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1349
1409
  return null;
1350
1410
  }
1351
1411
  const projects = projectList.filter(
@@ -1357,7 +1417,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1357
1417
  };
1358
1418
  });
1359
1419
  if (projects.length === 0) {
1360
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1420
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1361
1421
  return null;
1362
1422
  }
1363
1423
  return projects;
@@ -1375,7 +1435,7 @@ async function getUserSelection(ctx, projects) {
1375
1435
  type: "list",
1376
1436
  name: "linkProject",
1377
1437
  message: "Which project do you want to link?",
1378
- choices: [...projects, { name: chalk.grey(`(${QUIT_OPTION})`), value: null }]
1438
+ choices: [...projects, { name: chalk.grey(`(${QUIT_OPTION$1})`), value: null }]
1379
1439
  }
1380
1440
  ]);
1381
1441
  if (!answer.linkProject) {
@@ -1388,7 +1448,7 @@ async function getUserSelection(ctx, projects) {
1388
1448
  return null;
1389
1449
  }
1390
1450
  }
1391
- const action$3 = async (ctx) => {
1451
+ const action$4 = async (ctx) => {
1392
1452
  const { getValidToken } = await tokenServiceFactory(ctx);
1393
1453
  const token = await getValidToken(ctx, promptLogin);
1394
1454
  const { logger } = ctx;
@@ -1396,7 +1456,7 @@ const action$3 = async (ctx) => {
1396
1456
  return;
1397
1457
  }
1398
1458
  const cloudApiService = await cloudApiFactory(ctx, token);
1399
- const existingConfig = await getExistingConfig(ctx);
1459
+ const existingConfig = await getLocalConfig(ctx);
1400
1460
  const shouldRelink = await promptForRelink(ctx, cloudApiService, existingConfig);
1401
1461
  if (!shouldRelink) {
1402
1462
  return;
@@ -1431,7 +1491,9 @@ const action$3 = async (ctx) => {
1431
1491
  return;
1432
1492
  }
1433
1493
  await save({ project: answer.linkProject });
1434
- logger.log(`Project ${chalk.cyan(answer.linkProject.displayName)} linked successfully.`);
1494
+ logger.log(
1495
+ ` You have successfully linked your project to ${chalk.cyan(answer.linkProject.displayName)}. You are now able to deploy your project.`
1496
+ );
1435
1497
  await trackEvent(ctx, cloudApiService, "didLinkProject", {
1436
1498
  projectInternalName: answer.linkProject
1437
1499
  });
@@ -1443,16 +1505,16 @@ const action$3 = async (ctx) => {
1443
1505
  });
1444
1506
  }
1445
1507
  };
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));
1508
+ const command$6 = ({ command: command2, ctx }) => {
1509
+ 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
1510
  };
1449
1511
  const link = {
1450
1512
  name: "link-project",
1451
1513
  description: "Link a local directory to a Strapi Cloud project",
1452
- action: action$3,
1453
- command: command$5
1514
+ action: action$4,
1515
+ command: command$6
1454
1516
  };
1455
- const command$4 = ({ ctx }) => {
1517
+ const command$5 = ({ ctx }) => {
1456
1518
  return createCommand("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1457
1519
  "after",
1458
1520
  "\nAfter running this command, you will be prompted to enter your authentication information."
@@ -1462,10 +1524,10 @@ const login = {
1462
1524
  name: "login",
1463
1525
  description: "Strapi Cloud Login",
1464
1526
  action: loginAction,
1465
- command: command$4
1527
+ command: command$5
1466
1528
  };
1467
1529
  const openModule = import("open");
1468
- const action$2 = async (ctx) => {
1530
+ const action$3 = async (ctx) => {
1469
1531
  const { logger } = ctx;
1470
1532
  const { retrieveToken, eraseToken } = await tokenServiceFactory(ctx);
1471
1533
  const token = await retrieveToken();
@@ -1497,25 +1559,25 @@ const action$2 = async (ctx) => {
1497
1559
  }
1498
1560
  await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1499
1561
  };
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));
1562
+ const command$4 = ({ ctx }) => {
1563
+ 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
1564
  };
1503
1565
  const logout = {
1504
1566
  name: "logout",
1505
1567
  description: "Strapi Cloud Logout",
1506
- action: action$2,
1507
- command: command$3
1568
+ action: action$3,
1569
+ command: command$4
1508
1570
  };
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));
1571
+ const command$3 = ({ ctx }) => {
1572
+ 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
1573
  };
1512
1574
  const createProject = {
1513
1575
  name: "create-project",
1514
1576
  description: "Create a new project",
1515
- action: action$5,
1516
- command: command$2
1577
+ action: action$6,
1578
+ command: command$3
1517
1579
  };
1518
- const action$1 = async (ctx) => {
1580
+ const action$2 = async (ctx) => {
1519
1581
  const { getValidToken } = await tokenServiceFactory(ctx);
1520
1582
  const token = await getValidToken(ctx, promptLogin);
1521
1583
  const { logger } = ctx;
@@ -1535,37 +1597,23 @@ const action$1 = async (ctx) => {
1535
1597
  spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1536
1598
  }
1537
1599
  };
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));
1600
+ const command$2 = ({ command: command2, ctx }) => {
1601
+ 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
1602
  };
1541
1603
  const listProjects = {
1542
1604
  name: "list-projects",
1543
1605
  description: "List Strapi Cloud projects",
1544
- action: action$1,
1545
- command: command$1
1606
+ action: action$2,
1607
+ command: command$2
1546
1608
  };
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) => {
1609
+ const action$1 = async (ctx) => {
1562
1610
  const { getValidToken } = await tokenServiceFactory(ctx);
1563
1611
  const token = await getValidToken(ctx, promptLogin);
1564
1612
  const { logger } = ctx;
1565
1613
  if (!token) {
1566
1614
  return;
1567
1615
  }
1568
- const project = await getProject(ctx);
1616
+ const project = await getLocalProject(ctx);
1569
1617
  if (!project) {
1570
1618
  ctx.logger.debug(`No valid local project configuration was found.`);
1571
1619
  return;
@@ -1605,7 +1653,7 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1605
1653
  };
1606
1654
  function defineCloudNamespace(command2, ctx) {
1607
1655
  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));
1656
+ cloud.command("environments").description("Alias for cloud environment list").action(() => runAction("list", action$1)(ctx));
1609
1657
  return cloud;
1610
1658
  }
1611
1659
  let environmentCmd = null;
@@ -1616,13 +1664,127 @@ const initializeEnvironmentCommand = (command2, ctx) => {
1616
1664
  }
1617
1665
  return environmentCmd;
1618
1666
  };
1619
- const command = ({ command: command2, ctx }) => {
1667
+ const command$1 = ({ command: command2, ctx }) => {
1620
1668
  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));
1669
+ 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
1670
  };
1623
1671
  const listEnvironments = {
1624
1672
  name: "list-environments",
1625
1673
  description: "List Strapi Cloud environments",
1674
+ action: action$1,
1675
+ command: command$1
1676
+ };
1677
+ const QUIT_OPTION = "Quit";
1678
+ const action = async (ctx) => {
1679
+ const { getValidToken } = await tokenServiceFactory(ctx);
1680
+ const token = await getValidToken(ctx, promptLogin);
1681
+ const { logger } = ctx;
1682
+ if (!token) {
1683
+ return;
1684
+ }
1685
+ const project = await getLocalProject(ctx);
1686
+ if (!project) {
1687
+ logger.debug(`No valid local project configuration was found.`);
1688
+ return;
1689
+ }
1690
+ const cloudApiService = await cloudApiFactory(ctx, token);
1691
+ const environments = await getEnvironmentsList(ctx, cloudApiService, project);
1692
+ if (!environments) {
1693
+ logger.debug(`Fetching environments failed.`);
1694
+ return;
1695
+ }
1696
+ if (environments.length === 0) {
1697
+ logger.log(
1698
+ `The only available environment is already linked. You can add a new one from your project settings on the Strapi Cloud dashboard.`
1699
+ );
1700
+ return;
1701
+ }
1702
+ const answer = await promptUserForEnvironment(ctx, environments);
1703
+ if (!answer) {
1704
+ return;
1705
+ }
1706
+ await trackEvent(ctx, cloudApiService, "willLinkEnvironment", {
1707
+ projectName: project.name,
1708
+ environmentName: answer.targetEnvironment
1709
+ });
1710
+ try {
1711
+ await patch({ project: { targetEnvironment: answer.targetEnvironment } });
1712
+ } catch (e) {
1713
+ await trackEvent(ctx, cloudApiService, "didNotLinkEnvironment", {
1714
+ projectName: project.name,
1715
+ environmentName: answer.targetEnvironment
1716
+ });
1717
+ logger.debug("Failed to link environment", e);
1718
+ logger.error(
1719
+ "Failed to link the environment. If this issue persists, try re-linking your project or contact support."
1720
+ );
1721
+ process.exit(1);
1722
+ }
1723
+ logger.log(
1724
+ ` You have successfully linked your project to ${chalk.cyan(answer.targetEnvironment)}, on ${chalk.cyan(project.displayName)}. You are now able to deploy your project.`
1725
+ );
1726
+ await trackEvent(ctx, cloudApiService, "didLinkEnvironment", {
1727
+ projectName: project.name,
1728
+ environmentName: answer.targetEnvironment
1729
+ });
1730
+ };
1731
+ async function promptUserForEnvironment(ctx, environments) {
1732
+ const { logger } = ctx;
1733
+ try {
1734
+ const answer = await inquirer.prompt([
1735
+ {
1736
+ type: "list",
1737
+ name: "targetEnvironment",
1738
+ message: "Which environment do you want to link?",
1739
+ choices: [...environments, { name: chalk.grey(`(${QUIT_OPTION})`), value: null }]
1740
+ }
1741
+ ]);
1742
+ if (!answer.targetEnvironment) {
1743
+ return null;
1744
+ }
1745
+ return answer;
1746
+ } catch (e) {
1747
+ logger.debug("Failed to get user input", e);
1748
+ logger.error("An error occurred while trying to get your environment selection.");
1749
+ return null;
1750
+ }
1751
+ }
1752
+ async function getEnvironmentsList(ctx, cloudApiService, project) {
1753
+ const spinner = ctx.logger.spinner("Fetching environments...\n").start();
1754
+ try {
1755
+ const {
1756
+ data: { data: environmentsList }
1757
+ } = await cloudApiService.listLinkEnvironments({ name: project.name });
1758
+ if (!Array.isArray(environmentsList) || environmentsList.length === 0) {
1759
+ throw new Error("Environments not found in server response");
1760
+ }
1761
+ spinner.succeed();
1762
+ return environmentsList.filter(
1763
+ (environment) => environment.name !== project.targetEnvironment
1764
+ );
1765
+ } catch (e) {
1766
+ if (e.response && e.response.status === 404) {
1767
+ spinner.succeed();
1768
+ ctx.logger.warn(
1769
+ `
1770
+ The project associated with this folder does not exist in Strapi Cloud.
1771
+ Please link your local project to an existing Strapi Cloud project using the ${chalk.cyan(
1772
+ "link"
1773
+ )} command.`
1774
+ );
1775
+ } else {
1776
+ spinner.fail("An error occurred while fetching environments data from Strapi Cloud.");
1777
+ ctx.logger.debug("Failed to list environments", e);
1778
+ }
1779
+ }
1780
+ }
1781
+ const command = ({ command: command2, ctx }) => {
1782
+ const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1783
+ 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));
1784
+ };
1785
+ const linkEnvironment = {
1786
+ name: "link-environment",
1787
+ description: "Link Strapi Cloud environment to a local project",
1626
1788
  action,
1627
1789
  command
1628
1790
  };
@@ -1632,12 +1794,21 @@ const cli = {
1632
1794
  login,
1633
1795
  logout,
1634
1796
  createProject,
1797
+ linkEnvironment,
1635
1798
  listProjects,
1636
1799
  listEnvironments
1637
1800
  };
1638
- const cloudCommands = [deployProject, link, login, logout, listProjects, listEnvironments];
1801
+ const cloudCommands = [
1802
+ deployProject,
1803
+ link,
1804
+ login,
1805
+ logout,
1806
+ linkEnvironment,
1807
+ listProjects,
1808
+ listEnvironments
1809
+ ];
1639
1810
  async function initCloudCLIConfig() {
1640
- const localConfig = await getLocalConfig();
1811
+ const localConfig = await getLocalConfig$1();
1641
1812
  if (!localConfig.deviceId) {
1642
1813
  localConfig.deviceId = crypto$1.randomUUID();
1643
1814
  }