@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.js CHANGED
@@ -20004,6 +20004,25 @@ const checkNodeVersion = async () => {
20004
20004
  process.exit(1);
20005
20005
  }
20006
20006
  };
20007
+ /**
20008
+ * Get the value of a process argument by key
20009
+ * Example: getArgumentByKey("--email") or getArgumentByKey(["--email", "-e"])
20010
+ * @param key The key to search for in the process arguments
20011
+ * @returns
20012
+ */
20013
+ const getArgumentByKey = (key) => {
20014
+ if (Array.isArray(key)) {
20015
+ for (const k of key) {
20016
+ if (process.argv.includes(k)) {
20017
+ const index = process.argv.indexOf(k);
20018
+ return index !== -1 ? process.argv[index + 1] : undefined;
20019
+ }
20020
+ }
20021
+ return undefined;
20022
+ }
20023
+ const index = process.argv.indexOf(key);
20024
+ return index !== -1 ? process.argv[index + 1] : undefined;
20025
+ };
20007
20026
 
20008
20027
  var build = async () => {
20009
20028
  try {
@@ -20114,29 +20133,57 @@ const inquirerSelect = import('@inquirer/select');
20114
20133
  const YAML_OR_JS_FILES = /^(.*)\.(cube|sc)\.(ya?ml|js)$/;
20115
20134
  let ora$1;
20116
20135
  var push = async () => {
20117
- var _a;
20136
+ var _a, _b;
20118
20137
  let spinnerPushing;
20119
20138
  try {
20120
20139
  checkNodeVersion();
20121
20140
  ora$1 = (await oraP$1).default;
20122
20141
  const config = await provideConfig();
20123
20142
  const token = await verify(config);
20143
+ if (process.argv.includes("--api-key") || process.argv.includes("-k")) {
20144
+ spinnerPushing = ora$1("Using API key...").start();
20145
+ await pushByApiKey(config, spinnerPushing);
20146
+ spinnerPushing.succeed("Published using API key");
20147
+ return;
20148
+ }
20124
20149
  const { workspaceId, name: workspaceName } = await selectWorkspace(config, token);
20125
- const spinnerArchive = ora$1("Building...").start();
20126
- const filesList = await findFiles(config.client.srcDir, YAML_OR_JS_FILES);
20127
- await archive(config, filesList);
20128
- spinnerArchive.succeed("Bundling completed");
20150
+ await buildArchive(config);
20129
20151
  spinnerPushing = ora$1(`Publishing to ${workspaceName} using ${config.pushBaseUrl}...`).start();
20130
20152
  await sendBuild(config, { workspaceId, token });
20131
20153
  spinnerPushing.succeed(`Published to ${workspaceName} using ${config.pushBaseUrl}`);
20132
20154
  }
20133
20155
  catch (error) {
20134
20156
  spinnerPushing === null || spinnerPushing === void 0 ? void 0 : spinnerPushing.fail("Publishing failed");
20135
- console.error(((_a = error.response) === null || _a === void 0 ? void 0 : _a.data) || (error === null || error === void 0 ? void 0 : error.message) || error);
20157
+ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.statusText) === "Unauthorized") {
20158
+ console.error("Unauthorized. Please check your credentials.");
20159
+ }
20160
+ else {
20161
+ console.error(((_b = error.response) === null || _b === void 0 ? void 0 : _b.data) || (error === null || error === void 0 ? void 0 : error.message) || error);
20162
+ }
20136
20163
  await reportErrorToRollbar(error);
20137
20164
  process.exit(1);
20138
20165
  }
20139
20166
  };
20167
+ async function pushByApiKey(config, spinner) {
20168
+ const apiKey = getArgumentByKey(["--api-key", "-k"]);
20169
+ if (!apiKey) {
20170
+ spinner.fail("No API key provided");
20171
+ process.exit(1);
20172
+ }
20173
+ const email = getArgumentByKey(["--email", "-e"]);
20174
+ if (!email || !/\S+@\S+\.\S+/.test(email)) {
20175
+ spinner.fail("Invalid email provided. Please provide a valid email using --email (-e) flag");
20176
+ process.exit(1);
20177
+ }
20178
+ // message is optional
20179
+ const message = getArgumentByKey(["--message", "-m"]);
20180
+ await buildArchive(config);
20181
+ return sendBuildByApiKey(config, {
20182
+ apiKey,
20183
+ email,
20184
+ message,
20185
+ });
20186
+ }
20140
20187
  async function selectWorkspace(ctx, token) {
20141
20188
  const workspaceSpinner = ora$1({
20142
20189
  text: `Fetching workspaces using ${ctx.pushBaseUrl}...`,
@@ -20182,6 +20229,12 @@ async function verify(ctx) {
20182
20229
  }
20183
20230
  return token;
20184
20231
  }
20232
+ async function buildArchive(config) {
20233
+ const spinnerArchive = ora$1("Building...").start();
20234
+ const filesList = await findFiles(config.client.srcDir, YAML_OR_JS_FILES);
20235
+ await archive(config, filesList);
20236
+ return spinnerArchive.succeed("Bundling completed");
20237
+ }
20185
20238
  async function archive(ctx, yamlFiles, includeBuild = true) {
20186
20239
  const output = fs__namespace$1.createWriteStream(ctx.client.archiveFile);
20187
20240
  const _archiver = archiver__namespace.create("zip", {
@@ -20202,13 +20255,30 @@ async function archive(ctx, yamlFiles, includeBuild = true) {
20202
20255
  output.on("close", resolve);
20203
20256
  });
20204
20257
  }
20258
+ async function sendBuildByApiKey(ctx, { apiKey, email, message }) {
20259
+ var _a;
20260
+ const { FormData, Blob } = await import('formdata-node');
20261
+ const { fileFromPath } = await Promise.resolve().then(function () { return fileFromPath$1; });
20262
+ const file = await fileFromPath(ctx.client.archiveFile, "embeddable-build.zip");
20263
+ const form = new FormData();
20264
+ form.set("file", file, "embeddable-build.zip");
20265
+ const metadataBlob = new Blob([JSON.stringify({ authorEmail: email, description: message })], { type: "application/json" });
20266
+ form.set("metadata", metadataBlob, "metadata.json");
20267
+ const response = await uploadFile(form, `${ctx.pushBaseUrl}/api/v1/bundle/upload`, apiKey);
20268
+ await fs__namespace.rm(ctx.client.archiveFile);
20269
+ return { bundleId: (_a = response.data) === null || _a === void 0 ? void 0 : _a.bundleId, email, message };
20270
+ }
20205
20271
  async function sendBuild(ctx, { workspaceId, token }) {
20206
20272
  const { FormData } = await import('formdata-node');
20207
20273
  const { fileFromPath } = await Promise.resolve().then(function () { return fileFromPath$1; });
20208
20274
  const file = await fileFromPath(ctx.client.archiveFile, "embeddable-build.zip");
20209
20275
  const form = new FormData();
20210
20276
  form.set("file", file, "embeddable-build.zip");
20211
- await axios.post(`${ctx.pushBaseUrl}/bundle/${workspaceId}/upload`, form, {
20277
+ await uploadFile(form, `${ctx.pushBaseUrl}/bundle/${workspaceId}/upload`, token);
20278
+ await fs__namespace.rm(ctx.client.archiveFile);
20279
+ }
20280
+ async function uploadFile(formData, url, token) {
20281
+ return axios.post(url, formData, {
20212
20282
  headers: {
20213
20283
  "Content-Type": "multipart/form-data",
20214
20284
  Authorization: `Bearer ${token}`,
@@ -20216,7 +20286,6 @@ async function sendBuild(ctx, { workspaceId, token }) {
20216
20286
  maxContentLength: Infinity,
20217
20287
  maxBodyLength: Infinity,
20218
20288
  });
20219
- await fs__namespace.rm(ctx.client.archiveFile);
20220
20289
  }
20221
20290
  async function getWorkspaces(ctx, token, workspaceSpinner) {
20222
20291
  var _a;