Skip to content

Commit

Permalink
Merge pull request #7 from wpdas/feature/path-alias
Browse files Browse the repository at this point in the history
Feature: path alias support
  • Loading branch information
wpdas authored Apr 2, 2024
2 parents 429f88b + ff2870d commit f438cb5
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 9 deletions.
77 changes: 77 additions & 0 deletions lib/actions/compilerOptions.js
Original file line number Diff line number Diff line change
@@ -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,
};
13 changes: 11 additions & 2 deletions lib/actions/loadFilesInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 16 additions & 4 deletions lib/actions/transformSchemaToWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
getImportedElementFileSource,
getFilePathBasedOnParentAndChildFilePath,
convertObjectToArray,
getFilePathWithType,
} = require("../helpers");
const { process_file_content, removeImports } = require("../parse");
const {
Expand All @@ -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;

Expand Down Expand Up @@ -163,10 +165,20 @@ 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,
);
}
}

componentImportItems[importedItem] = importedItemFileSource;
Expand Down
1 change: 0 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function generate_data_json() {
key === "isIndex" ||
key === "mainnetAccount" ||
key === "testnetAccount" ||
key === "options" ||
key === "compilerOptions" ||
key === "tags"
) {
return;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit f438cb5

Please sign in to comment.