@embeddable.com/sdk-core 2.4.14 → 2.4.15

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.
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "embeddable-wrapper",
3
+ "version": "0.0.1",
4
+ "main": "dist/index.cjs.js",
5
+ "module": "dist/index.js",
6
+ "es2015": "dist/esm/index.mjs",
7
+ "es2017": "dist/esm/index.mjs",
8
+ "types": "dist/types/index.d.ts",
9
+ "collection": "dist/collection/collection-manifest.json",
10
+ "collection:main": "dist/collection/index.js",
11
+ "unpkg": "dist/embeddable/embeddable.esm.js",
12
+ "license": "MIT"
13
+ }
@@ -1,2 +1,4 @@
1
+ export declare const EMB_TYPE_FILE_REGEX: RegExp;
2
+ export declare const EMB_OPTIONS_FILE_REGEX: RegExp;
1
3
  declare const _default: (ctx: any) => Promise<void>;
2
4
  export default _default;
@@ -12,8 +12,9 @@ export type EmbeddableConfig = {
12
12
  errorFallbackComponent?: string;
13
13
  applicationEnvironment?: string;
14
14
  rollbarAccessToken?: string;
15
+ previewBaseUrl?: string;
15
16
  };
16
- declare const _default: ({ plugins, pushBaseUrl, audienceUrl, authDomain, authClientId, errorFallbackComponent, applicationEnvironment, rollbarAccessToken, }: EmbeddableConfig) => {
17
+ declare const _default: ({ plugins, pushBaseUrl, audienceUrl, authDomain, authClientId, errorFallbackComponent, applicationEnvironment, rollbarAccessToken, previewBaseUrl, }: EmbeddableConfig) => {
17
18
  core: {
18
19
  rootDir: string;
19
20
  templatesDir: string;
@@ -34,6 +35,7 @@ declare const _default: ({ plugins, pushBaseUrl, audienceUrl, authDomain, authCl
34
35
  };
35
36
  pushBaseUrl: string;
36
37
  audienceUrl: string;
38
+ previewBaseUrl: string;
37
39
  authDomain: string;
38
40
  authClientId: string;
39
41
  applicationEnvironment: string;
package/lib/dev.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ declare const _default: () => Promise<void>;
2
+ export default _default;
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { default as build } from "./build";
2
2
  export { default as login } from "./login";
3
3
  export { default as push } from "./push";
4
+ export { default as dev } from "./dev";
4
5
  export { default as defineConfig } from "./defineConfig";
package/lib/index.esm.js CHANGED
@@ -8,10 +8,10 @@ import * as fsSync from 'node:fs';
8
8
  import { createNodeLogger, createNodeSys } from '@stencil/core/sys/node';
9
9
  import { loadConfig, createCompiler } from '@stencil/core/compiler';
10
10
  import * as YAML from 'yaml';
11
- import * as require$$1 from 'path';
12
- import require$$1__default, { basename } from 'path';
11
+ import * as path$2 from 'path';
12
+ import path__default, { basename } from 'path';
13
13
  import require$$4$1 from 'util';
14
- import require$$1$1 from 'os';
14
+ import require$$1 from 'os';
15
15
  import require$$3 from 'http';
16
16
  import require$$4 from 'https';
17
17
  import require$$0$1 from 'url';
@@ -19,6 +19,8 @@ import require$$2$1, { createReadStream } from 'fs';
19
19
  import * as os$1 from 'node:os';
20
20
  import axios from 'axios';
21
21
  import * as archiver from 'archiver';
22
+ import { WebSocketServer } from 'ws';
23
+ import * as chokidar from 'chokidar';
22
24
  import { stat } from 'fs/promises';
23
25
 
24
26
  var findFiles = async (initialSrcDir, regex) => {
@@ -333,11 +335,11 @@ const formatErrorPath = (path) => {
333
335
  return formatted;
334
336
  };
335
337
 
336
- const oraP$3 = import('ora');
338
+ const oraP$4 = import('ora');
337
339
  const EMB_TYPE_FILE_REGEX = /^(.*)\.type\.emb\.[jt]s$/;
338
340
  const EMB_OPTIONS_FILE_REGEX = /^(.*)\.options\.emb\.[jt]s$/;
339
341
  var buildTypes = async (ctx) => {
340
- const ora = (await oraP$3).default;
342
+ const ora = (await oraP$4).default;
341
343
  const progress = ora("building types...").start();
342
344
  await generate$1(ctx);
343
345
  await build$1(ctx);
@@ -354,17 +356,16 @@ async function generate$1(ctx) {
354
356
  await fs$1.writeFile(path$1.resolve(ctx.client.rootDir, ctx.outputOptions.typesEntryPointFilename), typeImports);
355
357
  }
356
358
  async function build$1(ctx) {
357
- process.chdir(ctx.client.rootDir);
358
359
  await vite.build({
359
360
  logLevel: "error",
360
361
  build: {
361
362
  emptyOutDir: false,
362
363
  lib: {
363
- entry: `./${ctx.outputOptions.typesEntryPointFilename}`,
364
+ entry: path$1.resolve(ctx.client.rootDir, ctx.outputOptions.typesEntryPointFilename),
364
365
  formats: ["es"],
365
366
  fileName: "embeddable-types",
366
367
  },
367
- outDir: ".embeddable-build/",
368
+ outDir: ctx.client.buildDir,
368
369
  },
369
370
  });
370
371
  }
@@ -413,15 +414,21 @@ async function injectBundleRender(ctx, pluginName) {
413
414
  await fs$1.writeFile(path$1.resolve(ctx.client.componentDir, "component.tsx"), content.replace(RENDER_IMPORT_TOKEN, importStr));
414
415
  }
415
416
  async function runStencil(ctx) {
416
- process.chdir(ctx.client.buildDir);
417
- const logger = createNodeLogger({ process });
418
- const sys = createNodeSys({ process });
417
+ var _a, _b;
418
+ const logger = ((_a = ctx.dev) === null || _a === void 0 ? void 0 : _a.logger) || createNodeLogger({ process });
419
+ const sys = ((_b = ctx.dev) === null || _b === void 0 ? void 0 : _b.sys) || createNodeSys({ process });
420
+ const devMode = !!ctx.dev;
419
421
  const validated = await loadConfig({
422
+ initTsConfig: true,
420
423
  logger,
421
424
  sys,
422
425
  config: {
426
+ devMode,
427
+ rootDir: ctx.client.buildDir,
428
+ configPath: path$1.resolve(ctx.client.buildDir, "stencil.config.ts"),
429
+ tsconfig: path$1.resolve(ctx.client.buildDir, "tsconfig.json"),
423
430
  namespace: "embeddable-wrapper",
424
- srcDir: "component",
431
+ srcDir: path$1.resolve(ctx.client.buildDir, "component"),
425
432
  outputTargets: [
426
433
  {
427
434
  type: "dist",
@@ -432,6 +439,7 @@ async function runStencil(ctx) {
432
439
  const compiler = await createCompiler(validated.config);
433
440
  await compiler.build();
434
441
  await compiler.destroy();
442
+ process.chdir(ctx.client.rootDir);
435
443
  }
436
444
  async function generateSourceMap(ctx, pluginName) {
437
445
  const componentBuildDir = path$1.resolve(ctx.client.buildDir, ctx[pluginName].outputOptions.buildName);
@@ -4496,7 +4504,7 @@ const checkNodeVersion$1 = () => {
4496
4504
  throw new Error(`Node version ${minMajor}.${minMinor} or higher is required. You are running ${major}.${minor}.`);
4497
4505
  }
4498
4506
  };
4499
- var validate = async (ctx) => {
4507
+ var validate = async (ctx, exitIfInvalid = true) => {
4500
4508
  checkNodeVersion$1();
4501
4509
  const ora = (await import('ora')).default;
4502
4510
  const spinnerValidate = ora("data model validation...").start();
@@ -4505,9 +4513,12 @@ var validate = async (ctx) => {
4505
4513
  if (dataModelErrors.length) {
4506
4514
  spinnerValidate.fail("One or more cube.yaml files are invalid:");
4507
4515
  dataModelErrors.forEach((errorMessage) => spinnerValidate.info(errorMessage));
4508
- process.exit(1);
4516
+ if (exitIfInvalid) {
4517
+ process.exit(1);
4518
+ }
4509
4519
  }
4510
4520
  spinnerValidate.succeed("data model validation completed");
4521
+ return dataModelErrors.length === 0;
4511
4522
  };
4512
4523
  async function dataModelsValidation(filesList) {
4513
4524
  const errors = [];
@@ -16790,7 +16801,7 @@ sourceMap.SourceMapConsumer = sourceMapConsumer.SourceMapConsumer;
16790
16801
  sourceMap.SourceNode = sourceNode.SourceNode;
16791
16802
 
16792
16803
  var SourceMapConsumer = sourceMap.SourceMapConsumer;
16793
- var path = require$$1__default;
16804
+ var path = path__default;
16794
16805
  var fs = require$$2$1;
16795
16806
 
16796
16807
  /**
@@ -19057,7 +19068,7 @@ var setupCustomJSON = function(JSON) {
19057
19068
  var json3 = setupCustomJSON;
19058
19069
 
19059
19070
  var util = require$$4$1;
19060
- var os = require$$1$1;
19071
+ var os = require$$1;
19061
19072
 
19062
19073
  var packageJson = require$$2;
19063
19074
  var Client = rollbar$1;
@@ -19833,7 +19844,7 @@ const reportErrorToRollbar = async (error) => {
19833
19844
 
19834
19845
  const getSdkPackageVersionInfo = (config) => {
19835
19846
  try {
19836
- const packageJsonFilePath = require$$1.resolve(
19847
+ const packageJsonFilePath = path$2.resolve(
19837
19848
  config.client.rootDir,
19838
19849
  "package.json",
19839
19850
  );
@@ -19877,11 +19888,11 @@ async function getUserData() {
19877
19888
  }
19878
19889
  }
19879
19890
 
19880
- const oraP$2 = import('ora');
19881
- let ora$1;
19891
+ const oraP$3 = import('ora');
19892
+ let ora$2;
19882
19893
  const checkNodeVersion = async () => {
19883
- ora$1 = (await oraP$2).default;
19884
- ora$1("checking node version...");
19894
+ ora$2 = (await oraP$3).default;
19895
+ ora$2("checking node version...");
19885
19896
  const [major, minor] = process.versions.node.split(".").map(Number);
19886
19897
  const engines = require("../package.json").engines.node;
19887
19898
  const [minMajor, minMinor] = engines
@@ -19889,7 +19900,7 @@ const checkNodeVersion = async () => {
19889
19900
  .map((v) => v.replace(/[^\d]/g, ""))
19890
19901
  .map(Number);
19891
19902
  if (major < minMajor || (major === minMajor && minor < minMinor)) {
19892
- ora$1({
19903
+ ora$2({
19893
19904
  text: `Node version ${minMajor}.${minMinor} or higher is required. You are running ${major}.${minor}.`,
19894
19905
  color: "red",
19895
19906
  }).fail();
@@ -19921,12 +19932,12 @@ var build = async () => {
19921
19932
  }
19922
19933
  };
19923
19934
 
19924
- const oraP$1 = import('ora');
19935
+ const oraP$2 = import('ora');
19925
19936
  const openP = import('open');
19926
19937
  var login = async () => {
19927
19938
  var _a;
19928
19939
  try {
19929
- const ora = (await oraP$1).default;
19940
+ const ora = (await oraP$2).default;
19930
19941
  const open = (await openP).default;
19931
19942
  const config = await provideConfig();
19932
19943
  await resolveFiles();
@@ -19997,22 +20008,22 @@ async function resolveFiles() {
19997
20008
  }
19998
20009
  }
19999
20010
 
20000
- const oraP = import('ora');
20011
+ const oraP$1 = import('ora');
20001
20012
  const inquirerSelect = import('@inquirer/select');
20002
20013
  const CUBE_YAML_FILE_REGEX = /^(.*)\.cube\.ya?ml$/;
20003
- let ora;
20014
+ let ora$1;
20004
20015
  var push = async () => {
20005
20016
  try {
20006
20017
  checkNodeVersion();
20007
- ora = (await oraP).default;
20018
+ ora$1 = (await oraP$1).default;
20008
20019
  const config = await provideConfig();
20009
20020
  const token = await verify(config);
20010
20021
  const { workspaceId, name: workspaceName } = await selectWorkspace(config, token);
20011
- const spinnerArchive = ora("archivation...").start();
20022
+ const spinnerArchive = ora$1("archivation...").start();
20012
20023
  const filesList = await findFiles(config.client.srcDir, CUBE_YAML_FILE_REGEX);
20013
20024
  await archive(config, filesList);
20014
20025
  spinnerArchive.succeed("archivation competed");
20015
- const spinnerPushing = ora(`publishing to ${workspaceName} using ${config.pushBaseUrl}...`).start();
20026
+ const spinnerPushing = ora$1(`publishing to ${workspaceName} using ${config.pushBaseUrl}...`).start();
20016
20027
  await sendBuild(config, { workspaceId, token });
20017
20028
  spinnerPushing.succeed(`published to ${workspaceName} using ${config.pushBaseUrl}`);
20018
20029
  }
@@ -20023,7 +20034,7 @@ var push = async () => {
20023
20034
  }
20024
20035
  };
20025
20036
  async function selectWorkspace(ctx, token) {
20026
- const workspaceSpinner = ora({
20037
+ const workspaceSpinner = ora$1({
20027
20038
  text: `Fetching workspaces using ${ctx.pushBaseUrl}...`,
20028
20039
  color: "green",
20029
20040
  discardStdin: false,
@@ -20067,13 +20078,15 @@ async function verify(ctx) {
20067
20078
  }
20068
20079
  return token;
20069
20080
  }
20070
- async function archive(ctx, yamlFiles) {
20081
+ async function archive(ctx, yamlFiles, includeBuild = true) {
20071
20082
  const output = fsSync.createWriteStream(ctx.client.archiveFile);
20072
20083
  const _archiver = archiver.create("zip", {
20073
20084
  zlib: { level: 9 },
20074
20085
  });
20075
20086
  _archiver.pipe(output);
20076
- _archiver.directory(ctx.client.buildDir, false);
20087
+ if (includeBuild) {
20088
+ _archiver.directory(ctx.client.buildDir, false);
20089
+ }
20077
20090
  for (const fileData of yamlFiles) {
20078
20091
  _archiver.file(fileData[1], { name: `${fileData[0]}.cube.yaml` });
20079
20092
  }
@@ -20099,13 +20112,14 @@ async function sendBuild(ctx, { workspaceId, token }) {
20099
20112
  await fs$1.rm(ctx.client.archiveFile);
20100
20113
  }
20101
20114
  async function getWorkspaces(ctx, token, workspaceSpinner) {
20115
+ var _a;
20102
20116
  try {
20103
20117
  const response = await axios.get(`${ctx.pushBaseUrl}/workspace`, {
20104
20118
  headers: {
20105
20119
  Authorization: `Bearer ${token}`,
20106
20120
  },
20107
20121
  });
20108
- return response.data;
20122
+ return (_a = response.data) === null || _a === void 0 ? void 0 : _a.filter((w) => !w.devWorkspace);
20109
20123
  }
20110
20124
  catch (e) {
20111
20125
  if (e.response.status === 401) {
@@ -20118,7 +20132,171 @@ async function getWorkspaces(ctx, token, workspaceSpinner) {
20118
20132
  }
20119
20133
  }
20120
20134
 
20121
- var defineConfig = ({ plugins, pushBaseUrl, audienceUrl, authDomain, authClientId, errorFallbackComponent, applicationEnvironment, rollbarAccessToken, }) => {
20135
+ const oraP = import('ora');
20136
+ let wss;
20137
+ let changedFiles = [];
20138
+ let browserWindow = null;
20139
+ let ora;
20140
+ let previewWorkspace;
20141
+ const SERVER_PORT = 8926;
20142
+ const buildWebComponent = async (config) => {
20143
+ await generate(config, "sdk-react");
20144
+ };
20145
+ var dev = async () => {
20146
+ const http = require("http");
20147
+ ora = (await oraP).default;
20148
+ process.on("warning", (e) => console.warn(e.stack));
20149
+ const logger = createNodeLogger({ process });
20150
+ const sys = createNodeSys({ process });
20151
+ const config = {
20152
+ dev: {
20153
+ watch: true,
20154
+ logger,
20155
+ sys,
20156
+ },
20157
+ ...(await provideConfig()),
20158
+ };
20159
+ await prepare(config);
20160
+ const finalhandler = require("finalhandler");
20161
+ const serveStatic = require("serve-static");
20162
+ const serve = serveStatic(config.client.buildDir);
20163
+ const workspacePreparation = ora("Preparing workspace...").start();
20164
+ previewWorkspace = await getPreviewWorkspace(config);
20165
+ workspacePreparation.succeed("Workspace is ready");
20166
+ const server = http.createServer((request, res) => {
20167
+ res.setHeader("Access-Control-Allow-Origin", "*");
20168
+ const done = finalhandler(request, res);
20169
+ serve(request, res, done);
20170
+ });
20171
+ wss = new WebSocketServer({ server });
20172
+ server.listen(SERVER_PORT, async () => {
20173
+ const watchers = [];
20174
+ if (sys === null || sys === void 0 ? void 0 : sys.onProcessInterrupt) {
20175
+ sys.onProcessInterrupt(async () => await onClose(server, sys, watchers, config));
20176
+ }
20177
+ await sendDataModels(config);
20178
+ for (const getPlugin of config.plugins) {
20179
+ const plugin = getPlugin();
20180
+ await plugin.validate(config);
20181
+ const watcher = await plugin.build(config);
20182
+ await configureWatcher(watcher, config);
20183
+ watchers.push(watcher);
20184
+ }
20185
+ const modelWatcher = dataModelWatcher(config);
20186
+ watchers.push(modelWatcher);
20187
+ });
20188
+ };
20189
+ const configureWatcher = async (watcher, ctx) => {
20190
+ watcher.on("change", (path) => {
20191
+ changedFiles.push(path);
20192
+ });
20193
+ watcher.on("event", async (e) => {
20194
+ var _a;
20195
+ if (e.code === "BUNDLE_START") {
20196
+ await onBuildStart(ctx);
20197
+ }
20198
+ if (e.code === "BUNDLE_END") {
20199
+ await onBundleBuildEnd(ctx);
20200
+ changedFiles = [];
20201
+ }
20202
+ if (e.code === "ERROR") {
20203
+ sendMessage("componentsBuildError", { error: (_a = e.error) === null || _a === void 0 ? void 0 : _a.message });
20204
+ changedFiles = [];
20205
+ }
20206
+ });
20207
+ };
20208
+ const sendMessage = (type, meta = {}) => {
20209
+ wss.clients.forEach((ws) => {
20210
+ ws.send(JSON.stringify({ type, ...meta }));
20211
+ });
20212
+ };
20213
+ const typeFilesFilter = (f) => EMB_OPTIONS_FILE_REGEX.test(f) || EMB_TYPE_FILE_REGEX.test(f);
20214
+ const onlyTypesChanged = () => changedFiles.length !== 0 &&
20215
+ changedFiles.filter(typeFilesFilter).length === changedFiles.length;
20216
+ const isTypeFileChanged = () => changedFiles.findIndex(typeFilesFilter) >= 0;
20217
+ const onBuildStart = async (ctx) => {
20218
+ if (changedFiles.length === 0 || isTypeFileChanged()) {
20219
+ await buildTypes(ctx);
20220
+ }
20221
+ sendMessage("componentsBuildStart", { changedFiles });
20222
+ };
20223
+ const onBundleBuildEnd = async (ctx) => {
20224
+ if (!onlyTypesChanged() || changedFiles.length === 0) {
20225
+ await buildWebComponent(ctx);
20226
+ }
20227
+ if (browserWindow == null) {
20228
+ const open = (await import('open')).default;
20229
+ browserWindow = await open(`${ctx.previewBaseUrl}/workspace/${previewWorkspace}`);
20230
+ }
20231
+ else {
20232
+ sendMessage("componentsBuildSuccess");
20233
+ }
20234
+ };
20235
+ const dataModelWatcher = (ctx) => {
20236
+ const fsWatcher = chokidar.watch([path$2.resolve(ctx.client.srcDir, "**/*.cube.{yaml,yml}")], {
20237
+ ignoreInitial: true,
20238
+ });
20239
+ fsWatcher.on("all", async () => {
20240
+ await sendDataModels(ctx);
20241
+ });
20242
+ return fsWatcher;
20243
+ };
20244
+ const sendDataModels = async (ctx) => {
20245
+ sendMessage("dataModelsUpdateStart");
20246
+ const isValid = await validate(ctx, false);
20247
+ if (isValid) {
20248
+ const token = await getToken();
20249
+ const sending = ora("synchronising data models...").start();
20250
+ const filesList = await findFiles(ctx.client.srcDir, CUBE_YAML_FILE_REGEX);
20251
+ await archive(ctx, filesList, false);
20252
+ await sendBuild(ctx, { workspaceId: previewWorkspace, token });
20253
+ sending.succeed(`data models synchronized`);
20254
+ sendMessage("dataModelsUpdateSuccess");
20255
+ }
20256
+ else {
20257
+ sendMessage("dataModelsUpdateError");
20258
+ }
20259
+ };
20260
+ const onClose = async (server, sys, watchers, config) => {
20261
+ server.close();
20262
+ wss.close();
20263
+ browserWindow === null || browserWindow === void 0 ? void 0 : browserWindow.unref();
20264
+ for (const watcher of watchers) {
20265
+ if (watcher.close) {
20266
+ await watcher.close();
20267
+ }
20268
+ }
20269
+ for (const getPlugin of config.plugins) {
20270
+ const plugin = getPlugin();
20271
+ await plugin.cleanup(config);
20272
+ }
20273
+ await removeIfExists(config);
20274
+ await sys.destroy();
20275
+ process.exit(0);
20276
+ };
20277
+ const getPreviewWorkspace = async (ctx) => {
20278
+ const token = await getToken();
20279
+ try {
20280
+ const response = await axios.get(`${ctx.pushBaseUrl}/workspace/dev-workspace`, {
20281
+ headers: {
20282
+ Authorization: `Bearer ${token}`,
20283
+ },
20284
+ });
20285
+ return response.data;
20286
+ }
20287
+ catch (e) {
20288
+ if (e.response.status === 401) {
20289
+ // login and retry
20290
+ await login();
20291
+ return await getPreviewWorkspace(ctx);
20292
+ }
20293
+ else {
20294
+ throw e;
20295
+ }
20296
+ }
20297
+ };
20298
+
20299
+ var defineConfig = ({ plugins, pushBaseUrl, audienceUrl, authDomain, authClientId, errorFallbackComponent, applicationEnvironment, rollbarAccessToken, previewBaseUrl, }) => {
20122
20300
  const coreRoot = path$1.resolve(__dirname, "..");
20123
20301
  const clientRoot = process.cwd();
20124
20302
  return {
@@ -20144,6 +20322,7 @@ var defineConfig = ({ plugins, pushBaseUrl, audienceUrl, authDomain, authClientI
20144
20322
  },
20145
20323
  pushBaseUrl: pushBaseUrl !== null && pushBaseUrl !== void 0 ? pushBaseUrl : "https://api.embeddable.com",
20146
20324
  audienceUrl: audienceUrl !== null && audienceUrl !== void 0 ? audienceUrl : "https://auth.embeddable.com",
20325
+ previewBaseUrl: previewBaseUrl !== null && previewBaseUrl !== void 0 ? previewBaseUrl : "https://app.embeddable.com",
20147
20326
  authDomain: authDomain !== null && authDomain !== void 0 ? authDomain : "auth.embeddable.com",
20148
20327
  authClientId: authClientId !== null && authClientId !== void 0 ? authClientId : "dygrSUmI6HmgY5ymVbEAoLDEBxIOyr1V",
20149
20328
  applicationEnvironment: applicationEnvironment !== null && applicationEnvironment !== void 0 ? applicationEnvironment : "production",
@@ -20552,5 +20731,5 @@ var fileFromPath$1 = /*#__PURE__*/Object.freeze({
20552
20731
  fileFromPath: fileFromPath
20553
20732
  });
20554
20733
 
20555
- export { build, defineConfig, login, push };
20734
+ export { build, defineConfig, dev, login, push };
20556
20735
  //# sourceMappingURL=index.esm.js.map