New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@korautils/alias-fixer

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@korautils/alias-fixer - npm Package Compare versions

Comparing version 1.0.4 to 1.0.5

59

dist/index.js

@@ -29,13 +29,2 @@ // src/index.ts

};
function extractSubPath(folderPath, fileFolder) {
const cleanFolderPath = folderPath.replace(/\/\*{1,2}(\*)?$/, "");
const normalizePath = (path2) => path2.replace(/\/+/g, "/").replace(/\/$/, "");
const normalizedFolderPath = normalizePath(cleanFolderPath);
const normalizedFileFolder = normalizePath(fileFolder);
if (!normalizedFileFolder.startsWith(normalizedFolderPath + "/")) {
return null;
}
const difference = normalizedFileFolder.slice(normalizedFolderPath.length + 1);
return difference;
}
function replaceImportsWithAliases(files, tsconfig) {

@@ -50,4 +39,7 @@ const paths = tsconfig.compilerOptions?.paths || {};

if (Object.prototype.hasOwnProperty.call(paths, alias)) {
const folderPath = paths[alias];
computedPaths.push({ alias, folder: folderPath[0] });
const folderPaths = paths[alias];
computedPaths.push({
alias: alias.replace(/\/\*{1,2}(\*)?$/, ""),
folders: folderPaths.map((folder) => path.join(process.cwd(), folder.replace(/\/\*{1,2}(\*)?$/, "")))
});
}

@@ -59,4 +51,4 @@ }

/(import\s+.*?['"])(\.{1,2}\/.*?)(['"])/g,
(_, prefix, relativePath, suffix) => {
let newPath = relativePath.replace(/\.\.\//g, "");
(fullMatch, prefix, relativePath, suffix) => {
const importedFilePath = path.resolve(path.dirname(file), relativePath);
const backFoldersTo = countRelativePaths(relativePath);

@@ -68,19 +60,36 @@ let currentDir = path.dirname(file);

if (!currentDir.startsWith(dir)) {
return `${prefix}${relativePath}${suffix}`;
return fullMatch;
}
const isOutsideAllPaths = computedPaths.every(
(aliasPath) => !aliasPath.folders.some(
(folderPath) => importedFilePath.startsWith(folderPath)
)
);
if (isOutsideAllPaths) {
return fullMatch;
}
let bestMatch = null;
computedPaths.forEach((aliasPath) => {
const folderPath = path.join(process.cwd(), aliasPath.folder.trim());
const fileFolder = path.dirname(file);
const subpath = extractSubPath(folderPath, fileFolder);
newPath = `${aliasPath.alias.replace(
/\/\*{1,2}(\*)?$/,
""
)}/${subpath}/${newPath}`;
aliasPath.folders.forEach((folderPath) => {
if (importedFilePath.startsWith(folderPath)) {
const relativeImportPath = path.relative(folderPath, importedFilePath);
const newPathCandidate = path.join(
aliasPath.alias,
relativeImportPath
);
if (!bestMatch || newPathCandidate.length > bestMatch.relativeImportPath.length) {
bestMatch = {
alias: aliasPath.alias,
relativeImportPath: newPathCandidate
};
}
}
});
});
return `${prefix}${newPath}${suffix}`;
return bestMatch ? `${prefix}${getProp(bestMatch, "relativeImportPath")}${suffix}` : fullMatch;
}
);
if (content !== updatedContent) {
console.log("Updated import in:", file);
fs.writeFileSync(file, updatedContent, "utf-8");
console.log(`Updated imports in: ${file}`);
}

@@ -87,0 +96,0 @@ });

{
"name": "@korautils/alias-fixer",
"version": "1.0.4",
"version": "1.0.5",
"description": "A development utility to automatically fix relative imports by replacing them with path aliases defined in your tsconfig.json. Ideal for streamlining large codebases with consistent import paths.",

@@ -5,0 +5,0 @@ "type": "module",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc