@strapi/cloud-cli 0.0.0-next.48485bcb4aa83c14d6d2cb99cbd47676d0d31b97 → 0.0.0-next.506c866896b405bfee0715be82ad4b0f8da1d26b

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/index.js +365 -132
  2. package/dist/index.js.map +1 -1
  3. package/dist/index.mjs +364 -129
  4. package/dist/index.mjs.map +1 -1
  5. package/dist/src/create-project/action.d.ts.map +1 -1
  6. package/dist/src/create-project/utils/apply-default-name.d.ts +7 -0
  7. package/dist/src/create-project/utils/apply-default-name.d.ts.map +1 -0
  8. package/dist/src/create-project/utils/get-project-name-from-pkg.d.ts +3 -0
  9. package/dist/src/create-project/utils/get-project-name-from-pkg.d.ts.map +1 -0
  10. package/dist/src/deploy-project/action.d.ts.map +1 -1
  11. package/dist/src/index.d.ts +2 -0
  12. package/dist/src/index.d.ts.map +1 -1
  13. package/dist/src/link/action.d.ts +4 -0
  14. package/dist/src/link/action.d.ts.map +1 -0
  15. package/dist/src/link/command.d.ts +7 -0
  16. package/dist/src/link/command.d.ts.map +1 -0
  17. package/dist/src/link/index.d.ts +7 -0
  18. package/dist/src/link/index.d.ts.map +1 -0
  19. package/dist/src/list-projects/action.d.ts +4 -0
  20. package/dist/src/list-projects/action.d.ts.map +1 -0
  21. package/dist/src/list-projects/command.d.ts +7 -0
  22. package/dist/src/list-projects/command.d.ts.map +1 -0
  23. package/dist/src/list-projects/index.d.ts +7 -0
  24. package/dist/src/list-projects/index.d.ts.map +1 -0
  25. package/dist/src/login/action.d.ts.map +1 -1
  26. package/dist/src/logout/action.d.ts.map +1 -1
  27. package/dist/src/services/build-logs.d.ts.map +1 -1
  28. package/dist/src/services/cli-api.d.ts +19 -7
  29. package/dist/src/services/cli-api.d.ts.map +1 -1
  30. package/dist/src/services/strapi-info-save.d.ts +1 -1
  31. package/dist/src/services/strapi-info-save.d.ts.map +1 -1
  32. package/dist/src/types.d.ts +1 -0
  33. package/dist/src/types.d.ts.map +1 -1
  34. package/dist/src/utils/analytics.d.ts +4 -0
  35. package/dist/src/utils/analytics.d.ts.map +1 -0
  36. package/dist/src/utils/compress-files.d.ts.map +1 -1
  37. package/dist/src/utils/pkg.d.ts.map +1 -1
  38. package/package.json +6 -5
  39. package/dist/src/utils/tests/compress-files.test.d.ts +0 -2
  40. package/dist/src/utils/tests/compress-files.test.d.ts.map +0 -1
package/dist/index.mjs CHANGED
@@ -1,12 +1,12 @@
1
1
  import crypto$1 from "crypto";
2
- import fse from "fs-extra";
2
+ import * as fse from "fs-extra";
3
+ import fse__default from "fs-extra";
3
4
  import * as path from "path";
4
5
  import path__default from "path";
5
6
  import chalk from "chalk";
6
7
  import axios, { AxiosError } from "axios";
7
8
  import * as crypto from "node:crypto";
8
9
  import { env } from "@strapi/utils";
9
- import * as fs from "fs";
10
10
  import * as tar from "tar";
11
11
  import { minimatch } from "minimatch";
12
12
  import inquirer from "inquirer";
@@ -18,10 +18,10 @@ import jwt from "jsonwebtoken";
18
18
  import stringify from "fast-safe-stringify";
19
19
  import ora from "ora";
20
20
  import * as cliProgress from "cli-progress";
21
- import EventSource from "eventsource";
22
- import fs$1 from "fs/promises";
23
21
  import pkgUp from "pkg-up";
24
22
  import * as yup from "yup";
23
+ import _ from "lodash";
24
+ import EventSource from "eventsource";
25
25
  const apiConfig = {
26
26
  apiBaseUrl: env("STRAPI_CLI_CLOUD_API", "https://cloud-cli-api.strapi.io"),
27
27
  dashboardBaseUrl: env("STRAPI_CLI_CLOUD_DASHBOARD", "https://cloud.strapi.io")
@@ -40,23 +40,6 @@ const IGNORED_PATTERNS = [
40
40
  "**/.idea/**",
41
41
  "**/.vscode/**"
42
42
  ];
43
- const getFiles = (dirPath, ignorePatterns = [], arrayOfFiles = [], subfolder = "") => {
44
- const entries = fs.readdirSync(path.join(dirPath, subfolder));
45
- entries.forEach((entry) => {
46
- const entryPathFromRoot = path.join(subfolder, entry);
47
- const entryPath = path.relative(dirPath, entryPathFromRoot);
48
- const isIgnored = isIgnoredFile(dirPath, entryPathFromRoot, ignorePatterns);
49
- if (isIgnored) {
50
- return;
51
- }
52
- if (fs.statSync(entryPath).isDirectory()) {
53
- getFiles(dirPath, ignorePatterns, arrayOfFiles, entryPathFromRoot);
54
- } else {
55
- arrayOfFiles.push(entryPath);
56
- }
57
- });
58
- return arrayOfFiles;
59
- };
60
43
  const isIgnoredFile = (folderPath, file, ignorePatterns) => {
61
44
  ignorePatterns.push(...IGNORED_PATTERNS);
62
45
  const relativeFilePath = path.join(folderPath, file);
@@ -74,16 +57,35 @@ const isIgnoredFile = (folderPath, file, ignorePatterns) => {
74
57
  }
75
58
  return isIgnored;
76
59
  };
77
- const readGitignore = (folderPath) => {
60
+ const getFiles = async (dirPath, ignorePatterns = [], subfolder = "") => {
61
+ const arrayOfFiles = [];
62
+ const entries = await fse.readdir(path.join(dirPath, subfolder));
63
+ for (const entry of entries) {
64
+ const entryPathFromRoot = path.join(subfolder, entry);
65
+ const entryPath = path.relative(dirPath, entryPathFromRoot);
66
+ const isIgnored = isIgnoredFile(dirPath, entryPathFromRoot, ignorePatterns);
67
+ if (!isIgnored) {
68
+ if (fse.statSync(entryPath).isDirectory()) {
69
+ const subFiles = await getFiles(dirPath, ignorePatterns, entryPathFromRoot);
70
+ arrayOfFiles.push(...subFiles);
71
+ } else {
72
+ arrayOfFiles.push(entryPath);
73
+ }
74
+ }
75
+ }
76
+ return arrayOfFiles;
77
+ };
78
+ const readGitignore = async (folderPath) => {
78
79
  const gitignorePath = path.resolve(folderPath, ".gitignore");
79
- if (!fs.existsSync(gitignorePath))
80
+ const pathExist = await fse.pathExists(gitignorePath);
81
+ if (!pathExist)
80
82
  return [];
81
- const gitignoreContent = fs.readFileSync(gitignorePath, "utf8");
83
+ const gitignoreContent = await fse.readFile(gitignorePath, "utf8");
82
84
  return gitignoreContent.split(/\r?\n/).filter((line) => Boolean(line.trim()) && !line.startsWith("#"));
83
85
  };
84
86
  const compressFilesToTar = async (storagePath, folderToCompress, filename) => {
85
- const ignorePatterns = readGitignore(folderToCompress);
86
- const filesToCompress = getFiles(folderToCompress, ignorePatterns);
87
+ const ignorePatterns = await readGitignore(folderToCompress);
88
+ const filesToCompress = await getFiles(folderToCompress, ignorePatterns);
87
89
  return tar.c(
88
90
  {
89
91
  gzip: true,
@@ -96,7 +98,7 @@ const APP_FOLDER_NAME = "com.strapi.cli";
96
98
  const CONFIG_FILENAME = "config.json";
97
99
  async function checkDirectoryExists(directoryPath) {
98
100
  try {
99
- const fsStat = await fse.lstat(directoryPath);
101
+ const fsStat = await fse__default.lstat(directoryPath);
100
102
  return fsStat.isDirectory();
101
103
  } catch (e) {
102
104
  return false;
@@ -104,14 +106,14 @@ async function checkDirectoryExists(directoryPath) {
104
106
  }
105
107
  async function getTmpStoragePath() {
106
108
  const storagePath = path__default.join(os.tmpdir(), APP_FOLDER_NAME);
107
- await fse.ensureDir(storagePath);
109
+ await fse__default.ensureDir(storagePath);
108
110
  return storagePath;
109
111
  }
110
112
  async function getConfigPath() {
111
113
  const configDirs = XDGAppPaths(APP_FOLDER_NAME).configDirs();
112
114
  const configPath = configDirs.find(checkDirectoryExists);
113
115
  if (!configPath) {
114
- await fse.ensureDir(configDirs[0]);
116
+ await fse__default.ensureDir(configDirs[0]);
115
117
  return configDirs[0];
116
118
  }
117
119
  return configPath;
@@ -119,9 +121,9 @@ async function getConfigPath() {
119
121
  async function getLocalConfig() {
120
122
  const configPath = await getConfigPath();
121
123
  const configFilePath = path__default.join(configPath, CONFIG_FILENAME);
122
- await fse.ensureFile(configFilePath);
124
+ await fse__default.ensureFile(configFilePath);
123
125
  try {
124
- return await fse.readJSON(configFilePath, { encoding: "utf8", throws: true });
126
+ return await fse__default.readJSON(configFilePath, { encoding: "utf8", throws: true });
125
127
  } catch (e) {
126
128
  return {};
127
129
  }
@@ -129,10 +131,10 @@ async function getLocalConfig() {
129
131
  async function saveLocalConfig(data) {
130
132
  const configPath = await getConfigPath();
131
133
  const configFilePath = path__default.join(configPath, CONFIG_FILENAME);
132
- await fse.writeJson(configFilePath, data, { encoding: "utf8", spaces: 2, mode: 384 });
134
+ await fse__default.writeJson(configFilePath, data, { encoding: "utf8", spaces: 2, mode: 384 });
133
135
  }
134
136
  const name = "@strapi/cloud-cli";
135
- const version = "4.25.1";
137
+ const version = "4.25.5";
136
138
  const description = "Commands to interact with the Strapi Cloud";
137
139
  const keywords = [
138
140
  "strapi",
@@ -173,10 +175,11 @@ const scripts = {
173
175
  build: "pack-up build",
174
176
  clean: "run -T rimraf ./dist",
175
177
  lint: "run -T eslint .",
178
+ "test:unit": "run -T jest",
176
179
  watch: "pack-up watch"
177
180
  };
178
181
  const dependencies = {
179
- "@strapi/utils": "4.25.1",
182
+ "@strapi/utils": "4.25.5",
180
183
  axios: "1.6.0",
181
184
  chalk: "4.1.2",
182
185
  "cli-progress": "3.12.0",
@@ -201,8 +204,8 @@ const devDependencies = {
201
204
  "@types/cli-progress": "3.11.5",
202
205
  "@types/eventsource": "1.1.15",
203
206
  "@types/lodash": "^4.14.191",
204
- "eslint-config-custom": "4.25.1",
205
- tsconfig: "4.25.1"
207
+ "eslint-config-custom": "4.25.5",
208
+ tsconfig: "4.25.5"
206
209
  };
207
210
  const engines = {
208
211
  node: ">=18.0.0 <=20.x.x",
@@ -255,7 +258,7 @@ async function cloudApiFactory({ logger }, token) {
255
258
  deploy({ filePath, project }, { onUploadProgress }) {
256
259
  return axiosCloudAPI.post(
257
260
  `/deploy/${project.name}`,
258
- { file: fse.createReadStream(filePath) },
261
+ { file: fse__default.createReadStream(filePath) },
259
262
  {
260
263
  headers: {
261
264
  "Content-Type": "multipart/form-data"
@@ -298,8 +301,33 @@ async function cloudApiFactory({ logger }, token) {
298
301
  throw error;
299
302
  }
300
303
  },
301
- listProjects() {
302
- return axiosCloudAPI.get("/projects");
304
+ async listProjects() {
305
+ try {
306
+ const response = await axiosCloudAPI.get("/projects");
307
+ if (response.status !== 200) {
308
+ throw new Error("Error fetching cloud projects from the server.");
309
+ }
310
+ return response;
311
+ } catch (error) {
312
+ logger.debug(
313
+ "🥲 Oops! Couldn't retrieve your project's list from the server. Please try again."
314
+ );
315
+ throw error;
316
+ }
317
+ },
318
+ async listLinkProjects() {
319
+ try {
320
+ const response = await axiosCloudAPI.get("/projects/linkable");
321
+ if (response.status !== 200) {
322
+ throw new Error("Error fetching cloud projects from the server.");
323
+ }
324
+ return response;
325
+ } catch (error) {
326
+ logger.debug(
327
+ "🥲 Oops! Couldn't retrieve your project's list from the server. Please try again."
328
+ );
329
+ throw error;
330
+ }
303
331
  },
304
332
  track(event, payload = {}) {
305
333
  return axiosCloudAPI.post("/track", {
@@ -314,18 +342,18 @@ async function save(data, { directoryPath } = {}) {
314
342
  const alreadyInFileData = await retrieve({ directoryPath });
315
343
  const storedData = { ...alreadyInFileData, ...data };
316
344
  const pathToFile = path__default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
317
- await fse.ensureDir(path__default.dirname(pathToFile));
318
- await fse.writeJson(pathToFile, storedData, { encoding: "utf8" });
345
+ await fse__default.ensureDir(path__default.dirname(pathToFile));
346
+ await fse__default.writeJson(pathToFile, storedData, { encoding: "utf8" });
319
347
  }
320
348
  async function retrieve({
321
349
  directoryPath
322
350
  } = {}) {
323
351
  const pathToFile = path__default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
324
- const pathExists = await fse.pathExists(pathToFile);
352
+ const pathExists = await fse__default.pathExists(pathToFile);
325
353
  if (!pathExists) {
326
354
  return {};
327
355
  }
328
- return fse.readJSON(pathToFile, { encoding: "utf8" });
356
+ return fse__default.readJSON(pathToFile, { encoding: "utf8" });
329
357
  }
330
358
  const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
331
359
  __proto__: null,
@@ -570,6 +598,65 @@ const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.definePropert
570
598
  local: strapiInfoSave,
571
599
  tokenServiceFactory
572
600
  }, Symbol.toStringTag, { value: "Module" }));
601
+ yup.object({
602
+ name: yup.string().required(),
603
+ exports: yup.lazy(
604
+ (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
620
+ ).optional()
621
+ )
622
+ });
623
+ const loadPkg = async ({ cwd, logger }) => {
624
+ const pkgPath = await pkgUp({ cwd });
625
+ if (!pkgPath) {
626
+ throw new Error("Could not find a package.json in the current directory");
627
+ }
628
+ const buffer = await fse.readFile(pkgPath);
629
+ const pkg = JSON.parse(buffer.toString());
630
+ logger.debug("Loaded package.json:", os.EOL, pkg);
631
+ return pkg;
632
+ };
633
+ async function getProjectNameFromPackageJson(ctx) {
634
+ try {
635
+ const packageJson2 = await loadPkg(ctx);
636
+ return packageJson2.name || "my-strapi-project";
637
+ } catch (e) {
638
+ return "my-strapi-project";
639
+ }
640
+ }
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
+ const trackEvent = async (ctx, cloudApiService, eventName, eventData) => {
654
+ try {
655
+ await cloudApiService.track(eventName, eventData);
656
+ } catch (e) {
657
+ ctx.logger.debug(`Failed to track ${eventName}`, e);
658
+ }
659
+ };
573
660
  const openModule$1 = import("open");
574
661
  async function promptLogin(ctx) {
575
662
  const response = await inquirer.prompt([
@@ -590,13 +677,6 @@ async function loginAction(ctx) {
590
677
  const tokenService = await tokenServiceFactory(ctx);
591
678
  const existingToken = await tokenService.retrieveToken();
592
679
  const cloudApiService = await cloudApiFactory(ctx, existingToken || void 0);
593
- const trackFailedLogin = async () => {
594
- try {
595
- await cloudApiService.track("didNotLogin", { loginMethod: "cli" });
596
- } catch (e) {
597
- logger.debug("Failed to track failed login", e);
598
- }
599
- };
600
680
  if (existingToken) {
601
681
  const isTokenValid = await tokenService.isTokenValid(existingToken);
602
682
  if (isTokenValid) {
@@ -628,11 +708,7 @@ async function loginAction(ctx) {
628
708
  logger.debug(e);
629
709
  return false;
630
710
  }
631
- try {
632
- await cloudApiService.track("willLoginAttempt", {});
633
- } catch (e) {
634
- logger.debug("Failed to track login attempt", e);
635
- }
711
+ await trackEvent(ctx, cloudApiService, "willLoginAttempt", {});
636
712
  logger.debug("🔐 Creating device authentication request...", {
637
713
  client_id: cliConfig2.clientId,
638
714
  scope: cliConfig2.scope,
@@ -712,13 +788,13 @@ async function loginAction(ctx) {
712
788
  "There seems to be a problem with your login information. Please try logging in again."
713
789
  );
714
790
  spinnerFail();
715
- await trackFailedLogin();
791
+ await trackEvent(ctx, cloudApiService, "didNotLogin", { loginMethod: "cli" });
716
792
  return false;
717
793
  }
718
794
  if (e.response?.data.error && !["authorization_pending", "slow_down"].includes(e.response.data.error)) {
719
795
  logger.debug(e);
720
796
  spinnerFail();
721
- await trackFailedLogin();
797
+ await trackEvent(ctx, cloudApiService, "didNotLogin", { loginMethod: "cli" });
722
798
  return false;
723
799
  }
724
800
  await new Promise((resolve) => {
@@ -732,11 +808,7 @@ async function loginAction(ctx) {
732
808
  "To access your dashboard, please copy and paste the following URL into your web browser:"
733
809
  );
734
810
  logger.log(chalk.underline(`${apiConfig.dashboardBaseUrl}/projects`));
735
- try {
736
- await cloudApiService.track("didLogin", { loginMethod: "cli" });
737
- } catch (e) {
738
- logger.debug("Failed to track login", e);
739
- }
811
+ await trackEvent(ctx, cloudApiService, "didLogin", { loginMethod: "cli" });
740
812
  };
741
813
  await authenticate();
742
814
  return isAuthenticated;
@@ -785,7 +857,7 @@ async function createProject$1(ctx, cloudApi, projectInput) {
785
857
  throw e;
786
858
  }
787
859
  }
788
- const action$2 = async (ctx) => {
860
+ const action$4 = async (ctx) => {
789
861
  const { logger } = ctx;
790
862
  const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
791
863
  const token = await getValidToken(ctx, promptLogin);
@@ -794,7 +866,11 @@ const action$2 = async (ctx) => {
794
866
  }
795
867
  const cloudApi = await cloudApiFactory(ctx, token);
796
868
  const { data: config } = await cloudApi.config();
797
- const { questions, defaults: defaultValues } = config.projectCreation;
869
+ const { newQuestions: questions, newDefaultValues: defaultValues } = applyDefaultName(
870
+ await getProjectNameFromPackageJson(ctx),
871
+ config.projectCreation.questions,
872
+ config.projectCreation.defaults
873
+ );
798
874
  const projectAnswersDefaulted = defaults(defaultValues);
799
875
  const projectAnswers = await inquirer.prompt(questions);
800
876
  const projectInput = projectAnswersDefaulted(projectAnswers);
@@ -843,38 +919,6 @@ function notificationServiceFactory({ logger }) {
843
919
  };
844
920
  };
845
921
  }
846
- yup.object({
847
- name: yup.string().required(),
848
- exports: yup.lazy(
849
- (value) => yup.object(
850
- typeof value === "object" ? Object.entries(value).reduce((acc, [key, value2]) => {
851
- if (typeof value2 === "object") {
852
- acc[key] = yup.object({
853
- types: yup.string().optional(),
854
- source: yup.string().required(),
855
- module: yup.string().optional(),
856
- import: yup.string().required(),
857
- require: yup.string().required(),
858
- default: yup.string().required()
859
- }).noUnknown(true);
860
- } else {
861
- acc[key] = yup.string().matches(/^\.\/.*\.json$/).required();
862
- }
863
- return acc;
864
- }, {}) : void 0
865
- ).optional()
866
- )
867
- });
868
- const loadPkg = async ({ cwd, logger }) => {
869
- const pkgPath = await pkgUp({ cwd });
870
- if (!pkgPath) {
871
- throw new Error("Could not find a package.json in the current directory");
872
- }
873
- const buffer = await fs$1.readFile(pkgPath);
874
- const pkg = JSON.parse(buffer.toString());
875
- logger.debug("Loaded package.json:", os.EOL, pkg);
876
- return pkg;
877
- };
878
922
  const buildLogsServiceFactory = ({ logger }) => {
879
923
  return async (url, token, cliConfig2) => {
880
924
  const CONN_TIMEOUT = Number(cliConfig2.buildLogsConnectionTimeout);
@@ -928,6 +972,7 @@ const buildLogsServiceFactory = ({ logger }) => {
928
972
  if (retries > MAX_RETRIES) {
929
973
  spinner.fail("We were unable to connect to the server to get build logs at this time.");
930
974
  es.close();
975
+ clearExistingTimeout();
931
976
  reject(new Error("Max retries reached"));
932
977
  }
933
978
  };
@@ -970,13 +1015,13 @@ async function upload(ctx, project, token, maxProjectFileSize) {
970
1015
  process.exit(1);
971
1016
  }
972
1017
  const tarFilePath = path__default.resolve(storagePath, compressedFilename);
973
- const fileStats = await fse.stat(tarFilePath);
1018
+ const fileStats = await fse__default.stat(tarFilePath);
974
1019
  if (fileStats.size > maxProjectFileSize) {
975
1020
  ctx.logger.log(
976
1021
  "Unable to proceed: Your project is too big to be transferred, please use a git repo instead."
977
1022
  );
978
1023
  try {
979
- await fse.remove(tarFilePath);
1024
+ await fse__default.remove(tarFilePath);
980
1025
  } catch (e) {
981
1026
  ctx.logger.log("Unable to remove file: ", tarFilePath);
982
1027
  ctx.logger.debug(e);
@@ -1015,7 +1060,7 @@ async function upload(ctx, project, token, maxProjectFileSize) {
1015
1060
  }
1016
1061
  ctx.logger.debug(e);
1017
1062
  } finally {
1018
- await fse.remove(tarFilePath);
1063
+ await fse__default.remove(tarFilePath);
1019
1064
  }
1020
1065
  process.exit(0);
1021
1066
  } catch (e) {
@@ -1028,7 +1073,7 @@ async function getProject(ctx) {
1028
1073
  const { project } = await retrieve();
1029
1074
  if (!project) {
1030
1075
  try {
1031
- return await action$2(ctx);
1076
+ return await action$4(ctx);
1032
1077
  } catch (e) {
1033
1078
  ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1034
1079
  ctx.logger.debug(e);
@@ -1037,7 +1082,19 @@ async function getProject(ctx) {
1037
1082
  }
1038
1083
  return project;
1039
1084
  }
1040
- const action$1 = async (ctx) => {
1085
+ async function getConfig({
1086
+ ctx,
1087
+ cloudApiService
1088
+ }) {
1089
+ try {
1090
+ const { data: cliConfig2 } = await cloudApiService.config();
1091
+ return cliConfig2;
1092
+ } catch (e) {
1093
+ ctx.logger.debug("Failed to get cli config", e);
1094
+ return null;
1095
+ }
1096
+ }
1097
+ const action$3 = async (ctx) => {
1041
1098
  const { getValidToken } = await tokenServiceFactory(ctx);
1042
1099
  const token = await getValidToken(ctx, promptLogin);
1043
1100
  if (!token) {
@@ -1048,14 +1105,18 @@ const action$1 = async (ctx) => {
1048
1105
  return;
1049
1106
  }
1050
1107
  const cloudApiService = await cloudApiFactory(ctx);
1051
- try {
1052
- await cloudApiService.track("willDeployWithCLI", { projectInternalName: project.name });
1053
- } catch (e) {
1054
- ctx.logger.debug("Failed to track willDeploy", e);
1055
- }
1108
+ await trackEvent(ctx, cloudApiService, "willDeployWithCLI", {
1109
+ projectInternalName: project.name
1110
+ });
1056
1111
  const notificationService = notificationServiceFactory(ctx);
1057
1112
  const buildLogsService = buildLogsServiceFactory(ctx);
1058
- const { data: cliConfig2 } = await cloudApiService.config();
1113
+ const cliConfig2 = await getConfig({ ctx, cloudApiService });
1114
+ if (!cliConfig2) {
1115
+ ctx.logger.error(
1116
+ "An error occurred while retrieving data from Strapi Cloud. Please try check your network or again later."
1117
+ );
1118
+ return;
1119
+ }
1059
1120
  let maxSize = parseInt(cliConfig2.maxProjectFileSize, 10);
1060
1121
  if (Number.isNaN(maxSize)) {
1061
1122
  ctx.logger.debug(
@@ -1077,10 +1138,11 @@ const action$1 = async (ctx) => {
1077
1138
  chalk.underline(`${apiConfig.dashboardBaseUrl}/projects/${project.name}/deployments`)
1078
1139
  );
1079
1140
  } catch (e) {
1141
+ ctx.logger.debug(e);
1080
1142
  if (e instanceof Error) {
1081
1143
  ctx.logger.error(e.message);
1082
1144
  } else {
1083
- throw e;
1145
+ ctx.logger.error("An error occurred while deploying the project. Please try again later.");
1084
1146
  }
1085
1147
  }
1086
1148
  };
@@ -1111,16 +1173,162 @@ const runAction = (name2, action2) => (...args) => {
1111
1173
  process.exit(1);
1112
1174
  });
1113
1175
  };
1114
- const command$3 = ({ command: command2, ctx }) => {
1115
- 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$1)(ctx));
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));
1116
1178
  };
1117
1179
  const deployProject = {
1118
1180
  name: "deploy-project",
1119
1181
  description: "Deploy a Strapi Cloud project",
1120
- action: action$1,
1121
- command: command$3
1182
+ action: action$3,
1183
+ command: command$5
1122
1184
  };
1123
- const command$2 = ({ command: command2, ctx }) => {
1185
+ const QUIT_OPTION = "Quit";
1186
+ async function getExistingConfig(ctx) {
1187
+ try {
1188
+ return await retrieve();
1189
+ } catch (e) {
1190
+ ctx.logger.debug("Failed to get project config", e);
1191
+ ctx.logger.error("An error occurred while retrieving config data from your local project.");
1192
+ return null;
1193
+ }
1194
+ }
1195
+ async function promptForRelink(ctx, cloudApiService, existingConfig) {
1196
+ if (existingConfig && existingConfig.project) {
1197
+ const { shouldRelink } = await inquirer.prompt([
1198
+ {
1199
+ type: "confirm",
1200
+ name: "shouldRelink",
1201
+ message: `A project named ${chalk.cyan(
1202
+ existingConfig.project.displayName ? existingConfig.project.displayName : existingConfig.project.name
1203
+ )} is already linked to this local folder. Do you want to update the link?`,
1204
+ default: false
1205
+ }
1206
+ ]);
1207
+ if (!shouldRelink) {
1208
+ await trackEvent(ctx, cloudApiService, "didNotLinkProject", {
1209
+ currentProjectName: existingConfig.project?.name
1210
+ });
1211
+ return false;
1212
+ }
1213
+ }
1214
+ return true;
1215
+ }
1216
+ async function getProjectsList(ctx, cloudApiService, existingConfig) {
1217
+ const spinner = ctx.logger.spinner("Fetching your projects...\n").start();
1218
+ try {
1219
+ const {
1220
+ data: { data: projectList }
1221
+ } = await cloudApiService.listLinkProjects();
1222
+ spinner.succeed();
1223
+ if (!Array.isArray(projectList)) {
1224
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1225
+ return null;
1226
+ }
1227
+ const projects = projectList.filter(
1228
+ (project) => !(project.isMaintainer || project.name === existingConfig?.project?.name)
1229
+ ).map((project) => {
1230
+ return {
1231
+ name: project.displayName,
1232
+ value: { name: project.name, displayName: project.displayName }
1233
+ };
1234
+ });
1235
+ if (projects.length === 0) {
1236
+ ctx.logger.log("We couldn't find any projects available for linking in Strapi Cloud");
1237
+ return null;
1238
+ }
1239
+ return projects;
1240
+ } catch (e) {
1241
+ spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1242
+ ctx.logger.debug("Failed to list projects", e);
1243
+ return null;
1244
+ }
1245
+ }
1246
+ async function getUserSelection(ctx, projects) {
1247
+ const { logger } = ctx;
1248
+ try {
1249
+ const answer = await inquirer.prompt([
1250
+ {
1251
+ type: "list",
1252
+ name: "linkProject",
1253
+ message: "Which project do you want to link?",
1254
+ choices: [...projects, { name: chalk.grey(`(${QUIT_OPTION})`), value: null }]
1255
+ }
1256
+ ]);
1257
+ if (!answer.linkProject) {
1258
+ return null;
1259
+ }
1260
+ return answer;
1261
+ } catch (e) {
1262
+ logger.debug("Failed to get user input", e);
1263
+ logger.error("An error occurred while trying to get your input.");
1264
+ return null;
1265
+ }
1266
+ }
1267
+ const action$2 = async (ctx) => {
1268
+ const { getValidToken } = await tokenServiceFactory(ctx);
1269
+ const token = await getValidToken(ctx, promptLogin);
1270
+ const { logger } = ctx;
1271
+ if (!token) {
1272
+ return;
1273
+ }
1274
+ const cloudApiService = await cloudApiFactory(ctx, token);
1275
+ const existingConfig = await getExistingConfig(ctx);
1276
+ const shouldRelink = await promptForRelink(ctx, cloudApiService, existingConfig);
1277
+ if (!shouldRelink) {
1278
+ return;
1279
+ }
1280
+ await trackEvent(ctx, cloudApiService, "willLinkProject", {});
1281
+ const projects = await getProjectsList(
1282
+ ctx,
1283
+ cloudApiService,
1284
+ existingConfig
1285
+ );
1286
+ if (!projects) {
1287
+ return;
1288
+ }
1289
+ const answer = await getUserSelection(ctx, projects);
1290
+ if (!answer) {
1291
+ return;
1292
+ }
1293
+ try {
1294
+ const { confirmAction } = await inquirer.prompt([
1295
+ {
1296
+ type: "confirm",
1297
+ name: "confirmAction",
1298
+ message: "Warning: Once linked, deploying from CLI will replace the existing project and its data. Confirm to proceed:",
1299
+ default: false
1300
+ }
1301
+ ]);
1302
+ if (!confirmAction) {
1303
+ await trackEvent(ctx, cloudApiService, "didNotLinkProject", {
1304
+ cancelledProjectName: answer.linkProject.name,
1305
+ currentProjectName: existingConfig ? existingConfig.project?.name : null
1306
+ });
1307
+ return;
1308
+ }
1309
+ await save({ project: answer.linkProject });
1310
+ logger.log(`Project ${chalk.cyan(answer.linkProject.displayName)} linked successfully.`);
1311
+ await trackEvent(ctx, cloudApiService, "didLinkProject", {
1312
+ projectInternalName: answer.linkProject
1313
+ });
1314
+ } catch (e) {
1315
+ logger.debug("Failed to link project", e);
1316
+ logger.error("An error occurred while linking the project.");
1317
+ await trackEvent(ctx, cloudApiService, "didNotLinkProject", {
1318
+ projectInternalName: answer.linkProject
1319
+ });
1320
+ }
1321
+ };
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));
1324
+ };
1325
+ const link = {
1326
+ name: "link-project",
1327
+ description: "Link a local directory to a Strapi Cloud project",
1328
+ action: action$2,
1329
+ command: command$4
1330
+ };
1331
+ const command$3 = ({ command: command2, ctx }) => {
1124
1332
  command2.command("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
1125
1333
  "after",
1126
1334
  "\nAfter running this command, you will be prompted to enter your authentication information."
@@ -1130,10 +1338,10 @@ const login = {
1130
1338
  name: "login",
1131
1339
  description: "Strapi Cloud Login",
1132
1340
  action: loginAction,
1133
- command: command$2
1341
+ command: command$3
1134
1342
  };
1135
1343
  const openModule = import("open");
1136
- const action = async (ctx) => {
1344
+ const action$1 = async (ctx) => {
1137
1345
  const { logger } = ctx;
1138
1346
  const { retrieveToken, eraseToken } = await tokenServiceFactory(ctx);
1139
1347
  const token = await retrieveToken();
@@ -1163,37 +1371,64 @@ const action = async (ctx) => {
1163
1371
  logger.error("🥲 Oops! Something went wrong while logging you out. Please try again.");
1164
1372
  logger.debug(e);
1165
1373
  }
1166
- try {
1167
- await cloudApiService.track("didLogout", { loginMethod: "cli" });
1168
- } catch (e) {
1169
- logger.debug("Failed to track logout event", e);
1170
- }
1374
+ await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
1171
1375
  };
1172
- const command$1 = ({ command: command2, ctx }) => {
1173
- 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)(ctx));
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));
1174
1378
  };
1175
1379
  const logout = {
1176
1380
  name: "logout",
1177
1381
  description: "Strapi Cloud Logout",
1178
- action,
1179
- command: command$1
1382
+ action: action$1,
1383
+ command: command$2
1180
1384
  };
1181
- const command = ({ command: command2, ctx }) => {
1182
- 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$2)(ctx));
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));
1183
1387
  };
1184
1388
  const createProject = {
1185
1389
  name: "create-project",
1186
1390
  description: "Create a new project",
1187
- action: action$2,
1391
+ action: action$4,
1392
+ command: command$1
1393
+ };
1394
+ const action = async (ctx) => {
1395
+ const { getValidToken } = await tokenServiceFactory(ctx);
1396
+ const token = await getValidToken(ctx, promptLogin);
1397
+ const { logger } = ctx;
1398
+ if (!token) {
1399
+ return;
1400
+ }
1401
+ const cloudApiService = await cloudApiFactory(ctx, token);
1402
+ const spinner = logger.spinner("Fetching your projects...").start();
1403
+ try {
1404
+ const {
1405
+ data: { data: projectList }
1406
+ } = await cloudApiService.listProjects();
1407
+ spinner.succeed();
1408
+ logger.log(projectList);
1409
+ } catch (e) {
1410
+ ctx.logger.debug("Failed to list projects", e);
1411
+ spinner.fail("An error occurred while fetching your projects from Strapi Cloud.");
1412
+ }
1413
+ };
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));
1416
+ };
1417
+ const listProjects = {
1418
+ name: "list-projects",
1419
+ description: "List Strapi Cloud projects",
1420
+ action,
1188
1421
  command
1189
1422
  };
1190
1423
  const cli = {
1191
1424
  deployProject,
1425
+ link,
1192
1426
  login,
1193
1427
  logout,
1194
- createProject
1428
+ createProject,
1429
+ listProjects
1195
1430
  };
1196
- const cloudCommands = [deployProject, login, logout];
1431
+ const cloudCommands = [deployProject, link, login, logout, listProjects];
1197
1432
  async function initCloudCLIConfig() {
1198
1433
  const localConfig = await getLocalConfig();
1199
1434
  if (!localConfig.deviceId) {