@embeddable.com/sdk-core 3.1.4 → 3.1.5
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/lib/index.esm.js +77 -8
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +77 -8
- package/lib/index.js.map +1 -1
- package/lib/push.d.ts +5 -0
- package/lib/utils.d.ts +7 -0
- package/package.json +1 -1
- package/src/push.ts +97 -12
- package/src/utils.ts +22 -0
package/lib/push.d.ts
CHANGED
|
@@ -2,6 +2,11 @@ export declare const YAML_OR_JS_FILES: RegExp;
|
|
|
2
2
|
declare const _default: () => Promise<void>;
|
|
3
3
|
export default _default;
|
|
4
4
|
export declare function archive(ctx: any, yamlFiles: [string, string][], includeBuild?: boolean): Promise<unknown>;
|
|
5
|
+
export declare function sendBuildByApiKey(ctx: any, { apiKey, email, message }: any): Promise<{
|
|
6
|
+
bundleId: any;
|
|
7
|
+
email: any;
|
|
8
|
+
message: any;
|
|
9
|
+
}>;
|
|
5
10
|
export declare function sendBuild(ctx: any, { workspaceId, token }: {
|
|
6
11
|
workspaceId: string;
|
|
7
12
|
token: string;
|
package/lib/utils.d.ts
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
1
|
export declare const checkNodeVersion: () => Promise<void>;
|
|
2
|
+
/**
|
|
3
|
+
* Get the value of a process argument by key
|
|
4
|
+
* Example: getArgumentByKey("--email") or getArgumentByKey(["--email", "-e"])
|
|
5
|
+
* @param key The key to search for in the process arguments
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
export declare const getArgumentByKey: (key: string | string[]) => string | undefined;
|
package/package.json
CHANGED
package/src/push.ts
CHANGED
|
@@ -10,7 +10,7 @@ import reportErrorToRollbar from "./rollbar.mjs";
|
|
|
10
10
|
|
|
11
11
|
import { findFiles } from "@embeddable.com/sdk-utils";
|
|
12
12
|
import { getToken } from "./login";
|
|
13
|
-
import { checkNodeVersion } from "./utils";
|
|
13
|
+
import { checkNodeVersion, getArgumentByKey } from "./utils";
|
|
14
14
|
|
|
15
15
|
// grab .cube.yml|js and .sc.yml|js files
|
|
16
16
|
export const YAML_OR_JS_FILES = /^(.*)\.(cube|sc)\.(ya?ml|js)$/;
|
|
@@ -27,18 +27,21 @@ export default async () => {
|
|
|
27
27
|
|
|
28
28
|
const token = await verify(config);
|
|
29
29
|
|
|
30
|
+
if (process.argv.includes("--api-key") || process.argv.includes("-k")) {
|
|
31
|
+
spinnerPushing = ora("Using API key...").start();
|
|
32
|
+
await pushByApiKey(config, spinnerPushing);
|
|
33
|
+
|
|
34
|
+
spinnerPushing.succeed("Published using API key");
|
|
35
|
+
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
30
39
|
const { workspaceId, name: workspaceName } = await selectWorkspace(
|
|
31
40
|
config,
|
|
32
41
|
token,
|
|
33
42
|
);
|
|
34
43
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const filesList = await findFiles(config.client.srcDir, YAML_OR_JS_FILES);
|
|
38
|
-
|
|
39
|
-
await archive(config, filesList);
|
|
40
|
-
spinnerArchive.succeed("Bundling completed");
|
|
41
|
-
|
|
44
|
+
await buildArchive(config);
|
|
42
45
|
spinnerPushing = ora(
|
|
43
46
|
`Publishing to ${workspaceName} using ${config.pushBaseUrl}...`,
|
|
44
47
|
).start();
|
|
@@ -49,12 +52,46 @@ export default async () => {
|
|
|
49
52
|
);
|
|
50
53
|
} catch (error: any) {
|
|
51
54
|
spinnerPushing?.fail("Publishing failed");
|
|
52
|
-
|
|
55
|
+
if (error.response?.statusText === "Unauthorized") {
|
|
56
|
+
console.error("Unauthorized. Please check your credentials.");
|
|
57
|
+
} else {
|
|
58
|
+
console.error(error.response?.data || error?.message || error);
|
|
59
|
+
}
|
|
60
|
+
|
|
53
61
|
await reportErrorToRollbar(error);
|
|
54
62
|
process.exit(1);
|
|
55
63
|
}
|
|
56
64
|
};
|
|
57
65
|
|
|
66
|
+
async function pushByApiKey(config: any, spinner: any) {
|
|
67
|
+
const apiKey = getArgumentByKey(["--api-key", "-k"]);
|
|
68
|
+
|
|
69
|
+
if (!apiKey) {
|
|
70
|
+
spinner.fail("No API key provided");
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const email = getArgumentByKey(["--email", "-e"]);
|
|
75
|
+
|
|
76
|
+
if (!email || !/\S+@\S+\.\S+/.test(email)) {
|
|
77
|
+
spinner.fail(
|
|
78
|
+
"Invalid email provided. Please provide a valid email using --email (-e) flag",
|
|
79
|
+
);
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// message is optional
|
|
84
|
+
const message = getArgumentByKey(["--message", "-m"]);
|
|
85
|
+
|
|
86
|
+
await buildArchive(config);
|
|
87
|
+
|
|
88
|
+
return sendBuildByApiKey(config, {
|
|
89
|
+
apiKey,
|
|
90
|
+
email,
|
|
91
|
+
message,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
58
95
|
async function selectWorkspace(ctx: any, token: string) {
|
|
59
96
|
const workspaceSpinner = ora({
|
|
60
97
|
text: `Fetching workspaces using ${ctx.pushBaseUrl}...`,
|
|
@@ -112,6 +149,15 @@ async function verify(ctx: any) {
|
|
|
112
149
|
return token;
|
|
113
150
|
}
|
|
114
151
|
|
|
152
|
+
async function buildArchive(config: any) {
|
|
153
|
+
const spinnerArchive = ora("Building...").start();
|
|
154
|
+
|
|
155
|
+
const filesList = await findFiles(config.client.srcDir, YAML_OR_JS_FILES);
|
|
156
|
+
|
|
157
|
+
await archive(config, filesList);
|
|
158
|
+
return spinnerArchive.succeed("Bundling completed");
|
|
159
|
+
}
|
|
160
|
+
|
|
115
161
|
export async function archive(
|
|
116
162
|
ctx: any,
|
|
117
163
|
yamlFiles: [string, string][],
|
|
@@ -143,6 +189,37 @@ export async function archive(
|
|
|
143
189
|
});
|
|
144
190
|
}
|
|
145
191
|
|
|
192
|
+
export async function sendBuildByApiKey(
|
|
193
|
+
ctx: any,
|
|
194
|
+
{ apiKey, email, message }: any,
|
|
195
|
+
) {
|
|
196
|
+
const { FormData, Blob } = await import("formdata-node");
|
|
197
|
+
const { fileFromPath } = await import("formdata-node/file-from-path");
|
|
198
|
+
|
|
199
|
+
const file = await fileFromPath(
|
|
200
|
+
ctx.client.archiveFile,
|
|
201
|
+
"embeddable-build.zip",
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
const form = new FormData();
|
|
205
|
+
form.set("file", file, "embeddable-build.zip");
|
|
206
|
+
|
|
207
|
+
const metadataBlob = new Blob(
|
|
208
|
+
[JSON.stringify({ authorEmail: email, description: message })],
|
|
209
|
+
{ type: "application/json" },
|
|
210
|
+
);
|
|
211
|
+
form.set("metadata", metadataBlob, "metadata.json");
|
|
212
|
+
|
|
213
|
+
const response = await uploadFile(
|
|
214
|
+
form,
|
|
215
|
+
`${ctx.pushBaseUrl}/api/v1/bundle/upload`,
|
|
216
|
+
apiKey,
|
|
217
|
+
);
|
|
218
|
+
await fs.rm(ctx.client.archiveFile);
|
|
219
|
+
|
|
220
|
+
return { bundleId: response.data?.bundleId, email, message };
|
|
221
|
+
}
|
|
222
|
+
|
|
146
223
|
export async function sendBuild(
|
|
147
224
|
ctx: any,
|
|
148
225
|
{ workspaceId, token }: { workspaceId: string; token: string },
|
|
@@ -158,7 +235,17 @@ export async function sendBuild(
|
|
|
158
235
|
const form = new FormData();
|
|
159
236
|
form.set("file", file, "embeddable-build.zip");
|
|
160
237
|
|
|
161
|
-
await
|
|
238
|
+
await uploadFile(
|
|
239
|
+
form,
|
|
240
|
+
`${ctx.pushBaseUrl}/bundle/${workspaceId}/upload`,
|
|
241
|
+
token,
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
await fs.rm(ctx.client.archiveFile);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function uploadFile(formData: any, url: string, token: string) {
|
|
248
|
+
return axios.post(url, formData, {
|
|
162
249
|
headers: {
|
|
163
250
|
"Content-Type": "multipart/form-data",
|
|
164
251
|
Authorization: `Bearer ${token}`,
|
|
@@ -166,8 +253,6 @@ export async function sendBuild(
|
|
|
166
253
|
maxContentLength: Infinity,
|
|
167
254
|
maxBodyLength: Infinity,
|
|
168
255
|
});
|
|
169
|
-
|
|
170
|
-
await fs.rm(ctx.client.archiveFile);
|
|
171
256
|
}
|
|
172
257
|
|
|
173
258
|
async function getWorkspaces(ctx: any, token: string, workspaceSpinner: any) {
|
package/src/utils.ts
CHANGED
|
@@ -22,3 +22,25 @@ export const checkNodeVersion = async () => {
|
|
|
22
22
|
process.exit(1);
|
|
23
23
|
}
|
|
24
24
|
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get the value of a process argument by key
|
|
28
|
+
* Example: getArgumentByKey("--email") or getArgumentByKey(["--email", "-e"])
|
|
29
|
+
* @param key The key to search for in the process arguments
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
export const getArgumentByKey = (key: string | string[]) => {
|
|
33
|
+
if (Array.isArray(key)) {
|
|
34
|
+
for (const k of key) {
|
|
35
|
+
if (process.argv.includes(k)) {
|
|
36
|
+
const index = process.argv.indexOf(k);
|
|
37
|
+
return index !== -1 ? process.argv[index + 1] : undefined;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const index = process.argv.indexOf(key);
|
|
45
|
+
return index !== -1 ? process.argv[index + 1] : undefined;
|
|
46
|
+
};
|