@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.
package/lib/index.js CHANGED
@@ -8,9 +8,9 @@ var fsSync = require('node:fs');
8
8
  var node = require('@stencil/core/sys/node');
9
9
  var compiler = require('@stencil/core/compiler');
10
10
  var YAML = require('yaml');
11
- var require$$1 = require('path');
11
+ var path$2 = require('path');
12
12
  var require$$4$1 = require('util');
13
- var require$$1$1 = require('os');
13
+ var require$$1 = require('os');
14
14
  var require$$3 = require('http');
15
15
  var require$$4 = require('https');
16
16
  var require$$0$1 = require('url');
@@ -18,6 +18,8 @@ var require$$2$1 = require('fs');
18
18
  var os$1 = require('node:os');
19
19
  var axios = require('axios');
20
20
  var archiver = require('archiver');
21
+ var ws = require('ws');
22
+ var chokidar = require('chokidar');
21
23
  var promises = require('fs/promises');
22
24
 
23
25
  function _interopNamespaceDefault(e) {
@@ -42,9 +44,10 @@ var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path$1);
42
44
  var vite__namespace = /*#__PURE__*/_interopNamespaceDefault(vite);
43
45
  var fsSync__namespace = /*#__PURE__*/_interopNamespaceDefault(fsSync);
44
46
  var YAML__namespace = /*#__PURE__*/_interopNamespaceDefault(YAML);
45
- var require$$1__namespace = /*#__PURE__*/_interopNamespaceDefault(require$$1);
47
+ var path__namespace$1 = /*#__PURE__*/_interopNamespaceDefault(path$2);
46
48
  var os__namespace = /*#__PURE__*/_interopNamespaceDefault(os$1);
47
49
  var archiver__namespace = /*#__PURE__*/_interopNamespaceDefault(archiver);
50
+ var chokidar__namespace = /*#__PURE__*/_interopNamespaceDefault(chokidar);
48
51
 
49
52
  var findFiles = async (initialSrcDir, regex) => {
50
53
  const filesList = [];
@@ -358,11 +361,11 @@ const formatErrorPath = (path) => {
358
361
  return formatted;
359
362
  };
360
363
 
361
- const oraP$3 = import('ora');
364
+ const oraP$4 = import('ora');
362
365
  const EMB_TYPE_FILE_REGEX = /^(.*)\.type\.emb\.[jt]s$/;
363
366
  const EMB_OPTIONS_FILE_REGEX = /^(.*)\.options\.emb\.[jt]s$/;
364
367
  var buildTypes = async (ctx) => {
365
- const ora = (await oraP$3).default;
368
+ const ora = (await oraP$4).default;
366
369
  const progress = ora("building types...").start();
367
370
  await generate$1(ctx);
368
371
  await build$1(ctx);
@@ -379,17 +382,16 @@ async function generate$1(ctx) {
379
382
  await fs__namespace.writeFile(path__namespace.resolve(ctx.client.rootDir, ctx.outputOptions.typesEntryPointFilename), typeImports);
380
383
  }
381
384
  async function build$1(ctx) {
382
- process.chdir(ctx.client.rootDir);
383
385
  await vite__namespace.build({
384
386
  logLevel: "error",
385
387
  build: {
386
388
  emptyOutDir: false,
387
389
  lib: {
388
- entry: `./${ctx.outputOptions.typesEntryPointFilename}`,
390
+ entry: path__namespace.resolve(ctx.client.rootDir, ctx.outputOptions.typesEntryPointFilename),
389
391
  formats: ["es"],
390
392
  fileName: "embeddable-types",
391
393
  },
392
- outDir: ".embeddable-build/",
394
+ outDir: ctx.client.buildDir,
393
395
  },
394
396
  });
395
397
  }
@@ -438,15 +440,21 @@ async function injectBundleRender(ctx, pluginName) {
438
440
  await fs__namespace.writeFile(path__namespace.resolve(ctx.client.componentDir, "component.tsx"), content.replace(RENDER_IMPORT_TOKEN, importStr));
439
441
  }
440
442
  async function runStencil(ctx) {
441
- process.chdir(ctx.client.buildDir);
442
- const logger = node.createNodeLogger({ process });
443
- const sys = node.createNodeSys({ process });
443
+ var _a, _b;
444
+ const logger = ((_a = ctx.dev) === null || _a === void 0 ? void 0 : _a.logger) || node.createNodeLogger({ process });
445
+ const sys = ((_b = ctx.dev) === null || _b === void 0 ? void 0 : _b.sys) || node.createNodeSys({ process });
446
+ const devMode = !!ctx.dev;
444
447
  const validated = await compiler.loadConfig({
448
+ initTsConfig: true,
445
449
  logger,
446
450
  sys,
447
451
  config: {
452
+ devMode,
453
+ rootDir: ctx.client.buildDir,
454
+ configPath: path__namespace.resolve(ctx.client.buildDir, "stencil.config.ts"),
455
+ tsconfig: path__namespace.resolve(ctx.client.buildDir, "tsconfig.json"),
448
456
  namespace: "embeddable-wrapper",
449
- srcDir: "component",
457
+ srcDir: path__namespace.resolve(ctx.client.buildDir, "component"),
450
458
  outputTargets: [
451
459
  {
452
460
  type: "dist",
@@ -457,6 +465,7 @@ async function runStencil(ctx) {
457
465
  const compiler$1 = await compiler.createCompiler(validated.config);
458
466
  await compiler$1.build();
459
467
  await compiler$1.destroy();
468
+ process.chdir(ctx.client.rootDir);
460
469
  }
461
470
  async function generateSourceMap(ctx, pluginName) {
462
471
  const componentBuildDir = path__namespace.resolve(ctx.client.buildDir, ctx[pluginName].outputOptions.buildName);
@@ -4521,7 +4530,7 @@ const checkNodeVersion$1 = () => {
4521
4530
  throw new Error(`Node version ${minMajor}.${minMinor} or higher is required. You are running ${major}.${minor}.`);
4522
4531
  }
4523
4532
  };
4524
- var validate = async (ctx) => {
4533
+ var validate = async (ctx, exitIfInvalid = true) => {
4525
4534
  checkNodeVersion$1();
4526
4535
  const ora = (await import('ora')).default;
4527
4536
  const spinnerValidate = ora("data model validation...").start();
@@ -4530,9 +4539,12 @@ var validate = async (ctx) => {
4530
4539
  if (dataModelErrors.length) {
4531
4540
  spinnerValidate.fail("One or more cube.yaml files are invalid:");
4532
4541
  dataModelErrors.forEach((errorMessage) => spinnerValidate.info(errorMessage));
4533
- process.exit(1);
4542
+ if (exitIfInvalid) {
4543
+ process.exit(1);
4544
+ }
4534
4545
  }
4535
4546
  spinnerValidate.succeed("data model validation completed");
4547
+ return dataModelErrors.length === 0;
4536
4548
  };
4537
4549
  async function dataModelsValidation(filesList) {
4538
4550
  const errors = [];
@@ -16815,7 +16827,7 @@ sourceMap.SourceMapConsumer = sourceMapConsumer.SourceMapConsumer;
16815
16827
  sourceMap.SourceNode = sourceNode.SourceNode;
16816
16828
 
16817
16829
  var SourceMapConsumer = sourceMap.SourceMapConsumer;
16818
- var path = require$$1;
16830
+ var path = path$2;
16819
16831
  var fs = require$$2$1;
16820
16832
 
16821
16833
  /**
@@ -19082,7 +19094,7 @@ var setupCustomJSON = function(JSON) {
19082
19094
  var json3 = setupCustomJSON;
19083
19095
 
19084
19096
  var util = require$$4$1;
19085
- var os = require$$1$1;
19097
+ var os = require$$1;
19086
19098
 
19087
19099
  var packageJson = require$$2;
19088
19100
  var Client = rollbar$1;
@@ -19858,7 +19870,7 @@ const reportErrorToRollbar = async (error) => {
19858
19870
 
19859
19871
  const getSdkPackageVersionInfo = (config) => {
19860
19872
  try {
19861
- const packageJsonFilePath = require$$1__namespace.resolve(
19873
+ const packageJsonFilePath = path__namespace$1.resolve(
19862
19874
  config.client.rootDir,
19863
19875
  "package.json",
19864
19876
  );
@@ -19902,11 +19914,11 @@ async function getUserData() {
19902
19914
  }
19903
19915
  }
19904
19916
 
19905
- const oraP$2 = import('ora');
19906
- let ora$1;
19917
+ const oraP$3 = import('ora');
19918
+ let ora$2;
19907
19919
  const checkNodeVersion = async () => {
19908
- ora$1 = (await oraP$2).default;
19909
- ora$1("checking node version...");
19920
+ ora$2 = (await oraP$3).default;
19921
+ ora$2("checking node version...");
19910
19922
  const [major, minor] = process.versions.node.split(".").map(Number);
19911
19923
  const engines = require("../package.json").engines.node;
19912
19924
  const [minMajor, minMinor] = engines
@@ -19914,7 +19926,7 @@ const checkNodeVersion = async () => {
19914
19926
  .map((v) => v.replace(/[^\d]/g, ""))
19915
19927
  .map(Number);
19916
19928
  if (major < minMajor || (major === minMajor && minor < minMinor)) {
19917
- ora$1({
19929
+ ora$2({
19918
19930
  text: `Node version ${minMajor}.${minMinor} or higher is required. You are running ${major}.${minor}.`,
19919
19931
  color: "red",
19920
19932
  }).fail();
@@ -19946,12 +19958,12 @@ var build = async () => {
19946
19958
  }
19947
19959
  };
19948
19960
 
19949
- const oraP$1 = import('ora');
19961
+ const oraP$2 = import('ora');
19950
19962
  const openP = import('open');
19951
19963
  var login = async () => {
19952
19964
  var _a;
19953
19965
  try {
19954
- const ora = (await oraP$1).default;
19966
+ const ora = (await oraP$2).default;
19955
19967
  const open = (await openP).default;
19956
19968
  const config = await provideConfig();
19957
19969
  await resolveFiles();
@@ -20022,22 +20034,22 @@ async function resolveFiles() {
20022
20034
  }
20023
20035
  }
20024
20036
 
20025
- const oraP = import('ora');
20037
+ const oraP$1 = import('ora');
20026
20038
  const inquirerSelect = import('@inquirer/select');
20027
20039
  const CUBE_YAML_FILE_REGEX = /^(.*)\.cube\.ya?ml$/;
20028
- let ora;
20040
+ let ora$1;
20029
20041
  var push = async () => {
20030
20042
  try {
20031
20043
  checkNodeVersion();
20032
- ora = (await oraP).default;
20044
+ ora$1 = (await oraP$1).default;
20033
20045
  const config = await provideConfig();
20034
20046
  const token = await verify(config);
20035
20047
  const { workspaceId, name: workspaceName } = await selectWorkspace(config, token);
20036
- const spinnerArchive = ora("archivation...").start();
20048
+ const spinnerArchive = ora$1("archivation...").start();
20037
20049
  const filesList = await findFiles(config.client.srcDir, CUBE_YAML_FILE_REGEX);
20038
20050
  await archive(config, filesList);
20039
20051
  spinnerArchive.succeed("archivation competed");
20040
- const spinnerPushing = ora(`publishing to ${workspaceName} using ${config.pushBaseUrl}...`).start();
20052
+ const spinnerPushing = ora$1(`publishing to ${workspaceName} using ${config.pushBaseUrl}...`).start();
20041
20053
  await sendBuild(config, { workspaceId, token });
20042
20054
  spinnerPushing.succeed(`published to ${workspaceName} using ${config.pushBaseUrl}`);
20043
20055
  }
@@ -20048,7 +20060,7 @@ var push = async () => {
20048
20060
  }
20049
20061
  };
20050
20062
  async function selectWorkspace(ctx, token) {
20051
- const workspaceSpinner = ora({
20063
+ const workspaceSpinner = ora$1({
20052
20064
  text: `Fetching workspaces using ${ctx.pushBaseUrl}...`,
20053
20065
  color: "green",
20054
20066
  discardStdin: false,
@@ -20092,13 +20104,15 @@ async function verify(ctx) {
20092
20104
  }
20093
20105
  return token;
20094
20106
  }
20095
- async function archive(ctx, yamlFiles) {
20107
+ async function archive(ctx, yamlFiles, includeBuild = true) {
20096
20108
  const output = fsSync__namespace.createWriteStream(ctx.client.archiveFile);
20097
20109
  const _archiver = archiver__namespace.create("zip", {
20098
20110
  zlib: { level: 9 },
20099
20111
  });
20100
20112
  _archiver.pipe(output);
20101
- _archiver.directory(ctx.client.buildDir, false);
20113
+ if (includeBuild) {
20114
+ _archiver.directory(ctx.client.buildDir, false);
20115
+ }
20102
20116
  for (const fileData of yamlFiles) {
20103
20117
  _archiver.file(fileData[1], { name: `${fileData[0]}.cube.yaml` });
20104
20118
  }
@@ -20124,13 +20138,14 @@ async function sendBuild(ctx, { workspaceId, token }) {
20124
20138
  await fs__namespace.rm(ctx.client.archiveFile);
20125
20139
  }
20126
20140
  async function getWorkspaces(ctx, token, workspaceSpinner) {
20141
+ var _a;
20127
20142
  try {
20128
20143
  const response = await axios.get(`${ctx.pushBaseUrl}/workspace`, {
20129
20144
  headers: {
20130
20145
  Authorization: `Bearer ${token}`,
20131
20146
  },
20132
20147
  });
20133
- return response.data;
20148
+ return (_a = response.data) === null || _a === void 0 ? void 0 : _a.filter((w) => !w.devWorkspace);
20134
20149
  }
20135
20150
  catch (e) {
20136
20151
  if (e.response.status === 401) {
@@ -20143,7 +20158,171 @@ async function getWorkspaces(ctx, token, workspaceSpinner) {
20143
20158
  }
20144
20159
  }
20145
20160
 
20146
- var defineConfig = ({ plugins, pushBaseUrl, audienceUrl, authDomain, authClientId, errorFallbackComponent, applicationEnvironment, rollbarAccessToken, }) => {
20161
+ const oraP = import('ora');
20162
+ let wss;
20163
+ let changedFiles = [];
20164
+ let browserWindow = null;
20165
+ let ora;
20166
+ let previewWorkspace;
20167
+ const SERVER_PORT = 8926;
20168
+ const buildWebComponent = async (config) => {
20169
+ await generate(config, "sdk-react");
20170
+ };
20171
+ var dev = async () => {
20172
+ const http = require("http");
20173
+ ora = (await oraP).default;
20174
+ process.on("warning", (e) => console.warn(e.stack));
20175
+ const logger = node.createNodeLogger({ process });
20176
+ const sys = node.createNodeSys({ process });
20177
+ const config = {
20178
+ dev: {
20179
+ watch: true,
20180
+ logger,
20181
+ sys,
20182
+ },
20183
+ ...(await provideConfig()),
20184
+ };
20185
+ await prepare(config);
20186
+ const finalhandler = require("finalhandler");
20187
+ const serveStatic = require("serve-static");
20188
+ const serve = serveStatic(config.client.buildDir);
20189
+ const workspacePreparation = ora("Preparing workspace...").start();
20190
+ previewWorkspace = await getPreviewWorkspace(config);
20191
+ workspacePreparation.succeed("Workspace is ready");
20192
+ const server = http.createServer((request, res) => {
20193
+ res.setHeader("Access-Control-Allow-Origin", "*");
20194
+ const done = finalhandler(request, res);
20195
+ serve(request, res, done);
20196
+ });
20197
+ wss = new ws.WebSocketServer({ server });
20198
+ server.listen(SERVER_PORT, async () => {
20199
+ const watchers = [];
20200
+ if (sys === null || sys === void 0 ? void 0 : sys.onProcessInterrupt) {
20201
+ sys.onProcessInterrupt(async () => await onClose(server, sys, watchers, config));
20202
+ }
20203
+ await sendDataModels(config);
20204
+ for (const getPlugin of config.plugins) {
20205
+ const plugin = getPlugin();
20206
+ await plugin.validate(config);
20207
+ const watcher = await plugin.build(config);
20208
+ await configureWatcher(watcher, config);
20209
+ watchers.push(watcher);
20210
+ }
20211
+ const modelWatcher = dataModelWatcher(config);
20212
+ watchers.push(modelWatcher);
20213
+ });
20214
+ };
20215
+ const configureWatcher = async (watcher, ctx) => {
20216
+ watcher.on("change", (path) => {
20217
+ changedFiles.push(path);
20218
+ });
20219
+ watcher.on("event", async (e) => {
20220
+ var _a;
20221
+ if (e.code === "BUNDLE_START") {
20222
+ await onBuildStart(ctx);
20223
+ }
20224
+ if (e.code === "BUNDLE_END") {
20225
+ await onBundleBuildEnd(ctx);
20226
+ changedFiles = [];
20227
+ }
20228
+ if (e.code === "ERROR") {
20229
+ sendMessage("componentsBuildError", { error: (_a = e.error) === null || _a === void 0 ? void 0 : _a.message });
20230
+ changedFiles = [];
20231
+ }
20232
+ });
20233
+ };
20234
+ const sendMessage = (type, meta = {}) => {
20235
+ wss.clients.forEach((ws) => {
20236
+ ws.send(JSON.stringify({ type, ...meta }));
20237
+ });
20238
+ };
20239
+ const typeFilesFilter = (f) => EMB_OPTIONS_FILE_REGEX.test(f) || EMB_TYPE_FILE_REGEX.test(f);
20240
+ const onlyTypesChanged = () => changedFiles.length !== 0 &&
20241
+ changedFiles.filter(typeFilesFilter).length === changedFiles.length;
20242
+ const isTypeFileChanged = () => changedFiles.findIndex(typeFilesFilter) >= 0;
20243
+ const onBuildStart = async (ctx) => {
20244
+ if (changedFiles.length === 0 || isTypeFileChanged()) {
20245
+ await buildTypes(ctx);
20246
+ }
20247
+ sendMessage("componentsBuildStart", { changedFiles });
20248
+ };
20249
+ const onBundleBuildEnd = async (ctx) => {
20250
+ if (!onlyTypesChanged() || changedFiles.length === 0) {
20251
+ await buildWebComponent(ctx);
20252
+ }
20253
+ if (browserWindow == null) {
20254
+ const open = (await import('open')).default;
20255
+ browserWindow = await open(`${ctx.previewBaseUrl}/workspace/${previewWorkspace}`);
20256
+ }
20257
+ else {
20258
+ sendMessage("componentsBuildSuccess");
20259
+ }
20260
+ };
20261
+ const dataModelWatcher = (ctx) => {
20262
+ const fsWatcher = chokidar__namespace.watch([path__namespace$1.resolve(ctx.client.srcDir, "**/*.cube.{yaml,yml}")], {
20263
+ ignoreInitial: true,
20264
+ });
20265
+ fsWatcher.on("all", async () => {
20266
+ await sendDataModels(ctx);
20267
+ });
20268
+ return fsWatcher;
20269
+ };
20270
+ const sendDataModels = async (ctx) => {
20271
+ sendMessage("dataModelsUpdateStart");
20272
+ const isValid = await validate(ctx, false);
20273
+ if (isValid) {
20274
+ const token = await getToken();
20275
+ const sending = ora("synchronising data models...").start();
20276
+ const filesList = await findFiles(ctx.client.srcDir, CUBE_YAML_FILE_REGEX);
20277
+ await archive(ctx, filesList, false);
20278
+ await sendBuild(ctx, { workspaceId: previewWorkspace, token });
20279
+ sending.succeed(`data models synchronized`);
20280
+ sendMessage("dataModelsUpdateSuccess");
20281
+ }
20282
+ else {
20283
+ sendMessage("dataModelsUpdateError");
20284
+ }
20285
+ };
20286
+ const onClose = async (server, sys, watchers, config) => {
20287
+ server.close();
20288
+ wss.close();
20289
+ browserWindow === null || browserWindow === void 0 ? void 0 : browserWindow.unref();
20290
+ for (const watcher of watchers) {
20291
+ if (watcher.close) {
20292
+ await watcher.close();
20293
+ }
20294
+ }
20295
+ for (const getPlugin of config.plugins) {
20296
+ const plugin = getPlugin();
20297
+ await plugin.cleanup(config);
20298
+ }
20299
+ await removeIfExists(config);
20300
+ await sys.destroy();
20301
+ process.exit(0);
20302
+ };
20303
+ const getPreviewWorkspace = async (ctx) => {
20304
+ const token = await getToken();
20305
+ try {
20306
+ const response = await axios.get(`${ctx.pushBaseUrl}/workspace/dev-workspace`, {
20307
+ headers: {
20308
+ Authorization: `Bearer ${token}`,
20309
+ },
20310
+ });
20311
+ return response.data;
20312
+ }
20313
+ catch (e) {
20314
+ if (e.response.status === 401) {
20315
+ // login and retry
20316
+ await login();
20317
+ return await getPreviewWorkspace(ctx);
20318
+ }
20319
+ else {
20320
+ throw e;
20321
+ }
20322
+ }
20323
+ };
20324
+
20325
+ var defineConfig = ({ plugins, pushBaseUrl, audienceUrl, authDomain, authClientId, errorFallbackComponent, applicationEnvironment, rollbarAccessToken, previewBaseUrl, }) => {
20147
20326
  const coreRoot = path__namespace.resolve(__dirname, "..");
20148
20327
  const clientRoot = process.cwd();
20149
20328
  return {
@@ -20169,6 +20348,7 @@ var defineConfig = ({ plugins, pushBaseUrl, audienceUrl, authDomain, authClientI
20169
20348
  },
20170
20349
  pushBaseUrl: pushBaseUrl !== null && pushBaseUrl !== void 0 ? pushBaseUrl : "https://api.embeddable.com",
20171
20350
  audienceUrl: audienceUrl !== null && audienceUrl !== void 0 ? audienceUrl : "https://auth.embeddable.com",
20351
+ previewBaseUrl: previewBaseUrl !== null && previewBaseUrl !== void 0 ? previewBaseUrl : "https://app.embeddable.com",
20172
20352
  authDomain: authDomain !== null && authDomain !== void 0 ? authDomain : "auth.embeddable.com",
20173
20353
  authClientId: authClientId !== null && authClientId !== void 0 ? authClientId : "dygrSUmI6HmgY5ymVbEAoLDEBxIOyr1V",
20174
20354
  applicationEnvironment: applicationEnvironment !== null && applicationEnvironment !== void 0 ? applicationEnvironment : "production",
@@ -20516,7 +20696,7 @@ var _FileFromPath = class _FileFromPath {
20516
20696
  __privateAdd(this, _start, void 0);
20517
20697
  __privateSet(this, _path, input.path);
20518
20698
  __privateSet(this, _start, input.start || 0);
20519
- this.name = require$$1.basename(__privateGet(this, _path));
20699
+ this.name = path$2.basename(__privateGet(this, _path));
20520
20700
  this.size = input.size;
20521
20701
  this.lastModified = input.lastModified;
20522
20702
  }
@@ -20579,6 +20759,7 @@ var fileFromPath$1 = /*#__PURE__*/Object.freeze({
20579
20759
 
20580
20760
  exports.build = build;
20581
20761
  exports.defineConfig = defineConfig;
20762
+ exports.dev = dev;
20582
20763
  exports.login = login;
20583
20764
  exports.push = push;
20584
20765
  //# sourceMappingURL=index.js.map