Angular 13 with RxJS 7: How migrate $obs.toPromise() to lastValueFrom($obs) with a script?

Viewed 673

I'm migrating a Angular 12 project to Angular 13. Angular 13 come with RxJS 7. RxJS 7 has deprecated $obs.toPromise() in favor of lastValueFrom($obs).

Unfortunally, I'have tons of $obs.toPromise() and I'm searching a pratical and time saver way to make the migration.

There is a migration script?

Basically seem:

import * as fs from 'fs';
import * as glob from 'glob';

const files = glob.sync(`/**/*.ts`);
files.forEach(file => {
    let fileContent = fs.readFileSync(file, 'utf-8');
    if( fileContent.includes('toPromise()') ){
        fileContent = fileContent.replace(/await\s(.*)\.toPromise\(\)/gm, 'await lastValueFrom($1)');
        if( fileContent.includes('lastValueFrom') ){
          fileContent = 'import { lastValueFrom } from \'rxjs\';' + '\n' + fileContent;
          fs.writeFileSync(file, fileContent, 'utf-8');
        }
      }
    f
});
0 Answers
Related