UNPKG

@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) 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "updateRelativeImportsInMovedFile", { enumerable: true, get: function() { return updateRelativeImportsInMovedFile; } }); const _devkit = require("@nx/devkit"); const _nodepath = require("node:path"); const _treecache = require("../tree-cache"); const _jscodeshiftutils = require("../jscodeshift-utils"); const _getrelativeimportspecifier = require("../path-utils/get-relative-import-specifier"); function updateRelativeImportsInMovedFile(tree, normalizedSource, normalizedTarget) { const content = _treecache.treeReadCache.read(tree, normalizedTarget, 'utf-8'); if (!content) { return; } _devkit.logger.verbose(`Updating relative imports in moved file to maintain correct paths`); // Use jscodeshift to update relative imports in the moved file (0, _jscodeshiftutils.updateImportSpecifierPattern)(tree, normalizedTarget, (specifier)=>{ // Only process relative imports return specifier.startsWith('.'); }, (oldImportPath)=>{ // Calculate the new relative path from target to the imported file const sourceDir = _nodepath.posix.dirname(normalizedSource); // Resolve the import path relative to the original location const absoluteImportPath = _nodepath.posix.join(sourceDir, oldImportPath); // Calculate the new relative path from the target location const newRelativePath = (0, _getrelativeimportspecifier.getRelativeImportSpecifier)(normalizedTarget, absoluteImportPath); if (newRelativePath !== oldImportPath) { _devkit.logger.verbose(`Updated import '${oldImportPath}' to '${newRelativePath}' in moved file`); } return newRelativePath; }); } //# sourceMappingURL=update-relative-imports-in-moved-file.js.map