@strapi/cloud-cli 0.0.0-next.b11829e6c6aacb45bc1ec2f341609d04d88080d2 → 0.0.0-next.d54a672641d83234230d3d80d92aaf87f60f8c75
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +18 -3
- package/dist/index.js +578 -293
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +577 -290
- package/dist/index.mjs.map +1 -1
- package/dist/src/create-project/action.d.ts +1 -1
- package/dist/src/create-project/action.d.ts.map +1 -1
- package/dist/src/create-project/utils/apply-default-name.d.ts +7 -0
- package/dist/src/create-project/utils/apply-default-name.d.ts.map +1 -0
- package/dist/src/create-project/utils/get-project-name-from-pkg.d.ts +3 -0
- package/dist/src/create-project/utils/get-project-name-from-pkg.d.ts.map +1 -0
- package/dist/src/deploy-project/action.d.ts.map +1 -1
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/link/action.d.ts +4 -0
- package/dist/src/link/action.d.ts.map +1 -0
- package/dist/src/link/command.d.ts +7 -0
- package/dist/src/link/command.d.ts.map +1 -0
- package/dist/src/link/index.d.ts +7 -0
- package/dist/src/link/index.d.ts.map +1 -0
- package/dist/src/list-projects/action.d.ts +4 -0
- package/dist/src/list-projects/action.d.ts.map +1 -0
- package/dist/src/list-projects/command.d.ts +7 -0
- package/dist/src/list-projects/command.d.ts.map +1 -0
- package/dist/src/list-projects/index.d.ts +7 -0
- package/dist/src/list-projects/index.d.ts.map +1 -0
- package/dist/src/login/action.d.ts +2 -2
- package/dist/src/login/action.d.ts.map +1 -1
- package/dist/src/logout/action.d.ts.map +1 -1
- package/dist/src/services/build-logs.d.ts.map +1 -1
- package/dist/src/services/cli-api.d.ts +22 -8
- package/dist/src/services/cli-api.d.ts.map +1 -1
- package/dist/src/services/strapi-info-save.d.ts +1 -1
- package/dist/src/services/strapi-info-save.d.ts.map +1 -1
- package/dist/src/services/token.d.ts +1 -1
- package/dist/src/services/token.d.ts.map +1 -1
- package/dist/src/types.d.ts +1 -0
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/utils/analytics.d.ts +4 -0
- package/dist/src/utils/analytics.d.ts.map +1 -0
- package/dist/src/utils/compress-files.d.ts.map +1 -1
- package/dist/src/utils/pkg.d.ts.map +1 -1
- package/package.json +6 -5
- package/dist/src/utils/tests/compress-files.test.d.ts +0 -2
- 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
|
|
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
|
-
|
|
80
|
+
const pathExist = await fse.pathExists(gitignorePath);
|
|
81
|
+
if (!pathExist)
|
|
80
82
|
return [];
|
|
81
|
-
const gitignoreContent =
|
|
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
|
|
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
|
|
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
|
|
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
|
|
124
|
+
await fse__default.ensureFile(configFilePath);
|
|
123
125
|
try {
|
|
124
|
-
return await
|
|
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
|
|
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.
|
|
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.
|
|
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.
|
|
205
|
-
tsconfig: "4.25.
|
|
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",
|
|
@@ -231,7 +234,7 @@ const packageJson = {
|
|
|
231
234
|
engines
|
|
232
235
|
};
|
|
233
236
|
const VERSION = "v1";
|
|
234
|
-
async function cloudApiFactory(token) {
|
|
237
|
+
async function cloudApiFactory({ logger }, token) {
|
|
235
238
|
const localConfig = await getLocalConfig();
|
|
236
239
|
const customHeaders = {
|
|
237
240
|
"x-device-id": localConfig.deviceId,
|
|
@@ -255,7 +258,7 @@ async function cloudApiFactory(token) {
|
|
|
255
258
|
deploy({ filePath, project }, { onUploadProgress }) {
|
|
256
259
|
return axiosCloudAPI.post(
|
|
257
260
|
`/deploy/${project.name}`,
|
|
258
|
-
{ file:
|
|
261
|
+
{ file: fse__default.createReadStream(filePath) },
|
|
259
262
|
{
|
|
260
263
|
headers: {
|
|
261
264
|
"Content-Type": "multipart/form-data"
|
|
@@ -284,11 +287,47 @@ async function cloudApiFactory(token) {
|
|
|
284
287
|
getUserInfo() {
|
|
285
288
|
return axiosCloudAPI.get("/user");
|
|
286
289
|
},
|
|
287
|
-
config() {
|
|
288
|
-
|
|
290
|
+
async config() {
|
|
291
|
+
try {
|
|
292
|
+
const response = await axiosCloudAPI.get("/config");
|
|
293
|
+
if (response.status !== 200) {
|
|
294
|
+
throw new Error("Error fetching cloud CLI config from the server.");
|
|
295
|
+
}
|
|
296
|
+
return response;
|
|
297
|
+
} catch (error) {
|
|
298
|
+
logger.debug(
|
|
299
|
+
"🥲 Oops! Couldn't retrieve the cloud CLI config from the server. Please try again."
|
|
300
|
+
);
|
|
301
|
+
throw error;
|
|
302
|
+
}
|
|
303
|
+
},
|
|
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
|
+
}
|
|
289
317
|
},
|
|
290
|
-
|
|
291
|
-
|
|
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
|
+
}
|
|
292
331
|
},
|
|
293
332
|
track(event, payload = {}) {
|
|
294
333
|
return axiosCloudAPI.post("/track", {
|
|
@@ -303,18 +342,18 @@ async function save(data, { directoryPath } = {}) {
|
|
|
303
342
|
const alreadyInFileData = await retrieve({ directoryPath });
|
|
304
343
|
const storedData = { ...alreadyInFileData, ...data };
|
|
305
344
|
const pathToFile = path__default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
|
|
306
|
-
await
|
|
307
|
-
await
|
|
345
|
+
await fse__default.ensureDir(path__default.dirname(pathToFile));
|
|
346
|
+
await fse__default.writeJson(pathToFile, storedData, { encoding: "utf8" });
|
|
308
347
|
}
|
|
309
348
|
async function retrieve({
|
|
310
349
|
directoryPath
|
|
311
350
|
} = {}) {
|
|
312
351
|
const pathToFile = path__default.join(directoryPath || process.cwd(), LOCAL_SAVE_FILENAME);
|
|
313
|
-
const pathExists = await
|
|
352
|
+
const pathExists = await fse__default.pathExists(pathToFile);
|
|
314
353
|
if (!pathExists) {
|
|
315
354
|
return {};
|
|
316
355
|
}
|
|
317
|
-
return
|
|
356
|
+
return fse__default.readJSON(pathToFile, { encoding: "utf8" });
|
|
318
357
|
}
|
|
319
358
|
const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
320
359
|
__proto__: null,
|
|
@@ -324,7 +363,7 @@ const strapiInfoSave = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defi
|
|
|
324
363
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
325
364
|
let cliConfig;
|
|
326
365
|
async function tokenServiceFactory({ logger }) {
|
|
327
|
-
const cloudApiService = await cloudApiFactory();
|
|
366
|
+
const cloudApiService = await cloudApiFactory({ logger });
|
|
328
367
|
async function saveToken(str) {
|
|
329
368
|
const appConfig = await getLocalConfig();
|
|
330
369
|
if (!appConfig) {
|
|
@@ -373,14 +412,17 @@ async function tokenServiceFactory({ logger }) {
|
|
|
373
412
|
"There seems to be a problem with your login information. Please try logging in again."
|
|
374
413
|
);
|
|
375
414
|
}
|
|
415
|
+
return Promise.reject(new Error("Invalid token"));
|
|
376
416
|
}
|
|
377
417
|
return new Promise((resolve, reject) => {
|
|
378
418
|
jwt.verify(idToken, getKey, (err) => {
|
|
379
419
|
if (err) {
|
|
380
420
|
reject(err);
|
|
381
|
-
} else {
|
|
382
|
-
resolve();
|
|
383
421
|
}
|
|
422
|
+
if (decodedToken.payload.exp < Math.floor(Date.now() / 1e3)) {
|
|
423
|
+
reject(new Error("Token is expired"));
|
|
424
|
+
}
|
|
425
|
+
resolve();
|
|
384
426
|
});
|
|
385
427
|
});
|
|
386
428
|
}
|
|
@@ -414,15 +456,15 @@ async function tokenServiceFactory({ logger }) {
|
|
|
414
456
|
throw e;
|
|
415
457
|
}
|
|
416
458
|
}
|
|
417
|
-
async function getValidToken() {
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
logger.log(
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
459
|
+
async function getValidToken(ctx, loginAction2) {
|
|
460
|
+
let token = await retrieveToken();
|
|
461
|
+
while (!token || !await isTokenValid(token)) {
|
|
462
|
+
logger.log(
|
|
463
|
+
token ? "Oops! Your token seems expired or invalid. Please login again." : "We couldn't find a valid token. You need to be logged in to use this feature."
|
|
464
|
+
);
|
|
465
|
+
if (!await loginAction2(ctx))
|
|
466
|
+
return null;
|
|
467
|
+
token = await retrieveToken();
|
|
426
468
|
}
|
|
427
469
|
return token;
|
|
428
470
|
}
|
|
@@ -556,17 +598,227 @@ const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.definePropert
|
|
|
556
598
|
local: strapiInfoSave,
|
|
557
599
|
tokenServiceFactory
|
|
558
600
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
559
|
-
|
|
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
|
+
};
|
|
660
|
+
const openModule$1 = import("open");
|
|
661
|
+
async function promptLogin(ctx) {
|
|
662
|
+
const response = await inquirer.prompt([
|
|
663
|
+
{
|
|
664
|
+
type: "confirm",
|
|
665
|
+
name: "login",
|
|
666
|
+
message: "Would you like to login?"
|
|
667
|
+
}
|
|
668
|
+
]);
|
|
669
|
+
if (response.login) {
|
|
670
|
+
const loginSuccessful = await loginAction(ctx);
|
|
671
|
+
return loginSuccessful;
|
|
672
|
+
}
|
|
673
|
+
return false;
|
|
674
|
+
}
|
|
675
|
+
async function loginAction(ctx) {
|
|
676
|
+
const { logger } = ctx;
|
|
560
677
|
const tokenService = await tokenServiceFactory(ctx);
|
|
678
|
+
const existingToken = await tokenService.retrieveToken();
|
|
679
|
+
const cloudApiService = await cloudApiFactory(ctx, existingToken || void 0);
|
|
680
|
+
if (existingToken) {
|
|
681
|
+
const isTokenValid = await tokenService.isTokenValid(existingToken);
|
|
682
|
+
if (isTokenValid) {
|
|
683
|
+
try {
|
|
684
|
+
const userInfo = await cloudApiService.getUserInfo();
|
|
685
|
+
const { email } = userInfo.data.data;
|
|
686
|
+
if (email) {
|
|
687
|
+
logger.log(`You are already logged into your account (${email}).`);
|
|
688
|
+
} else {
|
|
689
|
+
logger.log("You are already logged in.");
|
|
690
|
+
}
|
|
691
|
+
logger.log(
|
|
692
|
+
"To access your dashboard, please copy and paste the following URL into your web browser:"
|
|
693
|
+
);
|
|
694
|
+
logger.log(chalk.underline(`${apiConfig.dashboardBaseUrl}/projects`));
|
|
695
|
+
return true;
|
|
696
|
+
} catch (e) {
|
|
697
|
+
logger.debug("Failed to fetch user info", e);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
let cliConfig2;
|
|
702
|
+
try {
|
|
703
|
+
logger.info("🔌 Connecting to the Strapi Cloud API...");
|
|
704
|
+
const config = await cloudApiService.config();
|
|
705
|
+
cliConfig2 = config.data;
|
|
706
|
+
} catch (e) {
|
|
707
|
+
logger.error("🥲 Oops! Something went wrong while logging you in. Please try again.");
|
|
708
|
+
logger.debug(e);
|
|
709
|
+
return false;
|
|
710
|
+
}
|
|
711
|
+
await trackEvent(ctx, cloudApiService, "willLoginAttempt", {});
|
|
712
|
+
logger.debug("🔐 Creating device authentication request...", {
|
|
713
|
+
client_id: cliConfig2.clientId,
|
|
714
|
+
scope: cliConfig2.scope,
|
|
715
|
+
audience: cliConfig2.audience
|
|
716
|
+
});
|
|
717
|
+
const deviceAuthResponse = await axios.post(cliConfig2.deviceCodeAuthUrl, {
|
|
718
|
+
client_id: cliConfig2.clientId,
|
|
719
|
+
scope: cliConfig2.scope,
|
|
720
|
+
audience: cliConfig2.audience
|
|
721
|
+
}).catch((e) => {
|
|
722
|
+
logger.error("There was an issue with the authentication process. Please try again.");
|
|
723
|
+
if (e.message) {
|
|
724
|
+
logger.debug(e.message, e);
|
|
725
|
+
} else {
|
|
726
|
+
logger.debug(e);
|
|
727
|
+
}
|
|
728
|
+
});
|
|
729
|
+
openModule$1.then((open) => {
|
|
730
|
+
open.default(deviceAuthResponse.data.verification_uri_complete).catch((e) => {
|
|
731
|
+
logger.error("We encountered an issue opening the browser. Please try again later.");
|
|
732
|
+
logger.debug(e.message, e);
|
|
733
|
+
});
|
|
734
|
+
});
|
|
735
|
+
logger.log("If a browser tab does not open automatically, please follow the next steps:");
|
|
736
|
+
logger.log(
|
|
737
|
+
`1. Open this url in your device: ${deviceAuthResponse.data.verification_uri_complete}`
|
|
738
|
+
);
|
|
739
|
+
logger.log(
|
|
740
|
+
`2. Enter the following code: ${deviceAuthResponse.data.user_code} and confirm to login.
|
|
741
|
+
`
|
|
742
|
+
);
|
|
743
|
+
const tokenPayload = {
|
|
744
|
+
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
|
|
745
|
+
device_code: deviceAuthResponse.data.device_code,
|
|
746
|
+
client_id: cliConfig2.clientId
|
|
747
|
+
};
|
|
748
|
+
let isAuthenticated = false;
|
|
749
|
+
const authenticate = async () => {
|
|
750
|
+
const spinner = logger.spinner("Waiting for authentication");
|
|
751
|
+
spinner.start();
|
|
752
|
+
const spinnerFail = () => spinner.fail("Authentication failed!");
|
|
753
|
+
while (!isAuthenticated) {
|
|
754
|
+
try {
|
|
755
|
+
const tokenResponse = await axios.post(cliConfig2.tokenUrl, tokenPayload);
|
|
756
|
+
const authTokenData = tokenResponse.data;
|
|
757
|
+
if (tokenResponse.status === 200) {
|
|
758
|
+
try {
|
|
759
|
+
logger.debug("🔐 Validating token...");
|
|
760
|
+
await tokenService.validateToken(authTokenData.id_token, cliConfig2.jwksUrl);
|
|
761
|
+
logger.debug("🔐 Token validation successful!");
|
|
762
|
+
} catch (e) {
|
|
763
|
+
logger.debug(e);
|
|
764
|
+
spinnerFail();
|
|
765
|
+
throw new Error("Unable to proceed: Token validation failed");
|
|
766
|
+
}
|
|
767
|
+
logger.debug("🔍 Fetching user information...");
|
|
768
|
+
const cloudApiServiceWithToken = await cloudApiFactory(ctx, authTokenData.access_token);
|
|
769
|
+
await cloudApiServiceWithToken.getUserInfo();
|
|
770
|
+
logger.debug("🔍 User information fetched successfully!");
|
|
771
|
+
try {
|
|
772
|
+
logger.debug("📝 Saving login information...");
|
|
773
|
+
await tokenService.saveToken(authTokenData.access_token);
|
|
774
|
+
logger.debug("📝 Login information saved successfully!");
|
|
775
|
+
isAuthenticated = true;
|
|
776
|
+
} catch (e) {
|
|
777
|
+
logger.error(
|
|
778
|
+
"There was a problem saving your login information. Please try logging in again."
|
|
779
|
+
);
|
|
780
|
+
logger.debug(e);
|
|
781
|
+
spinnerFail();
|
|
782
|
+
return false;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
} catch (e) {
|
|
786
|
+
if (e.message === "Unable to proceed: Token validation failed") {
|
|
787
|
+
logger.error(
|
|
788
|
+
"There seems to be a problem with your login information. Please try logging in again."
|
|
789
|
+
);
|
|
790
|
+
spinnerFail();
|
|
791
|
+
await trackEvent(ctx, cloudApiService, "didNotLogin", { loginMethod: "cli" });
|
|
792
|
+
return false;
|
|
793
|
+
}
|
|
794
|
+
if (e.response?.data.error && !["authorization_pending", "slow_down"].includes(e.response.data.error)) {
|
|
795
|
+
logger.debug(e);
|
|
796
|
+
spinnerFail();
|
|
797
|
+
await trackEvent(ctx, cloudApiService, "didNotLogin", { loginMethod: "cli" });
|
|
798
|
+
return false;
|
|
799
|
+
}
|
|
800
|
+
await new Promise((resolve) => {
|
|
801
|
+
setTimeout(resolve, deviceAuthResponse.data.interval * 1e3);
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
spinner.succeed("Authentication successful!");
|
|
806
|
+
logger.log("You are now logged into Strapi Cloud.");
|
|
807
|
+
logger.log(
|
|
808
|
+
"To access your dashboard, please copy and paste the following URL into your web browser:"
|
|
809
|
+
);
|
|
810
|
+
logger.log(chalk.underline(`${apiConfig.dashboardBaseUrl}/projects`));
|
|
811
|
+
await trackEvent(ctx, cloudApiService, "didLogin", { loginMethod: "cli" });
|
|
812
|
+
};
|
|
813
|
+
await authenticate();
|
|
814
|
+
return isAuthenticated;
|
|
815
|
+
}
|
|
816
|
+
async function handleError(ctx, error) {
|
|
561
817
|
const { logger } = ctx;
|
|
562
818
|
logger.debug(error);
|
|
563
819
|
if (error instanceof AxiosError) {
|
|
564
820
|
const errorMessage = typeof error.response?.data === "string" ? error.response.data : null;
|
|
565
821
|
switch (error.response?.status) {
|
|
566
|
-
case 401:
|
|
567
|
-
logger.error("Your session has expired. Please log in again.");
|
|
568
|
-
await tokenService.eraseToken();
|
|
569
|
-
return;
|
|
570
822
|
case 403:
|
|
571
823
|
logger.error(
|
|
572
824
|
errorMessage || "You do not have permission to create a project. Please contact support for assistance."
|
|
@@ -592,28 +844,48 @@ async function handleError(ctx, error) {
|
|
|
592
844
|
"We encountered an issue while creating your project. Please try again in a moment. If the problem persists, contact support for assistance."
|
|
593
845
|
);
|
|
594
846
|
}
|
|
595
|
-
|
|
847
|
+
async function createProject$1(ctx, cloudApi, projectInput) {
|
|
596
848
|
const { logger } = ctx;
|
|
597
|
-
const
|
|
598
|
-
|
|
849
|
+
const spinner = logger.spinner("Setting up your project...").start();
|
|
850
|
+
try {
|
|
851
|
+
const { data } = await cloudApi.createProject(projectInput);
|
|
852
|
+
await save({ project: data });
|
|
853
|
+
spinner.succeed("Project created successfully!");
|
|
854
|
+
return data;
|
|
855
|
+
} catch (e) {
|
|
856
|
+
spinner.fail("An error occurred while creating the project on Strapi Cloud.");
|
|
857
|
+
throw e;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
const action$4 = async (ctx) => {
|
|
861
|
+
const { logger } = ctx;
|
|
862
|
+
const { getValidToken, eraseToken } = await tokenServiceFactory(ctx);
|
|
863
|
+
const token = await getValidToken(ctx, promptLogin);
|
|
599
864
|
if (!token) {
|
|
600
865
|
return;
|
|
601
866
|
}
|
|
602
|
-
const cloudApi = await cloudApiFactory(token);
|
|
867
|
+
const cloudApi = await cloudApiFactory(ctx, token);
|
|
603
868
|
const { data: config } = await cloudApi.config();
|
|
604
|
-
const { questions,
|
|
869
|
+
const { newQuestions: questions, newDefaultValues: defaultValues } = applyDefaultName(
|
|
870
|
+
await getProjectNameFromPackageJson(ctx),
|
|
871
|
+
config.projectCreation.questions,
|
|
872
|
+
config.projectCreation.defaults
|
|
873
|
+
);
|
|
605
874
|
const projectAnswersDefaulted = defaults(defaultValues);
|
|
606
875
|
const projectAnswers = await inquirer.prompt(questions);
|
|
607
876
|
const projectInput = projectAnswersDefaulted(projectAnswers);
|
|
608
|
-
const spinner = logger.spinner("Setting up your project...").start();
|
|
609
877
|
try {
|
|
610
|
-
|
|
611
|
-
await save({ project: data });
|
|
612
|
-
spinner.succeed("Project created successfully!");
|
|
613
|
-
return data;
|
|
878
|
+
return await createProject$1(ctx, cloudApi, projectInput);
|
|
614
879
|
} catch (e) {
|
|
615
|
-
|
|
616
|
-
|
|
880
|
+
if (e instanceof AxiosError && e.response?.status === 401) {
|
|
881
|
+
logger.warn("Oops! Your session has expired. Please log in again to retry.");
|
|
882
|
+
await eraseToken();
|
|
883
|
+
if (await promptLogin(ctx)) {
|
|
884
|
+
return await createProject$1(ctx, cloudApi, projectInput);
|
|
885
|
+
}
|
|
886
|
+
} else {
|
|
887
|
+
await handleError(ctx, e);
|
|
888
|
+
}
|
|
617
889
|
}
|
|
618
890
|
};
|
|
619
891
|
function notificationServiceFactory({ logger }) {
|
|
@@ -647,38 +919,6 @@ function notificationServiceFactory({ logger }) {
|
|
|
647
919
|
};
|
|
648
920
|
};
|
|
649
921
|
}
|
|
650
|
-
yup.object({
|
|
651
|
-
name: yup.string().required(),
|
|
652
|
-
exports: yup.lazy(
|
|
653
|
-
(value) => yup.object(
|
|
654
|
-
typeof value === "object" ? Object.entries(value).reduce((acc, [key, value2]) => {
|
|
655
|
-
if (typeof value2 === "object") {
|
|
656
|
-
acc[key] = yup.object({
|
|
657
|
-
types: yup.string().optional(),
|
|
658
|
-
source: yup.string().required(),
|
|
659
|
-
module: yup.string().optional(),
|
|
660
|
-
import: yup.string().required(),
|
|
661
|
-
require: yup.string().required(),
|
|
662
|
-
default: yup.string().required()
|
|
663
|
-
}).noUnknown(true);
|
|
664
|
-
} else {
|
|
665
|
-
acc[key] = yup.string().matches(/^\.\/.*\.json$/).required();
|
|
666
|
-
}
|
|
667
|
-
return acc;
|
|
668
|
-
}, {}) : void 0
|
|
669
|
-
).optional()
|
|
670
|
-
)
|
|
671
|
-
});
|
|
672
|
-
const loadPkg = async ({ cwd, logger }) => {
|
|
673
|
-
const pkgPath = await pkgUp({ cwd });
|
|
674
|
-
if (!pkgPath) {
|
|
675
|
-
throw new Error("Could not find a package.json in the current directory");
|
|
676
|
-
}
|
|
677
|
-
const buffer = await fs$1.readFile(pkgPath);
|
|
678
|
-
const pkg = JSON.parse(buffer.toString());
|
|
679
|
-
logger.debug("Loaded package.json:", os.EOL, pkg);
|
|
680
|
-
return pkg;
|
|
681
|
-
};
|
|
682
922
|
const buildLogsServiceFactory = ({ logger }) => {
|
|
683
923
|
return async (url, token, cliConfig2) => {
|
|
684
924
|
const CONN_TIMEOUT = Number(cliConfig2.buildLogsConnectionTimeout);
|
|
@@ -732,6 +972,7 @@ const buildLogsServiceFactory = ({ logger }) => {
|
|
|
732
972
|
if (retries > MAX_RETRIES) {
|
|
733
973
|
spinner.fail("We were unable to connect to the server to get build logs at this time.");
|
|
734
974
|
es.close();
|
|
975
|
+
clearExistingTimeout();
|
|
735
976
|
reject(new Error("Max retries reached"));
|
|
736
977
|
}
|
|
737
978
|
};
|
|
@@ -741,7 +982,7 @@ const buildLogsServiceFactory = ({ logger }) => {
|
|
|
741
982
|
};
|
|
742
983
|
};
|
|
743
984
|
async function upload(ctx, project, token, maxProjectFileSize) {
|
|
744
|
-
const cloudApi = await cloudApiFactory(token);
|
|
985
|
+
const cloudApi = await cloudApiFactory(ctx, token);
|
|
745
986
|
try {
|
|
746
987
|
const storagePath = await getTmpStoragePath();
|
|
747
988
|
const projectFolder = path__default.resolve(process.cwd());
|
|
@@ -774,13 +1015,13 @@ async function upload(ctx, project, token, maxProjectFileSize) {
|
|
|
774
1015
|
process.exit(1);
|
|
775
1016
|
}
|
|
776
1017
|
const tarFilePath = path__default.resolve(storagePath, compressedFilename);
|
|
777
|
-
const fileStats = await
|
|
1018
|
+
const fileStats = await fse__default.stat(tarFilePath);
|
|
778
1019
|
if (fileStats.size > maxProjectFileSize) {
|
|
779
1020
|
ctx.logger.log(
|
|
780
1021
|
"Unable to proceed: Your project is too big to be transferred, please use a git repo instead."
|
|
781
1022
|
);
|
|
782
1023
|
try {
|
|
783
|
-
await
|
|
1024
|
+
await fse__default.remove(tarFilePath);
|
|
784
1025
|
} catch (e) {
|
|
785
1026
|
ctx.logger.log("Unable to remove file: ", tarFilePath);
|
|
786
1027
|
ctx.logger.debug(e);
|
|
@@ -819,7 +1060,7 @@ async function upload(ctx, project, token, maxProjectFileSize) {
|
|
|
819
1060
|
}
|
|
820
1061
|
ctx.logger.debug(e);
|
|
821
1062
|
} finally {
|
|
822
|
-
await
|
|
1063
|
+
await fse__default.remove(tarFilePath);
|
|
823
1064
|
}
|
|
824
1065
|
process.exit(0);
|
|
825
1066
|
} catch (e) {
|
|
@@ -832,7 +1073,7 @@ async function getProject(ctx) {
|
|
|
832
1073
|
const { project } = await retrieve();
|
|
833
1074
|
if (!project) {
|
|
834
1075
|
try {
|
|
835
|
-
return await action$
|
|
1076
|
+
return await action$4(ctx);
|
|
836
1077
|
} catch (e) {
|
|
837
1078
|
ctx.logger.error("An error occurred while deploying the project. Please try again later.");
|
|
838
1079
|
ctx.logger.debug(e);
|
|
@@ -841,10 +1082,21 @@ async function getProject(ctx) {
|
|
|
841
1082
|
}
|
|
842
1083
|
return project;
|
|
843
1084
|
}
|
|
844
|
-
|
|
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) => {
|
|
845
1098
|
const { getValidToken } = await tokenServiceFactory(ctx);
|
|
846
|
-
const
|
|
847
|
-
const token = await getValidToken();
|
|
1099
|
+
const token = await getValidToken(ctx, promptLogin);
|
|
848
1100
|
if (!token) {
|
|
849
1101
|
return;
|
|
850
1102
|
}
|
|
@@ -852,14 +1104,19 @@ const action$2 = async (ctx) => {
|
|
|
852
1104
|
if (!project) {
|
|
853
1105
|
return;
|
|
854
1106
|
}
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
}
|
|
1107
|
+
const cloudApiService = await cloudApiFactory(ctx);
|
|
1108
|
+
await trackEvent(ctx, cloudApiService, "willDeployWithCLI", {
|
|
1109
|
+
projectInternalName: project.name
|
|
1110
|
+
});
|
|
860
1111
|
const notificationService = notificationServiceFactory(ctx);
|
|
861
1112
|
const buildLogsService = buildLogsServiceFactory(ctx);
|
|
862
|
-
const
|
|
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
|
+
}
|
|
863
1120
|
let maxSize = parseInt(cliConfig2.maxProjectFileSize, 10);
|
|
864
1121
|
if (Number.isNaN(maxSize)) {
|
|
865
1122
|
ctx.logger.debug(
|
|
@@ -881,10 +1138,11 @@ const action$2 = async (ctx) => {
|
|
|
881
1138
|
chalk.underline(`${apiConfig.dashboardBaseUrl}/projects/${project.name}/deployments`)
|
|
882
1139
|
);
|
|
883
1140
|
} catch (e) {
|
|
1141
|
+
ctx.logger.debug(e);
|
|
884
1142
|
if (e instanceof Error) {
|
|
885
1143
|
ctx.logger.error(e.message);
|
|
886
1144
|
} else {
|
|
887
|
-
|
|
1145
|
+
ctx.logger.error("An error occurred while deploying the project. Please try again later.");
|
|
888
1146
|
}
|
|
889
1147
|
}
|
|
890
1148
|
};
|
|
@@ -915,185 +1173,175 @@ const runAction = (name2, action2) => (...args) => {
|
|
|
915
1173
|
process.exit(1);
|
|
916
1174
|
});
|
|
917
1175
|
};
|
|
918
|
-
const command$
|
|
919
|
-
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$
|
|
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));
|
|
920
1178
|
};
|
|
921
1179
|
const deployProject = {
|
|
922
1180
|
name: "deploy-project",
|
|
923
1181
|
description: "Deploy a Strapi Cloud project",
|
|
924
|
-
action: action$
|
|
925
|
-
command: command$
|
|
1182
|
+
action: action$3,
|
|
1183
|
+
command: command$5
|
|
926
1184
|
};
|
|
927
|
-
const
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
logger.log(`You are already logged into your account (${email}).`);
|
|
948
|
-
} else {
|
|
949
|
-
logger.log("You are already logged in.");
|
|
950
|
-
}
|
|
951
|
-
logger.log(
|
|
952
|
-
"To access your dashboard, please copy and paste the following URL into your web browser:"
|
|
953
|
-
);
|
|
954
|
-
logger.log(chalk.underline(`${apiConfig.dashboardBaseUrl}/projects`));
|
|
955
|
-
return true;
|
|
956
|
-
} catch (e) {
|
|
957
|
-
logger.debug("Failed to fetch user info", e);
|
|
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
|
|
958
1205
|
}
|
|
1206
|
+
]);
|
|
1207
|
+
if (!shouldRelink) {
|
|
1208
|
+
await trackEvent(ctx, cloudApiService, "didNotLinkProject", {
|
|
1209
|
+
currentProjectName: existingConfig.project?.name
|
|
1210
|
+
});
|
|
1211
|
+
return false;
|
|
959
1212
|
}
|
|
960
1213
|
}
|
|
961
|
-
|
|
1214
|
+
return true;
|
|
1215
|
+
}
|
|
1216
|
+
async function getProjectsList(ctx, cloudApiService, existingConfig) {
|
|
1217
|
+
const spinner = ctx.logger.spinner("Fetching your projects...\n").start();
|
|
962
1218
|
try {
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
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;
|
|
966
1240
|
} catch (e) {
|
|
967
|
-
|
|
968
|
-
logger.debug(e);
|
|
969
|
-
return
|
|
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;
|
|
970
1244
|
}
|
|
1245
|
+
}
|
|
1246
|
+
async function getUserSelection(ctx, projects) {
|
|
1247
|
+
const { logger } = ctx;
|
|
971
1248
|
try {
|
|
972
|
-
await
|
|
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;
|
|
973
1261
|
} catch (e) {
|
|
974
|
-
logger.debug("Failed to
|
|
1262
|
+
logger.debug("Failed to get user input", e);
|
|
1263
|
+
logger.error("An error occurred while trying to get your input.");
|
|
1264
|
+
return null;
|
|
975
1265
|
}
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
}
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
logger.error("We encountered an issue opening the browser. Please try again later.");
|
|
996
|
-
logger.debug(e.message, e);
|
|
997
|
-
});
|
|
998
|
-
});
|
|
999
|
-
logger.log("If a browser tab does not open automatically, please follow the next steps:");
|
|
1000
|
-
logger.log(
|
|
1001
|
-
`1. Open this url in your device: ${deviceAuthResponse.data.verification_uri_complete}`
|
|
1002
|
-
);
|
|
1003
|
-
logger.log(
|
|
1004
|
-
`2. Enter the following code: ${deviceAuthResponse.data.user_code} and confirm to login.
|
|
1005
|
-
`
|
|
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
|
|
1006
1285
|
);
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
if (tokenResponse.status === 200) {
|
|
1022
|
-
try {
|
|
1023
|
-
logger.debug("🔐 Validating token...");
|
|
1024
|
-
await tokenService.validateToken(authTokenData.id_token, cliConfig2.jwksUrl);
|
|
1025
|
-
logger.debug("🔐 Token validation successful!");
|
|
1026
|
-
} catch (e) {
|
|
1027
|
-
logger.debug(e);
|
|
1028
|
-
spinnerFail();
|
|
1029
|
-
throw new Error("Unable to proceed: Token validation failed");
|
|
1030
|
-
}
|
|
1031
|
-
logger.debug("🔍 Fetching user information...");
|
|
1032
|
-
const cloudApiServiceWithToken = await cloudApiFactory(authTokenData.access_token);
|
|
1033
|
-
await cloudApiServiceWithToken.getUserInfo();
|
|
1034
|
-
logger.debug("🔍 User information fetched successfully!");
|
|
1035
|
-
try {
|
|
1036
|
-
logger.debug("📝 Saving login information...");
|
|
1037
|
-
await tokenService.saveToken(authTokenData.access_token);
|
|
1038
|
-
logger.debug("📝 Login information saved successfully!");
|
|
1039
|
-
isAuthenticated = true;
|
|
1040
|
-
} catch (e) {
|
|
1041
|
-
logger.error(
|
|
1042
|
-
"There was a problem saving your login information. Please try logging in again."
|
|
1043
|
-
);
|
|
1044
|
-
logger.debug(e);
|
|
1045
|
-
spinnerFail();
|
|
1046
|
-
return false;
|
|
1047
|
-
}
|
|
1048
|
-
}
|
|
1049
|
-
} catch (e) {
|
|
1050
|
-
if (e.message === "Unable to proceed: Token validation failed") {
|
|
1051
|
-
logger.error(
|
|
1052
|
-
"There seems to be a problem with your login information. Please try logging in again."
|
|
1053
|
-
);
|
|
1054
|
-
spinnerFail();
|
|
1055
|
-
await trackFailedLogin();
|
|
1056
|
-
return false;
|
|
1057
|
-
}
|
|
1058
|
-
if (e.response?.data.error && !["authorization_pending", "slow_down"].includes(e.response.data.error)) {
|
|
1059
|
-
logger.debug(e);
|
|
1060
|
-
spinnerFail();
|
|
1061
|
-
await trackFailedLogin();
|
|
1062
|
-
return false;
|
|
1063
|
-
}
|
|
1064
|
-
await new Promise((resolve) => {
|
|
1065
|
-
setTimeout(resolve, deviceAuthResponse.data.interval * 1e3);
|
|
1066
|
-
});
|
|
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
|
|
1067
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;
|
|
1068
1308
|
}
|
|
1069
|
-
|
|
1070
|
-
logger.log(
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
);
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
}
|
|
1080
|
-
}
|
|
1081
|
-
await authenticate();
|
|
1082
|
-
return isAuthenticated;
|
|
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
|
+
}
|
|
1083
1321
|
};
|
|
1084
|
-
const command$
|
|
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 }) => {
|
|
1085
1332
|
command2.command("cloud:login").alias("login").description("Strapi Cloud Login").addHelpText(
|
|
1086
1333
|
"after",
|
|
1087
1334
|
"\nAfter running this command, you will be prompted to enter your authentication information."
|
|
1088
|
-
).option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("login",
|
|
1335
|
+
).option("-d, --debug", "Enable debugging mode with verbose logs").option("-s, --silent", "Don't log anything").action(() => runAction("login", loginAction)(ctx));
|
|
1089
1336
|
};
|
|
1090
1337
|
const login = {
|
|
1091
1338
|
name: "login",
|
|
1092
1339
|
description: "Strapi Cloud Login",
|
|
1093
|
-
action:
|
|
1094
|
-
command: command$
|
|
1340
|
+
action: loginAction,
|
|
1341
|
+
command: command$3
|
|
1095
1342
|
};
|
|
1096
|
-
const
|
|
1343
|
+
const openModule = import("open");
|
|
1344
|
+
const action$1 = async (ctx) => {
|
|
1097
1345
|
const { logger } = ctx;
|
|
1098
1346
|
const { retrieveToken, eraseToken } = await tokenServiceFactory(ctx);
|
|
1099
1347
|
const token = await retrieveToken();
|
|
@@ -1101,9 +1349,21 @@ const action = async (ctx) => {
|
|
|
1101
1349
|
logger.log("You're already logged out.");
|
|
1102
1350
|
return;
|
|
1103
1351
|
}
|
|
1104
|
-
const cloudApiService = await cloudApiFactory(token);
|
|
1352
|
+
const cloudApiService = await cloudApiFactory(ctx, token);
|
|
1353
|
+
const config = await cloudApiService.config();
|
|
1354
|
+
const cliConfig2 = config.data;
|
|
1105
1355
|
try {
|
|
1106
1356
|
await eraseToken();
|
|
1357
|
+
openModule.then((open) => {
|
|
1358
|
+
open.default(
|
|
1359
|
+
`${cliConfig2.baseUrl}/oidc/logout?client_id=${encodeURIComponent(
|
|
1360
|
+
cliConfig2.clientId
|
|
1361
|
+
)}&logout_hint=${encodeURIComponent(token)}
|
|
1362
|
+
`
|
|
1363
|
+
).catch((e) => {
|
|
1364
|
+
logger.debug(e.message, e);
|
|
1365
|
+
});
|
|
1366
|
+
});
|
|
1107
1367
|
logger.log(
|
|
1108
1368
|
"🔌 You have been logged out from the CLI. If you are on a shared computer, please make sure to log out from the Strapi Cloud Dashboard as well."
|
|
1109
1369
|
);
|
|
@@ -1111,37 +1371,64 @@ const action = async (ctx) => {
|
|
|
1111
1371
|
logger.error("🥲 Oops! Something went wrong while logging you out. Please try again.");
|
|
1112
1372
|
logger.debug(e);
|
|
1113
1373
|
}
|
|
1114
|
-
|
|
1115
|
-
await cloudApiService.track("didLogout", { loginMethod: "cli" });
|
|
1116
|
-
} catch (e) {
|
|
1117
|
-
logger.debug("Failed to track logout event", e);
|
|
1118
|
-
}
|
|
1374
|
+
await trackEvent(ctx, cloudApiService, "didLogout", { loginMethod: "cli" });
|
|
1119
1375
|
};
|
|
1120
|
-
const command$
|
|
1121
|
-
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));
|
|
1122
1378
|
};
|
|
1123
1379
|
const logout = {
|
|
1124
1380
|
name: "logout",
|
|
1125
1381
|
description: "Strapi Cloud Logout",
|
|
1126
|
-
action,
|
|
1127
|
-
command: command$
|
|
1382
|
+
action: action$1,
|
|
1383
|
+
command: command$2
|
|
1128
1384
|
};
|
|
1129
|
-
const command = ({ command: command2, ctx }) => {
|
|
1130
|
-
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$
|
|
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));
|
|
1131
1387
|
};
|
|
1132
1388
|
const createProject = {
|
|
1133
1389
|
name: "create-project",
|
|
1134
1390
|
description: "Create a new project",
|
|
1135
|
-
action: action$
|
|
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,
|
|
1136
1421
|
command
|
|
1137
1422
|
};
|
|
1138
1423
|
const cli = {
|
|
1139
1424
|
deployProject,
|
|
1425
|
+
link,
|
|
1140
1426
|
login,
|
|
1141
1427
|
logout,
|
|
1142
|
-
createProject
|
|
1428
|
+
createProject,
|
|
1429
|
+
listProjects
|
|
1143
1430
|
};
|
|
1144
|
-
const cloudCommands = [deployProject, login, logout];
|
|
1431
|
+
const cloudCommands = [deployProject, link, login, logout, listProjects];
|
|
1145
1432
|
async function initCloudCLIConfig() {
|
|
1146
1433
|
const localConfig = await getLocalConfig();
|
|
1147
1434
|
if (!localConfig.deviceId) {
|