File

libs/nx/src/migrations/update-validation-errors-imports/cherry-picked/file-change-recorder.ts

Index

Methods
Accessors

Constructor

constructor(tree: Tree, filePath: string)
Parameters :
Name Type Optional
tree Tree No
filePath string No

Methods

applyChanges
applyChanges()
Returns : void
hasChanged
hasChanged()
Returns : boolean
insertLeft
insertLeft(index: number, content: string)
Parameters :
Name Type Optional
index number No
content string No
Returns : void
insertRight
insertRight(index: number, content: string)
Parameters :
Name Type Optional
index number No
content string No
Returns : void
remove
remove(index: number, end: number)
Parameters :
Name Type Optional
index number No
end number No
Returns : void
replace
replace(node: Node, content: string)
Parameters :
Name Type Optional
node Node No
content string No
Returns : void
setContentToFileContent
setContentToFileContent()
Returns : void

Accessors

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);
  }
}

results matching ""

    No results matching ""