import { Path, resolve } from '@angular-devkit/core'; import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; import { rxTransformJsonFile } from '@ibm-wch-sdk/schematics-utils'; import { isNotNil } from '@ibm-wch-sdk/utils'; import { of } from 'rxjs'; import { mapTo } from 'rxjs/operators'; import { findWchToolsOptions } from '../utilities/wch'; import { Schema } from './schema'; export const OLD_WCHTOOLS_OPTIONS: Path = '../.wchtoolsoptions' as Path; export function updateWchtoolsOptions(options: Schema): Rule { return (host: Tree, context: SchematicContext) => { // data dir const url = options.url; const opts = findWchToolsOptions(host); if (!host.exists(opts)) { // check if we can move the other file const oldOpts = resolve(opts, OLD_WCHTOOLS_OPTIONS); if (host.exists(oldOpts)) { // move host.rename(oldOpts, opts); } } if (isNotNil(url)) { return rxTransformJsonFile( opts, (aOpts: any) => of({ ...aOpts, 'x-ibm-dx-tenant-base-url': url }), host ).pipe(mapTo(host)); } return host; }; }