@strapi/cloud-cli 0.0.0-next.7f1333f1967e625c57ab16648c057aea08c9dddb → 0.0.0-next.820adfc37c687bbcc4a213ae6929c541358b71e8

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 (40) hide show
  1. package/dist/bin.js +2 -2
  2. package/dist/bin.js.map +1 -1
  3. package/dist/index.js +394 -60
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +391 -57
  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 +5 -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 +30 -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/services/token.d.ts.map +1 -1
  34. package/dist/src/types.d.ts +6 -0
  35. package/dist/src/types.d.ts.map +1 -1
  36. package/dist/src/utils/get-local-config.d.ts +6 -0
  37. package/dist/src/utils/get-local-config.d.ts.map +1 -0
  38. package/dist/src/utils/helpers.d.ts.map +1 -1
  39. package/dist/src/utils/pkg.d.ts.map +1 -1
  40. package/package.json +8 -7
package/dist/index.js CHANGED
@@ -24,6 +24,8 @@ 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");
28
+ const boxen = require("boxen");
27
29
  const path = require("path");
28
30
  const chalk = require("chalk");
29
31
  const axios = require("axios");
@@ -31,10 +33,10 @@ const crypto = require("node:crypto");
31
33
  const utils = require("@strapi/utils");
32
34
  const tar = require("tar");
33
35
  const minimatch = require("minimatch");
34
- const inquirer = require("inquirer");
35
36
  const fp = require("lodash/fp");
36
37
  const os = require("os");
37
38
  const XDGAppPaths = require("xdg-app-paths");
39
+ const lodash = require("lodash");
38
40
  const jwksClient = require("jwks-rsa");
39
41
  const jwt = require("jsonwebtoken");
40
42
  const stringify = require("fast-safe-stringify");
@@ -46,8 +48,7 @@ const EventSource = require("eventsource");
46
48
  const commander = require("commander");
47
49
  const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
48
50
  function _interopNamespace(e) {
49
- if (e && e.__esModule)
50
- return e;
51
+ if (e && e.__esModule) return e;
51
52
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
52
53
  if (e) {
53
54
  for (const k in e) {
@@ -65,12 +66,13 @@ 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);
70
+ const boxen__default = /* @__PURE__ */ _interopDefault(boxen);
68
71
  const path__namespace = /* @__PURE__ */ _interopNamespace(path);
69
72
  const chalk__default = /* @__PURE__ */ _interopDefault(chalk);
70
73
  const axios__default = /* @__PURE__ */ _interopDefault(axios);
71
74
  const crypto__namespace = /* @__PURE__ */ _interopNamespace(crypto);
72
75
  const tar__namespace = /* @__PURE__ */ _interopNamespace(tar);
73
- const inquirer__default = /* @__PURE__ */ _interopDefault(inquirer);
74
76
  const os__default = /* @__PURE__ */ _interopDefault(os);
75
77
  const XDGAppPaths__default = /* @__PURE__ */ _interopDefault(XDGAppPaths);
76
78
  const jwksClient__default = /* @__PURE__ */ _interopDefault(jwksClient);
@@ -137,8 +139,7 @@ const getFiles = async (dirPath, ignorePatterns = [], subfolder = "") => {
137
139
  const readGitignore = async (folderPath) => {
138
140
  const gitignorePath = path__namespace.resolve(folderPath, ".gitignore");
139
141
  const pathExist = await fse__namespace.pathExists(gitignorePath);
140
- if (!pathExist)
141
- return [];
142
+ if (!pathExist) return [];
142
143
  const gitignoreContent = await fse__namespace.readFile(gitignorePath, "utf8");
143
144
  return gitignoreContent.split(/\r?\n/).filter((line) => Boolean(line.trim()) && !line.startsWith("#"));
144
145
  };
@@ -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.2-beta.0";
197
+ const version = "5.5.0";
197
198
  const description = "Commands to interact with the Strapi Cloud";
198
199
  const keywords = [
199
200
  "strapi",
@@ -240,6 +241,7 @@ const scripts = {
240
241
  const dependencies = {
241
242
  "@strapi/utils": "workspace:*",
242
243
  axios: "1.7.4",
244
+ boxen: "5.1.2",
243
245
  chalk: "4.1.2",
244
246
  "cli-progress": "3.12.0",
245
247
  commander: "8.3.0",
@@ -259,7 +261,7 @@ const dependencies = {
259
261
  yup: "0.32.9"
260
262
  };
261
263
  const devDependencies = {
262
- "@strapi/pack-up": "4.23.0",
264
+ "@strapi/pack-up": "5.0.2",
263
265
  "@types/cli-progress": "3.11.5",
264
266
  "@types/eventsource": "1.1.15",
265
267
  "@types/lodash": "^4.14.191",
@@ -267,9 +269,10 @@ const devDependencies = {
267
269
  tsconfig: "workspace:*"
268
270
  };
269
271
  const engines = {
270
- node: ">=18.0.0 <=20.x.x",
272
+ node: ">=18.0.0 <=22.x.x",
271
273
  npm: ">=6.0.0"
272
274
  };
275
+ const gitHead = "7d785703f52464577d077c4618cbe68b44f8a9cd";
273
276
  const packageJson = {
274
277
  name,
275
278
  version,
@@ -290,11 +293,12 @@ const packageJson = {
290
293
  scripts,
291
294
  dependencies,
292
295
  devDependencies,
293
- engines
296
+ engines,
297
+ gitHead
294
298
  };
295
299
  const VERSION = "v1";
296
300
  async function cloudApiFactory({ logger }, token) {
297
- const localConfig = await getLocalConfig();
301
+ const localConfig = await getLocalConfig$1();
298
302
  const customHeaders = {
299
303
  "x-device-id": localConfig.deviceId,
300
304
  "x-app-version": packageJson.version,
@@ -317,7 +321,7 @@ async function cloudApiFactory({ logger }, token) {
317
321
  deploy({ filePath, project }, { onUploadProgress }) {
318
322
  return axiosCloudAPI.post(
319
323
  `/deploy/${project.name}`,
320
- { file: fse__namespace.default.createReadStream(filePath) },
324
+ { file: fse__namespace.default.createReadStream(filePath), targetEnvironment: project.targetEnvironment },
321
325
  {
322
326
  headers: {
323
327
  "Content-Type": "multipart/form-data"
@@ -388,6 +392,34 @@ async function cloudApiFactory({ logger }, token) {
388
392
  throw error;
389
393
  }
390
394
  },
395
+ async listEnvironments({ name: name2 }) {
396
+ try {
397
+ const response = await axiosCloudAPI.get(`/projects/${name2}/environments`);
398
+ if (response.status !== 200) {
399
+ throw new Error("Error fetching cloud environments from the server.");
400
+ }
401
+ return response;
402
+ } catch (error) {
403
+ logger.debug(
404
+ "🥲 Oops! Couldn't retrieve your project's environments from the server. Please try again."
405
+ );
406
+ throw error;
407
+ }
408
+ },
409
+ async listLinkEnvironments({ name: name2 }) {
410
+ try {
411
+ const response = await axiosCloudAPI.get(`/projects/${name2}/environments-linkable`);
412
+ if (response.status !== 200) {
413
+ throw new Error("Error fetching cloud environments from the server.");
414
+ }
415
+ return response;
416
+ } catch (error) {
417
+ logger.debug(
418
+ "🥲 Oops! Couldn't retrieve your project's environments from the server. Please try again."
419
+ );
420
+ throw error;
421
+ }
422
+ },
391
423
  async getProject({ name: name2 }) {
392
424
  try {
393
425
  const response = await axiosCloudAPI.get(`/projects/${name2}`);
@@ -411,26 +443,43 @@ async function cloudApiFactory({ logger }, token) {
411
443
  };
412
444
  }
413
445
  const LOCAL_SAVE_FILENAME = ".strapi-cloud.json";
446
+ const getFilePath = (directoryPath) => path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
414
447
  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);
448
+ const pathToFile = getFilePath(directoryPath);
418
449
  await fse__namespace.default.ensureDir(path__namespace.default.dirname(pathToFile));
419
- await fse__namespace.default.writeJson(pathToFile, storedData, { encoding: "utf8" });
450
+ await fse__namespace.default.writeJson(pathToFile, data, { encoding: "utf8" });
420
451
  }
421
452
  async function retrieve({
422
453
  directoryPath
423
454
  } = {}) {
424
- const pathToFile = path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
455
+ const pathToFile = getFilePath(directoryPath);
425
456
  const pathExists = await fse__namespace.default.pathExists(pathToFile);
426
457
  if (!pathExists) {
427
458
  return {};
428
459
  }
429
460
  return fse__namespace.default.readJSON(pathToFile, { encoding: "utf8" });
430
461
  }
462
+ async function patch(patchData, { directoryPath } = {}) {
463
+ const pathToFile = getFilePath(directoryPath);
464
+ const existingData = await retrieve({ directoryPath });
465
+ if (!existingData) {
466
+ throw new Error("No configuration data found to patch.");
467
+ }
468
+ const newData = lodash.merge(existingData, patchData);
469
+ await fse__namespace.default.writeJson(pathToFile, newData, { encoding: "utf8" });
470
+ }
471
+ async function deleteConfig({ directoryPath } = {}) {
472
+ const pathToFile = getFilePath(directoryPath);
473
+ const pathExists = await fse__namespace.default.pathExists(pathToFile);
474
+ if (pathExists) {
475
+ await fse__namespace.default.remove(pathToFile);
476
+ }
477
+ }
431
478
  const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
432
479
  __proto__: null,
433
480
  LOCAL_SAVE_FILENAME,
481
+ deleteConfig,
482
+ patch,
434
483
  retrieve,
435
484
  save
436
485
  }, Symbol.toStringTag, { value: "Module" }));
@@ -438,7 +487,7 @@ let cliConfig;
438
487
  async function tokenServiceFactory({ logger }) {
439
488
  const cloudApiService = await cloudApiFactory({ logger });
440
489
  async function saveToken(str) {
441
- const appConfig = await getLocalConfig();
490
+ const appConfig = await getLocalConfig$1();
442
491
  if (!appConfig) {
443
492
  logger.error("There was a problem saving your token. Please try again.");
444
493
  return;
@@ -452,7 +501,7 @@ async function tokenServiceFactory({ logger }) {
452
501
  }
453
502
  }
454
503
  async function retrieveToken() {
455
- const appConfig = await getLocalConfig();
504
+ const appConfig = await getLocalConfig$1();
456
505
  if (appConfig.token) {
457
506
  if (await isTokenValid(appConfig.token)) {
458
507
  return appConfig.token;
@@ -514,7 +563,7 @@ async function tokenServiceFactory({ logger }) {
514
563
  }
515
564
  }
516
565
  async function eraseToken() {
517
- const appConfig = await getLocalConfig();
566
+ const appConfig = await getLocalConfig$1();
518
567
  if (!appConfig) {
519
568
  return;
520
569
  }
@@ -535,8 +584,7 @@ async function tokenServiceFactory({ logger }) {
535
584
  logger.log(
536
585
  token ? "Oops! Your token seems expired or invalid. Please login again." : "We couldn't find a valid token. You need to be logged in to use this feature."
537
586
  );
538
- if (!await loginAction2(ctx))
539
- return null;
587
+ if (!await loginAction2(ctx)) return null;
540
588
  token = await retrieveToken();
541
589
  }
542
590
  return token;
@@ -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,29 @@ const buildLogsServiceFactory = ({ logger }) => {
1089
1137
  });
1090
1138
  };
1091
1139
  };
1140
+ const boxenOptions = {
1141
+ padding: 1,
1142
+ margin: 1,
1143
+ align: "center",
1144
+ borderColor: "yellow",
1145
+ borderStyle: "round"
1146
+ };
1147
+ const QUIT_OPTION$2 = "Quit";
1148
+ async function promptForEnvironment(environments) {
1149
+ const choices = environments.map((env) => ({ name: env, value: env }));
1150
+ const { selectedEnvironment } = await inquirer__default.default.prompt([
1151
+ {
1152
+ type: "list",
1153
+ name: "selectedEnvironment",
1154
+ message: "Select the environment to deploy:",
1155
+ choices: [...choices, { name: chalk__default.default.grey(`(${QUIT_OPTION$2})`), value: null }]
1156
+ }
1157
+ ]);
1158
+ if (selectedEnvironment === null) {
1159
+ process.exit(1);
1160
+ }
1161
+ return selectedEnvironment;
1162
+ }
1092
1163
  async function upload(ctx, project, token, maxProjectFileSize) {
1093
1164
  const cloudApi = await cloudApiFactory(ctx, token);
1094
1165
  try {
@@ -1171,7 +1242,7 @@ async function getProject(ctx) {
1171
1242
  const { project } = await retrieve();
1172
1243
  if (!project) {
1173
1244
  try {
1174
- return await action$4(ctx);
1245
+ return await action$6(ctx);
1175
1246
  } catch (e) {
1176
1247
  ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1177
1248
  ctx.logger.debug(e);
@@ -1192,7 +1263,33 @@ async function getConfig({
1192
1263
  return null;
1193
1264
  }
1194
1265
  }
1195
- const action$3 = async (ctx) => {
1266
+ function validateEnvironment(ctx, environment, environments) {
1267
+ if (!environments.includes(environment)) {
1268
+ ctx.logger.error(`Environment ${environment} does not exist.`);
1269
+ process.exit(1);
1270
+ }
1271
+ }
1272
+ async function getTargetEnvironment(ctx, opts, project, environments) {
1273
+ if (opts.env) {
1274
+ validateEnvironment(ctx, opts.env, environments);
1275
+ return opts.env;
1276
+ }
1277
+ if (project.targetEnvironment) {
1278
+ return project.targetEnvironment;
1279
+ }
1280
+ if (environments.length > 1) {
1281
+ return promptForEnvironment(environments);
1282
+ }
1283
+ return environments[0];
1284
+ }
1285
+ function hasPendingOrLiveDeployment(environments, targetEnvironment) {
1286
+ const environment = environments.find((env) => env.name === targetEnvironment);
1287
+ if (!environment) {
1288
+ throw new Error(`Environment details ${targetEnvironment} not found.`);
1289
+ }
1290
+ return environment.hasPendingDeployment || environment.hasLiveDeployment || false;
1291
+ }
1292
+ const action$5 = async (ctx, opts) => {
1196
1293
  const { getValidToken } = await tokenServiceFactory(ctx);
1197
1294
  const token = await getValidToken(ctx, promptLogin);
1198
1295
  if (!token) {
@@ -1203,10 +1300,16 @@ const action$3 = async (ctx) => {
1203
1300
  return;
1204
1301
  }
1205
1302
  const cloudApiService = await cloudApiFactory(ctx, token);
1303
+ let projectData;
1304
+ let environments;
1305
+ let environmentsDetails;
1206
1306
  try {
1207
1307
  const {
1208
- data: { data: projectData, metadata }
1308
+ data: { data, metadata }
1209
1309
  } = await cloudApiService.getProject({ name: project.name });
1310
+ projectData = data;
1311
+ environments = projectData.environments;
1312
+ environmentsDetails = projectData.environmentsDetails;
1210
1313
  const isProjectSuspended = projectData.suspendedAt;
1211
1314
  if (isProjectSuspended) {
1212
1315
  ctx.logger.log(
@@ -1254,11 +1357,34 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1254
1357
  );
1255
1358
  maxSize = 1e8;
1256
1359
  }
1360
+ project.targetEnvironment = await getTargetEnvironment(ctx, opts, project, environments);
1361
+ if (!opts.force) {
1362
+ const shouldDisplayWarning = hasPendingOrLiveDeployment(
1363
+ environmentsDetails,
1364
+ project.targetEnvironment
1365
+ );
1366
+ if (shouldDisplayWarning) {
1367
+ ctx.logger.log(boxen__default.default(cliConfig2.projectDeployment.confirmationText, boxenOptions));
1368
+ const { confirm } = await inquirer__default.default.prompt([
1369
+ {
1370
+ type: "confirm",
1371
+ name: "confirm",
1372
+ message: `Do you want to proceed with deployment to ${chalk__default.default.cyan(projectData.displayName)} on ${chalk__default.default.cyan(project.targetEnvironment)} environment?`
1373
+ }
1374
+ ]);
1375
+ if (!confirm) {
1376
+ process.exit(1);
1377
+ }
1378
+ }
1379
+ }
1257
1380
  const buildId = await upload(ctx, project, token, maxSize);
1258
1381
  if (!buildId) {
1259
1382
  return;
1260
1383
  }
1261
1384
  try {
1385
+ ctx.logger.log(
1386
+ `🚀 Deploying project to ${chalk__default.default.cyan(project.targetEnvironment ?? `production`)} environment...`
1387
+ );
1262
1388
  notificationService(`${apiConfig.apiBaseUrl}/notifications`, token, cliConfig2);
1263
1389
  await buildLogsService(`${apiConfig.apiBaseUrl}/v1/logs/${buildId}`, token, cliConfig2);
1264
1390
  ctx.logger.log(
@@ -1303,17 +1429,16 @@ const runAction = (name2, action2) => (...args) => {
1303
1429
  process.exit(1);
1304
1430
  });
1305
1431
  };
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));
1432
+ const command$7 = ({ ctx }) => {
1433
+ return commander.createCommand("cloud:deploy").alias("deploy").description("Deploy a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").option("-f, --force", "Skip confirmation to deploy").option("-e, --env <name>", "Specify the environment to deploy").action((opts) => runAction("deploy", action$5)(ctx, opts));
1308
1434
  };
1309
1435
  const deployProject = {
1310
1436
  name: "deploy-project",
1311
1437
  description: "Deploy a Strapi Cloud project",
1312
- action: action$3,
1313
- command: command$5
1438
+ action: action$5,
1439
+ command: command$7
1314
1440
  };
1315
- const QUIT_OPTION = "Quit";
1316
- async function getExistingConfig(ctx) {
1441
+ async function getLocalConfig(ctx) {
1317
1442
  try {
1318
1443
  return await retrieve();
1319
1444
  } catch (e) {
@@ -1322,6 +1447,21 @@ async function getExistingConfig(ctx) {
1322
1447
  return null;
1323
1448
  }
1324
1449
  }
1450
+ async function getLocalProject(ctx) {
1451
+ const localConfig = await getLocalConfig(ctx);
1452
+ if (!localConfig || !localConfig.project) {
1453
+ ctx.logger.warn(
1454
+ `
1455
+ We couldn't find a valid local project config.
1456
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1457
+ "link"
1458
+ )} command.`
1459
+ );
1460
+ process.exit(1);
1461
+ }
1462
+ return localConfig.project;
1463
+ }
1464
+ const QUIT_OPTION$1 = "Quit";
1325
1465
  async function promptForRelink(ctx, cloudApiService, existingConfig) {
1326
1466
  if (existingConfig && existingConfig.project) {
1327
1467
  const { shouldRelink } = await inquirer__default.default.prompt([
@@ -1351,7 +1491,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1351
1491
  } = await cloudApiService.listLinkProjects();
1352
1492
  spinner.succeed();
1353
1493
  if (!Array.isArray(projectList)) {
1354
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1494
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1355
1495
  return null;
1356
1496
  }
1357
1497
  const projects = projectList.filter(
@@ -1363,7 +1503,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1363
1503
  };
1364
1504
  });
1365
1505
  if (projects.length === 0) {
1366
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1506
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1367
1507
  return null;
1368
1508
  }
1369
1509
  return projects;
@@ -1381,7 +1521,7 @@ async function getUserSelection(ctx, projects) {
1381
1521
  type: "list",
1382
1522
  name: "linkProject",
1383
1523
  message: "Which project do you want to link?",
1384
- choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION})`), value: null }]
1524
+ choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION$1})`), value: null }]
1385
1525
  }
1386
1526
  ]);
1387
1527
  if (!answer.linkProject) {
@@ -1394,7 +1534,7 @@ async function getUserSelection(ctx, projects) {
1394
1534
  return null;
1395
1535
  }
1396
1536
  }
1397
- const action$2 = async (ctx) => {
1537
+ const action$4 = async (ctx) => {
1398
1538
  const { getValidToken } = await tokenServiceFactory(ctx);
1399
1539
  const token = await getValidToken(ctx, promptLogin);
1400
1540
  const { logger } = ctx;
@@ -1402,7 +1542,7 @@ const action$2 = async (ctx) => {
1402
1542
  return;
1403
1543
  }
1404
1544
  const cloudApiService = await cloudApiFactory(ctx, token);
1405
- const existingConfig = await getExistingConfig(ctx);
1545
+ const existingConfig = await getLocalConfig(ctx);
1406
1546
  const shouldRelink = await promptForRelink(ctx, cloudApiService, existingConfig);
1407
1547
  if (!shouldRelink) {
1408
1548
  return;
@@ -1437,7 +1577,9 @@ const action$2 = async (ctx) => {
1437
1577
  return;
1438
1578
  }
1439
1579
  await save({ project: answer.linkProject });
1440
- logger.log(`Project ${chalk__default.default.cyan(answer.linkProject.displayName)} linked successfully.`);
1580
+ logger.log(
1581
+ ` You have successfully linked your project to ${chalk__default.default.cyan(answer.linkProject.displayName)}. You are now able to deploy your project.`
1582
+ );
1441
1583
  await trackEvent(ctx, cloudApiService, "didLinkProject", {
1442
1584
  projectInternalName: answer.linkProject
1443
1585
  });
@@ -1449,16 +1591,16 @@ const action$2 = async (ctx) => {
1449
1591
  });
1450
1592
  }
1451
1593
  };
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));
1594
+ const command$6 = ({ command: command2, ctx }) => {
1595
+ 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
1596
  };
1455
1597
  const link = {
1456
1598
  name: "link-project",
1457
1599
  description: "Link a local directory to a Strapi Cloud project",
1458
- action: action$2,
1459
- command: command$4
1600
+ action: action$4,
1601
+ command: command$6
1460
1602
  };
1461
- const command$3 = ({ ctx }) => {
1603
+ const command$5 = ({ ctx }) => {
1462
1604
  return commander.createCommand("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1463
1605
  "after",
1464
1606
  "\nAfter running this command, you will be prompted to enter your authentication information."
@@ -1468,10 +1610,10 @@ const login = {
1468
1610
  name: "login",
1469
1611
  description: "Strapi Cloud Login",
1470
1612
  action: loginAction,
1471
- command: command$3
1613
+ command: command$5
1472
1614
  };
1473
1615
  const openModule = import("open");
1474
- const action$1 = async (ctx) => {
1616
+ const action$3 = async (ctx) => {
1475
1617
  const { logger } = ctx;
1476
1618
  const { retrieveToken, eraseToken } = await tokenServiceFactory(ctx);
1477
1619
  const token = await retrieveToken();
@@ -1503,25 +1645,25 @@ const action$1 = async (ctx) => {
1503
1645
  }
1504
1646
  await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1505
1647
  };
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));
1648
+ const command$4 = ({ ctx }) => {
1649
+ 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
1650
  };
1509
1651
  const logout = {
1510
1652
  name: "logout",
1511
1653
  description: "Strapi Cloud Logout",
1512
- action: action$1,
1513
- command: command$2
1654
+ action: action$3,
1655
+ command: command$4
1514
1656
  };
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));
1657
+ const command$3 = ({ ctx }) => {
1658
+ 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
1659
  };
1518
1660
  const createProject = {
1519
1661
  name: "create-project",
1520
1662
  description: "Create a new project",
1521
- action: action$4,
1522
- command: command$1
1663
+ action: action$6,
1664
+ command: command$3
1523
1665
  };
1524
- const action = async (ctx) => {
1666
+ const action$2 = async (ctx) => {
1525
1667
  const { getValidToken } = await tokenServiceFactory(ctx);
1526
1668
  const token = await getValidToken(ctx, promptLogin);
1527
1669
  const { logger } = ctx;
@@ -1541,12 +1683,194 @@ const action = async (ctx) => {
1541
1683
  spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1542
1684
  }
1543
1685
  };
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));
1686
+ const command$2 = ({ command: command2, ctx }) => {
1687
+ 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
1688
  };
1547
1689
  const listProjects = {
1548
1690
  name: "list-projects",
1549
1691
  description: "List Strapi Cloud projects",
1692
+ action: action$2,
1693
+ command: command$2
1694
+ };
1695
+ const action$1 = async (ctx) => {
1696
+ const { getValidToken } = await tokenServiceFactory(ctx);
1697
+ const token = await getValidToken(ctx, promptLogin);
1698
+ const { logger } = ctx;
1699
+ if (!token) {
1700
+ return;
1701
+ }
1702
+ const project = await getLocalProject(ctx);
1703
+ if (!project) {
1704
+ ctx.logger.debug(`No valid local project configuration was found.`);
1705
+ return;
1706
+ }
1707
+ const cloudApiService = await cloudApiFactory(ctx, token);
1708
+ const spinner = logger.spinner("Fetching environments...").start();
1709
+ await trackEvent(ctx, cloudApiService, "willListEnvironment", {
1710
+ projectInternalName: project.name
1711
+ });
1712
+ try {
1713
+ const {
1714
+ data: { data: environmentsList }
1715
+ } = await cloudApiService.listEnvironments({ name: project.name });
1716
+ spinner.succeed();
1717
+ logger.log(environmentsList);
1718
+ await trackEvent(ctx, cloudApiService, "didListEnvironment", {
1719
+ projectInternalName: project.name
1720
+ });
1721
+ } catch (e) {
1722
+ if (e.response && e.response.status === 404) {
1723
+ spinner.succeed();
1724
+ logger.warn(
1725
+ `
1726
+ The project associated with this folder does not exist in Strapi Cloud.
1727
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1728
+ "link"
1729
+ )} command`
1730
+ );
1731
+ } else {
1732
+ spinner.fail("An error occurred while fetching environments data from Strapi Cloud.");
1733
+ logger.debug("Failed to list environments", e);
1734
+ }
1735
+ await trackEvent(ctx, cloudApiService, "didNotListEnvironment", {
1736
+ projectInternalName: project.name
1737
+ });
1738
+ }
1739
+ };
1740
+ function defineCloudNamespace(command2, ctx) {
1741
+ const cloud = command2.command("cloud").description("Manage Strapi Cloud projects");
1742
+ cloud.command("environments").description("Alias for cloud environment list").action(() => runAction("list", action$1)(ctx));
1743
+ return cloud;
1744
+ }
1745
+ let environmentCmd = null;
1746
+ const initializeEnvironmentCommand = (command2, ctx) => {
1747
+ if (!environmentCmd) {
1748
+ const cloud = defineCloudNamespace(command2, ctx);
1749
+ environmentCmd = cloud.command("environment").description("Manage environments");
1750
+ }
1751
+ return environmentCmd;
1752
+ };
1753
+ const command$1 = ({ command: command2, ctx }) => {
1754
+ const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1755
+ 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));
1756
+ };
1757
+ const listEnvironments = {
1758
+ name: "list-environments",
1759
+ description: "List Strapi Cloud environments",
1760
+ action: action$1,
1761
+ command: command$1
1762
+ };
1763
+ const QUIT_OPTION = "Quit";
1764
+ const action = async (ctx) => {
1765
+ const { getValidToken } = await tokenServiceFactory(ctx);
1766
+ const token = await getValidToken(ctx, promptLogin);
1767
+ const { logger } = ctx;
1768
+ if (!token) {
1769
+ return;
1770
+ }
1771
+ const project = await getLocalProject(ctx);
1772
+ if (!project) {
1773
+ logger.debug(`No valid local project configuration was found.`);
1774
+ return;
1775
+ }
1776
+ const cloudApiService = await cloudApiFactory(ctx, token);
1777
+ const environments = await getEnvironmentsList(ctx, cloudApiService, project);
1778
+ if (!environments) {
1779
+ logger.debug(`Fetching environments failed.`);
1780
+ return;
1781
+ }
1782
+ if (environments.length === 0) {
1783
+ logger.log(
1784
+ `The only available environment is already linked. You can add a new one from your project settings on the Strapi Cloud dashboard.`
1785
+ );
1786
+ return;
1787
+ }
1788
+ const answer = await promptUserForEnvironment(ctx, environments);
1789
+ if (!answer) {
1790
+ return;
1791
+ }
1792
+ await trackEvent(ctx, cloudApiService, "willLinkEnvironment", {
1793
+ projectName: project.name,
1794
+ environmentName: answer.targetEnvironment
1795
+ });
1796
+ try {
1797
+ await patch({ project: { targetEnvironment: answer.targetEnvironment } });
1798
+ } catch (e) {
1799
+ await trackEvent(ctx, cloudApiService, "didNotLinkEnvironment", {
1800
+ projectName: project.name,
1801
+ environmentName: answer.targetEnvironment
1802
+ });
1803
+ logger.debug("Failed to link environment", e);
1804
+ logger.error(
1805
+ "Failed to link the environment. If this issue persists, try re-linking your project or contact support."
1806
+ );
1807
+ process.exit(1);
1808
+ }
1809
+ logger.log(
1810
+ ` 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.`
1811
+ );
1812
+ await trackEvent(ctx, cloudApiService, "didLinkEnvironment", {
1813
+ projectName: project.name,
1814
+ environmentName: answer.targetEnvironment
1815
+ });
1816
+ };
1817
+ async function promptUserForEnvironment(ctx, environments) {
1818
+ const { logger } = ctx;
1819
+ try {
1820
+ const answer = await inquirer__default.default.prompt([
1821
+ {
1822
+ type: "list",
1823
+ name: "targetEnvironment",
1824
+ message: "Which environment do you want to link?",
1825
+ choices: [...environments, { name: chalk__default.default.grey(`(${QUIT_OPTION})`), value: null }]
1826
+ }
1827
+ ]);
1828
+ if (!answer.targetEnvironment) {
1829
+ return null;
1830
+ }
1831
+ return answer;
1832
+ } catch (e) {
1833
+ logger.debug("Failed to get user input", e);
1834
+ logger.error("An error occurred while trying to get your environment selection.");
1835
+ return null;
1836
+ }
1837
+ }
1838
+ async function getEnvironmentsList(ctx, cloudApiService, project) {
1839
+ const spinner = ctx.logger.spinner("Fetching environments...\n").start();
1840
+ try {
1841
+ const {
1842
+ data: { data: environmentsList }
1843
+ } = await cloudApiService.listLinkEnvironments({ name: project.name });
1844
+ if (!Array.isArray(environmentsList) || environmentsList.length === 0) {
1845
+ throw new Error("Environments not found in server response");
1846
+ }
1847
+ spinner.succeed();
1848
+ return environmentsList.filter(
1849
+ (environment) => environment.name !== project.targetEnvironment
1850
+ );
1851
+ } catch (e) {
1852
+ if (e.response && e.response.status === 404) {
1853
+ spinner.succeed();
1854
+ ctx.logger.warn(
1855
+ `
1856
+ The project associated with this folder does not exist in Strapi Cloud.
1857
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1858
+ "link"
1859
+ )} command.`
1860
+ );
1861
+ } else {
1862
+ spinner.fail("An error occurred while fetching environments data from Strapi Cloud.");
1863
+ ctx.logger.debug("Failed to list environments", e);
1864
+ }
1865
+ }
1866
+ }
1867
+ const command = ({ command: command2, ctx }) => {
1868
+ const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1869
+ 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));
1870
+ };
1871
+ const linkEnvironment = {
1872
+ name: "link-environment",
1873
+ description: "Link Strapi Cloud environment to a local project",
1550
1874
  action,
1551
1875
  command
1552
1876
  };
@@ -1556,11 +1880,21 @@ const cli = {
1556
1880
  login,
1557
1881
  logout,
1558
1882
  createProject,
1559
- listProjects
1883
+ linkEnvironment,
1884
+ listProjects,
1885
+ listEnvironments
1560
1886
  };
1561
- const cloudCommands = [deployProject, link, login, logout, listProjects];
1887
+ const cloudCommands = [
1888
+ deployProject,
1889
+ link,
1890
+ login,
1891
+ logout,
1892
+ linkEnvironment,
1893
+ listProjects,
1894
+ listEnvironments
1895
+ ];
1562
1896
  async function initCloudCLIConfig() {
1563
- const localConfig = await getLocalConfig();
1897
+ const localConfig = await getLocalConfig$1();
1564
1898
  if (!localConfig.deviceId) {
1565
1899
  localConfig.deviceId = crypto__default.default.randomUUID();
1566
1900
  }