From 2c681d454a3af326d43195e1a159541933064b18 Mon Sep 17 00:00:00 2001 From: Wenderson Pires Date: Tue, 2 Apr 2024 10:55:10 -0300 Subject: [PATCH 1/2] support to path alias added --- lib/actions/compilerOptions.js | 77 ++++++++++++++++++++++++++ lib/actions/loadFilesInfo.js | 13 ++++- lib/actions/transformSchemaToWidget.js | 22 ++++++-- lib/config.js | 1 - lib/data.js | 2 +- 5 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 lib/actions/compilerOptions.js diff --git a/lib/actions/compilerOptions.js b/lib/actions/compilerOptions.js new file mode 100644 index 0000000..e327921 --- /dev/null +++ b/lib/actions/compilerOptions.js @@ -0,0 +1,77 @@ +const path = require("path"); +const { read_bos_config } = require("../config"); + +/** + * Trata as configurações do compilador na qual o usuário pode definir os valores. + * Ver arquivo bos.config.json -> compilerOptions + * @param {*} config + * @returns + */ +function getConfiguredPaths(config) { + const pathsConfig = config.compilerOptions.paths; + const basePath = config.compilerOptions.baseUrl; + + // Resolvendo caminhos absolutos e mantendo os relativos + const resolvedPaths = Object.keys(pathsConfig).reduce((acc, key) => { + // Assume que todos os paths terminam com "/*", indicando um diretório + const relativePath = pathsConfig[key].replace("/*", ""); + const absolutePath = path.resolve(process.cwd(), basePath, relativePath); + + // Adicionando tanto caminho absoluto quanto relativo no objeto de retorno + acc[key] = { + absolute: absolutePath + "/", + relative: path.join(basePath, relativePath), + }; + + return acc; + }, {}); + + return resolvedPaths; +} + +const compilerOptions = () => { + const config = read_bos_config(); + const paths = getConfiguredPaths(config); + + return { + paths, + }; +}; + +const { paths } = compilerOptions(); +const pathsKeys = Object.keys(paths); +const replacePathAlias = (path) => { + pathsKeys.forEach((pathAlias) => { + //ex: path includes @app + if (path.includes(pathAlias)) { + path = path.replace(pathAlias, paths[pathAlias].relative); + } + }); + + return path; +}; + +/** + * Check if path is using path alias + * + * ex: @app/foo/bar => return true because of "@app" (if available inside bos.config.json -> compilerOptions) + * @param {string} path + * @returns + */ +const hasPathAlias = (path) => { + let includes = false; + pathsKeys.forEach((pathAlias) => { + //ex: path includes @app + if (path.includes(pathAlias) && !includes) { + includes = true; + } + }); + + return includes; +}; + +module.exports = { + compilerOptions, + replacePathAlias, + hasPathAlias, +}; diff --git a/lib/actions/loadFilesInfo.js b/lib/actions/loadFilesInfo.js index eb0e85a..8147fe5 100644 --- a/lib/actions/loadFilesInfo.js +++ b/lib/actions/loadFilesInfo.js @@ -6,6 +6,7 @@ const removeCommentsFromTSX = require("../parsers/removeCommentsFromTSX"); const checkSyntaxError = require("../parsers/checkSyntaxError"); const { log } = require("../utils"); const checkForWildcardImports = require("../parsers/checkForWildcardImports"); +const compilerOptions = require("./compilerOptions"); /** * To be like: @@ -86,8 +87,16 @@ const processFileSchema = (filePath) => { fileImportsPath.forEach((importPath) => { // console.log("Check import Path:", importPath); - // Usa src para inicio ou o caminho do pai do arquivo sendo processado atualmente - let importedFileContentPath = path.join(parentFolder, importPath); + let importedFileContentPath = ""; + + // Replace path aliases + // Check if its has path alias + if (compilerOptions.hasPathAlias(importPath)) { + importedFileContentPath = compilerOptions.replacePathAlias(importPath); + } else { + // Usa src para inicio ou o caminho do pai do arquivo sendo processado atualmente + importedFileContentPath = path.join(parentFolder, importPath); + } importedFileContentPath = helpers.getFilePathWithType( importedFileContentPath, diff --git a/lib/actions/transformSchemaToWidget.js b/lib/actions/transformSchemaToWidget.js index c7cedb6..000531e 100644 --- a/lib/actions/transformSchemaToWidget.js +++ b/lib/actions/transformSchemaToWidget.js @@ -8,6 +8,7 @@ const { getImportedElementFileSource, getFilePathBasedOnParentAndChildFilePath, convertObjectToArray, + getFilePathWithType, } = require("../helpers"); const { process_file_content, removeImports } = require("../parse"); const { @@ -29,6 +30,7 @@ const transformImports = require("../parsers/transformImports"); const analyzeFunctionSignature = require("../parsers/analyzeFunctionSignature"); const removeFunctionParams = require("../parsers/removeFunctionParams"); const transformAsyncAwait = require("../parsers/transformAsyncAwait"); +const compilerOptions = require("./compilerOptions"); let processError = null; @@ -163,10 +165,22 @@ const processSchema = (fileSchema) => { // 2 - Se não for, continua o processo normalmente, se for, ignora o tratamento abaixo if (!isAlemFile) { - importedItemFileSource = getFilePathBasedOnParentAndChildFilePath( - fileSchema.filePath, - importedItemFileSource, - ); + // Check if its has path alias + if (compilerOptions.hasPathAlias(importedItemFileSource)) { + importedItemFileSource = compilerOptions.replacePathAlias( + importedItemFileSource, + ); + + // Set the final type file (.js / .ts / .jsx / .tsx) + importedItemFileSource = getFilePathWithType(importedItemFileSource); + } else { + importedItemFileSource = getFilePathBasedOnParentAndChildFilePath( + fileSchema.filePath, + importedItemFileSource, + ); + + console.log("C", importedItemFileSource); + } } componentImportItems[importedItem] = importedItemFileSource; diff --git a/lib/config.js b/lib/config.js index a0a32ab..1b72904 100644 --- a/lib/config.js +++ b/lib/config.js @@ -4,7 +4,6 @@ const fs = require("fs"); const path = require("path"); -// NOTE: not used function read_bos_config() { const configPath = path.join(".", "bos.config.json"); if (!fs.existsSync(configPath)) { diff --git a/lib/data.js b/lib/data.js index b24c8f8..ce1c0f2 100644 --- a/lib/data.js +++ b/lib/data.js @@ -29,7 +29,7 @@ function generate_data_json() { key === "isIndex" || key === "mainnetAccount" || key === "testnetAccount" || - key === "options" || + key === "compilerOptions" || key === "tags" ) { return; From ff2870dfe083b5184425570b10126791b74c8612 Mon Sep 17 00:00:00 2001 From: Wenderson Pires Date: Tue, 2 Apr 2024 11:09:16 -0300 Subject: [PATCH 2/2] beta.2 --- lib/actions/transformSchemaToWidget.js | 2 -- package.json | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/actions/transformSchemaToWidget.js b/lib/actions/transformSchemaToWidget.js index 000531e..686b7de 100644 --- a/lib/actions/transformSchemaToWidget.js +++ b/lib/actions/transformSchemaToWidget.js @@ -178,8 +178,6 @@ const processSchema = (fileSchema) => { fileSchema.filePath, importedItemFileSource, ); - - console.log("C", importedItemFileSource); } } diff --git a/package.json b/package.json index a14ab51..a585d42 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.1", + "version": "1.0.0-beta.2", "main": "main.js", "types": "index.d.ts", "author": "Wenderson Pires - wendersonpires.near",