INNER CODE UNIT · TypeScript

updateMethod

maciejtreder/ng-toolkit · schematics/_utils/index.ts:149

export function updateMethod(tree: Tree, filePath: string, name: string, newBody: string): void {
    let fileContent = getFileContent(tree, filePath);
    let oldSignature = getMethodSignature(tree, filePath, name);
    if (oldSignature) {
        const oldBody = getMethodBody(tree, filePath, name) || '';
        let newMethodContent = oldSignature + newBody;
        let oldMethod = oldSignature + oldBody;

        tree.overwrite(filePath, fileContent.replace(oldMethod, newMethodContent));
    } else {
        throw new NgToolkitException(`Method ${name} not found in ${filePath}`, { fileContent: fileContent });
    }
}

export function getMethodSignature(tree: Tree, filePath: string, name: string): string | null {
    let fileContent = getFileContent(tree, filePath);
    let results: any = fileContent.match(new RegExp("(?:public|private|).*" + name + ".*?\\(([\\s\\S]*?\\))"));
    if (results) {

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…