Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
folderPath = path.normalize(folderPath);
const endsWithSlash: boolean = folderPath.charAt(folderPath.length - 1) === path.sep;
const parsedPath: path.ParsedPath = path.parse(folderPath);
const pathRoot: string = parsedPath.root;
const pathWithoutRoot: string = parsedPath.dir.substr(pathRoot.length);
const pathParts: string[] = [...pathWithoutRoot.split(path.sep), parsedPath.name].filter((part) => !!part);
// Starting with all path sections, and eliminating one from the end during each loop iteration,
// run trueCasePathSync. If trueCasePathSync returns without exception, we've found a subset
// of the path that exists and we've now gotten the correct casing.
//
// Once we've found a parent folder that exists, append the path sections that didn't exist.
for (let i: number = pathParts.length; i >= 0; i--) {
const constructedPath: string = path.join(pathRoot, ...pathParts.slice(0, i));
try {
const normalizedConstructedPath: string = trueCasePathSync(constructedPath);
const result: string = path.join(normalizedConstructedPath, ...pathParts.slice(i));
if (endsWithSlash) {
return `${result}${path.sep}`;
} else {
return result;
}
} catch (e) {
// This path doesn't exist, continue to the next subpath
}
}
return undefined;
}
}
) {
absSource = path.resolve("src", source);
}
if (absSource) {
if (!fs.existsSync(absSource)) absSource = `${absSource}.js`;
if (!fs.existsSync(absSource)) {
console.error("#########################");
console.error("Incorrect import");
console.error("#########################");
console.error("IN ", fileInfo.path);
console.error("SOURCE ", source);
console.error("ABS ", absSource);
}
const trueFsPath = trueCasePathSync(absSource);
if (absSource !== trueFsPath) {
let newSource;
if (isRel) {
newSource = path.relative(fileInfo.path, trueFsPath);
} else if (endsWith(trueFsPath, ".js")) {
newSource = trueFsPath.slice(source.length * -1 - 3);
} else {
newSource = trueFsPath.slice(source.length * -1);
}
if (startsWith(source, "./") && startsWith(newSource, "../"))
newSource = newSource.substr(1);
if (startsWith(source, "../") && startsWith(newSource, "../"))
newSource = newSource.substr(3);
if (endsWith(newSource, ".js")) newSource = newSource.slice(0, -3);
console.log(`Transforming import in ${fileInfo.path}:`);
console.log(` from: ${source}`);