@strapi/cloud-cli 0.0.0-next.506c866896b405bfee0715be82ad4b0f8da1d26b → 0.0.0-next.51dcef446efe662f00da6d85d6bea2218c4c538e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/dist/bin.js +1 -0
  2. package/dist/bin.js.map +1 -1
  3. package/dist/index.js +488 -108
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +487 -106
  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/action.d.ts.map +1 -1
  10. package/dist/src/create-project/command.d.ts.map +1 -1
  11. package/dist/src/create-project/utils/project-questions.utils.d.ts +20 -0
  12. package/dist/src/create-project/utils/project-questions.utils.d.ts.map +1 -0
  13. package/dist/src/deploy-project/action.d.ts +4 -1
  14. package/dist/src/deploy-project/action.d.ts.map +1 -1
  15. package/dist/src/deploy-project/command.d.ts.map +1 -1
  16. package/dist/src/environment/command.d.ts +3 -0
  17. package/dist/src/environment/command.d.ts.map +1 -0
  18. package/dist/src/environment/link/action.d.ts +4 -0
  19. package/dist/src/environment/link/action.d.ts.map +1 -0
  20. package/dist/src/environment/link/command.d.ts +4 -0
  21. package/dist/src/environment/link/command.d.ts.map +1 -0
  22. package/dist/src/environment/link/index.d.ts +7 -0
  23. package/dist/src/environment/link/index.d.ts.map +1 -0
  24. package/dist/src/environment/list/action.d.ts +4 -0
  25. package/dist/src/environment/list/action.d.ts.map +1 -0
  26. package/dist/src/environment/list/command.d.ts +4 -0
  27. package/dist/src/environment/list/command.d.ts.map +1 -0
  28. package/dist/src/environment/list/index.d.ts +7 -0
  29. package/dist/src/environment/list/index.d.ts.map +1 -0
  30. package/dist/src/index.d.ts +2 -0
  31. package/dist/src/index.d.ts.map +1 -1
  32. package/dist/src/link/action.d.ts.map +1 -1
  33. package/dist/src/login/command.d.ts.map +1 -1
  34. package/dist/src/logout/command.d.ts.map +1 -1
  35. package/dist/src/services/cli-api.d.ts +39 -3
  36. package/dist/src/services/cli-api.d.ts.map +1 -1
  37. package/dist/src/services/strapi-info-save.d.ts +15 -2
  38. package/dist/src/services/strapi-info-save.d.ts.map +1 -1
  39. package/dist/src/types.d.ts +4 -1
  40. package/dist/src/types.d.ts.map +1 -1
  41. package/dist/src/utils/get-local-config.d.ts +6 -0
  42. package/dist/src/utils/get-local-config.d.ts.map +1 -0
  43. package/dist/src/utils/pkg.d.ts.map +1 -1
  44. package/dist/src/utils/tests/compress-files.test.d.ts +2 -0
  45. package/dist/src/utils/tests/compress-files.test.d.ts.map +1 -0
  46. package/package.json +9 -9
  47. package/dist/src/create-project/utils/apply-default-name.d.ts +0 -7
  48. package/dist/src/create-project/utils/apply-default-name.d.ts.map +0 -1
package/dist/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import crypto$1 from "crypto";
2
2
  import * as fse from "fs-extra";
3
3
  import fse__default from "fs-extra";
4
+ import inquirer from "inquirer";
4
5
  import * as path from "path";
5
6
  import path__default from "path";
6
7
  import chalk from "chalk";
@@ -9,10 +10,10 @@ import * as crypto from "node:crypto";
9
10
  import { env } from "@strapi/utils";
10
11
  import * as tar from "tar";
11
12
  import { minimatch } from "minimatch";
12
- import inquirer from "inquirer";
13
13
  import { defaults, has } from "lodash/fp";
14
14
  import os from "os";
15
15
  import XDGAppPaths from "xdg-app-paths";
16
+ import { merge } from "lodash";
16
17
  import jwksClient from "jwks-rsa";
17
18
  import jwt from "jsonwebtoken";
18
19
  import stringify from "fast-safe-stringify";
@@ -20,8 +21,8 @@ import ora from "ora";
20
21
  import * as cliProgress from "cli-progress";
21
22
  import pkgUp from "pkg-up";
22
23
  import * as yup from "yup";
23
- import _ from "lodash";
24
24
  import EventSource from "eventsource";
25
+ import { createCommand } from "commander";
25
26
  const apiConfig = {
26
27
  apiBaseUrl: env("STRAPI_CLI_CLOUD_API", "https://cloud-cli-api.strapi.io"),
27
28
  dashboardBaseUrl: env("STRAPI_CLI_CLOUD_DASHBOARD", "https://cloud.strapi.io")
@@ -118,7 +119,7 @@ async function getConfigPath() {
118
119
  }
119
120
  return configPath;
120
121
  }
121
- async function getLocalConfig() {
122
+ async function getLocalConfig$1() {
122
123
  const configPath = await getConfigPath();
123
124
  const configFilePath = path__default.join(configPath, CONFIG_FILENAME);
124
125
  await fse__default.ensureFile(configFilePath);
@@ -134,7 +135,7 @@ async function saveLocalConfig(data) {
134
135
  await fse__default.writeJson(configFilePath, data, { encoding: "utf8", spaces: 2, mode: 384 });
135
136
  }
136
137
  const name = "@strapi/cloud-cli";
137
- const version = "4.25.5";
138
+ const version = "5.4.0";
138
139
  const description = "Commands to interact with the Strapi Cloud";
139
140
  const keywords = [
140
141
  "strapi",
@@ -179,14 +180,14 @@ const scripts = {
179
180
  watch: "pack-up watch"
180
181
  };
181
182
  const dependencies = {
182
- "@strapi/utils": "4.25.5",
183
- axios: "1.6.0",
183
+ "@strapi/utils": "5.4.0",
184
+ axios: "1.7.4",
184
185
  chalk: "4.1.2",
185
186
  "cli-progress": "3.12.0",
186
187
  commander: "8.3.0",
187
188
  eventsource: "2.0.2",
188
189
  "fast-safe-stringify": "2.1.1",
189
- "fs-extra": "10.0.0",
190
+ "fs-extra": "11.2.0",
190
191
  inquirer: "8.2.5",
191
192
  jsonwebtoken: "9.0.0",
192
193
  "jwks-rsa": "3.1.0",
@@ -195,7 +196,7 @@ const dependencies = {
195
196
  open: "8.4.0",
196
197
  ora: "5.4.1",
197
198
  "pkg-up": "3.1.0",
198
- tar: "6.1.13",
199
+ tar: "6.2.1",
199
200
  "xdg-app-paths": "8.3.0",
200
201
  yup: "0.32.9"
201
202
  };
@@ -204,13 +205,14 @@ const devDependencies = {
204
205
  "@types/cli-progress": "3.11.5",
205
206
  "@types/eventsource": "1.1.15",
206
207
  "@types/lodash": "^4.14.191",
207
- "eslint-config-custom": "4.25.5",
208
- tsconfig: "4.25.5"
208
+ "eslint-config-custom": "5.4.0",
209
+ tsconfig: "5.4.0"
209
210
  };
210
211
  const engines = {
211
- node: ">=18.0.0 <=20.x.x",
212
+ node: ">=18.0.0 <=22.x.x",
212
213
  npm: ">=6.0.0"
213
214
  };
215
+ const gitHead = "7d785703f52464577d077c4618cbe68b44f8a9cd";
214
216
  const packageJson = {
215
217
  name,
216
218
  version,
@@ -231,11 +233,12 @@ const packageJson = {
231
233
  scripts,
232
234
  dependencies,
233
235
  devDependencies,
234
- engines
236
+ engines,
237
+ gitHead
235
238
  };
236
239
  const VERSION = "v1";
237
240
  async function cloudApiFactory({ logger }, token) {
238
- const localConfig = await getLocalConfig();
241
+ const localConfig = await getLocalConfig$1();
239
242
  const customHeaders = {
240
243
  "x-device-id": localConfig.deviceId,
241
244
  "x-app-version": packageJson.version,
@@ -258,7 +261,7 @@ async function cloudApiFactory({ logger }, token) {
258
261
  deploy({ filePath, project }, { onUploadProgress }) {
259
262
  return axiosCloudAPI.post(
260
263
  `/deploy/${project.name}`,
261
- { file: fse__default.createReadStream(filePath) },
264
+ { file: fse__default.createReadStream(filePath), targetEnvironment: project.targetEnvironment },
262
265
  {
263
266
  headers: {
264
267
  "Content-Type": "multipart/form-data"
@@ -317,7 +320,7 @@ async function cloudApiFactory({ logger }, token) {
317
320
  },
318
321
  async listLinkProjects() {
319
322
  try {
320
- const response = await axiosCloudAPI.get("/projects/linkable");
323
+ const response = await axiosCloudAPI.get("/projects-linkable");
321
324
  if (response.status !== 200) {
322
325
  throw new Error("Error fetching cloud projects from the server.");
323
326
  }
@@ -329,6 +332,48 @@ async function cloudApiFactory({ logger }, token) {
329
332
  throw error;
330
333
  }
331
334
  },
335
+ async listEnvironments({ name: name2 }) {
336
+ try {
337
+ const response = await axiosCloudAPI.get(`/projects/${name2}/environments`);
338
+ if (response.status !== 200) {
339
+ throw new Error("Error fetching cloud environments from the server.");
340
+ }
341
+ return response;
342
+ } catch (error) {
343
+ logger.debug(
344
+ "🥲 Oops! Couldn't retrieve your project's environments from the server. Please try again."
345
+ );
346
+ throw error;
347
+ }
348
+ },
349
+ async listLinkEnvironments({ name: name2 }) {
350
+ try {
351
+ const response = await axiosCloudAPI.get(`/projects/${name2}/environments-linkable`);
352
+ if (response.status !== 200) {
353
+ throw new Error("Error fetching cloud environments from the server.");
354
+ }
355
+ return response;
356
+ } catch (error) {
357
+ logger.debug(
358
+ "🥲 Oops! Couldn't retrieve your project's environments from the server. Please try again."
359
+ );
360
+ throw error;
361
+ }
362
+ },
363
+ async getProject({ name: name2 }) {
364
+ try {
365
+ const response = await axiosCloudAPI.get(`/projects/${name2}`);
366
+ if (response.status !== 200) {
367
+ throw new Error("Error fetching project's details.");
368
+ }
369
+ return response;
370
+ } catch (error) {
371
+ logger.debug(
372
+ "🥲 Oops! There was a problem retrieving your project's details. Please try again."
373
+ );
374
+ throw error;
375
+ }
376
+ },
332
377
  track(event, payload = {}) {
333
378
  return axiosCloudAPI.post("/track", {
334
379
  event,
@@ -338,26 +383,43 @@ async function cloudApiFactory({ logger }, token) {
338
383
  };
339
384
  }
340
385
  const LOCAL_SAVE_FILENAME = ".strapi-cloud.json";
386
+ const getFilePath = (directoryPath) => path__default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
341
387
  async function save(data, { directoryPath } = {}) {
342
- const alreadyInFileData = await retrieve({ directoryPath });
343
- const storedData = { ...alreadyInFileData, ...data };
344
- const pathToFile = path__default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
388
+ const pathToFile = getFilePath(directoryPath);
345
389
  await fse__default.ensureDir(path__default.dirname(pathToFile));
346
- await fse__default.writeJson(pathToFile, storedData, { encoding: "utf8" });
390
+ await fse__default.writeJson(pathToFile, data, { encoding: "utf8" });
347
391
  }
348
392
  async function retrieve({
349
393
  directoryPath
350
394
  } = {}) {
351
- const pathToFile = path__default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
395
+ const pathToFile = getFilePath(directoryPath);
352
396
  const pathExists = await fse__default.pathExists(pathToFile);
353
397
  if (!pathExists) {
354
398
  return {};
355
399
  }
356
400
  return fse__default.readJSON(pathToFile, { encoding: "utf8" });
357
401
  }
402
+ async function patch(patchData, { directoryPath } = {}) {
403
+ const pathToFile = getFilePath(directoryPath);
404
+ const existingData = await retrieve({ directoryPath });
405
+ if (!existingData) {
406
+ throw new Error("No configuration data found to patch.");
407
+ }
408
+ const newData = merge(existingData, patchData);
409
+ await fse__default.writeJson(pathToFile, newData, { encoding: "utf8" });
410
+ }
411
+ async function deleteConfig({ directoryPath } = {}) {
412
+ const pathToFile = getFilePath(directoryPath);
413
+ const pathExists = await fse__default.pathExists(pathToFile);
414
+ if (pathExists) {
415
+ await fse__default.remove(pathToFile);
416
+ }
417
+ }
358
418
  const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
359
419
  __proto__: null,
360
420
  LOCAL_SAVE_FILENAME,
421
+ deleteConfig,
422
+ patch,
361
423
  retrieve,
362
424
  save
363
425
  }, Symbol.toStringTag, { value: "Module" }));
@@ -365,7 +427,7 @@ let cliConfig;
365
427
  async function tokenServiceFactory({ logger }) {
366
428
  const cloudApiService = await cloudApiFactory({ logger });
367
429
  async function saveToken(str) {
368
- const appConfig = await getLocalConfig();
430
+ const appConfig = await getLocalConfig$1();
369
431
  if (!appConfig) {
370
432
  logger.error("There was a problem saving your token. Please try again.");
371
433
  return;
@@ -379,7 +441,7 @@ async function tokenServiceFactory({ logger }) {
379
441
  }
380
442
  }
381
443
  async function retrieveToken() {
382
- const appConfig = await getLocalConfig();
444
+ const appConfig = await getLocalConfig$1();
383
445
  if (appConfig.token) {
384
446
  if (await isTokenValid(appConfig.token)) {
385
447
  return appConfig.token;
@@ -441,7 +503,7 @@ async function tokenServiceFactory({ logger }) {
441
503
  }
442
504
  }
443
505
  async function eraseToken() {
444
- const appConfig = await getLocalConfig();
506
+ const appConfig = await getLocalConfig$1();
445
507
  if (!appConfig) {
446
508
  return;
447
509
  }
@@ -602,21 +664,24 @@ yup.object({
602
664
  name: yup.string().required(),
603
665
  exports: yup.lazy(
604
666
  (value) => yup.object(
605
- typeof value === "object" ? Object.entries(value).reduce((acc, [key, value2]) => {
606
- if (typeof value2 === "object") {
607
- acc[key] = yup.object({
608
- types: yup.string().optional(),
609
- source: yup.string().required(),
610
- module: yup.string().optional(),
611
- import: yup.string().required(),
612
- require: yup.string().required(),
613
- default: yup.string().required()
614
- }).noUnknown(true);
615
- } else {
616
- acc[key] = yup.string().matches(/^\.\/.*\.json$/).required();
617
- }
618
- return acc;
619
- }, {}) : void 0
667
+ typeof value === "object" ? Object.entries(value).reduce(
668
+ (acc, [key, value2]) => {
669
+ if (typeof value2 === "object") {
670
+ acc[key] = yup.object({
671
+ types: yup.string().optional(),
672
+ source: yup.string().required(),
673
+ module: yup.string().optional(),
674
+ import: yup.string().required(),
675
+ require: yup.string().required(),
676
+ default: yup.string().required()
677
+ }).noUnknown(true);
678
+ } else {
679
+ acc[key] = yup.string().matches(/^\.\/.*\.json$/).required();
680
+ }
681
+ return acc;
682
+ },
683
+ {}
684
+ ) : void 0
620
685
  ).optional()
621
686
  )
622
687
  });
@@ -638,18 +703,6 @@ async function getProjectNameFromPackageJson(ctx) {
638
703
  return "my-strapi-project";
639
704
  }
640
705
  }
641
- function applyDefaultName(newDefaultName, questions, defaultValues) {
642
- const newDefaultValues = _.cloneDeep(defaultValues);
643
- newDefaultValues.name = newDefaultName;
644
- const newQuestions = questions.map((question) => {
645
- const questionCopy = _.cloneDeep(question);
646
- if (questionCopy.name === "name") {
647
- questionCopy.default = newDefaultName;
648
- }
649
- return questionCopy;
650
- });
651
- return { newQuestions, newDefaultValues };
652
- }
653
706
  const trackEvent = async (ctx, cloudApiService, eventName, eventData) => {
654
707
  try {
655
708
  await cloudApiService.track(eventName, eventData);
@@ -813,6 +866,45 @@ async function loginAction(ctx) {
813
866
  await authenticate();
814
867
  return isAuthenticated;
815
868
  }
869
+ function questionDefaultValuesMapper(questionsMap) {
870
+ return (questions) => {
871
+ return questions.map((question) => {
872
+ const questionName = question.name;
873
+ if (questionName in questionsMap) {
874
+ const questionDefault = questionsMap[questionName];
875
+ if (typeof questionDefault === "function") {
876
+ return {
877
+ ...question,
878
+ default: questionDefault(question)
879
+ };
880
+ }
881
+ return {
882
+ ...question,
883
+ default: questionDefault
884
+ };
885
+ }
886
+ return question;
887
+ });
888
+ };
889
+ }
890
+ function getDefaultsFromQuestions(questions) {
891
+ return questions.reduce((acc, question) => {
892
+ if (question.default && question.name) {
893
+ return { ...acc, [question.name]: question.default };
894
+ }
895
+ return acc;
896
+ }, {});
897
+ }
898
+ function getProjectNodeVersionDefault(question) {
899
+ const currentNodeVersion = process.versions.node.split(".")[0];
900
+ if (question.type === "list" && Array.isArray(question.choices)) {
901
+ const choice = question.choices.find((choice2) => choice2.value === currentNodeVersion);
902
+ if (choice) {
903
+ return choice.value;
904
+ }
905
+ }
906
+ return question.default;
907
+ }
816
908
  async function handleError(ctx, error) {
817
909
  const { logger } = ctx;
818
910
  logger.debug(error);
@@ -857,7 +949,7 @@ async function createProject$1(ctx, cloudApi, projectInput) {
857
949
  throw e;
858
950
  }
859
951
  }
860
- const action$4 = async (ctx) => {
952
+ const action$6 = async (ctx) => {
861
953
  const { logger } = ctx;
862
954
  const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
863
955
  const token = await getValidToken(ctx, promptLogin);
@@ -866,11 +958,16 @@ const action$4 = async (ctx) => {
866
958
  }
867
959
  const cloudApi = await cloudApiFactory(ctx, token);
868
960
  const { data: config } = await cloudApi.config();
869
- const { newQuestions: questions, newDefaultValues: defaultValues } = applyDefaultName(
870
- await getProjectNameFromPackageJson(ctx),
871
- config.projectCreation.questions,
872
- config.projectCreation.defaults
873
- );
961
+ const projectName = await getProjectNameFromPackageJson(ctx);
962
+ const defaultAnswersMapper = questionDefaultValuesMapper({
963
+ name: projectName,
964
+ nodeVersion: getProjectNodeVersionDefault
965
+ });
966
+ const questions = defaultAnswersMapper(config.projectCreation.questions);
967
+ const defaultValues = {
968
+ ...config.projectCreation.defaults,
969
+ ...getDefaultsFromQuestions(questions)
970
+ };
874
971
  const projectAnswersDefaulted = defaults(defaultValues);
875
972
  const projectAnswers = await inquirer.prompt(questions);
876
973
  const projectInput = projectAnswersDefaulted(projectAnswers);
@@ -981,6 +1078,32 @@ const buildLogsServiceFactory = ({ logger }) => {
981
1078
  });
982
1079
  };
983
1080
  };
1081
+ const QUIT_OPTION$2 = "Quit";
1082
+ async function promptForEnvironment(environments) {
1083
+ const choices = environments.map((env2) => ({ name: env2, value: env2 }));
1084
+ const { selectedEnvironment } = await inquirer.prompt([
1085
+ {
1086
+ type: "list",
1087
+ name: "selectedEnvironment",
1088
+ message: "Select the environment to deploy:",
1089
+ choices: [...choices, { name: chalk.grey(`(${QUIT_OPTION$2})`), value: null }]
1090
+ }
1091
+ ]);
1092
+ if (selectedEnvironment === null) {
1093
+ process.exit(1);
1094
+ }
1095
+ const { confirm } = await inquirer.prompt([
1096
+ {
1097
+ type: "confirm",
1098
+ name: "confirm",
1099
+ message: `Do you want to proceed with deployment to ${chalk.cyan(selectedEnvironment)}?`
1100
+ }
1101
+ ]);
1102
+ if (!confirm) {
1103
+ process.exit(1);
1104
+ }
1105
+ return selectedEnvironment;
1106
+ }
984
1107
  async function upload(ctx, project, token, maxProjectFileSize) {
985
1108
  const cloudApi = await cloudApiFactory(ctx, token);
986
1109
  try {
@@ -1047,17 +1170,7 @@ async function upload(ctx, project, token, maxProjectFileSize) {
1047
1170
  return data.build_id;
1048
1171
  } catch (e) {
1049
1172
  progressBar.stop();
1050
- if (e instanceof AxiosError && e.response?.data) {
1051
- if (e.response.status === 404) {
1052
- ctx.logger.error(
1053
- `The project does not exist. Remove the ${LOCAL_SAVE_FILENAME} file and try again.`
1054
- );
1055
- } else {
1056
- ctx.logger.error(e.response.data);
1057
- }
1058
- } else {
1059
- ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1060
- }
1173
+ ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1061
1174
  ctx.logger.debug(e);
1062
1175
  } finally {
1063
1176
  await fse__default.remove(tarFilePath);
@@ -1073,7 +1186,7 @@ async function getProject(ctx) {
1073
1186
  const { project } = await retrieve();
1074
1187
  if (!project) {
1075
1188
  try {
1076
- return await action$4(ctx);
1189
+ return await action$6(ctx);
1077
1190
  } catch (e) {
1078
1191
  ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1079
1192
  ctx.logger.debug(e);
@@ -1094,7 +1207,26 @@ async function getConfig({
1094
1207
  return null;
1095
1208
  }
1096
1209
  }
1097
- const action$3 = async (ctx) => {
1210
+ function validateEnvironment(ctx, environment, environments) {
1211
+ if (!environments.includes(environment)) {
1212
+ ctx.logger.error(`Environment ${environment} does not exist.`);
1213
+ process.exit(1);
1214
+ }
1215
+ }
1216
+ async function getTargetEnvironment(ctx, opts, project, environments) {
1217
+ if (opts.env) {
1218
+ validateEnvironment(ctx, opts.env, environments);
1219
+ return opts.env;
1220
+ }
1221
+ if (project.targetEnvironment) {
1222
+ return project.targetEnvironment;
1223
+ }
1224
+ if (environments.length > 1) {
1225
+ return promptForEnvironment(environments);
1226
+ }
1227
+ return environments[0];
1228
+ }
1229
+ const action$5 = async (ctx, opts) => {
1098
1230
  const { getValidToken } = await tokenServiceFactory(ctx);
1099
1231
  const token = await getValidToken(ctx, promptLogin);
1100
1232
  if (!token) {
@@ -1104,7 +1236,41 @@ const action$3 = async (ctx) => {
1104
1236
  if (!project) {
1105
1237
  return;
1106
1238
  }
1107
- const cloudApiService = await cloudApiFactory(ctx);
1239
+ const cloudApiService = await cloudApiFactory(ctx, token);
1240
+ let environments;
1241
+ try {
1242
+ const {
1243
+ data: { data: projectData, metadata }
1244
+ } = await cloudApiService.getProject({ name: project.name });
1245
+ const isProjectSuspended = projectData.suspendedAt;
1246
+ environments = projectData.environments;
1247
+ if (isProjectSuspended) {
1248
+ ctx.logger.log(
1249
+ "\n Oops! This project has been suspended. \n\n Please reactivate it from the dashboard to continue deploying: "
1250
+ );
1251
+ ctx.logger.log(chalk.underline(`${metadata.dashboardUrls.project}`));
1252
+ return;
1253
+ }
1254
+ } catch (e) {
1255
+ if (e instanceof AxiosError && e.response?.data) {
1256
+ if (e.response.status === 404) {
1257
+ ctx.logger.warn(
1258
+ `The project associated with this folder does not exist in Strapi Cloud.
1259
+ Please link your local project to an existing Strapi Cloud project using the ${chalk.cyan(
1260
+ "link"
1261
+ )} command before deploying.`
1262
+ );
1263
+ } else {
1264
+ ctx.logger.error(e.response.data);
1265
+ }
1266
+ } else {
1267
+ ctx.logger.error(
1268
+ "An error occurred while retrieving the project's information. Please try again later."
1269
+ );
1270
+ }
1271
+ ctx.logger.debug(e);
1272
+ return;
1273
+ }
1108
1274
  await trackEvent(ctx, cloudApiService, "willDeployWithCLI", {
1109
1275
  projectInternalName: project.name
1110
1276
  });
@@ -1113,7 +1279,7 @@ const action$3 = async (ctx) => {
1113
1279
  const cliConfig2 = await getConfig({ ctx, cloudApiService });
1114
1280
  if (!cliConfig2) {
1115
1281
  ctx.logger.error(
1116
- "An error occurred while retrieving data from Strapi Cloud. Please try check your network or again later."
1282
+ "An error occurred while retrieving data from Strapi Cloud. Please check your network or try again later."
1117
1283
  );
1118
1284
  return;
1119
1285
  }
@@ -1124,11 +1290,15 @@ const action$3 = async (ctx) => {
1124
1290
  );
1125
1291
  maxSize = 1e8;
1126
1292
  }
1293
+ project.targetEnvironment = await getTargetEnvironment(ctx, opts, project, environments);
1127
1294
  const buildId = await upload(ctx, project, token, maxSize);
1128
1295
  if (!buildId) {
1129
1296
  return;
1130
1297
  }
1131
1298
  try {
1299
+ ctx.logger.log(
1300
+ `🚀 Deploying project to ${chalk.cyan(project.targetEnvironment ?? `production`)} environment...`
1301
+ );
1132
1302
  notificationService(`${apiConfig.apiBaseUrl}/notifications`, token, cliConfig2);
1133
1303
  await buildLogsService(`${apiConfig.apiBaseUrl}/v1/logs/${buildId}`, token, cliConfig2);
1134
1304
  ctx.logger.log(
@@ -1173,17 +1343,16 @@ const runAction = (name2, action2) => (...args) => {
1173
1343
  process.exit(1);
1174
1344
  });
1175
1345
  };
1176
- const command$5 = ({ command: command2, ctx }) => {
1177
- 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));
1346
+ const command$7 = ({ ctx }) => {
1347
+ return 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));
1178
1348
  };
1179
1349
  const deployProject = {
1180
1350
  name: "deploy-project",
1181
1351
  description: "Deploy a Strapi Cloud project",
1182
- action: action$3,
1183
- command: command$5
1352
+ action: action$5,
1353
+ command: command$7
1184
1354
  };
1185
- const QUIT_OPTION = "Quit";
1186
- async function getExistingConfig(ctx) {
1355
+ async function getLocalConfig(ctx) {
1187
1356
  try {
1188
1357
  return await retrieve();
1189
1358
  } catch (e) {
@@ -1192,6 +1361,21 @@ async function getExistingConfig(ctx) {
1192
1361
  return null;
1193
1362
  }
1194
1363
  }
1364
+ async function getLocalProject(ctx) {
1365
+ const localConfig = await getLocalConfig(ctx);
1366
+ if (!localConfig || !localConfig.project) {
1367
+ ctx.logger.warn(
1368
+ `
1369
+ We couldn't find a valid local project config.
1370
+ Please link your local project to an existing Strapi Cloud project using the ${chalk.cyan(
1371
+ "link"
1372
+ )} command.`
1373
+ );
1374
+ process.exit(1);
1375
+ }
1376
+ return localConfig.project;
1377
+ }
1378
+ const QUIT_OPTION$1 = "Quit";
1195
1379
  async function promptForRelink(ctx, cloudApiService, existingConfig) {
1196
1380
  if (existingConfig && existingConfig.project) {
1197
1381
  const { shouldRelink } = await inquirer.prompt([
@@ -1221,7 +1405,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1221
1405
  } = await cloudApiService.listLinkProjects();
1222
1406
  spinner.succeed();
1223
1407
  if (!Array.isArray(projectList)) {
1224
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1408
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1225
1409
  return null;
1226
1410
  }
1227
1411
  const projects = projectList.filter(
@@ -1233,7 +1417,7 @@ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1233
1417
  };
1234
1418
  });
1235
1419
  if (projects.length === 0) {
1236
- ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1420
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud.");
1237
1421
  return null;
1238
1422
  }
1239
1423
  return projects;
@@ -1251,7 +1435,7 @@ async function getUserSelection(ctx, projects) {
1251
1435
  type: "list",
1252
1436
  name: "linkProject",
1253
1437
  message: "Which project do you want to link?",
1254
- choices: [...projects, { name: chalk.grey(`(${QUIT_OPTION})`), value: null }]
1438
+ choices: [...projects, { name: chalk.grey(`(${QUIT_OPTION$1})`), value: null }]
1255
1439
  }
1256
1440
  ]);
1257
1441
  if (!answer.linkProject) {
@@ -1264,7 +1448,7 @@ async function getUserSelection(ctx, projects) {
1264
1448
  return null;
1265
1449
  }
1266
1450
  }
1267
- const action$2 = async (ctx) => {
1451
+ const action$4 = async (ctx) => {
1268
1452
  const { getValidToken } = await tokenServiceFactory(ctx);
1269
1453
  const token = await getValidToken(ctx, promptLogin);
1270
1454
  const { logger } = ctx;
@@ -1272,7 +1456,7 @@ const action$2 = async (ctx) => {
1272
1456
  return;
1273
1457
  }
1274
1458
  const cloudApiService = await cloudApiFactory(ctx, token);
1275
- const existingConfig = await getExistingConfig(ctx);
1459
+ const existingConfig = await getLocalConfig(ctx);
1276
1460
  const shouldRelink = await promptForRelink(ctx, cloudApiService, existingConfig);
1277
1461
  if (!shouldRelink) {
1278
1462
  return;
@@ -1307,7 +1491,9 @@ const action$2 = async (ctx) => {
1307
1491
  return;
1308
1492
  }
1309
1493
  await save({ project: answer.linkProject });
1310
- logger.log(`Project ${chalk.cyan(answer.linkProject.displayName)} linked successfully.`);
1494
+ logger.log(
1495
+ ` You have successfully linked your project to ${chalk.cyan(answer.linkProject.displayName)}. You are now able to deploy your project.`
1496
+ );
1311
1497
  await trackEvent(ctx, cloudApiService, "didLinkProject", {
1312
1498
  projectInternalName: answer.linkProject
1313
1499
  });
@@ -1319,17 +1505,17 @@ const action$2 = async (ctx) => {
1319
1505
  });
1320
1506
  }
1321
1507
  };
1322
- const command$4 = ({ command: command2, ctx }) => {
1323
- 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));
1508
+ const command$6 = ({ command: command2, ctx }) => {
1509
+ 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));
1324
1510
  };
1325
1511
  const link = {
1326
1512
  name: "link-project",
1327
1513
  description: "Link a local directory to a Strapi Cloud project",
1328
- action: action$2,
1329
- command: command$4
1514
+ action: action$4,
1515
+ command: command$6
1330
1516
  };
1331
- const command$3 = ({ command: command2, ctx }) => {
1332
- command2.command("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1517
+ const command$5 = ({ ctx }) => {
1518
+ return createCommand("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1333
1519
  "after",
1334
1520
  "\nAfter running this command, you will be prompted to enter your authentication information."
1335
1521
  ).option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("login", loginAction)(ctx));
@@ -1338,10 +1524,10 @@ const login = {
1338
1524
  name: "login",
1339
1525
  description: "Strapi Cloud Login",
1340
1526
  action: loginAction,
1341
- command: command$3
1527
+ command: command$5
1342
1528
  };
1343
1529
  const openModule = import("open");
1344
- const action$1 = async (ctx) => {
1530
+ const action$3 = async (ctx) => {
1345
1531
  const { logger } = ctx;
1346
1532
  const { retrieveToken, eraseToken } = await tokenServiceFactory(ctx);
1347
1533
  const token = await retrieveToken();
@@ -1373,25 +1559,25 @@ const action$1 = async (ctx) => {
1373
1559
  }
1374
1560
  await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1375
1561
  };
1376
- const command$2 = ({ command: command2, ctx }) => {
1377
- 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));
1562
+ const command$4 = ({ ctx }) => {
1563
+ return 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));
1378
1564
  };
1379
1565
  const logout = {
1380
1566
  name: "logout",
1381
1567
  description: "Strapi Cloud Logout",
1382
- action: action$1,
1383
- command: command$2
1568
+ action: action$3,
1569
+ command: command$4
1384
1570
  };
1385
- const command$1 = ({ command: command2, ctx }) => {
1386
- 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));
1571
+ const command$3 = ({ ctx }) => {
1572
+ return 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));
1387
1573
  };
1388
1574
  const createProject = {
1389
1575
  name: "create-project",
1390
1576
  description: "Create a new project",
1391
- action: action$4,
1392
- command: command$1
1577
+ action: action$6,
1578
+ command: command$3
1393
1579
  };
1394
- const action = async (ctx) => {
1580
+ const action$2 = async (ctx) => {
1395
1581
  const { getValidToken } = await tokenServiceFactory(ctx);
1396
1582
  const token = await getValidToken(ctx, promptLogin);
1397
1583
  const { logger } = ctx;
@@ -1411,12 +1597,194 @@ const action = async (ctx) => {
1411
1597
  spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1412
1598
  }
1413
1599
  };
1414
- const command = ({ command: command2, ctx }) => {
1415
- 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));
1600
+ const command$2 = ({ command: command2, ctx }) => {
1601
+ 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));
1416
1602
  };
1417
1603
  const listProjects = {
1418
1604
  name: "list-projects",
1419
1605
  description: "List Strapi Cloud projects",
1606
+ action: action$2,
1607
+ command: command$2
1608
+ };
1609
+ const action$1 = async (ctx) => {
1610
+ const { getValidToken } = await tokenServiceFactory(ctx);
1611
+ const token = await getValidToken(ctx, promptLogin);
1612
+ const { logger } = ctx;
1613
+ if (!token) {
1614
+ return;
1615
+ }
1616
+ const project = await getLocalProject(ctx);
1617
+ if (!project) {
1618
+ ctx.logger.debug(`No valid local project configuration was found.`);
1619
+ return;
1620
+ }
1621
+ const cloudApiService = await cloudApiFactory(ctx, token);
1622
+ const spinner = logger.spinner("Fetching environments...").start();
1623
+ await trackEvent(ctx, cloudApiService, "willListEnvironment", {
1624
+ projectInternalName: project.name
1625
+ });
1626
+ try {
1627
+ const {
1628
+ data: { data: environmentsList }
1629
+ } = await cloudApiService.listEnvironments({ name: project.name });
1630
+ spinner.succeed();
1631
+ logger.log(environmentsList);
1632
+ await trackEvent(ctx, cloudApiService, "didListEnvironment", {
1633
+ projectInternalName: project.name
1634
+ });
1635
+ } catch (e) {
1636
+ if (e.response && e.response.status === 404) {
1637
+ spinner.succeed();
1638
+ logger.warn(
1639
+ `
1640
+ The project associated with this folder does not exist in Strapi Cloud.
1641
+ Please link your local project to an existing Strapi Cloud project using the ${chalk.cyan(
1642
+ "link"
1643
+ )} command`
1644
+ );
1645
+ } else {
1646
+ spinner.fail("An error occurred while fetching environments data from Strapi Cloud.");
1647
+ logger.debug("Failed to list environments", e);
1648
+ }
1649
+ await trackEvent(ctx, cloudApiService, "didNotListEnvironment", {
1650
+ projectInternalName: project.name
1651
+ });
1652
+ }
1653
+ };
1654
+ function defineCloudNamespace(command2, ctx) {
1655
+ const cloud = command2.command("cloud").description("Manage Strapi Cloud projects");
1656
+ cloud.command("environments").description("Alias for cloud environment list").action(() => runAction("list", action$1)(ctx));
1657
+ return cloud;
1658
+ }
1659
+ let environmentCmd = null;
1660
+ const initializeEnvironmentCommand = (command2, ctx) => {
1661
+ if (!environmentCmd) {
1662
+ const cloud = defineCloudNamespace(command2, ctx);
1663
+ environmentCmd = cloud.command("environment").description("Manage environments");
1664
+ }
1665
+ return environmentCmd;
1666
+ };
1667
+ const command$1 = ({ command: command2, ctx }) => {
1668
+ const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1669
+ 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));
1670
+ };
1671
+ const listEnvironments = {
1672
+ name: "list-environments",
1673
+ description: "List Strapi Cloud environments",
1674
+ action: action$1,
1675
+ command: command$1
1676
+ };
1677
+ const QUIT_OPTION = "Quit";
1678
+ const action = async (ctx) => {
1679
+ const { getValidToken } = await tokenServiceFactory(ctx);
1680
+ const token = await getValidToken(ctx, promptLogin);
1681
+ const { logger } = ctx;
1682
+ if (!token) {
1683
+ return;
1684
+ }
1685
+ const project = await getLocalProject(ctx);
1686
+ if (!project) {
1687
+ logger.debug(`No valid local project configuration was found.`);
1688
+ return;
1689
+ }
1690
+ const cloudApiService = await cloudApiFactory(ctx, token);
1691
+ const environments = await getEnvironmentsList(ctx, cloudApiService, project);
1692
+ if (!environments) {
1693
+ logger.debug(`Fetching environments failed.`);
1694
+ return;
1695
+ }
1696
+ if (environments.length === 0) {
1697
+ logger.log(
1698
+ `The only available environment is already linked. You can add a new one from your project settings on the Strapi Cloud dashboard.`
1699
+ );
1700
+ return;
1701
+ }
1702
+ const answer = await promptUserForEnvironment(ctx, environments);
1703
+ if (!answer) {
1704
+ return;
1705
+ }
1706
+ await trackEvent(ctx, cloudApiService, "willLinkEnvironment", {
1707
+ projectName: project.name,
1708
+ environmentName: answer.targetEnvironment
1709
+ });
1710
+ try {
1711
+ await patch({ project: { targetEnvironment: answer.targetEnvironment } });
1712
+ } catch (e) {
1713
+ await trackEvent(ctx, cloudApiService, "didNotLinkEnvironment", {
1714
+ projectName: project.name,
1715
+ environmentName: answer.targetEnvironment
1716
+ });
1717
+ logger.debug("Failed to link environment", e);
1718
+ logger.error(
1719
+ "Failed to link the environment. If this issue persists, try re-linking your project or contact support."
1720
+ );
1721
+ process.exit(1);
1722
+ }
1723
+ logger.log(
1724
+ ` You have successfully linked your project to ${chalk.cyan(answer.targetEnvironment)}, on ${chalk.cyan(project.displayName)}. You are now able to deploy your project.`
1725
+ );
1726
+ await trackEvent(ctx, cloudApiService, "didLinkEnvironment", {
1727
+ projectName: project.name,
1728
+ environmentName: answer.targetEnvironment
1729
+ });
1730
+ };
1731
+ async function promptUserForEnvironment(ctx, environments) {
1732
+ const { logger } = ctx;
1733
+ try {
1734
+ const answer = await inquirer.prompt([
1735
+ {
1736
+ type: "list",
1737
+ name: "targetEnvironment",
1738
+ message: "Which environment do you want to link?",
1739
+ choices: [...environments, { name: chalk.grey(`(${QUIT_OPTION})`), value: null }]
1740
+ }
1741
+ ]);
1742
+ if (!answer.targetEnvironment) {
1743
+ return null;
1744
+ }
1745
+ return answer;
1746
+ } catch (e) {
1747
+ logger.debug("Failed to get user input", e);
1748
+ logger.error("An error occurred while trying to get your environment selection.");
1749
+ return null;
1750
+ }
1751
+ }
1752
+ async function getEnvironmentsList(ctx, cloudApiService, project) {
1753
+ const spinner = ctx.logger.spinner("Fetching environments...\n").start();
1754
+ try {
1755
+ const {
1756
+ data: { data: environmentsList }
1757
+ } = await cloudApiService.listLinkEnvironments({ name: project.name });
1758
+ if (!Array.isArray(environmentsList) || environmentsList.length === 0) {
1759
+ throw new Error("Environments not found in server response");
1760
+ }
1761
+ spinner.succeed();
1762
+ return environmentsList.filter(
1763
+ (environment) => environment.name !== project.targetEnvironment
1764
+ );
1765
+ } catch (e) {
1766
+ if (e.response && e.response.status === 404) {
1767
+ spinner.succeed();
1768
+ ctx.logger.warn(
1769
+ `
1770
+ The project associated with this folder does not exist in Strapi Cloud.
1771
+ Please link your local project to an existing Strapi Cloud project using the ${chalk.cyan(
1772
+ "link"
1773
+ )} command.`
1774
+ );
1775
+ } else {
1776
+ spinner.fail("An error occurred while fetching environments data from Strapi Cloud.");
1777
+ ctx.logger.debug("Failed to list environments", e);
1778
+ }
1779
+ }
1780
+ }
1781
+ const command = ({ command: command2, ctx }) => {
1782
+ const environmentCmd2 = initializeEnvironmentCommand(command2, ctx);
1783
+ 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));
1784
+ };
1785
+ const linkEnvironment = {
1786
+ name: "link-environment",
1787
+ description: "Link Strapi Cloud environment to a local project",
1420
1788
  action,
1421
1789
  command
1422
1790
  };
@@ -1426,11 +1794,21 @@ const cli = {
1426
1794
  login,
1427
1795
  logout,
1428
1796
  createProject,
1429
- listProjects
1797
+ linkEnvironment,
1798
+ listProjects,
1799
+ listEnvironments
1430
1800
  };
1431
- const cloudCommands = [deployProject, link, login, logout, listProjects];
1801
+ const cloudCommands = [
1802
+ deployProject,
1803
+ link,
1804
+ login,
1805
+ logout,
1806
+ linkEnvironment,
1807
+ listProjects,
1808
+ listEnvironments
1809
+ ];
1432
1810
  async function initCloudCLIConfig() {
1433
- const localConfig = await getLocalConfig();
1811
+ const localConfig = await getLocalConfig$1();
1434
1812
  if (!localConfig.deviceId) {
1435
1813
  localConfig.deviceId = crypto$1.randomUUID();
1436
1814
  }
@@ -1444,7 +1822,10 @@ async function buildStrapiCloudCommands({
1444
1822
  await initCloudCLIConfig();
1445
1823
  for (const cloudCommand of cloudCommands) {
1446
1824
  try {
1447
- await cloudCommand.command({ command: command2, ctx, argv });
1825
+ const subCommand = await cloudCommand.command({ command: command2, ctx, argv });
1826
+ if (subCommand) {
1827
+ command2.addCommand(subCommand);
1828
+ }
1448
1829
  } catch (e) {
1449
1830
  console.error(`Failed to load command ${cloudCommand.name}`, e);
1450
1831
  }