79391441

Date: 2025-01-27 16:18:03
Score: 1
Natty:
Report link

ran some script like this u may modify this accordingly (i made chatgpt write this)

import path from "path";
import fs from "fs";

// Base directory for your source code
const directoryPath = "./src";
const aliasMapping = {
  components: "@Components",
  hooks: "@Hooks",
  assets: "@Assets",
  pages: "@Pages",
  routes: "@Routes",
  Animation: "@Animation",
  context: "@Context",
  utils: "@Utils",
};

// Function to resolve an absolute path
function resolveAbsolutePath(importPath, filePath) {
  const absolutePath = path.resolve(path.dirname(filePath), importPath);
  const relativeFromSrc = path.relative(
    path.resolve(directoryPath),
    absolutePath
  );

  const [topLevelFolder, ...rest] = relativeFromSrc.split(path.sep);
  const alias = aliasMapping[topLevelFolder];

  return alias ? `${alias}/${rest.join("/")}` : null;
}

// Function to update import paths
function updateImportPaths(filePath) {
  const fileContent = fs.readFileSync(filePath, "utf8");

  const updatedContent = fileContent.replace(
    /from\s+["'](\..*?)["']/g,
    (match, importPath) => {
      const resolvedPath = resolveAbsolutePath(importPath, filePath);
      return resolvedPath ? `from "${resolvedPath}"` : match;
    }
  );

  if (updatedContent !== fileContent) {
    fs.writeFileSync(filePath, updatedContent, "utf8");
    console.log(`Updated imports in ${filePath}`);
  }
}

// Recursively traverse the directory and update import paths
function traverseDirectory(dir) {
  const files = fs.readdirSync(dir);

  files.forEach((file) => {
    const filePath = path.join(dir, file);
    const stat = fs.statSync(filePath);

    if (stat.isDirectory()) {
      traverseDirectory(filePath); // Recursive call for directories
    } else if (filePath.endsWith(".tsx") || filePath.endsWith(".ts")) {
      updateImportPaths(filePath); // Update import paths for .tsx or .ts files
    }
  });
}

// Start the process
traverseDirectory(directoryPath);
Reasons:
  • RegEx Blacklisted phrase (1.5): resolvedPath ?
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: harshita rajpal