From fec718baeb0fd493ec4fd81543ccbf815b800c67 Mon Sep 17 00:00:00 2001 From: Wenderson Pires Date: Mon, 22 Apr 2024 18:05:27 -0300 Subject: [PATCH 1/2] added getAlemEnvironment feature; added new mechanism to scape regex found within each file. --- lib/actions/injectFoundRegExps.js | 24 +++++++++ lib/actions/loadFilesInfo.js | 13 ++++- lib/alem-vm/alem-vm.d.ts | 5 ++ lib/alem-vm/state.ts | 5 ++ lib/compiler.js | 6 +++ lib/config/parseAlemFeatures.js | 1 + .../convertRegexToStringLiteral.js | 51 +++++++++++++++++++ lib/parsers/regex-parser/regexObjects.js | 29 +++++++++++ package.json | 2 +- 9 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 lib/actions/injectFoundRegExps.js create mode 100644 lib/parsers/regex-parser/convertRegexToStringLiteral.js create mode 100644 lib/parsers/regex-parser/regexObjects.js diff --git a/lib/actions/injectFoundRegExps.js b/lib/actions/injectFoundRegExps.js new file mode 100644 index 0000000..3546299 --- /dev/null +++ b/lib/actions/injectFoundRegExps.js @@ -0,0 +1,24 @@ +const regexObjects = require("../parsers/regex-parser/regexObjects"); + +/** + * Troca as referencias de expressoes regulares com elas sendo devidamente escapadas. Isso é necessário + * pois todas as expressoes encontradas sao extraídas para depois serem realocadas de forma formatada/escapada + * corretamente no código final. + * + * @param {*} bundleContent + * @returns + */ +const injectFoundRegExps = (bundleContent) => { + const expressions = regexObjects.getExpressions(); + const exps = Object.keys(expressions); + exps.forEach((expressionKey) => { + bundleContent = bundleContent.replaceAll( + `"${expressionKey}"`, + expressions[expressionKey], + ); + }); + + return bundleContent; +}; + +module.exports = injectFoundRegExps; diff --git a/lib/actions/loadFilesInfo.js b/lib/actions/loadFilesInfo.js index 96676e2..7e30ba3 100644 --- a/lib/actions/loadFilesInfo.js +++ b/lib/actions/loadFilesInfo.js @@ -13,7 +13,8 @@ const transformComponentReferenceToJSX = require("../parsers/transformComponentR const hasWidgetPropsCheck = require("./hasWidgetPropsCheck"); const { removeImports } = require("../parse"); const filesContentCache = require("../config/filesContentCache"); - +const replaceRegexWithReferences = require("../parsers/regex-parser/convertRegexToStringLiteral"); +const regexObjects = require("../parsers/regex-parser/regexObjects"); /** * Transform statefull components references to JSX (this applies for stateful and stateless components) * Troca referencias de stateful components para JSX. Accesse o arquivo "transformComponentReferenceToJSX" para saber mais. @@ -105,6 +106,16 @@ const processFileSchema = (filePath, processOnlyThisFile) => { let fileContent = filesContentCache.getFileContent(filePath); + // Captura as expressoes regulares, salvam ela em um objeto para ser usado + // posteriormente + const replaceRegexResult = replaceRegexWithReferences(fileContent); + + if (replaceRegexResult.hasExpressions) { + // console.log(replaceRegexResult); + fileContent = replaceRegexResult.code; + regexObjects.addExpressions(replaceRegexResult.expressions); + } + // Remove comments from file // INFO: Esta sendo usado para remover comentários de arquivos jsx também const removeCommentsResult = removeCommentsFromTSX(fileContent, filePath); diff --git a/lib/alem-vm/alem-vm.d.ts b/lib/alem-vm/alem-vm.d.ts index 89cd256..29042bb 100644 --- a/lib/alem-vm/alem-vm.d.ts +++ b/lib/alem-vm/alem-vm.d.ts @@ -261,6 +261,11 @@ export declare const promisify: ( */ export declare const isDevelopment: boolean; +/** + * Get the current environment. This can be set using NODE_ENV env var. + */ +export declare const getAlemEnvironment: () => string; + /** * Create a debounced method to obtain the data after the desired interval. * @param cb Callback diff --git a/lib/alem-vm/state.ts b/lib/alem-vm/state.ts index 2fd0096..e73251f 100644 --- a/lib/alem-vm/state.ts +++ b/lib/alem-vm/state.ts @@ -200,6 +200,11 @@ export const props = { */ isDevelopment: alemState().alemEnvironment === "development", + /** + * Get the Environment + */ + getAlemEnvironment: () => alemState().alemEnvironment, + // ==================================== Components|Widgets Code ==================================== componentsCode: { COMPONENTS_CODE: {}, diff --git a/lib/compiler.js b/lib/compiler.js index ac87f2c..2c7cfd8 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -12,6 +12,7 @@ const saveFileSchemas = require("./actions/saveFileSchemas"); const renderErrorDisplay = require("./actions/renderErrorDisplay"); const injectModules = require("./actions/injectModules"); const applyOptions = require("./actions/applyOptions"); +const injectFoundRegExps = require("./actions/injectFoundRegExps"); const distFolder = process.env.DIST_FOLDER || "build"; @@ -100,6 +101,11 @@ function run_final_process(filesInfo) { // Add sinatures bundleContent = addSignatures(bundleContent); + // Troca as referencias de expressoes regulares com elas sendo devidamente escapadas. Isso é necessário + // pois todas as expressoes encontradas sao extraídas para depois serem realocadas de forma formatada/escapada + // corretamente no código final + bundleContent = injectFoundRegExps(bundleContent); + // Save final bundle file saveFinalBundleFile(bundleContent); diff --git a/lib/config/parseAlemFeatures.js b/lib/config/parseAlemFeatures.js index a293f2d..ccee05a 100644 --- a/lib/config/parseAlemFeatures.js +++ b/lib/config/parseAlemFeatures.js @@ -8,6 +8,7 @@ const parseAlemFeatures = (bundleContent) => { .replaceAll("useParams(", "props.alem.useParams(") .replaceAll("createRoute(", "props.alem.createRoute(") .replaceAll("promisify(", "props.alem.promisify(") + .replaceAll("getAlemEnvironment(", "props.alem.getAlemEnvironment(") .replaceAll( /(? 0, + }; +} + +module.exports = replaceRegexWithReferences; diff --git a/lib/parsers/regex-parser/regexObjects.js b/lib/parsers/regex-parser/regexObjects.js new file mode 100644 index 0000000..9838bf0 --- /dev/null +++ b/lib/parsers/regex-parser/regexObjects.js @@ -0,0 +1,29 @@ +let _regexObjects = {}; + +/** + * INFO: Houve a tentativa de guardar as referencias das expressoes no estado global, porém a VM não suporta + * passar expressoes regulares como parametros para os Widgets abaixo + */ +// function convertRegexStringsToLiterals(obj) { +// const entries = Object.keys(obj); +// let finalObjects = ""; + +// entries.forEach((key) => { +// const currentEntrieValue = obj[key]; +// let currentRegexString = `${key}: ${currentEntrieValue}`; +// finalObjects += `${currentRegexString},\n`; +// }); + +// return finalObjects; +// } + +const addExpressions = (expressions) => + (_regexObjects = { ..._regexObjects, ...expressions }); +const getExpressions = () => _regexObjects; +// const getStringExpressions = () => convertRegexStringsToLiterals(_regexObjects); + +module.exports = { + addExpressions, + getExpressions, + // getStringExpressions, +}; diff --git a/package.json b/package.json index 994022e..66f9d34 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "alem", "description": "Create web3 applications for NEAR BOS with a focus on performance and friendly development.", - "version": "1.0.0-beta.23", + "version": "1.0.0-beta.24", "main": "main.js", "types": "index.d.ts", "author": "Wenderson Pires - wendersonpires.near", From 9cf7e9fae54ce83d2eaa4e05f3a8af03c5ad46fd Mon Sep 17 00:00:00 2001 From: Wenderson Pires Date: Mon, 22 Apr 2024 18:08:36 -0300 Subject: [PATCH 2/2] better description for replaceRegexResult --- lib/actions/loadFilesInfo.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/actions/loadFilesInfo.js b/lib/actions/loadFilesInfo.js index 7e30ba3..61009ae 100644 --- a/lib/actions/loadFilesInfo.js +++ b/lib/actions/loadFilesInfo.js @@ -107,11 +107,9 @@ const processFileSchema = (filePath, processOnlyThisFile) => { let fileContent = filesContentCache.getFileContent(filePath); // Captura as expressoes regulares, salvam ela em um objeto para ser usado - // posteriormente + // posteriormente no compiler.js através do "injectFoundRegExps.js" const replaceRegexResult = replaceRegexWithReferences(fileContent); - if (replaceRegexResult.hasExpressions) { - // console.log(replaceRegexResult); fileContent = replaceRegexResult.code; regexObjects.addExpressions(replaceRegexResult.expressions); }