@strapi/cloud-cli 0.0.0-next.f4ec69568d980c6fee91ce2ee0f41c138347aa81 → 0.0.0-next.f698d55751345c4ca87477ef683475c1a68f310a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -25,6 +25,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
25
25
  const crypto$1 = require("crypto");
26
26
  const fse = require("fs-extra");
27
27
  const inquirer = require("inquirer");
28
+ const boxen = require("boxen");
28
29
  const path = require("path");
29
30
  const chalk = require("chalk");
30
31
  const axios = require("axios");
@@ -35,6 +36,7 @@ const minimatch = require("minimatch");
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) {
@@ -66,6 +67,7 @@ function _interopNamespace(e) {
66
67
  const crypto__default = /* @__PURE__ */ _interopDefault(crypto$1);
67
68
  const fse__namespace = /* @__PURE__ */ _interopNamespace(fse);
68
69
  const inquirer__default = /* @__PURE__ */ _interopDefault(inquirer);
70
+ const boxen__default = /* @__PURE__ */ _interopDefault(boxen);
69
71
  const path__namespace = /* @__PURE__ */ _interopNamespace(path);
70
72
  const chalk__default = /* @__PURE__ */ _interopDefault(chalk);
71
73
  const axios__default = /* @__PURE__ */ _interopDefault(axios);
@@ -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.3.0";
197
+ const version = "5.5.1";
197
198
  const description = "Commands to interact with the Strapi Cloud";
198
199
  const keywords = [
199
200
  "strapi",
@@ -238,8 +239,9 @@ const scripts = {
238
239
  watch: "pack-up watch"
239
240
  };
240
241
  const dependencies = {
241
- "@strapi/utils": "workspace:*",
242
+ "@strapi/utils": "5.5.1",
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,12 +261,12 @@ 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",
266
- "eslint-config-custom": "workspace:*",
267
- tsconfig: "workspace:*"
268
+ "eslint-config-custom": "5.5.1",
269
+ tsconfig: "5.5.1"
268
270
  };
269
271
  const engines = {
270
272
  node: ">=18.0.0 <=22.x.x",
@@ -294,7 +296,7 @@ const packageJson = {
294
296
  };
295
297
  const VERSION = "v1";
296
298
  async function cloudApiFactory({ logger }, token) {
297
- const localConfig = await getLocalConfig();
299
+ const localConfig = await getLocalConfig$1();
298
300
  const customHeaders = {
299
301
  "x-device-id": localConfig.deviceId,
300
302
  "x-app-version": packageJson.version,
@@ -402,6 +404,20 @@ async function cloudApiFactory({ logger }, token) {
402
404
  throw error;
403
405
  }
404
406
  },
407
+ async listLinkEnvironments({ name: name2 }) {
408
+ try {
409
+ const response = await axiosCloudAPI.get(`/projects/${name2}/environments-linkable`);
410
+ if (response.status !== 200) {
411
+ throw new Error("Error fetching cloud environments from the server.");
412
+ }
413
+ return response;
414
+ } catch (error) {
415
+ logger.debug(
416
+ "🥲 Oops! Couldn't retrieve your project's environments from the server. Please try again."
417
+ );
418
+ throw error;
419
+ }
420
+ },
405
421
  async getProject({ name: name2 }) {
406
422
  try {
407
423
  const response = await axiosCloudAPI.get(`/projects/${name2}`);
@@ -425,26 +441,43 @@ async function cloudApiFactory({ logger }, token) {
425
441
  };
426
442
  }
427
443
  const LOCAL_SAVE_FILENAME = ".strapi-cloud.json";
444
+ const getFilePath = (directoryPath) => path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
428
445
  async function save(data, { directoryPath } = {}) {
429
- const alreadyInFileData = await retrieve({ directoryPath });
430
- const storedData = { ...alreadyInFileData, ...data };
431
- const pathToFile = path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
446
+ const pathToFile = getFilePath(directoryPath);
432
447
  await fse__namespace.default.ensureDir(path__namespace.default.dirname(pathToFile));
433
- await fse__namespace.default.writeJson(pathToFile, storedData, { encoding: "utf8" });
448
+ await fse__namespace.default.writeJson(pathToFile, data, { encoding: "utf8" });
434
449
  }
435
450
  async function retrieve({
436
451
  directoryPath
437
452
  } = {}) {
438
- const pathToFile = path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
453
+ const pathToFile = getFilePath(directoryPath);
439
454
  const pathExists = await fse__namespace.default.pathExists(pathToFile);
440
455
  if (!pathExists) {
441
456
  return {};
442
457
  }
443
458
  return fse__namespace.default.readJSON(pathToFile, { encoding: "utf8" });
444
459
  }
460
+ async function patch(patchData, { directoryPath } = {}) {
461
+ const pathToFile = getFilePath(directoryPath);
462
+ const existingData = await retrieve({ directoryPath });
463
+ if (!existingData) {
464
+ throw new Error("No configuration data found to patch.");
465
+ }
466
+ const newData = lodash.merge(existingData, patchData);
467
+ await fse__namespace.default.writeJson(pathToFile, newData, { encoding: "utf8" });
468
+ }
469
+ async function deleteConfig({ directoryPath } = {}) {
470
+ const pathToFile = getFilePath(directoryPath);
471
+ const pathExists = await fse__namespace.default.pathExists(pathToFile);
472
+ if (pathExists) {
473
+ await fse__namespace.default.remove(pathToFile);
474
+ }
475
+ }
445
476
  const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
446
477
  __proto__: null,
447
478
  LOCAL_SAVE_FILENAME,
479
+ deleteConfig,
480
+ patch,
448
481
  retrieve,
449
482
  save
450
483
  }, Symbol.toStringTag, { value: "Module" }));
@@ -452,7 +485,7 @@ let cliConfig;
452
485
  async function tokenServiceFactory({ logger }) {
453
486
  const cloudApiService = await cloudApiFactory({ logger });
454
487
  async function saveToken(str) {
455
- const appConfig = await getLocalConfig();
488
+ const appConfig = await getLocalConfig$1();
456
489
  if (!appConfig) {
457
490
  logger.error("There was a problem saving your token. Please try again.");
458
491
  return;
@@ -466,7 +499,7 @@ async function tokenServiceFactory({ logger }) {
466
499
  }
467
500
  }
468
501
  async function retrieveToken() {
469
- const appConfig = await getLocalConfig();
502
+ const appConfig = await getLocalConfig$1();
470
503
  if (appConfig.token) {
471
504
  if (await isTokenValid(appConfig.token)) {
472
505
  return appConfig.token;
@@ -528,7 +561,7 @@ async function tokenServiceFactory({ logger }) {
528
561
  }
529
562
  }
530
563
  async function eraseToken() {
531
- const appConfig = await getLocalConfig();
564
+ const appConfig = await getLocalConfig$1();
532
565
  if (!appConfig) {
533
566
  return;
534
567
  }
@@ -549,8 +582,7 @@ async function tokenServiceFactory({ logger }) {
549
582
  logger.log(
550
583
  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."
551
584
  );
552
- if (!await loginAction2(ctx))
553
- return null;
585
+ if (!await loginAction2(ctx)) return null;
554
586
  token = await retrieveToken();
555
587
  }
556
588
  return token;
@@ -974,7 +1006,7 @@ async function createProject$1(ctx, cloudApi, projectInput) {
974
1006
  throw e;
975
1007
  }
976
1008
  }
977
- const action$5 = async (ctx) => {
1009
+ const action$6 = async (ctx) => {
978
1010
  const { logger } = ctx;
979
1011
  const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
980
1012
  const token = await getValidToken(ctx, promptLogin);
@@ -1103,7 +1135,14 @@ const buildLogsServiceFactory = ({ logger }) => {
1103
1135
  });
1104
1136
  };
1105
1137
  };
1106
- const QUIT_OPTION$1 = "Quit";
1138
+ const boxenOptions = {
1139
+ padding: 1,
1140
+ margin: 1,
1141
+ align: "center",
1142
+ borderColor: "yellow",
1143
+ borderStyle: "round"
1144
+ };
1145
+ const QUIT_OPTION$2 = "Quit";
1107
1146
  async function promptForEnvironment(environments) {
1108
1147
  const choices = environments.map((env) => ({ name: env, value: env }));
1109
1148
  const { selectedEnvironment } = await inquirer__default.default.prompt([
@@ -1111,22 +1150,12 @@ async function promptForEnvironment(environments) {
1111
1150
  type: "list",
1112
1151
  name: "selectedEnvironment",
1113
1152
  message: "Select the environment to deploy:",
1114
- choices: [...choices, { name: chalk__default.default.grey(`(${QUIT_OPTION$1})`), value: null }]
1153
+ choices: [...choices, { name: chalk__default.default.grey(`(${QUIT_OPTION$2})`), value: null }]
1115
1154
  }
1116
1155
  ]);
1117
1156
  if (selectedEnvironment === null) {
1118
1157
  process.exit(1);
1119
1158
  }
1120
- const { confirm } = await inquirer__default.default.prompt([
1121
- {
1122
- type: "confirm",
1123
- name: "confirm",
1124
- message: `Do you want to proceed with deployment to ${chalk__default.default.cyan(selectedEnvironment)}?`
1125
- }
1126
- ]);
1127
- if (!confirm) {
1128
- process.exit(1);
1129
- }
1130
1159
  return selectedEnvironment;
1131
1160
  }
1132
1161
  async function upload(ctx, project, token, maxProjectFileSize) {
@@ -1207,11 +1236,11 @@ async function upload(ctx, project, token, maxProjectFileSize) {
1207
1236
  process.exit(1);
1208
1237
  }
1209
1238
  }
1210
- async function getProject$1(ctx) {
1239
+ async function getProject(ctx) {
1211
1240
  const { project } = await retrieve();
1212
1241
  if (!project) {
1213
1242
  try {
1214
- return await action$5(ctx);
1243
+ return await action$6(ctx);
1215
1244
  } catch (e) {
1216
1245
  ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1217
1246
  ctx.logger.debug(e);
@@ -1232,24 +1261,54 @@ async function getConfig({
1232
1261
  return null;
1233
1262
  }
1234
1263
  }
1235
- const action$4 = async (ctx, opts) => {
1264
+ function validateEnvironment(ctx, environment, environments) {
1265
+ if (!environments.includes(environment)) {
1266
+ ctx.logger.error(`Environment ${environment} does not exist.`);
1267
+ process.exit(1);
1268
+ }
1269
+ }
1270
+ async function getTargetEnvironment(ctx, opts, project, environments) {
1271
+ if (opts.env) {
1272
+ validateEnvironment(ctx, opts.env, environments);
1273
+ return opts.env;
1274
+ }
1275
+ if (project.targetEnvironment) {
1276
+ return project.targetEnvironment;
1277
+ }
1278
+ if (environments.length > 1) {
1279
+ return promptForEnvironment(environments);
1280
+ }
1281
+ return environments[0];
1282
+ }
1283
+ function hasPendingOrLiveDeployment(environments, targetEnvironment) {
1284
+ const environment = environments.find((env) => env.name === targetEnvironment);
1285
+ if (!environment) {
1286
+ throw new Error(`Environment details ${targetEnvironment} not found.`);
1287
+ }
1288
+ return environment.hasPendingDeployment || environment.hasLiveDeployment || false;
1289
+ }
1290
+ const action$5 = async (ctx, opts) => {
1236
1291
  const { getValidToken } = await tokenServiceFactory(ctx);
1237
1292
  const token = await getValidToken(ctx, promptLogin);
1238
1293
  if (!token) {
1239
1294
  return;
1240
1295
  }
1241
- const project = await getProject$1(ctx);
1296
+ const project = await getProject(ctx);
1242
1297
  if (!project) {
1243
1298
  return;
1244
1299
  }
1245
1300
  const cloudApiService = await cloudApiFactory(ctx, token);
1301
+ let projectData;
1246
1302
  let environments;
1303
+ let environmentsDetails;
1247
1304
  try {
1248
1305
  const {
1249
- data: { data: projectData, metadata }
1306
+ data: { data, metadata }
1250
1307
  } = await cloudApiService.getProject({ name: project.name });
1251
- const isProjectSuspended = projectData.suspendedAt;
1308
+ projectData = data;
1252
1309
  environments = projectData.environments;
1310
+ environmentsDetails = projectData.environmentsDetails;
1311
+ const isProjectSuspended = projectData.suspendedAt;
1253
1312
  if (isProjectSuspended) {
1254
1313
  ctx.logger.log(
1255
1314
  "\n Oops! This project has been suspended. \n\n Please reactivate it from the dashboard to continue deploying: "
@@ -1296,22 +1355,34 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1296
1355
  );
1297
1356
  maxSize = 1e8;
1298
1357
  }
1299
- let targetEnvironment;
1300
- if (opts.environment) {
1301
- if (!environments.includes(opts.environment)) {
1302
- ctx.logger.error(`Environment ${opts.environment} does not exist.`);
1303
- return;
1358
+ project.targetEnvironment = await getTargetEnvironment(ctx, opts, project, environments);
1359
+ if (!opts.force) {
1360
+ const shouldDisplayWarning = hasPendingOrLiveDeployment(
1361
+ environmentsDetails,
1362
+ project.targetEnvironment
1363
+ );
1364
+ if (shouldDisplayWarning) {
1365
+ ctx.logger.log(boxen__default.default(cliConfig2.projectDeployment.confirmationText, boxenOptions));
1366
+ const { confirm } = await inquirer__default.default.prompt([
1367
+ {
1368
+ type: "confirm",
1369
+ name: "confirm",
1370
+ message: `Do you want to proceed with deployment to ${chalk__default.default.cyan(projectData.displayName)} on ${chalk__default.default.cyan(project.targetEnvironment)} environment?`
1371
+ }
1372
+ ]);
1373
+ if (!confirm) {
1374
+ process.exit(1);
1375
+ }
1304
1376
  }
1305
- targetEnvironment = opts.environment;
1306
- } else {
1307
- targetEnvironment = environments.length > 1 ? await promptForEnvironment(environments) : environments[0];
1308
1377
  }
1309
- project.targetEnvironment = targetEnvironment;
1310
1378
  const buildId = await upload(ctx, project, token, maxSize);
1311
1379
  if (!buildId) {
1312
1380
  return;
1313
1381
  }
1314
1382
  try {
1383
+ ctx.logger.log(
1384
+ `🚀 Deploying project to ${chalk__default.default.cyan(project.targetEnvironment ?? `production`)} environment...`
1385
+ );
1315
1386
  notificationService(`${apiConfig.apiBaseUrl}/notifications`, token, cliConfig2);
1316
1387
  await buildLogsService(`${apiConfig.apiBaseUrl}/v1/logs/${buildId}`, token, cliConfig2);
1317
1388
  ctx.logger.log(
@@ -1356,17 +1427,16 @@ const runAction = (name2, action2) => (...args) => {
1356
1427
  process.exit(1);
1357
1428
  });
1358
1429
  };
1359
- const command$6 = ({ ctx }) => {
1360
- return commander.createCommand("cloud:deploy").alias("deploy").description("Deploy a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").option("-e, --environment <name>", "Specify the environment to deploy").action((opts) => runAction("deploy", action$4)(ctx, opts));
1430
+ const command$7 = ({ ctx }) => {
1431
+ 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));
1361
1432
  };
1362
1433
  const deployProject = {
1363
1434
  name: "deploy-project",
1364
1435
  description: "Deploy a Strapi Cloud project",
1365
- action: action$4,
1366
- command: command$6
1436
+ action: action$5,
1437
+ command: command$7
1367
1438
  };
1368
- const QUIT_OPTION = "Quit";
1369
- async function getExistingConfig(ctx) {
1439
+ async function getLocalConfig(ctx) {
1370
1440
  try {
1371
1441
  return await retrieve();
1372
1442
  } catch (e) {
@@ -1375,6 +1445,21 @@ async function getExistingConfig(ctx) {
1375
1445
  return null;
1376
1446
  }
1377
1447
  }
1448
+ async function getLocalProject(ctx) {
1449
+ const localConfig = await getLocalConfig(ctx);
1450
+ if (!localConfig || !localConfig.project) {
1451
+ ctx.logger.warn(
1452
+ `
1453
+ We couldn't find a valid local project config.
1454
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1455
+ "link"
1456
+ )} command.`
1457
+ );
1458
+ process.exit(1);
1459
+ }
1460
+ return localConfig.project;
1461
+ }
1462
+ const QUIT_OPTION$1 = "Quit";
1378
1463
  async function promptForRelink(ctx, cloudApiService, existingConfig) {
1379
1464
  if (existingConfig && existingConfig.project) {
1380
1465
  const { shouldRelink } = await inquirer__default.default.prompt([
@@ -1404,7 +1489,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1404
1489
  } = await cloudApiService.listLinkProjects();
1405
1490
  spinner.succeed();
1406
1491
  if (!Array.isArray(projectList)) {
1407
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1492
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1408
1493
  return null;
1409
1494
  }
1410
1495
  const projects = projectList.filter(
@@ -1416,7 +1501,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1416
1501
  };
1417
1502
  });
1418
1503
  if (projects.length === 0) {
1419
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1504
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1420
1505
  return null;
1421
1506
  }
1422
1507
  return projects;
@@ -1434,7 +1519,7 @@ async function getUserSelection(ctx, projects) {
1434
1519
  type: "list",
1435
1520
  name: "linkProject",
1436
1521
  message: "Which project do you want to link?",
1437
- choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION})`), value: null }]
1522
+ choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION$1})`), value: null }]
1438
1523
  }
1439
1524
  ]);
1440
1525
  if (!answer.linkProject) {
@@ -1447,7 +1532,7 @@ async function getUserSelection(ctx, projects) {
1447
1532
  return null;
1448
1533
  }
1449
1534
  }
1450
- const action$3 = async (ctx) => {
1535
+ const action$4 = async (ctx) => {
1451
1536
  const { getValidToken } = await tokenServiceFactory(ctx);
1452
1537
  const token = await getValidToken(ctx, promptLogin);
1453
1538
  const { logger } = ctx;
@@ -1455,7 +1540,7 @@ const action$3 = async (ctx) => {
1455
1540
  return;
1456
1541
  }
1457
1542
  const cloudApiService = await cloudApiFactory(ctx, token);
1458
- const existingConfig = await getExistingConfig(ctx);
1543
+ const existingConfig = await getLocalConfig(ctx);
1459
1544
  const shouldRelink = await promptForRelink(ctx, cloudApiService, existingConfig);
1460
1545
  if (!shouldRelink) {
1461
1546
  return;
@@ -1490,7 +1575,9 @@ const action$3 = async (ctx) => {
1490
1575
  return;
1491
1576
  }
1492
1577
  await save({ project: answer.linkProject });
1493
- logger.log(`Project ${chalk__default.default.cyan(answer.linkProject.displayName)} linked successfully.`);
1578
+ logger.log(
1579
+ ` You have successfully linked your project to ${chalk__default.default.cyan(answer.linkProject.displayName)}. You are now able to deploy your project.`
1580
+ );
1494
1581
  await trackEvent(ctx, cloudApiService, "didLinkProject", {
1495
1582
  projectInternalName: answer.linkProject
1496
1583
  });
@@ -1502,16 +1589,16 @@ const action$3 = async (ctx) => {
1502
1589
  });
1503
1590
  }
1504
1591
  };
1505
- const command$5 = ({ command: command2, ctx }) => {
1506
- command2.command("cloud:link").alias("link").description("Link a local directory to a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("link", action$3)(ctx));
1592
+ const command$6 = ({ command: command2, ctx }) => {
1593
+ command2.command("cloud:link").alias("link").description("Link a local directory to a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("link", action$4)(ctx));
1507
1594
  };
1508
1595
  const link = {
1509
1596
  name: "link-project",
1510
1597
  description: "Link a local directory to a Strapi Cloud project",
1511
- action: action$3,
1512
- command: command$5
1598
+ action: action$4,
1599
+ command: command$6
1513
1600
  };
1514
- const command$4 = ({ ctx }) => {
1601
+ const command$5 = ({ ctx }) => {
1515
1602
  return commander.createCommand("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1516
1603
  "after",
1517
1604
  "\nAfter running this command, you will be prompted to enter your authentication information."
@@ -1521,10 +1608,10 @@ const login = {
1521
1608
  name: "login",
1522
1609
  description: "Strapi Cloud Login",
1523
1610
  action: loginAction,
1524
- command: command$4
1611
+ command: command$5
1525
1612
  };
1526
1613
  const openModule = import("open");
1527
- const action$2 = async (ctx) => {
1614
+ const action$3 = async (ctx) => {
1528
1615
  const { logger } = ctx;
1529
1616
  const { retrieveToken, eraseToken } = await tokenServiceFactory(ctx);
1530
1617
  const token = await retrieveToken();
@@ -1556,25 +1643,25 @@ const action$2 = async (ctx) => {
1556
1643
  }
1557
1644
  await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1558
1645
  };
1559
- const command$3 = ({ ctx }) => {
1560
- return commander.createCommand("cloud:logout").alias("logout").description("Strapi Cloud Logout").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("logout", action$2)(ctx));
1646
+ const command$4 = ({ ctx }) => {
1647
+ return commander.createCommand("cloud:logout").alias("logout").description("Strapi Cloud Logout").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("logout", action$3)(ctx));
1561
1648
  };
1562
1649
  const logout = {
1563
1650
  name: "logout",
1564
1651
  description: "Strapi Cloud Logout",
1565
- action: action$2,
1566
- command: command$3
1652
+ action: action$3,
1653
+ command: command$4
1567
1654
  };
1568
- const command$2 = ({ ctx }) => {
1569
- return commander.createCommand("cloud:create-project").description("Create a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("cloud:create-project", action$5)(ctx));
1655
+ const command$3 = ({ ctx }) => {
1656
+ return commander.createCommand("cloud:create-project").description("Create a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("cloud:create-project", action$6)(ctx));
1570
1657
  };
1571
1658
  const createProject = {
1572
1659
  name: "create-project",
1573
1660
  description: "Create a new project",
1574
- action: action$5,
1575
- command: command$2
1661
+ action: action$6,
1662
+ command: command$3
1576
1663
  };
1577
- const action$1 = async (ctx) => {
1664
+ const action$2 = async (ctx) => {
1578
1665
  const { getValidToken } = await tokenServiceFactory(ctx);
1579
1666
  const token = await getValidToken(ctx, promptLogin);
1580
1667
  const { logger } = ctx;
@@ -1594,37 +1681,23 @@ const action$1 = async (ctx) => {
1594
1681
  spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1595
1682
  }
1596
1683
  };
1597
- const command$1 = ({ command: command2, ctx }) => {
1598
- command2.command("cloud:projects").alias("projects").description("List Strapi Cloud projects").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("projects", action$1)(ctx));
1684
+ const command$2 = ({ command: command2, ctx }) => {
1685
+ command2.command("cloud:projects").alias("projects").description("List Strapi Cloud projects").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("projects", action$2)(ctx));
1599
1686
  };
1600
1687
  const listProjects = {
1601
1688
  name: "list-projects",
1602
1689
  description: "List Strapi Cloud projects",
1603
- action: action$1,
1604
- command: command$1
1690
+ action: action$2,
1691
+ command: command$2
1605
1692
  };
1606
- async function getProject(ctx) {
1607
- const { project } = await retrieve();
1608
- if (!project) {
1609
- ctx.logger.warn(
1610
- `
1611
- We couldn't find a valid local project config.
1612
- Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1613
- "link"
1614
- )} command`
1615
- );
1616
- process.exit(1);
1617
- }
1618
- return project;
1619
- }
1620
- const action = async (ctx) => {
1693
+ const action$1 = async (ctx) => {
1621
1694
  const { getValidToken } = await tokenServiceFactory(ctx);
1622
1695
  const token = await getValidToken(ctx, promptLogin);
1623
1696
  const { logger } = ctx;
1624
1697
  if (!token) {
1625
1698
  return;
1626
1699
  }
1627
- const project = await getProject(ctx);
1700
+ const project = await getLocalProject(ctx);
1628
1701
  if (!project) {
1629
1702
  ctx.logger.debug(`No valid local project configuration was found.`);
1630
1703
  return;
@@ -1664,7 +1737,7 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1664
1737
  };
1665
1738
  function defineCloudNamespace(command2, ctx) {
1666
1739
  const cloud = command2.command("cloud").description("Manage Strapi Cloud projects");
1667
- cloud.command("environments").description("Alias for cloud environment list").action(() => runAction("list", action)(ctx));
1740
+ cloud.command("environments").description("Alias for cloud environment list").action(() => runAction("list", action$1)(ctx));
1668
1741
  return cloud;
1669
1742
  }
1670
1743
  let environmentCmd = null;
@@ -1675,13 +1748,127 @@ const initializeEnvironmentCommand = (command2, ctx) => {
1675
1748
  }
1676
1749
  return environmentCmd;
1677
1750
  };
1678
- const command = ({ command: command2, ctx }) => {
1751
+ const command$1 = ({ command: command2, ctx }) => {
1679
1752
  const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1680
- environmentCmd2.command("list").description("List Strapi Cloud project environments").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("list", action)(ctx));
1753
+ environmentCmd2.command("list").description("List Strapi Cloud project environments").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("list", action$1)(ctx));
1681
1754
  };
1682
1755
  const listEnvironments = {
1683
1756
  name: "list-environments",
1684
1757
  description: "List Strapi Cloud environments",
1758
+ action: action$1,
1759
+ command: command$1
1760
+ };
1761
+ const QUIT_OPTION = "Quit";
1762
+ const action = async (ctx) => {
1763
+ const { getValidToken } = await tokenServiceFactory(ctx);
1764
+ const token = await getValidToken(ctx, promptLogin);
1765
+ const { logger } = ctx;
1766
+ if (!token) {
1767
+ return;
1768
+ }
1769
+ const project = await getLocalProject(ctx);
1770
+ if (!project) {
1771
+ logger.debug(`No valid local project configuration was found.`);
1772
+ return;
1773
+ }
1774
+ const cloudApiService = await cloudApiFactory(ctx, token);
1775
+ const environments = await getEnvironmentsList(ctx, cloudApiService, project);
1776
+ if (!environments) {
1777
+ logger.debug(`Fetching environments failed.`);
1778
+ return;
1779
+ }
1780
+ if (environments.length === 0) {
1781
+ logger.log(
1782
+ `The only available environment is already linked. You can add a new one from your project settings on the Strapi Cloud dashboard.`
1783
+ );
1784
+ return;
1785
+ }
1786
+ const answer = await promptUserForEnvironment(ctx, environments);
1787
+ if (!answer) {
1788
+ return;
1789
+ }
1790
+ await trackEvent(ctx, cloudApiService, "willLinkEnvironment", {
1791
+ projectName: project.name,
1792
+ environmentName: answer.targetEnvironment
1793
+ });
1794
+ try {
1795
+ await patch({ project: { targetEnvironment: answer.targetEnvironment } });
1796
+ } catch (e) {
1797
+ await trackEvent(ctx, cloudApiService, "didNotLinkEnvironment", {
1798
+ projectName: project.name,
1799
+ environmentName: answer.targetEnvironment
1800
+ });
1801
+ logger.debug("Failed to link environment", e);
1802
+ logger.error(
1803
+ "Failed to link the environment. If this issue persists, try re-linking your project or contact support."
1804
+ );
1805
+ process.exit(1);
1806
+ }
1807
+ logger.log(
1808
+ ` 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.`
1809
+ );
1810
+ await trackEvent(ctx, cloudApiService, "didLinkEnvironment", {
1811
+ projectName: project.name,
1812
+ environmentName: answer.targetEnvironment
1813
+ });
1814
+ };
1815
+ async function promptUserForEnvironment(ctx, environments) {
1816
+ const { logger } = ctx;
1817
+ try {
1818
+ const answer = await inquirer__default.default.prompt([
1819
+ {
1820
+ type: "list",
1821
+ name: "targetEnvironment",
1822
+ message: "Which environment do you want to link?",
1823
+ choices: [...environments, { name: chalk__default.default.grey(`(${QUIT_OPTION})`), value: null }]
1824
+ }
1825
+ ]);
1826
+ if (!answer.targetEnvironment) {
1827
+ return null;
1828
+ }
1829
+ return answer;
1830
+ } catch (e) {
1831
+ logger.debug("Failed to get user input", e);
1832
+ logger.error("An error occurred while trying to get your environment selection.");
1833
+ return null;
1834
+ }
1835
+ }
1836
+ async function getEnvironmentsList(ctx, cloudApiService, project) {
1837
+ const spinner = ctx.logger.spinner("Fetching environments...\n").start();
1838
+ try {
1839
+ const {
1840
+ data: { data: environmentsList }
1841
+ } = await cloudApiService.listLinkEnvironments({ name: project.name });
1842
+ if (!Array.isArray(environmentsList) || environmentsList.length === 0) {
1843
+ throw new Error("Environments not found in server response");
1844
+ }
1845
+ spinner.succeed();
1846
+ return environmentsList.filter(
1847
+ (environment) => environment.name !== project.targetEnvironment
1848
+ );
1849
+ } catch (e) {
1850
+ if (e.response && e.response.status === 404) {
1851
+ spinner.succeed();
1852
+ ctx.logger.warn(
1853
+ `
1854
+ The project associated with this folder does not exist in Strapi Cloud.
1855
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1856
+ "link"
1857
+ )} command.`
1858
+ );
1859
+ } else {
1860
+ spinner.fail("An error occurred while fetching environments data from Strapi Cloud.");
1861
+ ctx.logger.debug("Failed to list environments", e);
1862
+ }
1863
+ }
1864
+ }
1865
+ const command = ({ command: command2, ctx }) => {
1866
+ const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1867
+ 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));
1868
+ };
1869
+ const linkEnvironment = {
1870
+ name: "link-environment",
1871
+ description: "Link Strapi Cloud environment to a local project",
1685
1872
  action,
1686
1873
  command
1687
1874
  };
@@ -1691,12 +1878,21 @@ const cli = {
1691
1878
  login,
1692
1879
  logout,
1693
1880
  createProject,
1881
+ linkEnvironment,
1694
1882
  listProjects,
1695
1883
  listEnvironments
1696
1884
  };
1697
- const cloudCommands = [deployProject, link, login, logout, listProjects, listEnvironments];
1885
+ const cloudCommands = [
1886
+ deployProject,
1887
+ link,
1888
+ login,
1889
+ logout,
1890
+ linkEnvironment,
1891
+ listProjects,
1892
+ listEnvironments
1893
+ ];
1698
1894
  async function initCloudCLIConfig() {
1699
- const localConfig = await getLocalConfig();
1895
+ const localConfig = await getLocalConfig$1();
1700
1896
  if (!localConfig.deviceId) {
1701
1897
  localConfig.deviceId = crypto__default.default.randomUUID();
1702
1898
  }