@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
113 lines (112 loc) • 3.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
astCache: function() {
return astCache;
},
j: function() {
return j;
}
});
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
const _devkit = require("@nx/devkit");
const _jscodeshift = /*#__PURE__*/ _interop_require_wildcard._(require("jscodeshift"));
// Create parser instance once and reuse it for better performance
const j = _jscodeshift.withParser('tsx');
/**
* Cache for storing parsed ASTs to avoid re-parsing files during a move operation.
* This is particularly beneficial when multiple update operations touch the same files.
*/ let ASTCache = class ASTCache {
/**
* Gets the file content, using cache if available.
*/ getContent(tree, filePath) {
// Check cache first
const cached = this.contentCache.get(filePath);
if (cached !== undefined) {
return cached;
}
// Read from tree
const content = tree.read(filePath, 'utf-8');
if (!content) {
return null;
}
// Cache the content
this.contentCache.set(filePath, content);
return content;
}
/**
* Gets the parsed AST for a file, using cache if available.
* Returns null if the file cannot be parsed or doesn't exist.
*/ getAST(tree, filePath) {
// Check if this file previously failed to parse
if (this.parseAttempts.get(filePath) === false) {
return null;
}
// Check cache first
const cached = this.astCache.get(filePath);
if (cached !== undefined) {
return cached;
}
// Get content (from cache or read)
const content = this.getContent(tree, filePath);
if (!content || content.trim().length === 0) {
return null;
}
// Try to parse
try {
const ast = j(content);
this.astCache.set(filePath, ast);
this.parseAttempts.set(filePath, true);
return ast;
} catch (error) {
// Mark as failed to avoid repeated parse attempts
this.parseAttempts.set(filePath, false);
_devkit.logger.warn(`Unable to parse ${filePath}. Caching parse failure. Error: ${error}`);
return null;
}
}
/**
* Invalidates the cache entry for a file after it has been modified.
* This ensures subsequent reads get the updated content.
*/ invalidate(filePath) {
this.contentCache.delete(filePath);
this.astCache.delete(filePath);
this.parseAttempts.delete(filePath);
}
/**
* Clears all cached data. Useful when starting a new move operation.
*/ clear() {
this.contentCache.clear();
this.astCache.clear();
this.parseAttempts.clear();
}
/**
* Returns cache statistics for monitoring/debugging.
*/ getStats() {
let failedParseCount = 0;
for (const failed of this.parseAttempts.values()){
if (!failed) failedParseCount++;
}
return {
contentCacheSize: this.contentCache.size,
astCacheSize: this.astCache.size,
failedParseCount
};
}
constructor(){
this.contentCache = new Map();
this.astCache = new Map();
this.parseAttempts = new Map() // Track files that failed to parse
;
}
};
const astCache = new ASTCache();
//# sourceMappingURL=ast-cache.js.map