nextdevkit
Version:
A Comprehensive CLI Toolkit for Next.js Development
30 lines (29 loc) • 1.16 kB
JavaScript
import chalk from 'chalk';
import fs from 'fs-extra';
import path from 'path';
/**
* Retrieves the alias value from the project's tsconfig.json or jsconfig.json.
*
* @param {string} currentDirectory - The current working directory of the project.
* @param isTs - A boolean indicating whether the project uses TypeScript.
* @returns {Promise<string>} The alias value as a string. Defaults to '@' if not found.
*/
const getAliasValue = async (currentDirectory, isTs) => {
const configFileName = isTs ? 'tsconfig.json' : 'jsconfig.json';
const configFilePath = path.join(currentDirectory, configFileName);
if (await fs.pathExists(configFilePath)) {
try {
const config = await fs.readJson(configFilePath);
const paths = config.compilerOptions?.paths;
if (paths && typeof paths === 'object') {
const keys = Object.keys(paths);
return keys.length > 0 ? keys[0] : '@';
}
}
catch (error) {
console.error(chalk.red('Failed to parse config file for aliases.'), error);
}
}
return '@';
};
export default getAliasValue;