-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from wpdas/feature/path-alias
Feature: path alias support
- Loading branch information
Showing
6 changed files
with
106 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters