@strapi/cloud-cli 5.4.0 → 5.4.2

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