@nxworker/workspace
Version:
Nx plugin providing generators for managing workspace files, including the move-file generator for safely moving files between projects while updating all imports
43 lines (42 loc) • 1.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "checkForRelativeImportsInProject", {
enumerable: true,
get: function() {
return checkForRelativeImportsInProject;
}
});
const _devkit = require("@nx/devkit");
const _nodepath = require("node:path");
const _removesourcefileextension = require("../path-utils/remove-source-file-extension");
const _jscodeshiftutils = require("../jscodeshift-utils");
function checkForRelativeImportsInProject(tree, project, sourceFilePath, getProjectSourceFilesFn) {
const sourceFiles = getProjectSourceFilesFn(tree, project.root);
const normalizedSourceWithoutExt = (0, _devkit.normalizePath)((0, _removesourcefileextension.removeSourceFileExtension)(sourceFilePath));
for (const filePath of sourceFiles){
// Skip the source file itself
if (filePath === sourceFilePath) {
continue;
}
// Check if this file has an import that resolves to the source file
const hasImport = (0, _jscodeshiftutils.hasImportSpecifierMatching)(tree, filePath, (specifier)=>{
// Only check relative imports
if (!specifier.startsWith('.')) {
return false;
}
// Resolve the import specifier to an absolute path
const importerDir = _nodepath.posix.dirname(filePath);
const resolvedImport = _nodepath.posix.join(importerDir, specifier);
// Normalize and compare with source file (both without extension)
const normalizedResolvedImport = (0, _devkit.normalizePath)((0, _removesourcefileextension.removeSourceFileExtension)(resolvedImport));
return normalizedResolvedImport === normalizedSourceWithoutExt;
});
if (hasImport) {
return true;
}
}
return false;
}
//# sourceMappingURL=check-for-relative-imports-in-project.js.map