@strapi/cloud-cli 0.0.0-next.504076d2165a82f124620a3f1f3e3a6017e97f3f → 0.0.0-next.51dcef446efe662f00da6d85d6bea2218c4c538e

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.
Files changed (37) hide show
  1. package/dist/bin.js +1 -0
  2. package/dist/bin.js.map +1 -1
  3. package/dist/index.js +363 -56
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +362 -55
  6. package/dist/index.mjs.map +1 -1
  7. package/dist/src/cloud/command.d.ts +3 -0
  8. package/dist/src/cloud/command.d.ts.map +1 -0
  9. package/dist/src/deploy-project/action.d.ts +4 -1
  10. package/dist/src/deploy-project/action.d.ts.map +1 -1
  11. package/dist/src/deploy-project/command.d.ts.map +1 -1
  12. package/dist/src/environment/command.d.ts +3 -0
  13. package/dist/src/environment/command.d.ts.map +1 -0
  14. package/dist/src/environment/link/action.d.ts +4 -0
  15. package/dist/src/environment/link/action.d.ts.map +1 -0
  16. package/dist/src/environment/link/command.d.ts +4 -0
  17. package/dist/src/environment/link/command.d.ts.map +1 -0
  18. package/dist/src/environment/link/index.d.ts +7 -0
  19. package/dist/src/environment/link/index.d.ts.map +1 -0
  20. package/dist/src/environment/list/action.d.ts +4 -0
  21. package/dist/src/environment/list/action.d.ts.map +1 -0
  22. package/dist/src/environment/list/command.d.ts +4 -0
  23. package/dist/src/environment/list/command.d.ts.map +1 -0
  24. package/dist/src/environment/list/index.d.ts +7 -0
  25. package/dist/src/environment/list/index.d.ts.map +1 -0
  26. package/dist/src/index.d.ts +2 -0
  27. package/dist/src/index.d.ts.map +1 -1
  28. package/dist/src/link/action.d.ts.map +1 -1
  29. package/dist/src/services/cli-api.d.ts +23 -3
  30. package/dist/src/services/cli-api.d.ts.map +1 -1
  31. package/dist/src/services/strapi-info-save.d.ts +15 -2
  32. package/dist/src/services/strapi-info-save.d.ts.map +1 -1
  33. package/dist/src/types.d.ts +3 -0
  34. package/dist/src/types.d.ts.map +1 -1
  35. package/dist/src/utils/get-local-config.d.ts +6 -0
  36. package/dist/src/utils/get-local-config.d.ts.map +1 -0
  37. package/package.json +7 -7
package/dist/index.js CHANGED
@@ -24,6 +24,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
24
24
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
25
25
  const crypto$1 = require("crypto");
26
26
  const fse = require("fs-extra");
27
+ const inquirer = require("inquirer");
27
28
  const path = require("path");
28
29
  const chalk = require("chalk");
29
30
  const axios = require("axios");
@@ -31,10 +32,10 @@ const crypto = require("node:crypto");
31
32
  const utils = require("@strapi/utils");
32
33
  const tar = require("tar");
33
34
  const minimatch = require("minimatch");
34
- const inquirer = require("inquirer");
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");
@@ -65,12 +66,12 @@ function _interopNamespace(e) {
65
66
  }
66
67
  const crypto__default = /* @__PURE__ */ _interopDefault(crypto$1);
67
68
  const fse__namespace = /* @__PURE__ */ _interopNamespace(fse);
69
+ const inquirer__default = /* @__PURE__ */ _interopDefault(inquirer);
68
70
  const path__namespace = /* @__PURE__ */ _interopNamespace(path);
69
71
  const chalk__default = /* @__PURE__ */ _interopDefault(chalk);
70
72
  const axios__default = /* @__PURE__ */ _interopDefault(axios);
71
73
  const crypto__namespace = /* @__PURE__ */ _interopNamespace(crypto);
72
74
  const tar__namespace = /* @__PURE__ */ _interopNamespace(tar);
73
- const inquirer__default = /* @__PURE__ */ _interopDefault(inquirer);
74
75
  const os__default = /* @__PURE__ */ _interopDefault(os);
75
76
  const XDGAppPaths__default = /* @__PURE__ */ _interopDefault(XDGAppPaths);
76
77
  const jwksClient__default = /* @__PURE__ */ _interopDefault(jwksClient);
@@ -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.0.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",
@@ -254,7 +255,7 @@ const dependencies = {
254
255
  open: "8.4.0",
255
256
  ora: "5.4.1",
256
257
  "pkg-up": "3.1.0",
257
- tar: "6.1.13",
258
+ tar: "6.2.1",
258
259
  "xdg-app-paths": "8.3.0",
259
260
  yup: "0.32.9"
260
261
  };
@@ -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
- node: ">=18.0.0 <=20.x.x",
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,
@@ -317,7 +320,7 @@ async function cloudApiFactory({ logger }, token) {
317
320
  deploy({ filePath, project }, { onUploadProgress }) {
318
321
  return axiosCloudAPI.post(
319
322
  `/deploy/${project.name}`,
320
- { file: fse__namespace.default.createReadStream(filePath) },
323
+ { file: fse__namespace.default.createReadStream(filePath), targetEnvironment: project.targetEnvironment },
321
324
  {
322
325
  headers: {
323
326
  "Content-Type": "multipart/form-data"
@@ -388,6 +391,34 @@ async function cloudApiFactory({ logger }, token) {
388
391
  throw error;
389
392
  }
390
393
  },
394
+ async listEnvironments({ name: name2 }) {
395
+ try {
396
+ const response = await axiosCloudAPI.get(`/projects/${name2}/environments`);
397
+ if (response.status !== 200) {
398
+ throw new Error("Error fetching cloud environments from the server.");
399
+ }
400
+ return response;
401
+ } catch (error) {
402
+ logger.debug(
403
+ "🥲 Oops! Couldn't retrieve your project's environments from the server. Please try again."
404
+ );
405
+ throw error;
406
+ }
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
+ },
391
422
  async getProject({ name: name2 }) {
392
423
  try {
393
424
  const response = await axiosCloudAPI.get(`/projects/${name2}`);
@@ -411,26 +442,43 @@ async function cloudApiFactory({ logger }, token) {
411
442
  };
412
443
  }
413
444
  const LOCAL_SAVE_FILENAME = ".strapi-cloud.json";
445
+ const getFilePath = (directoryPath) => path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
414
446
  async function save(data, { directoryPath } = {}) {
415
- const alreadyInFileData = await retrieve({ directoryPath });
416
- const storedData = { ...alreadyInFileData, ...data };
417
- const pathToFile = path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
447
+ const pathToFile = getFilePath(directoryPath);
418
448
  await fse__namespace.default.ensureDir(path__namespace.default.dirname(pathToFile));
419
- await fse__namespace.default.writeJson(pathToFile, storedData, { encoding: "utf8" });
449
+ await fse__namespace.default.writeJson(pathToFile, data, { encoding: "utf8" });
420
450
  }
421
451
  async function retrieve({
422
452
  directoryPath
423
453
  } = {}) {
424
- const pathToFile = path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
454
+ const pathToFile = getFilePath(directoryPath);
425
455
  const pathExists = await fse__namespace.default.pathExists(pathToFile);
426
456
  if (!pathExists) {
427
457
  return {};
428
458
  }
429
459
  return fse__namespace.default.readJSON(pathToFile, { encoding: "utf8" });
430
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
+ }
431
477
  const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
432
478
  __proto__: null,
433
479
  LOCAL_SAVE_FILENAME,
480
+ deleteConfig,
481
+ patch,
434
482
  retrieve,
435
483
  save
436
484
  }, Symbol.toStringTag, { value: "Module" }));
@@ -438,7 +486,7 @@ let cliConfig;
438
486
  async function tokenServiceFactory({ logger }) {
439
487
  const cloudApiService = await cloudApiFactory({ logger });
440
488
  async function saveToken(str) {
441
- const appConfig = await getLocalConfig();
489
+ const appConfig = await getLocalConfig$1();
442
490
  if (!appConfig) {
443
491
  logger.error("There was a problem saving your token. Please try again.");
444
492
  return;
@@ -452,7 +500,7 @@ async function tokenServiceFactory({ logger }) {
452
500
  }
453
501
  }
454
502
  async function retrieveToken() {
455
- const appConfig = await getLocalConfig();
503
+ const appConfig = await getLocalConfig$1();
456
504
  if (appConfig.token) {
457
505
  if (await isTokenValid(appConfig.token)) {
458
506
  return appConfig.token;
@@ -514,7 +562,7 @@ async function tokenServiceFactory({ logger }) {
514
562
  }
515
563
  }
516
564
  async function eraseToken() {
517
- const appConfig = await getLocalConfig();
565
+ const appConfig = await getLocalConfig$1();
518
566
  if (!appConfig) {
519
567
  return;
520
568
  }
@@ -960,7 +1008,7 @@ async function createProject$1(ctx, cloudApi, projectInput) {
960
1008
  throw e;
961
1009
  }
962
1010
  }
963
- const action$4 = async (ctx) => {
1011
+ const action$6 = async (ctx) => {
964
1012
  const { logger } = ctx;
965
1013
  const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
966
1014
  const token = await getValidToken(ctx, promptLogin);
@@ -1089,6 +1137,32 @@ const buildLogsServiceFactory = ({ logger }) => {
1089
1137
  });
1090
1138
  };
1091
1139
  };
1140
+ const QUIT_OPTION$2 = "Quit";
1141
+ async function promptForEnvironment(environments) {
1142
+ const choices = environments.map((env) => ({ name: env, value: env }));
1143
+ const { selectedEnvironment } = await inquirer__default.default.prompt([
1144
+ {
1145
+ type: "list",
1146
+ name: "selectedEnvironment",
1147
+ message: "Select the environment to deploy:",
1148
+ choices: [...choices, { name: chalk__default.default.grey(`(${QUIT_OPTION$2})`), value: null }]
1149
+ }
1150
+ ]);
1151
+ if (selectedEnvironment === null) {
1152
+ process.exit(1);
1153
+ }
1154
+ const { confirm } = await inquirer__default.default.prompt([
1155
+ {
1156
+ type: "confirm",
1157
+ name: "confirm",
1158
+ message: `Do you want to proceed with deployment to ${chalk__default.default.cyan(selectedEnvironment)}?`
1159
+ }
1160
+ ]);
1161
+ if (!confirm) {
1162
+ process.exit(1);
1163
+ }
1164
+ return selectedEnvironment;
1165
+ }
1092
1166
  async function upload(ctx, project, token, maxProjectFileSize) {
1093
1167
  const cloudApi = await cloudApiFactory(ctx, token);
1094
1168
  try {
@@ -1171,7 +1245,7 @@ async function getProject(ctx) {
1171
1245
  const { project } = await retrieve();
1172
1246
  if (!project) {
1173
1247
  try {
1174
- return await action$4(ctx);
1248
+ return await action$6(ctx);
1175
1249
  } catch (e) {
1176
1250
  ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1177
1251
  ctx.logger.debug(e);
@@ -1192,7 +1266,26 @@ async function getConfig({
1192
1266
  return null;
1193
1267
  }
1194
1268
  }
1195
- const action$3 = async (ctx) => {
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) => {
1196
1289
  const { getValidToken } = await tokenServiceFactory(ctx);
1197
1290
  const token = await getValidToken(ctx, promptLogin);
1198
1291
  if (!token) {
@@ -1203,11 +1296,13 @@ const action$3 = async (ctx) => {
1203
1296
  return;
1204
1297
  }
1205
1298
  const cloudApiService = await cloudApiFactory(ctx, token);
1299
+ let environments;
1206
1300
  try {
1207
1301
  const {
1208
1302
  data: { data: projectData, metadata }
1209
1303
  } = await cloudApiService.getProject({ name: project.name });
1210
1304
  const isProjectSuspended = projectData.suspendedAt;
1305
+ environments = projectData.environments;
1211
1306
  if (isProjectSuspended) {
1212
1307
  ctx.logger.log(
1213
1308
  "\n Oops! This project has been suspended. \n\n Please reactivate it from the dashboard to continue deploying: "
@@ -1254,11 +1349,15 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1254
1349
  );
1255
1350
  maxSize = 1e8;
1256
1351
  }
1352
+ project.targetEnvironment = await getTargetEnvironment(ctx, opts, project, environments);
1257
1353
  const buildId = await upload(ctx, project, token, maxSize);
1258
1354
  if (!buildId) {
1259
1355
  return;
1260
1356
  }
1261
1357
  try {
1358
+ ctx.logger.log(
1359
+ `🚀 Deploying project to ${chalk__default.default.cyan(project.targetEnvironment ?? `production`)} environment...`
1360
+ );
1262
1361
  notificationService(`${apiConfig.apiBaseUrl}/notifications`, token, cliConfig2);
1263
1362
  await buildLogsService(`${apiConfig.apiBaseUrl}/v1/logs/${buildId}`, token, cliConfig2);
1264
1363
  ctx.logger.log(
@@ -1303,17 +1402,16 @@ const runAction = (name2, action2) => (...args) => {
1303
1402
  process.exit(1);
1304
1403
  });
1305
1404
  };
1306
- const command$5 = ({ ctx }) => {
1307
- 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").action(() => runAction("deploy", action$3)(ctx));
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));
1308
1407
  };
1309
1408
  const deployProject = {
1310
1409
  name: "deploy-project",
1311
1410
  description: "Deploy a Strapi Cloud project",
1312
- action: action$3,
1313
- command: command$5
1411
+ action: action$5,
1412
+ command: command$7
1314
1413
  };
1315
- const QUIT_OPTION = "Quit";
1316
- async function getExistingConfig(ctx) {
1414
+ async function getLocalConfig(ctx) {
1317
1415
  try {
1318
1416
  return await retrieve();
1319
1417
  } catch (e) {
@@ -1322,6 +1420,21 @@ async function getExistingConfig(ctx) {
1322
1420
  return null;
1323
1421
  }
1324
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";
1325
1438
  async function promptForRelink(ctx, cloudApiService, existingConfig) {
1326
1439
  if (existingConfig && existingConfig.project) {
1327
1440
  const { shouldRelink } = await inquirer__default.default.prompt([
@@ -1351,7 +1464,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1351
1464
  } = await cloudApiService.listLinkProjects();
1352
1465
  spinner.succeed();
1353
1466
  if (!Array.isArray(projectList)) {
1354
- 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.");
1355
1468
  return null;
1356
1469
  }
1357
1470
  const projects = projectList.filter(
@@ -1363,7 +1476,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1363
1476
  };
1364
1477
  });
1365
1478
  if (projects.length === 0) {
1366
- 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.");
1367
1480
  return null;
1368
1481
  }
1369
1482
  return projects;
@@ -1381,7 +1494,7 @@ async function getUserSelection(ctx, projects) {
1381
1494
  type: "list",
1382
1495
  name: "linkProject",
1383
1496
  message: "Which project do you want to link?",
1384
- choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION})`), value: null }]
1497
+ choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION$1})`), value: null }]
1385
1498
  }
1386
1499
  ]);
1387
1500
  if (!answer.linkProject) {
@@ -1394,7 +1507,7 @@ async function getUserSelection(ctx, projects) {
1394
1507
  return null;
1395
1508
  }
1396
1509
  }
1397
- const action$2 = async (ctx) => {
1510
+ const action$4 = async (ctx) => {
1398
1511
  const { getValidToken } = await tokenServiceFactory(ctx);
1399
1512
  const token = await getValidToken(ctx, promptLogin);
1400
1513
  const { logger } = ctx;
@@ -1402,7 +1515,7 @@ const action$2 = async (ctx) => {
1402
1515
  return;
1403
1516
  }
1404
1517
  const cloudApiService = await cloudApiFactory(ctx, token);
1405
- const existingConfig = await getExistingConfig(ctx);
1518
+ const existingConfig = await getLocalConfig(ctx);
1406
1519
  const shouldRelink = await promptForRelink(ctx, cloudApiService, existingConfig);
1407
1520
  if (!shouldRelink) {
1408
1521
  return;
@@ -1437,7 +1550,9 @@ const action$2 = async (ctx) => {
1437
1550
  return;
1438
1551
  }
1439
1552
  await save({ project: answer.linkProject });
1440
- 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
+ );
1441
1556
  await trackEvent(ctx, cloudApiService, "didLinkProject", {
1442
1557
  projectInternalName: answer.linkProject
1443
1558
  });
@@ -1449,16 +1564,16 @@ const action$2 = async (ctx) => {
1449
1564
  });
1450
1565
  }
1451
1566
  };
1452
- const command$4 = ({ command: command2, ctx }) => {
1453
- 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$2)(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));
1454
1569
  };
1455
1570
  const link = {
1456
1571
  name: "link-project",
1457
1572
  description: "Link a local directory to a Strapi Cloud project",
1458
- action: action$2,
1459
- command: command$4
1573
+ action: action$4,
1574
+ command: command$6
1460
1575
  };
1461
- const command$3 = ({ ctx }) => {
1576
+ const command$5 = ({ ctx }) => {
1462
1577
  return commander.createCommand("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1463
1578
  "after",
1464
1579
  "\nAfter running this command, you will be prompted to enter your authentication information."
@@ -1468,10 +1583,10 @@ const login = {
1468
1583
  name: "login",
1469
1584
  description: "Strapi Cloud Login",
1470
1585
  action: loginAction,
1471
- command: command$3
1586
+ command: command$5
1472
1587
  };
1473
1588
  const openModule = import("open");
1474
- const action$1 = async (ctx) => {
1589
+ const action$3 = async (ctx) => {
1475
1590
  const { logger } = ctx;
1476
1591
  const { retrieveToken, eraseToken } = await tokenServiceFactory(ctx);
1477
1592
  const token = await retrieveToken();
@@ -1503,25 +1618,25 @@ const action$1 = async (ctx) => {
1503
1618
  }
1504
1619
  await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1505
1620
  };
1506
- const command$2 = ({ ctx }) => {
1507
- 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$1)(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));
1508
1623
  };
1509
1624
  const logout = {
1510
1625
  name: "logout",
1511
1626
  description: "Strapi Cloud Logout",
1512
- action: action$1,
1513
- command: command$2
1627
+ action: action$3,
1628
+ command: command$4
1514
1629
  };
1515
- const command$1 = ({ ctx }) => {
1516
- 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$4)(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));
1517
1632
  };
1518
1633
  const createProject = {
1519
1634
  name: "create-project",
1520
1635
  description: "Create a new project",
1521
- action: action$4,
1522
- command: command$1
1636
+ action: action$6,
1637
+ command: command$3
1523
1638
  };
1524
- const action = async (ctx) => {
1639
+ const action$2 = async (ctx) => {
1525
1640
  const { getValidToken } = await tokenServiceFactory(ctx);
1526
1641
  const token = await getValidToken(ctx, promptLogin);
1527
1642
  const { logger } = ctx;
@@ -1541,12 +1656,194 @@ const action = async (ctx) => {
1541
1656
  spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1542
1657
  }
1543
1658
  };
1544
- const command = ({ command: command2, ctx }) => {
1545
- 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)(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));
1546
1661
  };
1547
1662
  const listProjects = {
1548
1663
  name: "list-projects",
1549
1664
  description: "List Strapi Cloud projects",
1665
+ action: action$2,
1666
+ command: command$2
1667
+ };
1668
+ const action$1 = async (ctx) => {
1669
+ const { getValidToken } = await tokenServiceFactory(ctx);
1670
+ const token = await getValidToken(ctx, promptLogin);
1671
+ const { logger } = ctx;
1672
+ if (!token) {
1673
+ return;
1674
+ }
1675
+ const project = await getLocalProject(ctx);
1676
+ if (!project) {
1677
+ ctx.logger.debug(`No valid local project configuration was found.`);
1678
+ return;
1679
+ }
1680
+ const cloudApiService = await cloudApiFactory(ctx, token);
1681
+ const spinner = logger.spinner("Fetching environments...").start();
1682
+ await trackEvent(ctx, cloudApiService, "willListEnvironment", {
1683
+ projectInternalName: project.name
1684
+ });
1685
+ try {
1686
+ const {
1687
+ data: { data: environmentsList }
1688
+ } = await cloudApiService.listEnvironments({ name: project.name });
1689
+ spinner.succeed();
1690
+ logger.log(environmentsList);
1691
+ await trackEvent(ctx, cloudApiService, "didListEnvironment", {
1692
+ projectInternalName: project.name
1693
+ });
1694
+ } catch (e) {
1695
+ if (e.response && e.response.status === 404) {
1696
+ spinner.succeed();
1697
+ logger.warn(
1698
+ `
1699
+ The project associated with this folder does not exist in Strapi Cloud.
1700
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1701
+ "link"
1702
+ )} command`
1703
+ );
1704
+ } else {
1705
+ spinner.fail("An error occurred while fetching environments data from Strapi Cloud.");
1706
+ logger.debug("Failed to list environments", e);
1707
+ }
1708
+ await trackEvent(ctx, cloudApiService, "didNotListEnvironment", {
1709
+ projectInternalName: project.name
1710
+ });
1711
+ }
1712
+ };
1713
+ function defineCloudNamespace(command2, ctx) {
1714
+ const cloud = command2.command("cloud").description("Manage Strapi Cloud projects");
1715
+ cloud.command("environments").description("Alias for cloud environment list").action(() => runAction("list", action$1)(ctx));
1716
+ return cloud;
1717
+ }
1718
+ let environmentCmd = null;
1719
+ const initializeEnvironmentCommand = (command2, ctx) => {
1720
+ if (!environmentCmd) {
1721
+ const cloud = defineCloudNamespace(command2, ctx);
1722
+ environmentCmd = cloud.command("environment").description("Manage environments");
1723
+ }
1724
+ return environmentCmd;
1725
+ };
1726
+ const command$1 = ({ command: command2, ctx }) => {
1727
+ const environmentCmd2 = initializeEnvironmentCommand(command2, 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));
1729
+ };
1730
+ const listEnvironments = {
1731
+ name: "list-environments",
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",
1550
1847
  action,
1551
1848
  command
1552
1849
  };
@@ -1556,11 +1853,21 @@ const cli = {
1556
1853
  login,
1557
1854
  logout,
1558
1855
  createProject,
1559
- listProjects
1856
+ linkEnvironment,
1857
+ listProjects,
1858
+ listEnvironments
1560
1859
  };
1561
- const cloudCommands = [deployProject, link, login, logout, listProjects];
1860
+ const cloudCommands = [
1861
+ deployProject,
1862
+ link,
1863
+ login,
1864
+ logout,
1865
+ linkEnvironment,
1866
+ listProjects,
1867
+ listEnvironments
1868
+ ];
1562
1869
  async function initCloudCLIConfig() {
1563
- const localConfig = await getLocalConfig();
1870
+ const localConfig = await getLocalConfig$1();
1564
1871
  if (!localConfig.deviceId) {
1565
1872
  localConfig.deviceId = crypto__default.default.randomUUID();
1566
1873
  }