import { TransformCallback, TransformWithPath, rxTransformTextFile } from '@ibm-wch-sdk/schematics-utils'; import { Tree } from '@angular-devkit/schematics'; import { Observable } from 'rxjs'; import { safeLoad, safeDump } from 'js-yaml'; import { map } from 'rxjs/operators'; /** * Reads a YAML file from the tree and then transforms it using the given function. If the result * is null or undefined, the file will be deleted, else replaced or created. * * @param aName name of the file * @param aOp the operator * @param aTree the tree to work in */ export function rxTransformYamlFile( aName: string, aOp: TransformCallback, aTree: Tree ): Observable { // cast const op: TransformWithPath = aOp as any; // dispatch return rxTransformTextFile( aName, (textContent, path) => op(textContent ? safeLoad(textContent) : undefined, path).pipe( map(data => safeDump(data)) ), aTree ); }