|
|
@ -1,7 +1,7 @@ |
|
|
import { SchematicsException, Tree } from '@angular-devkit/schematics'; |
|
|
import { SchematicsException, Tree } from '@angular-devkit/schematics'; |
|
|
import got from 'got'; |
|
|
import got from 'got'; |
|
|
import { Exception } from '../enums'; |
|
|
import { Exception } from '../enums'; |
|
|
import { ApiDefinition, Project, ProxyConfig } from '../models'; |
|
|
import { ApiDefinition, Project, ProxyConfig, WriteOp } from '../models'; |
|
|
import { getAssignedPropertyFromObjectliteral } from './ast'; |
|
|
import { getAssignedPropertyFromObjectliteral } from './ast'; |
|
|
import { interpolate } from './common'; |
|
|
import { interpolate } from './common'; |
|
|
import { readEnvironment } from './workspace'; |
|
|
import { readEnvironment } from './workspace'; |
|
|
@ -83,16 +83,37 @@ export function createProxyConfigReader(targetPath: string) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export function createProxyConfigSaver(apiDefinition: ApiDefinition, targetPath: string) { |
|
|
export function createProxyConfigSaver(apiDefinition: ApiDefinition, targetPath: string) { |
|
|
|
|
|
const createProxyConfigJson = createProxyConfigJsonCreator(apiDefinition); |
|
|
const readPreviousConfig = createProxyConfigReader(targetPath); |
|
|
const readPreviousConfig = createProxyConfigReader(targetPath); |
|
|
|
|
|
const writeNewConfig = createProxyConfigWriter(targetPath); |
|
|
|
|
|
|
|
|
return (tree: Tree) => { |
|
|
return (tree: Tree) => { |
|
|
|
|
|
const generated: string[] = []; |
|
|
|
|
|
let op: WriteOp = 'create'; |
|
|
|
|
|
|
|
|
if (tree.exists(targetPath)) { |
|
|
if (tree.exists(targetPath)) { |
|
|
const generated: string[] = []; |
|
|
op = 'overwrite'; |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
readPreviousConfig(tree).generated.forEach(m => generated.push(m)); |
|
|
readPreviousConfig(tree).generated.forEach(m => generated.push(m)); |
|
|
} catch (_) {} |
|
|
} catch (_) {} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
writeNewConfig(tree, op, createProxyConfigJson(generated)); |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
tree.overwrite(targetPath, JSON.stringify({ generated, ...apiDefinition }, null, 2)); |
|
|
export function createProxyConfigWriter(targetPath: string) { |
|
|
} else tree.create(targetPath, JSON.stringify(apiDefinition, null, 2)); |
|
|
return (tree: Tree, op: WriteOp, data: string) => { |
|
|
|
|
|
try { |
|
|
|
|
|
tree[op](targetPath, data); |
|
|
|
|
|
return tree; |
|
|
|
|
|
} catch (_) {} |
|
|
|
|
|
|
|
|
|
|
|
throw new SchematicsException(interpolate(Exception.FileWriteFailed, targetPath)); |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function createProxyConfigJsonCreator(apiDefinition: ApiDefinition) { |
|
|
|
|
|
return (generated: string[]) => JSON.stringify({ generated, ...apiDefinition }, null, 2); |
|
|
|
|
|
} |
|
|
|