@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 CHANGED
@@ -19977,6 +19977,25 @@ const checkNodeVersion = async () => {
19977
19977
  process.exit(1);
19978
19978
  }
19979
19979
  };
19980
+ /**
19981
+ * Get the value of a process argument by key
19982
+ * Example: getArgumentByKey("--email") or getArgumentByKey(["--email", "-e"])
19983
+ * @param key The key to search for in the process arguments
19984
+ * @returns
19985
+ */
19986
+ const getArgumentByKey = (key) => {
19987
+ if (Array.isArray(key)) {
19988
+ for (const k of key) {
19989
+ if (process.argv.includes(k)) {
19990
+ const index = process.argv.indexOf(k);
19991
+ return index !== -1 ? process.argv[index + 1] : undefined;
19992
+ }
19993
+ }
19994
+ return undefined;
19995
+ }
19996
+ const index = process.argv.indexOf(key);
19997
+ return index !== -1 ? process.argv[index + 1] : undefined;
19998
+ };
19980
19999
 
19981
20000
  var build = async () => {
19982
20001
  try {
@@ -20087,29 +20106,57 @@ const inquirerSelect = import('@inquirer/select');
20087
20106
  const YAML_OR_JS_FILES = /^(.*)\.(cube|sc)\.(ya?ml|js)$/;
20088
20107
  let ora$1;
20089
20108
  var push = async () => {
20090
- var _a;
20109
+ var _a, _b;
20091
20110
  let spinnerPushing;
20092
20111
  try {
20093
20112
  checkNodeVersion();
20094
20113
  ora$1 = (await oraP$1).default;
20095
20114
  const config = await provideConfig();
20096
20115
  const token = await verify(config);
20116
+ if (process.argv.includes("--api-key") || process.argv.includes("-k")) {
20117
+ spinnerPushing = ora$1("Using API key...").start();
20118
+ await pushByApiKey(config, spinnerPushing);
20119
+ spinnerPushing.succeed("Published using API key");
20120
+ return;
20121
+ }
20097
20122
  const { workspaceId, name: workspaceName } = await selectWorkspace(config, token);
20098
- const spinnerArchive = ora$1("Building...").start();
20099
- const filesList = await findFiles(config.client.srcDir, YAML_OR_JS_FILES);
20100
- await archive(config, filesList);
20101
- spinnerArchive.succeed("Bundling completed");
20123
+ await buildArchive(config);
20102
20124
  spinnerPushing = ora$1(`Publishing to ${workspaceName} using ${config.pushBaseUrl}...`).start();
20103
20125
  await sendBuild(config, { workspaceId, token });
20104
20126
  spinnerPushing.succeed(`Published to ${workspaceName} using ${config.pushBaseUrl}`);
20105
20127
  }
20106
20128
  catch (error) {
20107
20129
  spinnerPushing === null || spinnerPushing === void 0 ? void 0 : spinnerPushing.fail("Publishing failed");
20108
- console.error(((_a = error.response) === null || _a === void 0 ? void 0 : _a.data) || (error === null || error === void 0 ? void 0 : error.message) || error);
20130
+ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.statusText) === "Unauthorized") {
20131
+ console.error("Unauthorized. Please check your credentials.");
20132
+ }
20133
+ else {
20134
+ console.error(((_b = error.response) === null || _b === void 0 ? void 0 : _b.data) || (error === null || error === void 0 ? void 0 : error.message) || error);
20135
+ }
20109
20136
  await reportErrorToRollbar(error);
20110
20137
  process.exit(1);
20111
20138
  }
20112
20139
  };
20140
+ async function pushByApiKey(config, spinner) {
20141
+ const apiKey = getArgumentByKey(["--api-key", "-k"]);
20142
+ if (!apiKey) {
20143
+ spinner.fail("No API key provided");
20144
+ process.exit(1);
20145
+ }
20146
+ const email = getArgumentByKey(["--email", "-e"]);
20147
+ if (!email || !/\S+@\S+\.\S+/.test(email)) {
20148
+ spinner.fail("Invalid email provided. Please provide a valid email using --email (-e) flag");
20149
+ process.exit(1);
20150
+ }
20151
+ // message is optional
20152
+ const message = getArgumentByKey(["--message", "-m"]);
20153
+ await buildArchive(config);
20154
+ return sendBuildByApiKey(config, {
20155
+ apiKey,
20156
+ email,
20157
+ message,
20158
+ });
20159
+ }
20113
20160
  async function selectWorkspace(ctx, token) {
20114
20161
  const workspaceSpinner = ora$1({
20115
20162
  text: `Fetching workspaces using ${ctx.pushBaseUrl}...`,
@@ -20155,6 +20202,12 @@ async function verify(ctx) {
20155
20202
  }
20156
20203
  return token;
20157
20204
  }
20205
+ async function buildArchive(config) {
20206
+ const spinnerArchive = ora$1("Building...").start();
20207
+ const filesList = await findFiles(config.client.srcDir, YAML_OR_JS_FILES);
20208
+ await archive(config, filesList);
20209
+ return spinnerArchive.succeed("Bundling completed");
20210
+ }
20158
20211
  async function archive(ctx, yamlFiles, includeBuild = true) {
20159
20212
  const output = fs$2.createWriteStream(ctx.client.archiveFile);
20160
20213
  const _archiver = archiver.create("zip", {
@@ -20175,13 +20228,30 @@ async function archive(ctx, yamlFiles, includeBuild = true) {
20175
20228
  output.on("close", resolve);
20176
20229
  });
20177
20230
  }
20231
+ async function sendBuildByApiKey(ctx, { apiKey, email, message }) {
20232
+ var _a;
20233
+ const { FormData, Blob } = await import('formdata-node');
20234
+ const { fileFromPath } = await Promise.resolve().then(function () { return fileFromPath$1; });
20235
+ const file = await fileFromPath(ctx.client.archiveFile, "embeddable-build.zip");
20236
+ const form = new FormData();
20237
+ form.set("file", file, "embeddable-build.zip");
20238
+ const metadataBlob = new Blob([JSON.stringify({ authorEmail: email, description: message })], { type: "application/json" });
20239
+ form.set("metadata", metadataBlob, "metadata.json");
20240
+ const response = await uploadFile(form, `${ctx.pushBaseUrl}/api/v1/bundle/upload`, apiKey);
20241
+ await fs$1.rm(ctx.client.archiveFile);
20242
+ return { bundleId: (_a = response.data) === null || _a === void 0 ? void 0 : _a.bundleId, email, message };
20243
+ }
20178
20244
  async function sendBuild(ctx, { workspaceId, token }) {
20179
20245
  const { FormData } = await import('formdata-node');
20180
20246
  const { fileFromPath } = await Promise.resolve().then(function () { return fileFromPath$1; });
20181
20247
  const file = await fileFromPath(ctx.client.archiveFile, "embeddable-build.zip");
20182
20248
  const form = new FormData();
20183
20249
  form.set("file", file, "embeddable-build.zip");
20184
- await axios.post(`${ctx.pushBaseUrl}/bundle/${workspaceId}/upload`, form, {
20250
+ await uploadFile(form, `${ctx.pushBaseUrl}/bundle/${workspaceId}/upload`, token);
20251
+ await fs$1.rm(ctx.client.archiveFile);
20252
+ }
20253
+ async function uploadFile(formData, url, token) {
20254
+ return axios.post(url, formData, {
20185
20255
  headers: {
20186
20256
  "Content-Type": "multipart/form-data",
20187
20257
  Authorization: `Bearer ${token}`,
@@ -20189,7 +20259,6 @@ async function sendBuild(ctx, { workspaceId, token }) {
20189
20259
  maxContentLength: Infinity,
20190
20260
  maxBodyLength: Infinity,
20191
20261
  });
20192
- await fs$1.rm(ctx.client.archiveFile);
20193
20262
  }
20194
20263
  async function getWorkspaces(ctx, token, workspaceSpinner) {
20195
20264
  var _a;