@korautils/alias-fixer
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -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
43561
398