@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
40 lines (39 loc) • 2.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "updateImportPathsInProject", {
enumerable: true,
get: function() {
return updateImportPathsInProject;
}
});
const _devkit = require("@nx/devkit");
const _nodepath = require("node:path");
const _removesourcefileextension = require("../path-utils/remove-source-file-extension");
const _getrelativeimportspecifier = require("../path-utils/get-relative-import-specifier");
const _jscodeshiftutils = require("../jscodeshift-utils");
function updateImportPathsInProject(tree, project, sourceFilePath, targetFilePath, getProjectSourceFilesFn) {
const sourceFiles = getProjectSourceFilesFn(tree, project.root);
const normalizedSourceWithoutExt = (0, _devkit.normalizePath)((0, _removesourcefileextension.removeSourceFileExtension)(sourceFilePath));
for (const normalizedFilePath of sourceFiles){
if (normalizedFilePath === sourceFilePath || normalizedFilePath === targetFilePath) {
continue;
}
const relativeSpecifier = (0, _getrelativeimportspecifier.getRelativeImportSpecifier)(normalizedFilePath, targetFilePath);
// Use jscodeshift to update imports that reference the source file
(0, _jscodeshiftutils.updateImportSpecifierPattern)(tree, normalizedFilePath, (specifier)=>{
// Match relative imports that reference the source file
if (!specifier.startsWith('.')) {
return false;
}
// Resolve the import specifier to an absolute path
const importerDir = _nodepath.posix.dirname(normalizedFilePath);
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;
}, ()=>relativeSpecifier);
}
}
//# sourceMappingURL=update-import-paths-in-project.js.map