@strapi/cloud-cli 0.0.0-next.6ed779c066310248c506ce3d2cdae97f59f700ef → 0.0.0-next.7315bad3dd6542d457c3c837db874e3e6336ae9f

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 (36) hide show
  1. package/dist/bin.js +2 -2
  2. package/dist/bin.js.map +1 -1
  3. package/dist/index.js +333 -88
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +330 -85
  6. package/dist/index.mjs.map +1 -1
  7. package/dist/src/cloud/command.d.ts +1 -1
  8. package/dist/src/cloud/command.d.ts.map +1 -1
  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 +1 -1
  13. package/dist/src/environment/command.d.ts.map +1 -1
  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.map +1 -1
  21. package/dist/src/environment/list/command.d.ts.map +1 -1
  22. package/dist/src/index.d.ts +1 -0
  23. package/dist/src/index.d.ts.map +1 -1
  24. package/dist/src/link/action.d.ts.map +1 -1
  25. package/dist/src/services/cli-api.d.ts +21 -3
  26. package/dist/src/services/cli-api.d.ts.map +1 -1
  27. package/dist/src/services/strapi-info-save.d.ts +15 -2
  28. package/dist/src/services/strapi-info-save.d.ts.map +1 -1
  29. package/dist/src/services/token.d.ts.map +1 -1
  30. package/dist/src/types.d.ts +3 -0
  31. package/dist/src/types.d.ts.map +1 -1
  32. package/dist/src/utils/get-local-config.d.ts +6 -0
  33. package/dist/src/utils/get-local-config.d.ts.map +1 -0
  34. package/dist/src/utils/helpers.d.ts.map +1 -1
  35. package/dist/src/utils/pkg.d.ts.map +1 -1
  36. package/package.json +7 -6
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.1.0";
197
+ const version = "5.4.2";
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.4.2",
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,17 +261,18 @@ 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.4.2",
269
+ tsconfig: "5.4.2"
268
270
  };
269
271
  const engines = {
270
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"
@@ -402,6 +406,20 @@ async function cloudApiFactory({ logger }, token) {
402
406
  throw error;
403
407
  }
404
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
+ },
405
423
  async getProject({ name: name2 }) {
406
424
  try {
407
425
  const response = await axiosCloudAPI.get(`/projects/${name2}`);
@@ -425,26 +443,43 @@ async function cloudApiFactory({ logger }, token) {
425
443
  };
426
444
  }
427
445
  const LOCAL_SAVE_FILENAME = ".strapi-cloud.json";
446
+ const getFilePath = (directoryPath) => path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
428
447
  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);
448
+ const pathToFile = getFilePath(directoryPath);
432
449
  await fse__namespace.default.ensureDir(path__namespace.default.dirname(pathToFile));
433
- await fse__namespace.default.writeJson(pathToFile, storedData, { encoding: "utf8" });
450
+ await fse__namespace.default.writeJson(pathToFile, data, { encoding: "utf8" });
434
451
  }
435
452
  async function retrieve({
436
453
  directoryPath
437
454
  } = {}) {
438
- const pathToFile = path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
455
+ const pathToFile = getFilePath(directoryPath);
439
456
  const pathExists = await fse__namespace.default.pathExists(pathToFile);
440
457
  if (!pathExists) {
441
458
  return {};
442
459
  }
443
460
  return fse__namespace.default.readJSON(pathToFile, { encoding: "utf8" });
444
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
+ }
445
478
  const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
446
479
  __proto__: null,
447
480
  LOCAL_SAVE_FILENAME,
481
+ deleteConfig,
482
+ patch,
448
483
  retrieve,
449
484
  save
450
485
  }, Symbol.toStringTag, { value: "Module" }));
@@ -452,7 +487,7 @@ let cliConfig;
452
487
  async function tokenServiceFactory({ logger }) {
453
488
  const cloudApiService = await cloudApiFactory({ logger });
454
489
  async function saveToken(str) {
455
- const appConfig = await getLocalConfig();
490
+ const appConfig = await getLocalConfig$1();
456
491
  if (!appConfig) {
457
492
  logger.error("There was a problem saving your token. Please try again.");
458
493
  return;
@@ -466,7 +501,7 @@ async function tokenServiceFactory({ logger }) {
466
501
  }
467
502
  }
468
503
  async function retrieveToken() {
469
- const appConfig = await getLocalConfig();
504
+ const appConfig = await getLocalConfig$1();
470
505
  if (appConfig.token) {
471
506
  if (await isTokenValid(appConfig.token)) {
472
507
  return appConfig.token;
@@ -528,7 +563,7 @@ async function tokenServiceFactory({ logger }) {
528
563
  }
529
564
  }
530
565
  async function eraseToken() {
531
- const appConfig = await getLocalConfig();
566
+ const appConfig = await getLocalConfig$1();
532
567
  if (!appConfig) {
533
568
  return;
534
569
  }
@@ -549,8 +584,7 @@ async function tokenServiceFactory({ logger }) {
549
584
  logger.log(
550
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."
551
586
  );
552
- if (!await loginAction2(ctx))
553
- return null;
587
+ if (!await loginAction2(ctx)) return null;
554
588
  token = await retrieveToken();
555
589
  }
556
590
  return token;
@@ -974,7 +1008,7 @@ async function createProject$1(ctx, cloudApi, projectInput) {
974
1008
  throw e;
975
1009
  }
976
1010
  }
977
- const action$5 = async (ctx) => {
1011
+ const action$6 = async (ctx) => {
978
1012
  const { logger } = ctx;
979
1013
  const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
980
1014
  const token = await getValidToken(ctx, promptLogin);
@@ -1103,6 +1137,29 @@ const buildLogsServiceFactory = ({ logger }) => {
1103
1137
  });
1104
1138
  };
1105
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
+ }
1106
1163
  async function upload(ctx, project, token, maxProjectFileSize) {
1107
1164
  const cloudApi = await cloudApiFactory(ctx, token);
1108
1165
  try {
@@ -1181,11 +1238,11 @@ async function upload(ctx, project, token, maxProjectFileSize) {
1181
1238
  process.exit(1);
1182
1239
  }
1183
1240
  }
1184
- async function getProject$1(ctx) {
1241
+ async function getProject(ctx) {
1185
1242
  const { project } = await retrieve();
1186
1243
  if (!project) {
1187
1244
  try {
1188
- return await action$5(ctx);
1245
+ return await action$6(ctx);
1189
1246
  } catch (e) {
1190
1247
  ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1191
1248
  ctx.logger.debug(e);
@@ -1206,21 +1263,53 @@ async function getConfig({
1206
1263
  return null;
1207
1264
  }
1208
1265
  }
1209
- const action$4 = 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) => {
1210
1293
  const { getValidToken } = await tokenServiceFactory(ctx);
1211
1294
  const token = await getValidToken(ctx, promptLogin);
1212
1295
  if (!token) {
1213
1296
  return;
1214
1297
  }
1215
- const project = await getProject$1(ctx);
1298
+ const project = await getProject(ctx);
1216
1299
  if (!project) {
1217
1300
  return;
1218
1301
  }
1219
1302
  const cloudApiService = await cloudApiFactory(ctx, token);
1303
+ let projectData;
1304
+ let environments;
1305
+ let environmentsDetails;
1220
1306
  try {
1221
1307
  const {
1222
- data: { data: projectData, metadata }
1308
+ data: { data, metadata }
1223
1309
  } = await cloudApiService.getProject({ name: project.name });
1310
+ projectData = data;
1311
+ environments = projectData.environments;
1312
+ environmentsDetails = projectData.environmentsDetails;
1224
1313
  const isProjectSuspended = projectData.suspendedAt;
1225
1314
  if (isProjectSuspended) {
1226
1315
  ctx.logger.log(
@@ -1268,11 +1357,34 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1268
1357
  );
1269
1358
  maxSize = 1e8;
1270
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
+ }
1271
1380
  const buildId = await upload(ctx, project, token, maxSize);
1272
1381
  if (!buildId) {
1273
1382
  return;
1274
1383
  }
1275
1384
  try {
1385
+ ctx.logger.log(
1386
+ `🚀 Deploying project to ${chalk__default.default.cyan(project.targetEnvironment ?? `production`)} environment...`
1387
+ );
1276
1388
  notificationService(`${apiConfig.apiBaseUrl}/notifications`, token, cliConfig2);
1277
1389
  await buildLogsService(`${apiConfig.apiBaseUrl}/v1/logs/${buildId}`, token, cliConfig2);
1278
1390
  ctx.logger.log(
@@ -1317,17 +1429,16 @@ const runAction = (name2, action2) => (...args) => {
1317
1429
  process.exit(1);
1318
1430
  });
1319
1431
  };
1320
- const command$6 = ({ ctx }) => {
1321
- 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$4)(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));
1322
1434
  };
1323
1435
  const deployProject = {
1324
1436
  name: "deploy-project",
1325
1437
  description: "Deploy a Strapi Cloud project",
1326
- action: action$4,
1327
- command: command$6
1438
+ action: action$5,
1439
+ command: command$7
1328
1440
  };
1329
- const QUIT_OPTION = "Quit";
1330
- async function getExistingConfig(ctx) {
1441
+ async function getLocalConfig(ctx) {
1331
1442
  try {
1332
1443
  return await retrieve();
1333
1444
  } catch (e) {
@@ -1336,6 +1447,21 @@ async function getExistingConfig(ctx) {
1336
1447
  return null;
1337
1448
  }
1338
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";
1339
1465
  async function promptForRelink(ctx, cloudApiService, existingConfig) {
1340
1466
  if (existingConfig && existingConfig.project) {
1341
1467
  const { shouldRelink } = await inquirer__default.default.prompt([
@@ -1365,7 +1491,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1365
1491
  } = await cloudApiService.listLinkProjects();
1366
1492
  spinner.succeed();
1367
1493
  if (!Array.isArray(projectList)) {
1368
- 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.");
1369
1495
  return null;
1370
1496
  }
1371
1497
  const projects = projectList.filter(
@@ -1377,7 +1503,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1377
1503
  };
1378
1504
  });
1379
1505
  if (projects.length === 0) {
1380
- 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.");
1381
1507
  return null;
1382
1508
  }
1383
1509
  return projects;
@@ -1395,7 +1521,7 @@ async function getUserSelection(ctx, projects) {
1395
1521
  type: "list",
1396
1522
  name: "linkProject",
1397
1523
  message: "Which project do you want to link?",
1398
- choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION})`), value: null }]
1524
+ choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION$1})`), value: null }]
1399
1525
  }
1400
1526
  ]);
1401
1527
  if (!answer.linkProject) {
@@ -1408,7 +1534,7 @@ async function getUserSelection(ctx, projects) {
1408
1534
  return null;
1409
1535
  }
1410
1536
  }
1411
- const action$3 = async (ctx) => {
1537
+ const action$4 = async (ctx) => {
1412
1538
  const { getValidToken } = await tokenServiceFactory(ctx);
1413
1539
  const token = await getValidToken(ctx, promptLogin);
1414
1540
  const { logger } = ctx;
@@ -1416,7 +1542,7 @@ const action$3 = async (ctx) => {
1416
1542
  return;
1417
1543
  }
1418
1544
  const cloudApiService = await cloudApiFactory(ctx, token);
1419
- const existingConfig = await getExistingConfig(ctx);
1545
+ const existingConfig = await getLocalConfig(ctx);
1420
1546
  const shouldRelink = await promptForRelink(ctx, cloudApiService, existingConfig);
1421
1547
  if (!shouldRelink) {
1422
1548
  return;
@@ -1451,7 +1577,9 @@ const action$3 = async (ctx) => {
1451
1577
  return;
1452
1578
  }
1453
1579
  await save({ project: answer.linkProject });
1454
- 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
+ );
1455
1583
  await trackEvent(ctx, cloudApiService, "didLinkProject", {
1456
1584
  projectInternalName: answer.linkProject
1457
1585
  });
@@ -1463,16 +1591,16 @@ const action$3 = async (ctx) => {
1463
1591
  });
1464
1592
  }
1465
1593
  };
1466
- const command$5 = ({ command: command2, ctx }) => {
1467
- 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));
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));
1468
1596
  };
1469
1597
  const link = {
1470
1598
  name: "link-project",
1471
1599
  description: "Link a local directory to a Strapi Cloud project",
1472
- action: action$3,
1473
- command: command$5
1600
+ action: action$4,
1601
+ command: command$6
1474
1602
  };
1475
- const command$4 = ({ ctx }) => {
1603
+ const command$5 = ({ ctx }) => {
1476
1604
  return commander.createCommand("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1477
1605
  "after",
1478
1606
  "\nAfter running this command, you will be prompted to enter your authentication information."
@@ -1482,10 +1610,10 @@ const login = {
1482
1610
  name: "login",
1483
1611
  description: "Strapi Cloud Login",
1484
1612
  action: loginAction,
1485
- command: command$4
1613
+ command: command$5
1486
1614
  };
1487
1615
  const openModule = import("open");
1488
- const action$2 = async (ctx) => {
1616
+ const action$3 = async (ctx) => {
1489
1617
  const { logger } = ctx;
1490
1618
  const { retrieveToken, eraseToken } = await tokenServiceFactory(ctx);
1491
1619
  const token = await retrieveToken();
@@ -1517,25 +1645,25 @@ const action$2 = async (ctx) => {
1517
1645
  }
1518
1646
  await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1519
1647
  };
1520
- const command$3 = ({ ctx }) => {
1521
- 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));
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));
1522
1650
  };
1523
1651
  const logout = {
1524
1652
  name: "logout",
1525
1653
  description: "Strapi Cloud Logout",
1526
- action: action$2,
1527
- command: command$3
1654
+ action: action$3,
1655
+ command: command$4
1528
1656
  };
1529
- const command$2 = ({ ctx }) => {
1530
- 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));
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));
1531
1659
  };
1532
1660
  const createProject = {
1533
1661
  name: "create-project",
1534
1662
  description: "Create a new project",
1535
- action: action$5,
1536
- command: command$2
1663
+ action: action$6,
1664
+ command: command$3
1537
1665
  };
1538
- const action$1 = async (ctx) => {
1666
+ const action$2 = async (ctx) => {
1539
1667
  const { getValidToken } = await tokenServiceFactory(ctx);
1540
1668
  const token = await getValidToken(ctx, promptLogin);
1541
1669
  const { logger } = ctx;
@@ -1555,37 +1683,23 @@ const action$1 = async (ctx) => {
1555
1683
  spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1556
1684
  }
1557
1685
  };
1558
- const command$1 = ({ command: command2, ctx }) => {
1559
- 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));
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));
1560
1688
  };
1561
1689
  const listProjects = {
1562
1690
  name: "list-projects",
1563
1691
  description: "List Strapi Cloud projects",
1564
- action: action$1,
1565
- command: command$1
1692
+ action: action$2,
1693
+ command: command$2
1566
1694
  };
1567
- async function getProject(ctx) {
1568
- const { project } = await retrieve();
1569
- if (!project) {
1570
- ctx.logger.warn(
1571
- `
1572
- We couldn't find a valid local project config.
1573
- Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1574
- "link"
1575
- )} command`
1576
- );
1577
- process.exit(1);
1578
- }
1579
- return project;
1580
- }
1581
- const action = async (ctx) => {
1695
+ const action$1 = async (ctx) => {
1582
1696
  const { getValidToken } = await tokenServiceFactory(ctx);
1583
1697
  const token = await getValidToken(ctx, promptLogin);
1584
1698
  const { logger } = ctx;
1585
1699
  if (!token) {
1586
1700
  return;
1587
1701
  }
1588
- const project = await getProject(ctx);
1702
+ const project = await getLocalProject(ctx);
1589
1703
  if (!project) {
1590
1704
  ctx.logger.debug(`No valid local project configuration was found.`);
1591
1705
  return;
@@ -1623,18 +1737,140 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1623
1737
  });
1624
1738
  }
1625
1739
  };
1626
- function defineCloudNamespace(command2) {
1627
- return command2.command("cloud").description("Manage Strapi Cloud projects");
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;
1628
1744
  }
1629
- const command = ({ command: command2, ctx }) => {
1630
- const cloud = defineCloudNamespace(command2);
1631
- cloud.command("environments").description("Alias for cloud environment list").action(() => runAction("list", action)(ctx));
1632
- const environment = cloud.command("environment").description("Manage environments for a Strapi Cloud project");
1633
- environment.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));
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));
1634
1756
  };
1635
1757
  const listEnvironments = {
1636
1758
  name: "list-environments",
1637
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",
1638
1874
  action,
1639
1875
  command
1640
1876
  };
@@ -1644,12 +1880,21 @@ const cli = {
1644
1880
  login,
1645
1881
  logout,
1646
1882
  createProject,
1883
+ linkEnvironment,
1647
1884
  listProjects,
1648
1885
  listEnvironments
1649
1886
  };
1650
- const cloudCommands = [deployProject, link, login, logout, listProjects, listEnvironments];
1887
+ const cloudCommands = [
1888
+ deployProject,
1889
+ link,
1890
+ login,
1891
+ logout,
1892
+ linkEnvironment,
1893
+ listProjects,
1894
+ listEnvironments
1895
+ ];
1651
1896
  async function initCloudCLIConfig() {
1652
- const localConfig = await getLocalConfig();
1897
+ const localConfig = await getLocalConfig$1();
1653
1898
  if (!localConfig.deviceId) {
1654
1899
  localConfig.deviceId = crypto__default.default.randomUUID();
1655
1900
  }