@strapi/cloud-cli 0.0.0-next.e9b6852d1c05518ff6e37d599321f7aa7aa0683b → 0.0.0-next.ec9b1b708d4d319f2b8b39d9397bd752d250d541

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 (43) hide show
  1. package/dist/bin.js +1 -0
  2. package/dist/bin.js.map +1 -1
  3. package/dist/index.js +388 -74
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +387 -73
  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/create-project/command.d.ts.map +1 -1
  10. package/dist/src/deploy-project/action.d.ts +4 -1
  11. package/dist/src/deploy-project/action.d.ts.map +1 -1
  12. package/dist/src/deploy-project/command.d.ts.map +1 -1
  13. package/dist/src/environment/command.d.ts +3 -0
  14. package/dist/src/environment/command.d.ts.map +1 -0
  15. package/dist/src/environment/link/action.d.ts +4 -0
  16. package/dist/src/environment/link/action.d.ts.map +1 -0
  17. package/dist/src/environment/link/command.d.ts +4 -0
  18. package/dist/src/environment/link/command.d.ts.map +1 -0
  19. package/dist/src/environment/link/index.d.ts +7 -0
  20. package/dist/src/environment/link/index.d.ts.map +1 -0
  21. package/dist/src/environment/list/action.d.ts +4 -0
  22. package/dist/src/environment/list/action.d.ts.map +1 -0
  23. package/dist/src/environment/list/command.d.ts +4 -0
  24. package/dist/src/environment/list/command.d.ts.map +1 -0
  25. package/dist/src/environment/list/index.d.ts +7 -0
  26. package/dist/src/environment/list/index.d.ts.map +1 -0
  27. package/dist/src/index.d.ts +2 -0
  28. package/dist/src/index.d.ts.map +1 -1
  29. package/dist/src/link/action.d.ts.map +1 -1
  30. package/dist/src/login/command.d.ts.map +1 -1
  31. package/dist/src/logout/command.d.ts.map +1 -1
  32. package/dist/src/services/cli-api.d.ts +23 -3
  33. package/dist/src/services/cli-api.d.ts.map +1 -1
  34. package/dist/src/services/strapi-info-save.d.ts +15 -2
  35. package/dist/src/services/strapi-info-save.d.ts.map +1 -1
  36. package/dist/src/types.d.ts +4 -1
  37. package/dist/src/types.d.ts.map +1 -1
  38. package/dist/src/utils/get-local-config.d.ts +6 -0
  39. package/dist/src/utils/get-local-config.d.ts.map +1 -0
  40. package/dist/src/utils/pkg.d.ts.map +1 -1
  41. package/dist/src/utils/tests/compress-files.test.d.ts +2 -0
  42. package/dist/src/utils/tests/compress-files.test.d.ts.map +1 -0
  43. package/package.json +8 -8
package/dist/index.js CHANGED
@@ -24,6 +24,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
24
24
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
25
25
  const crypto$1 = require("crypto");
26
26
  const fse = require("fs-extra");
27
+ const inquirer = require("inquirer");
27
28
  const path = require("path");
28
29
  const chalk = require("chalk");
29
30
  const axios = require("axios");
@@ -31,10 +32,10 @@ const crypto = require("node:crypto");
31
32
  const utils = require("@strapi/utils");
32
33
  const tar = require("tar");
33
34
  const minimatch = require("minimatch");
34
- const inquirer = require("inquirer");
35
35
  const fp = require("lodash/fp");
36
36
  const os = require("os");
37
37
  const XDGAppPaths = require("xdg-app-paths");
38
+ const lodash = require("lodash");
38
39
  const jwksClient = require("jwks-rsa");
39
40
  const jwt = require("jsonwebtoken");
40
41
  const stringify = require("fast-safe-stringify");
@@ -43,6 +44,7 @@ const cliProgress = require("cli-progress");
43
44
  const pkgUp = require("pkg-up");
44
45
  const yup = require("yup");
45
46
  const EventSource = require("eventsource");
47
+ const commander = require("commander");
46
48
  const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
47
49
  function _interopNamespace(e) {
48
50
  if (e && e.__esModule)
@@ -64,12 +66,12 @@ function _interopNamespace(e) {
64
66
  }
65
67
  const crypto__default = /* @__PURE__ */ _interopDefault(crypto$1);
66
68
  const fse__namespace = /* @__PURE__ */ _interopNamespace(fse);
69
+ const inquirer__default = /* @__PURE__ */ _interopDefault(inquirer);
67
70
  const path__namespace = /* @__PURE__ */ _interopNamespace(path);
68
71
  const chalk__default = /* @__PURE__ */ _interopDefault(chalk);
69
72
  const axios__default = /* @__PURE__ */ _interopDefault(axios);
70
73
  const crypto__namespace = /* @__PURE__ */ _interopNamespace(crypto);
71
74
  const tar__namespace = /* @__PURE__ */ _interopNamespace(tar);
72
- const inquirer__default = /* @__PURE__ */ _interopDefault(inquirer);
73
75
  const os__default = /* @__PURE__ */ _interopDefault(os);
74
76
  const XDGAppPaths__default = /* @__PURE__ */ _interopDefault(XDGAppPaths);
75
77
  const jwksClient__default = /* @__PURE__ */ _interopDefault(jwksClient);
@@ -176,7 +178,7 @@ async function getConfigPath() {
176
178
  }
177
179
  return configPath;
178
180
  }
179
- async function getLocalConfig() {
181
+ async function getLocalConfig$1() {
180
182
  const configPath = await getConfigPath();
181
183
  const configFilePath = path__namespace.default.join(configPath, CONFIG_FILENAME);
182
184
  await fse__namespace.default.ensureFile(configFilePath);
@@ -192,7 +194,7 @@ async function saveLocalConfig(data) {
192
194
  await fse__namespace.default.writeJson(configFilePath, data, { encoding: "utf8", spaces: 2, mode: 384 });
193
195
  }
194
196
  const name = "@strapi/cloud-cli";
195
- const version = "4.25.9";
197
+ const version = "5.4.0";
196
198
  const description = "Commands to interact with the Strapi Cloud";
197
199
  const keywords = [
198
200
  "strapi",
@@ -237,14 +239,14 @@ const scripts = {
237
239
  watch: "pack-up watch"
238
240
  };
239
241
  const dependencies = {
240
- "@strapi/utils": "4.25.9",
242
+ "@strapi/utils": "5.4.0",
241
243
  axios: "1.7.4",
242
244
  chalk: "4.1.2",
243
245
  "cli-progress": "3.12.0",
244
246
  commander: "8.3.0",
245
247
  eventsource: "2.0.2",
246
248
  "fast-safe-stringify": "2.1.1",
247
- "fs-extra": "10.0.0",
249
+ "fs-extra": "11.2.0",
248
250
  inquirer: "8.2.5",
249
251
  jsonwebtoken: "9.0.0",
250
252
  "jwks-rsa": "3.1.0",
@@ -253,7 +255,7 @@ const dependencies = {
253
255
  open: "8.4.0",
254
256
  ora: "5.4.1",
255
257
  "pkg-up": "3.1.0",
256
- tar: "6.1.13",
258
+ tar: "6.2.1",
257
259
  "xdg-app-paths": "8.3.0",
258
260
  yup: "0.32.9"
259
261
  };
@@ -262,13 +264,14 @@ const devDependencies = {
262
264
  "@types/cli-progress": "3.11.5",
263
265
  "@types/eventsource": "1.1.15",
264
266
  "@types/lodash": "^4.14.191",
265
- "eslint-config-custom": "4.25.9",
266
- tsconfig: "4.25.9"
267
+ "eslint-config-custom": "5.4.0",
268
+ tsconfig: "5.4.0"
267
269
  };
268
270
  const engines = {
269
- node: ">=18.0.0 <=20.x.x",
271
+ node: ">=18.0.0 <=22.x.x",
270
272
  npm: ">=6.0.0"
271
273
  };
274
+ const gitHead = "7d785703f52464577d077c4618cbe68b44f8a9cd";
272
275
  const packageJson = {
273
276
  name,
274
277
  version,
@@ -289,11 +292,12 @@ const packageJson = {
289
292
  scripts,
290
293
  dependencies,
291
294
  devDependencies,
292
- engines
295
+ engines,
296
+ gitHead
293
297
  };
294
298
  const VERSION = "v1";
295
299
  async function cloudApiFactory({ logger }, token) {
296
- const localConfig = await getLocalConfig();
300
+ const localConfig = await getLocalConfig$1();
297
301
  const customHeaders = {
298
302
  "x-device-id": localConfig.deviceId,
299
303
  "x-app-version": packageJson.version,
@@ -316,7 +320,7 @@ async function cloudApiFactory({ logger }, token) {
316
320
  deploy({ filePath, project }, { onUploadProgress }) {
317
321
  return axiosCloudAPI.post(
318
322
  `/deploy/${project.name}`,
319
- { file: fse__namespace.default.createReadStream(filePath) },
323
+ { file: fse__namespace.default.createReadStream(filePath), targetEnvironment: project.targetEnvironment },
320
324
  {
321
325
  headers: {
322
326
  "Content-Type": "multipart/form-data"
@@ -387,6 +391,34 @@ async function cloudApiFactory({ logger }, token) {
387
391
  throw error;
388
392
  }
389
393
  },
394
+ async listEnvironments({ name: name2 }) {
395
+ try {
396
+ const response = await axiosCloudAPI.get(`/projects/${name2}/environments`);
397
+ if (response.status !== 200) {
398
+ throw new Error("Error fetching cloud environments from the server.");
399
+ }
400
+ return response;
401
+ } catch (error) {
402
+ logger.debug(
403
+ "🥲 Oops! Couldn't retrieve your project's environments from the server. Please try again."
404
+ );
405
+ throw error;
406
+ }
407
+ },
408
+ async listLinkEnvironments({ name: name2 }) {
409
+ try {
410
+ const response = await axiosCloudAPI.get(`/projects/${name2}/environments-linkable`);
411
+ if (response.status !== 200) {
412
+ throw new Error("Error fetching cloud environments from the server.");
413
+ }
414
+ return response;
415
+ } catch (error) {
416
+ logger.debug(
417
+ "🥲 Oops! Couldn't retrieve your project's environments from the server. Please try again."
418
+ );
419
+ throw error;
420
+ }
421
+ },
390
422
  async getProject({ name: name2 }) {
391
423
  try {
392
424
  const response = await axiosCloudAPI.get(`/projects/${name2}`);
@@ -410,26 +442,43 @@ async function cloudApiFactory({ logger }, token) {
410
442
  };
411
443
  }
412
444
  const LOCAL_SAVE_FILENAME = ".strapi-cloud.json";
445
+ const getFilePath = (directoryPath) => path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
413
446
  async function save(data, { directoryPath } = {}) {
414
- const alreadyInFileData = await retrieve({ directoryPath });
415
- const storedData = { ...alreadyInFileData, ...data };
416
- const pathToFile = path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
447
+ const pathToFile = getFilePath(directoryPath);
417
448
  await fse__namespace.default.ensureDir(path__namespace.default.dirname(pathToFile));
418
- await fse__namespace.default.writeJson(pathToFile, storedData, { encoding: "utf8" });
449
+ await fse__namespace.default.writeJson(pathToFile, data, { encoding: "utf8" });
419
450
  }
420
451
  async function retrieve({
421
452
  directoryPath
422
453
  } = {}) {
423
- const pathToFile = path__namespace.default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
454
+ const pathToFile = getFilePath(directoryPath);
424
455
  const pathExists = await fse__namespace.default.pathExists(pathToFile);
425
456
  if (!pathExists) {
426
457
  return {};
427
458
  }
428
459
  return fse__namespace.default.readJSON(pathToFile, { encoding: "utf8" });
429
460
  }
461
+ async function patch(patchData, { directoryPath } = {}) {
462
+ const pathToFile = getFilePath(directoryPath);
463
+ const existingData = await retrieve({ directoryPath });
464
+ if (!existingData) {
465
+ throw new Error("No configuration data found to patch.");
466
+ }
467
+ const newData = lodash.merge(existingData, patchData);
468
+ await fse__namespace.default.writeJson(pathToFile, newData, { encoding: "utf8" });
469
+ }
470
+ async function deleteConfig({ directoryPath } = {}) {
471
+ const pathToFile = getFilePath(directoryPath);
472
+ const pathExists = await fse__namespace.default.pathExists(pathToFile);
473
+ if (pathExists) {
474
+ await fse__namespace.default.remove(pathToFile);
475
+ }
476
+ }
430
477
  const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
431
478
  __proto__: null,
432
479
  LOCAL_SAVE_FILENAME,
480
+ deleteConfig,
481
+ patch,
433
482
  retrieve,
434
483
  save
435
484
  }, Symbol.toStringTag, { value: "Module" }));
@@ -437,7 +486,7 @@ let cliConfig;
437
486
  async function tokenServiceFactory({ logger }) {
438
487
  const cloudApiService = await cloudApiFactory({ logger });
439
488
  async function saveToken(str) {
440
- const appConfig = await getLocalConfig();
489
+ const appConfig = await getLocalConfig$1();
441
490
  if (!appConfig) {
442
491
  logger.error("There was a problem saving your token. Please try again.");
443
492
  return;
@@ -451,7 +500,7 @@ async function tokenServiceFactory({ logger }) {
451
500
  }
452
501
  }
453
502
  async function retrieveToken() {
454
- const appConfig = await getLocalConfig();
503
+ const appConfig = await getLocalConfig$1();
455
504
  if (appConfig.token) {
456
505
  if (await isTokenValid(appConfig.token)) {
457
506
  return appConfig.token;
@@ -513,7 +562,7 @@ async function tokenServiceFactory({ logger }) {
513
562
  }
514
563
  }
515
564
  async function eraseToken() {
516
- const appConfig = await getLocalConfig();
565
+ const appConfig = await getLocalConfig$1();
517
566
  if (!appConfig) {
518
567
  return;
519
568
  }
@@ -674,21 +723,24 @@ yup__namespace.object({
674
723
  name: yup__namespace.string().required(),
675
724
  exports: yup__namespace.lazy(
676
725
  (value) => yup__namespace.object(
677
- typeof value === "object" ? Object.entries(value).reduce((acc, [key, value2]) => {
678
- if (typeof value2 === "object") {
679
- acc[key] = yup__namespace.object({
680
- types: yup__namespace.string().optional(),
681
- source: yup__namespace.string().required(),
682
- module: yup__namespace.string().optional(),
683
- import: yup__namespace.string().required(),
684
- require: yup__namespace.string().required(),
685
- default: yup__namespace.string().required()
686
- }).noUnknown(true);
687
- } else {
688
- acc[key] = yup__namespace.string().matches(/^\.\/.*\.json$/).required();
689
- }
690
- return acc;
691
- }, {}) : void 0
726
+ typeof value === "object" ? Object.entries(value).reduce(
727
+ (acc, [key, value2]) => {
728
+ if (typeof value2 === "object") {
729
+ acc[key] = yup__namespace.object({
730
+ types: yup__namespace.string().optional(),
731
+ source: yup__namespace.string().required(),
732
+ module: yup__namespace.string().optional(),
733
+ import: yup__namespace.string().required(),
734
+ require: yup__namespace.string().required(),
735
+ default: yup__namespace.string().required()
736
+ }).noUnknown(true);
737
+ } else {
738
+ acc[key] = yup__namespace.string().matches(/^\.\/.*\.json$/).required();
739
+ }
740
+ return acc;
741
+ },
742
+ {}
743
+ ) : void 0
692
744
  ).optional()
693
745
  )
694
746
  });
@@ -956,7 +1008,7 @@ async function createProject$1(ctx, cloudApi, projectInput) {
956
1008
  throw e;
957
1009
  }
958
1010
  }
959
- const action$4 = async (ctx) => {
1011
+ const action$6 = async (ctx) => {
960
1012
  const { logger } = ctx;
961
1013
  const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
962
1014
  const token = await getValidToken(ctx, promptLogin);
@@ -1085,6 +1137,32 @@ const buildLogsServiceFactory = ({ logger }) => {
1085
1137
  });
1086
1138
  };
1087
1139
  };
1140
+ const QUIT_OPTION$2 = "Quit";
1141
+ async function promptForEnvironment(environments) {
1142
+ const choices = environments.map((env) => ({ name: env, value: env }));
1143
+ const { selectedEnvironment } = await inquirer__default.default.prompt([
1144
+ {
1145
+ type: "list",
1146
+ name: "selectedEnvironment",
1147
+ message: "Select the environment to deploy:",
1148
+ choices: [...choices, { name: chalk__default.default.grey(`(${QUIT_OPTION$2})`), value: null }]
1149
+ }
1150
+ ]);
1151
+ if (selectedEnvironment === null) {
1152
+ process.exit(1);
1153
+ }
1154
+ const { confirm } = await inquirer__default.default.prompt([
1155
+ {
1156
+ type: "confirm",
1157
+ name: "confirm",
1158
+ message: `Do you want to proceed with deployment to ${chalk__default.default.cyan(selectedEnvironment)}?`
1159
+ }
1160
+ ]);
1161
+ if (!confirm) {
1162
+ process.exit(1);
1163
+ }
1164
+ return selectedEnvironment;
1165
+ }
1088
1166
  async function upload(ctx, project, token, maxProjectFileSize) {
1089
1167
  const cloudApi = await cloudApiFactory(ctx, token);
1090
1168
  try {
@@ -1167,7 +1245,7 @@ async function getProject(ctx) {
1167
1245
  const { project } = await retrieve();
1168
1246
  if (!project) {
1169
1247
  try {
1170
- return await action$4(ctx);
1248
+ return await action$6(ctx);
1171
1249
  } catch (e) {
1172
1250
  ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1173
1251
  ctx.logger.debug(e);
@@ -1188,7 +1266,26 @@ async function getConfig({
1188
1266
  return null;
1189
1267
  }
1190
1268
  }
1191
- const action$3 = async (ctx) => {
1269
+ function validateEnvironment(ctx, environment, environments) {
1270
+ if (!environments.includes(environment)) {
1271
+ ctx.logger.error(`Environment ${environment} does not exist.`);
1272
+ process.exit(1);
1273
+ }
1274
+ }
1275
+ async function getTargetEnvironment(ctx, opts, project, environments) {
1276
+ if (opts.env) {
1277
+ validateEnvironment(ctx, opts.env, environments);
1278
+ return opts.env;
1279
+ }
1280
+ if (project.targetEnvironment) {
1281
+ return project.targetEnvironment;
1282
+ }
1283
+ if (environments.length > 1) {
1284
+ return promptForEnvironment(environments);
1285
+ }
1286
+ return environments[0];
1287
+ }
1288
+ const action$5 = async (ctx, opts) => {
1192
1289
  const { getValidToken } = await tokenServiceFactory(ctx);
1193
1290
  const token = await getValidToken(ctx, promptLogin);
1194
1291
  if (!token) {
@@ -1199,11 +1296,13 @@ const action$3 = async (ctx) => {
1199
1296
  return;
1200
1297
  }
1201
1298
  const cloudApiService = await cloudApiFactory(ctx, token);
1299
+ let environments;
1202
1300
  try {
1203
1301
  const {
1204
1302
  data: { data: projectData, metadata }
1205
1303
  } = await cloudApiService.getProject({ name: project.name });
1206
1304
  const isProjectSuspended = projectData.suspendedAt;
1305
+ environments = projectData.environments;
1207
1306
  if (isProjectSuspended) {
1208
1307
  ctx.logger.log(
1209
1308
  "\n Oops! This project has been suspended. \n\n Please reactivate it from the dashboard to continue deploying: "
@@ -1250,11 +1349,15 @@ Please link your local project to an existing Strapi Cloud project using the ${c
1250
1349
  );
1251
1350
  maxSize = 1e8;
1252
1351
  }
1352
+ project.targetEnvironment = await getTargetEnvironment(ctx, opts, project, environments);
1253
1353
  const buildId = await upload(ctx, project, token, maxSize);
1254
1354
  if (!buildId) {
1255
1355
  return;
1256
1356
  }
1257
1357
  try {
1358
+ ctx.logger.log(
1359
+ `🚀 Deploying project to ${chalk__default.default.cyan(project.targetEnvironment ?? `production`)} environment...`
1360
+ );
1258
1361
  notificationService(`${apiConfig.apiBaseUrl}/notifications`, token, cliConfig2);
1259
1362
  await buildLogsService(`${apiConfig.apiBaseUrl}/v1/logs/${buildId}`, token, cliConfig2);
1260
1363
  ctx.logger.log(
@@ -1299,17 +1402,16 @@ const runAction = (name2, action2) => (...args) => {
1299
1402
  process.exit(1);
1300
1403
  });
1301
1404
  };
1302
- const command$5 = ({ command: command2, ctx }) => {
1303
- command2.command("cloud:deploy").alias("deploy").description("Deploy a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("deploy", action$3)(ctx));
1405
+ const command$7 = ({ ctx }) => {
1406
+ return commander.createCommand("cloud:deploy").alias("deploy").description("Deploy a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").option("-e, --env <name>", "Specify the environment to deploy").action((opts) => runAction("deploy", action$5)(ctx, opts));
1304
1407
  };
1305
1408
  const deployProject = {
1306
1409
  name: "deploy-project",
1307
1410
  description: "Deploy a Strapi Cloud project",
1308
- action: action$3,
1309
- command: command$5
1411
+ action: action$5,
1412
+ command: command$7
1310
1413
  };
1311
- const QUIT_OPTION = "Quit";
1312
- async function getExistingConfig(ctx) {
1414
+ async function getLocalConfig(ctx) {
1313
1415
  try {
1314
1416
  return await retrieve();
1315
1417
  } catch (e) {
@@ -1318,6 +1420,21 @@ async function getExistingConfig(ctx) {
1318
1420
  return null;
1319
1421
  }
1320
1422
  }
1423
+ async function getLocalProject(ctx) {
1424
+ const localConfig = await getLocalConfig(ctx);
1425
+ if (!localConfig || !localConfig.project) {
1426
+ ctx.logger.warn(
1427
+ `
1428
+ We couldn't find a valid local project config.
1429
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1430
+ "link"
1431
+ )} command.`
1432
+ );
1433
+ process.exit(1);
1434
+ }
1435
+ return localConfig.project;
1436
+ }
1437
+ const QUIT_OPTION$1 = "Quit";
1321
1438
  async function promptForRelink(ctx, cloudApiService, existingConfig) {
1322
1439
  if (existingConfig && existingConfig.project) {
1323
1440
  const { shouldRelink } = await inquirer__default.default.prompt([
@@ -1347,7 +1464,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1347
1464
  } = await cloudApiService.listLinkProjects();
1348
1465
  spinner.succeed();
1349
1466
  if (!Array.isArray(projectList)) {
1350
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1467
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1351
1468
  return null;
1352
1469
  }
1353
1470
  const projects = projectList.filter(
@@ -1359,7 +1476,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1359
1476
  };
1360
1477
  });
1361
1478
  if (projects.length === 0) {
1362
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1479
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1363
1480
  return null;
1364
1481
  }
1365
1482
  return projects;
@@ -1377,7 +1494,7 @@ async function getUserSelection(ctx, projects) {
1377
1494
  type: "list",
1378
1495
  name: "linkProject",
1379
1496
  message: "Which project do you want to link?",
1380
- choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION})`), value: null }]
1497
+ choices: [...projects, { name: chalk__default.default.grey(`(${QUIT_OPTION$1})`), value: null }]
1381
1498
  }
1382
1499
  ]);
1383
1500
  if (!answer.linkProject) {
@@ -1390,7 +1507,7 @@ async function getUserSelection(ctx, projects) {
1390
1507
  return null;
1391
1508
  }
1392
1509
  }
1393
- const action$2 = async (ctx) => {
1510
+ const action$4 = async (ctx) => {
1394
1511
  const { getValidToken } = await tokenServiceFactory(ctx);
1395
1512
  const token = await getValidToken(ctx, promptLogin);
1396
1513
  const { logger } = ctx;
@@ -1398,7 +1515,7 @@ const action$2 = async (ctx) => {
1398
1515
  return;
1399
1516
  }
1400
1517
  const cloudApiService = await cloudApiFactory(ctx, token);
1401
- const existingConfig = await getExistingConfig(ctx);
1518
+ const existingConfig = await getLocalConfig(ctx);
1402
1519
  const shouldRelink = await promptForRelink(ctx, cloudApiService, existingConfig);
1403
1520
  if (!shouldRelink) {
1404
1521
  return;
@@ -1433,7 +1550,9 @@ const action$2 = async (ctx) => {
1433
1550
  return;
1434
1551
  }
1435
1552
  await save({ project: answer.linkProject });
1436
- logger.log(`Project ${chalk__default.default.cyan(answer.linkProject.displayName)} linked successfully.`);
1553
+ logger.log(
1554
+ ` You have successfully linked your project to ${chalk__default.default.cyan(answer.linkProject.displayName)}. You are now able to deploy your project.`
1555
+ );
1437
1556
  await trackEvent(ctx, cloudApiService, "didLinkProject", {
1438
1557
  projectInternalName: answer.linkProject
1439
1558
  });
@@ -1445,17 +1564,17 @@ const action$2 = async (ctx) => {
1445
1564
  });
1446
1565
  }
1447
1566
  };
1448
- const command$4 = ({ command: command2, ctx }) => {
1449
- command2.command("cloud:link").alias("link").description("Link a local directory to a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("link", action$2)(ctx));
1567
+ const command$6 = ({ command: command2, ctx }) => {
1568
+ command2.command("cloud:link").alias("link").description("Link a local directory to a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("link", action$4)(ctx));
1450
1569
  };
1451
1570
  const link = {
1452
1571
  name: "link-project",
1453
1572
  description: "Link a local directory to a Strapi Cloud project",
1454
- action: action$2,
1455
- command: command$4
1573
+ action: action$4,
1574
+ command: command$6
1456
1575
  };
1457
- const command$3 = ({ command: command2, ctx }) => {
1458
- command2.command("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1576
+ const command$5 = ({ ctx }) => {
1577
+ return commander.createCommand("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1459
1578
  "after",
1460
1579
  "\nAfter running this command, you will be prompted to enter your authentication information."
1461
1580
  ).option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("login", loginAction)(ctx));
@@ -1464,10 +1583,10 @@ const login = {
1464
1583
  name: "login",
1465
1584
  description: "Strapi Cloud Login",
1466
1585
  action: loginAction,
1467
- command: command$3
1586
+ command: command$5
1468
1587
  };
1469
1588
  const openModule = import("open");
1470
- const action$1 = async (ctx) => {
1589
+ const action$3 = async (ctx) => {
1471
1590
  const { logger } = ctx;
1472
1591
  const { retrieveToken, eraseToken } = await tokenServiceFactory(ctx);
1473
1592
  const token = await retrieveToken();
@@ -1499,25 +1618,25 @@ const action$1 = async (ctx) => {
1499
1618
  }
1500
1619
  await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1501
1620
  };
1502
- const command$2 = ({ command: command2, ctx }) => {
1503
- command2.command("cloud:logout").alias("logout").description("Strapi Cloud Logout").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("logout", action$1)(ctx));
1621
+ const command$4 = ({ ctx }) => {
1622
+ return commander.createCommand("cloud:logout").alias("logout").description("Strapi Cloud Logout").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("logout", action$3)(ctx));
1504
1623
  };
1505
1624
  const logout = {
1506
1625
  name: "logout",
1507
1626
  description: "Strapi Cloud Logout",
1508
- action: action$1,
1509
- command: command$2
1627
+ action: action$3,
1628
+ command: command$4
1510
1629
  };
1511
- const command$1 = ({ command: command2, ctx }) => {
1512
- command2.command("cloud:create-project").description("Create a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("cloud:create-project", action$4)(ctx));
1630
+ const command$3 = ({ ctx }) => {
1631
+ return commander.createCommand("cloud:create-project").description("Create a Strapi Cloud project").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("cloud:create-project", action$6)(ctx));
1513
1632
  };
1514
1633
  const createProject = {
1515
1634
  name: "create-project",
1516
1635
  description: "Create a new project",
1517
- action: action$4,
1518
- command: command$1
1636
+ action: action$6,
1637
+ command: command$3
1519
1638
  };
1520
- const action = async (ctx) => {
1639
+ const action$2 = async (ctx) => {
1521
1640
  const { getValidToken } = await tokenServiceFactory(ctx);
1522
1641
  const token = await getValidToken(ctx, promptLogin);
1523
1642
  const { logger } = ctx;
@@ -1537,12 +1656,194 @@ const action = async (ctx) => {
1537
1656
  spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1538
1657
  }
1539
1658
  };
1540
- const command = ({ command: command2, ctx }) => {
1541
- command2.command("cloud:projects").alias("projects").description("List Strapi Cloud projects").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("projects", action)(ctx));
1659
+ const command$2 = ({ command: command2, ctx }) => {
1660
+ command2.command("cloud:projects").alias("projects").description("List Strapi Cloud projects").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("projects", action$2)(ctx));
1542
1661
  };
1543
1662
  const listProjects = {
1544
1663
  name: "list-projects",
1545
1664
  description: "List Strapi Cloud projects",
1665
+ action: action$2,
1666
+ command: command$2
1667
+ };
1668
+ const action$1 = async (ctx) => {
1669
+ const { getValidToken } = await tokenServiceFactory(ctx);
1670
+ const token = await getValidToken(ctx, promptLogin);
1671
+ const { logger } = ctx;
1672
+ if (!token) {
1673
+ return;
1674
+ }
1675
+ const project = await getLocalProject(ctx);
1676
+ if (!project) {
1677
+ ctx.logger.debug(`No valid local project configuration was found.`);
1678
+ return;
1679
+ }
1680
+ const cloudApiService = await cloudApiFactory(ctx, token);
1681
+ const spinner = logger.spinner("Fetching environments...").start();
1682
+ await trackEvent(ctx, cloudApiService, "willListEnvironment", {
1683
+ projectInternalName: project.name
1684
+ });
1685
+ try {
1686
+ const {
1687
+ data: { data: environmentsList }
1688
+ } = await cloudApiService.listEnvironments({ name: project.name });
1689
+ spinner.succeed();
1690
+ logger.log(environmentsList);
1691
+ await trackEvent(ctx, cloudApiService, "didListEnvironment", {
1692
+ projectInternalName: project.name
1693
+ });
1694
+ } catch (e) {
1695
+ if (e.response && e.response.status === 404) {
1696
+ spinner.succeed();
1697
+ logger.warn(
1698
+ `
1699
+ The project associated with this folder does not exist in Strapi Cloud.
1700
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1701
+ "link"
1702
+ )} command`
1703
+ );
1704
+ } else {
1705
+ spinner.fail("An error occurred while fetching environments data from Strapi Cloud.");
1706
+ logger.debug("Failed to list environments", e);
1707
+ }
1708
+ await trackEvent(ctx, cloudApiService, "didNotListEnvironment", {
1709
+ projectInternalName: project.name
1710
+ });
1711
+ }
1712
+ };
1713
+ function defineCloudNamespace(command2, ctx) {
1714
+ const cloud = command2.command("cloud").description("Manage Strapi Cloud projects");
1715
+ cloud.command("environments").description("Alias for cloud environment list").action(() => runAction("list", action$1)(ctx));
1716
+ return cloud;
1717
+ }
1718
+ let environmentCmd = null;
1719
+ const initializeEnvironmentCommand = (command2, ctx) => {
1720
+ if (!environmentCmd) {
1721
+ const cloud = defineCloudNamespace(command2, ctx);
1722
+ environmentCmd = cloud.command("environment").description("Manage environments");
1723
+ }
1724
+ return environmentCmd;
1725
+ };
1726
+ const command$1 = ({ command: command2, ctx }) => {
1727
+ const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1728
+ environmentCmd2.command("list").description("List Strapi Cloud project environments").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("list", action$1)(ctx));
1729
+ };
1730
+ const listEnvironments = {
1731
+ name: "list-environments",
1732
+ description: "List Strapi Cloud environments",
1733
+ action: action$1,
1734
+ command: command$1
1735
+ };
1736
+ const QUIT_OPTION = "Quit";
1737
+ const action = async (ctx) => {
1738
+ const { getValidToken } = await tokenServiceFactory(ctx);
1739
+ const token = await getValidToken(ctx, promptLogin);
1740
+ const { logger } = ctx;
1741
+ if (!token) {
1742
+ return;
1743
+ }
1744
+ const project = await getLocalProject(ctx);
1745
+ if (!project) {
1746
+ logger.debug(`No valid local project configuration was found.`);
1747
+ return;
1748
+ }
1749
+ const cloudApiService = await cloudApiFactory(ctx, token);
1750
+ const environments = await getEnvironmentsList(ctx, cloudApiService, project);
1751
+ if (!environments) {
1752
+ logger.debug(`Fetching environments failed.`);
1753
+ return;
1754
+ }
1755
+ if (environments.length === 0) {
1756
+ logger.log(
1757
+ `The only available environment is already linked. You can add a new one from your project settings on the Strapi Cloud dashboard.`
1758
+ );
1759
+ return;
1760
+ }
1761
+ const answer = await promptUserForEnvironment(ctx, environments);
1762
+ if (!answer) {
1763
+ return;
1764
+ }
1765
+ await trackEvent(ctx, cloudApiService, "willLinkEnvironment", {
1766
+ projectName: project.name,
1767
+ environmentName: answer.targetEnvironment
1768
+ });
1769
+ try {
1770
+ await patch({ project: { targetEnvironment: answer.targetEnvironment } });
1771
+ } catch (e) {
1772
+ await trackEvent(ctx, cloudApiService, "didNotLinkEnvironment", {
1773
+ projectName: project.name,
1774
+ environmentName: answer.targetEnvironment
1775
+ });
1776
+ logger.debug("Failed to link environment", e);
1777
+ logger.error(
1778
+ "Failed to link the environment. If this issue persists, try re-linking your project or contact support."
1779
+ );
1780
+ process.exit(1);
1781
+ }
1782
+ logger.log(
1783
+ ` You have successfully linked your project to ${chalk__default.default.cyan(answer.targetEnvironment)}, on ${chalk__default.default.cyan(project.displayName)}. You are now able to deploy your project.`
1784
+ );
1785
+ await trackEvent(ctx, cloudApiService, "didLinkEnvironment", {
1786
+ projectName: project.name,
1787
+ environmentName: answer.targetEnvironment
1788
+ });
1789
+ };
1790
+ async function promptUserForEnvironment(ctx, environments) {
1791
+ const { logger } = ctx;
1792
+ try {
1793
+ const answer = await inquirer__default.default.prompt([
1794
+ {
1795
+ type: "list",
1796
+ name: "targetEnvironment",
1797
+ message: "Which environment do you want to link?",
1798
+ choices: [...environments, { name: chalk__default.default.grey(`(${QUIT_OPTION})`), value: null }]
1799
+ }
1800
+ ]);
1801
+ if (!answer.targetEnvironment) {
1802
+ return null;
1803
+ }
1804
+ return answer;
1805
+ } catch (e) {
1806
+ logger.debug("Failed to get user input", e);
1807
+ logger.error("An error occurred while trying to get your environment selection.");
1808
+ return null;
1809
+ }
1810
+ }
1811
+ async function getEnvironmentsList(ctx, cloudApiService, project) {
1812
+ const spinner = ctx.logger.spinner("Fetching environments...\n").start();
1813
+ try {
1814
+ const {
1815
+ data: { data: environmentsList }
1816
+ } = await cloudApiService.listLinkEnvironments({ name: project.name });
1817
+ if (!Array.isArray(environmentsList) || environmentsList.length === 0) {
1818
+ throw new Error("Environments not found in server response");
1819
+ }
1820
+ spinner.succeed();
1821
+ return environmentsList.filter(
1822
+ (environment) => environment.name !== project.targetEnvironment
1823
+ );
1824
+ } catch (e) {
1825
+ if (e.response && e.response.status === 404) {
1826
+ spinner.succeed();
1827
+ ctx.logger.warn(
1828
+ `
1829
+ The project associated with this folder does not exist in Strapi Cloud.
1830
+ Please link your local project to an existing Strapi Cloud project using the ${chalk__default.default.cyan(
1831
+ "link"
1832
+ )} command.`
1833
+ );
1834
+ } else {
1835
+ spinner.fail("An error occurred while fetching environments data from Strapi Cloud.");
1836
+ ctx.logger.debug("Failed to list environments", e);
1837
+ }
1838
+ }
1839
+ }
1840
+ const command = ({ command: command2, ctx }) => {
1841
+ const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1842
+ environmentCmd2.command("link").description("Link project to a specific Strapi Cloud project environment").option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("link", action)(ctx));
1843
+ };
1844
+ const linkEnvironment = {
1845
+ name: "link-environment",
1846
+ description: "Link Strapi Cloud environment to a local project",
1546
1847
  action,
1547
1848
  command
1548
1849
  };
@@ -1552,11 +1853,21 @@ const cli = {
1552
1853
  login,
1553
1854
  logout,
1554
1855
  createProject,
1555
- listProjects
1856
+ linkEnvironment,
1857
+ listProjects,
1858
+ listEnvironments
1556
1859
  };
1557
- const cloudCommands = [deployProject, link, login, logout, listProjects];
1860
+ const cloudCommands = [
1861
+ deployProject,
1862
+ link,
1863
+ login,
1864
+ logout,
1865
+ linkEnvironment,
1866
+ listProjects,
1867
+ listEnvironments
1868
+ ];
1558
1869
  async function initCloudCLIConfig() {
1559
- const localConfig = await getLocalConfig();
1870
+ const localConfig = await getLocalConfig$1();
1560
1871
  if (!localConfig.deviceId) {
1561
1872
  localConfig.deviceId = crypto__default.default.randomUUID();
1562
1873
  }
@@ -1570,7 +1881,10 @@ async function buildStrapiCloudCommands({
1570
1881
  await initCloudCLIConfig();
1571
1882
  for (const cloudCommand of cloudCommands) {
1572
1883
  try {
1573
- await cloudCommand.command({ command: command2, ctx, argv });
1884
+ const subCommand = await cloudCommand.command({ command: command2, ctx, argv });
1885
+ if (subCommand) {
1886
+ command2.addCommand(subCommand);
1887
+ }
1574
1888
  } catch (e) {
1575
1889
  console.error(`Failed to load command ${cloudCommand.name}`, e);
1576
1890
  }