Browse Source

feat: generate interfaces recursively in model schematics

pull/5137/head
Arman Ozak 6 years ago
parent
commit
adeab8a813
  1. 33
      npm/ng-packs/packages/schematics/src/utils/model.ts

33
npm/ng-packs/packages/schematics/src/utils/model.ts

@ -28,18 +28,11 @@ export function createImportRefsToModelMapper({ solution, types }: ModelGenerato
const namespace = parseNamespace(solution, importRefs[0]); const namespace = parseNamespace(solution, importRefs[0]);
const path = relativePathToModel(namespace, namespace); const path = relativePathToModel(namespace, namespace);
const model = new Model({ namespace, path }); const model = new Model({ namespace, path });
const imports: Import[] = [];
const reduceImportRefToImport = createImportRefToImportReducer(namespace); const reduceImportRefToImport = createImportRefToImportReducer(namespace, _import => {
const imports = importRefs.reduce((accumulatedImports, ref) => { if (_import.path === model.path) {
const interfaceDirect = mapImportRefToInterface(ref); _import.refs.forEach(ref => {
if (interfaceDirect && !types[ref].isEnum) model.interfaces.push(interfaceDirect);
return reduceImportRefToImport(accumulatedImports, ref);
}, []);
imports.forEach(_import => {
if (_import.path === model.path)
return _import.refs.forEach(ref => {
if (model.interfaces.some(i => i.ref === ref)) return; if (model.interfaces.some(i => i.ref === ref)) return;
const interfaceIndirect = mapImportRefToInterface(ref); const interfaceIndirect = mapImportRefToInterface(ref);
@ -47,9 +40,21 @@ export function createImportRefsToModelMapper({ solution, types }: ModelGenerato
reduceImportRefToImport(imports, ref); reduceImportRefToImport(imports, ref);
}); });
model.imports.push(_import); return false;
}
return true;
}); });
importRefs
.reduce((accumulatedImports, ref) => {
const interfaceDirect = mapImportRefToInterface(ref);
if (interfaceDirect && !types[ref].isEnum) model.interfaces.push(interfaceDirect);
return reduceImportRefToImport(accumulatedImports, ref);
}, imports)
.forEach(_import => model.imports.push(_import));
sortInterfaces(model.interfaces); sortInterfaces(model.interfaces);
return model; return model;
@ -92,7 +97,7 @@ export function createImportRefToImportReducerCreator(
solution: string, solution: string,
types: Record<string, Type>, types: Record<string, Type>,
) { ) {
return (namespace: string) => { return (namespace: string, filterFn: (imports: Import) => boolean) => {
const reduceTypesToImport = createTypesToImportsReducer(solution, namespace); const reduceTypesToImport = createTypesToImportsReducer(solution, namespace);
return (imports: Import[], importRef: string) => return (imports: Import[], importRef: string) =>
@ -105,7 +110,7 @@ export function createImportRefToImportReducerCreator(
return acc; return acc;
}, []), }, []),
); ).filter(filterFn);
}; };
} }

Loading…
Cancel
Save