libs/nx/src/migrations/update-validation-errors-imports/cherry-picked/file-change-recorder.ts
Methods |
Accessors |
constructor(tree: Tree, filePath: string)
|
|||||||||
Parameters :
|
applyChanges |
applyChanges()
|
Returns :
void
|
hasChanged |
hasChanged()
|
Returns :
boolean
|
insertLeft |
insertLeft(index: number, content: string)
|
Returns :
void
|
insertRight |
insertRight(index: number, content: string)
|
Returns :
void
|
remove |
remove(index: number, end: number)
|
Returns :
void
|
replace | |||||||||
replace(node: Node, content: string)
|
|||||||||
Parameters :
Returns :
void
|
setContentToFileContent |
setContentToFileContent()
|
Returns :
void
|
content |
getcontent()
|
originalContent |
getoriginalContent()
|
import type { Tree } from '@nx/devkit';
import MagicString from 'magic-string';
import type { Node } from 'typescript';
export class FileChangeRecorder {
private mutableContent!: MagicString;
get content(): string {
return this.mutableContent.toString();
}
get originalContent(): string {
return this.mutableContent.original;
}
constructor(private readonly tree: Tree, private readonly filePath: string) {
this.setContentToFileContent();
}
applyChanges(): void {
this.tree.write(this.filePath, this.mutableContent.toString());
}
hasChanged(): boolean {
return this.mutableContent.hasChanged();
}
insertLeft(index: number, content: string): void {
this.mutableContent.appendLeft(index, content);
}
insertRight(index: number, content: string): void {
this.mutableContent.appendRight(index, content);
}
remove(index: number, end: number): void {
this.mutableContent.remove(index, end);
}
replace(node: Node, content: string): void {
this.mutableContent.overwrite(node.getStart(), node.getEnd(), content);
}
setContentToFileContent(): void {
const content = this.tree.read(this.filePath, 'utf-8');
if (!content) {
throw new Error('Could not read file content');
}
this.mutableContent = new MagicString(content);
}
}