feat: sort service imports and methods in schematics

pull/5137/head
Arman Ozak 5 years ago
parent c6a4a3a62d
commit 6e5c426411

@ -1,8 +1,6 @@
import { RestService } from '@abp/ng.core';
import { Injectable } from '@angular/core';<%
for (let _import of imports) { %>
import type { <%= _import.specifiers.join(', ') %> } from '<%= _import.path %>';<% } %>
<% for (let _import of imports) {
%><%= _import.keyword %> { <%= _import.specifiers.join(', ') %> } from '<%= _import.path %>';
<% } %>
@Injectable({
providedIn: 'root',
})

@ -16,11 +16,29 @@ export function createControllerToServiceMapper(solution: string, apiName: strin
const namespace = parseNamespace(solution, controller.type);
const actions = Object.values(controller.actions);
const imports = actions.reduce(createActionToImportsReducer(solution, namespace), []);
imports.push(new Import({ path: '@abp/ng.core', specifiers: ['RestService'] }));
imports.push(new Import({ path: '@angular/core', specifiers: ['Injectable'] }));
sortImports(imports);
const methods = actions.map(mapActionToMethod);
sortMethods(methods);
return new Service({ apiName, imports, methods, name, namespace });
};
}
function sortImports(imports: Import[]) {
imports.sort((a, b) =>
removeRelative(a) > removeRelative(b) ? 1 : a.keyword > b.keyword ? 1 : -1,
);
}
function removeRelative(importDef: Import) {
return importDef.path.replace(/\.\.\//g, '');
}
function sortMethods(methods: Method[]) {
methods.sort((a, b) => (a.signature.name > b.signature.name ? 1 : -1));
}
export function createActionToImportsReducer(solution: string, namespace: string) {
const mapTypeToImport = createTypeToImportMapper(solution, namespace);
@ -41,6 +59,7 @@ export function createActionToImportsReducer(solution: string, namespace: string
...new Set([...existingImport.specifiers, ...def.specifiers]),
].sort();
});
return imports;
};
}

Loading…
Cancel
Save