feat: add rule creators for saving and reading api definition

pull/5137/head
Arman Ozak 5 years ago
parent c1807a49ec
commit bf1ba4c512

@ -1,12 +1,12 @@
import { SchematicsException, Tree } from '@angular-devkit/schematics';
import got from 'got';
import { Exception } from '../enums';
import { Project } from '../models';
import { ApiDefinition, Project } from '../models';
import { getAssignedPropertyFromObjectliteral } from './ast';
import { interpolate } from './common';
import { readEnvironment } from './workspace';
export async function getSourceJson(url: string) {
export async function getApiDefinition(url: string) {
let body: any;
try {
@ -45,3 +45,24 @@ export function getSourceUrl(tree: Tree, project: Project, moduleName: string) {
return assignment.replace(/[`'"]/g, '');
}
export function createApiDefinitionReader(targetPath: string) {
return (tree: Tree) => {
try {
const buffer = tree.read(targetPath);
const apiDefinition: ApiDefinition = JSON.parse(buffer!.toString());
return apiDefinition;
} catch (_) {}
throw new SchematicsException(interpolate(Exception.NoApiDefinition, targetPath));
};
}
export function createApiDefinitionSaver(apiDefinition: ApiDefinition, targetPath: string) {
return (tree: Tree) => {
tree[tree.exists(targetPath) ? 'overwrite' : 'create'](
targetPath,
JSON.stringify(apiDefinition, null, 2),
);
};
}

Loading…
Cancel
Save