@embeddable.com/sdk-react 2.2.21 → 2.2.23

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
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var path = require('node:path');
4
- var fs$1 = require('fs/promises');
4
+ var fs$2 = require('fs/promises');
5
5
  var path$1 = require('path');
6
6
  var vite = require('vite');
7
7
  var viteReactPlugin = require('@vitejs/plugin-react');
@@ -12,6 +12,7 @@ var traverse = require('@babel/traverse');
12
12
  require('node:child_process');
13
13
  var zod = require('zod');
14
14
  var core = require('@embeddable.com/core');
15
+ var fs$1 = require('fs');
15
16
 
16
17
  function _interopNamespaceDefault(e) {
17
18
  var n = Object.create(null);
@@ -31,10 +32,11 @@ function _interopNamespaceDefault(e) {
31
32
  }
32
33
 
33
34
  var path__namespace$1 = /*#__PURE__*/_interopNamespaceDefault(path);
34
- var fs__namespace$1 = /*#__PURE__*/_interopNamespaceDefault(fs$1);
35
+ var fs__namespace$2 = /*#__PURE__*/_interopNamespaceDefault(fs$2);
35
36
  var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path$1);
36
37
  var vite__namespace = /*#__PURE__*/_interopNamespaceDefault(vite);
37
38
  var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
39
+ var fs__namespace$1 = /*#__PURE__*/_interopNamespaceDefault(fs$1);
38
40
 
39
41
  var createContext = (pluginRoot, coreCtx) => {
40
42
  coreCtx["sdk-react"] = {
@@ -568,10 +570,10 @@ const validateComponentEvents = (metaInfo) => {
568
570
  };
569
571
  const validateModuleName = (metaInfo) => {
570
572
  const basename = path__namespace.basename(metaInfo.moduleId);
571
- const fileName = basename.split(".");
572
- if (!basename.includes(metaInfo.meta.name)) {
573
+ const fileName = basename.split(".")[0];
574
+ if (fileName !== metaInfo.meta.name) {
573
575
  return [
574
- `meta.name: The name of the .emb.* file and [meta.name]'s value must match. In this case, [meta.name] must be ${fileName[0]}`,
576
+ `meta.name: The name of the .emb.* file and [meta.name]'s value must match. In this case, [meta.name] must be ${fileName}`,
575
577
  ];
576
578
  }
577
579
  return [];
@@ -738,11 +740,80 @@ const entrypointModifiers = [
738
740
  styledComponentsEntrypointModifier,
739
741
  ];
740
742
 
743
+ const oraP$1 = import('ora');
744
+ function findFilesWithWrongUsage(directory, pattern, mustEndWith = []) {
745
+ const files = fs__namespace$1.readdirSync(directory);
746
+ const result = [];
747
+ files.forEach((file) => {
748
+ const filePath = path__namespace.join(directory, file);
749
+ if (fs__namespace$1.statSync(filePath).isDirectory()) {
750
+ // Recursively check files in subdirectories
751
+ result.push(...findFilesWithWrongUsage(filePath, pattern, mustEndWith));
752
+ }
753
+ else {
754
+ const fileContent = fs__namespace$1.readFileSync(filePath, "utf8");
755
+ if (fileContent.match(pattern)) {
756
+ result.push(filePath);
757
+ }
758
+ }
759
+ });
760
+ return result.filter((file) => !mustEndWith.some((ending) => file.endsWith(ending)));
761
+ }
762
+ const regexDefineComponentFunction = /defineComponent(<\w+>)?\(/;
763
+ const regexDefineTypeFunction = /defineType\(/;
764
+ const regexDefineOptionFunction = /defineOption\(/;
765
+ const regexCubeFunction = /cube\(/;
766
+ const regexCubes = /cubes:/;
767
+ const usageValidator = async (directory) => {
768
+ let isValid = true;
769
+ const ora = (await oraP$1).default;
770
+ const progress = ora("React: function usage validating...").start();
771
+ // defineComponent function
772
+ const filesWithWrongDefineComponentFunctionUsage = findFilesWithWrongUsage(directory, regexDefineComponentFunction, [".emb.js", ".emb.ts"]);
773
+ if (filesWithWrongDefineComponentFunctionUsage.length) {
774
+ isValid = false;
775
+ progress.fail("React: defineComponent() usage is only allowed inside files with the extension .emb.(js|ts) files.\nFix the following files:\n" +
776
+ filesWithWrongDefineComponentFunctionUsage.join("\n"));
777
+ }
778
+ // defineType function
779
+ const filesWithWrongDefineTypeFunctionUsage = findFilesWithWrongUsage(directory, regexDefineTypeFunction, [".type.emb.js", ".type.emb.ts"]);
780
+ if (filesWithWrongDefineTypeFunctionUsage.length) {
781
+ isValid = false;
782
+ progress.fail("React: defineType() usage is only allowed inside files with the extension .type.emb.(js|ts) files.\nFix the following files:\n" +
783
+ filesWithWrongDefineTypeFunctionUsage.join("\n"));
784
+ }
785
+ // defineOption function
786
+ const filesWithWrongDefineOptionFunctionUsage = findFilesWithWrongUsage(directory, regexDefineOptionFunction, [".type.emb.js", ".type.emb.ts"]);
787
+ if (filesWithWrongDefineOptionFunctionUsage.length) {
788
+ isValid = false;
789
+ progress.fail("React: defineOption() usage is only allowed inside files with the extension .type.emb.(js|ts) files.\nFix the following files:\n" +
790
+ filesWithWrongDefineOptionFunctionUsage.join("\n"));
791
+ }
792
+ // cube function
793
+ const filesWithWrongCubeFunctionUsage = findFilesWithWrongUsage(directory, regexCubeFunction, [".cube.js", ".cube.ts"]);
794
+ if (filesWithWrongCubeFunctionUsage.length) {
795
+ isValid = false;
796
+ progress.fail("React: cube() usage is only allowed inside files with the extension .cube.(ts|js) files.\nFix the following files:\n" +
797
+ filesWithWrongCubeFunctionUsage.join("\n"));
798
+ }
799
+ // cube in yml or yaml files
800
+ const filesWithWrongCubeUsage = findFilesWithWrongUsage(directory, regexCubes, [".cube.yml", ".cube.yaml"]);
801
+ if (filesWithWrongCubeUsage.length) {
802
+ isValid = false;
803
+ progress.fail("React: cubes: usage is only allowed inside files with the extension .cube.(yml|yaml) files.\nFix the following files:\n" +
804
+ filesWithWrongCubeUsage.join("\n"));
805
+ }
806
+ isValid
807
+ ? progress.succeed("React: function usage validated")
808
+ : process.exit(1);
809
+ };
810
+
741
811
  const oraP = import('ora');
742
812
  const EMB_FILE_REGEX = /^(.*)(?<!\.type|\.options)\.emb\.[jt]s$/;
743
813
  var generate = async (ctx) => {
744
814
  var _a;
745
815
  const ora = (await oraP).default;
816
+ await usageValidator(ctx.client.srcDir);
746
817
  const filesList = await findFiles(ctx.client.srcDir, EMB_FILE_REGEX);
747
818
  await prepareEntrypoint(ctx, filesList);
748
819
  let result;
@@ -825,8 +896,8 @@ async function prepareEntrypoint(ctx, filesList) {
825
896
  additionalContentEnd.unshift(entrypointModifier.getContentEnd(ctx));
826
897
  }
827
898
  }
828
- const content = await fs__namespace$1.readFile(path__namespace.resolve(ctx["sdk-react"].templatesDir, `${ctx["sdk-react"].outputOptions.componentsEntryPointFilename}.template`), "utf8");
829
- await fs__namespace$1.writeFile(path__namespace.resolve(ctx.client.rootDir, ctx["sdk-react"].outputOptions.componentsEntryPointFilename), content
899
+ const content = await fs__namespace$2.readFile(path__namespace.resolve(ctx["sdk-react"].templatesDir, `${ctx["sdk-react"].outputOptions.componentsEntryPointFilename}.template`), "utf8");
900
+ await fs__namespace$2.writeFile(path__namespace.resolve(ctx.client.rootDir, ctx["sdk-react"].outputOptions.componentsEntryPointFilename), content
830
901
  .replace(IMPORT_REPLACE_TOKEN, imports)
831
902
  .replace(ERROR_FALLBACK_COMPONENT_IMPORT_TOKEN, errorBoundaryImport)
832
903
  .replace(ERROR_FALLBACK_COMPONENT_TOKEN, errorFallbackComponent)