Browse Source

feat: sort service imports and methods in schematics

pull/5137/head
Arman Ozak 6 years ago
parent
commit
6e5c426411
  1. 8
      npm/ng-packs/packages/schematics/src/commands/api/files-service/shared/services/__namespace@dir__/__name@kebab__.service.ts.template
  2. 19
      npm/ng-packs/packages/schematics/src/utils/service.ts

8
npm/ng-packs/packages/schematics/src/commands/api/files-service/shared/services/__namespace@dir__/__name@kebab__.service.ts.template

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

19
npm/ng-packs/packages/schematics/src/utils/service.ts

@ -16,11 +16,29 @@ export function createControllerToServiceMapper(solution: string, apiName: strin
const namespace = parseNamespace(solution, controller.type); const namespace = parseNamespace(solution, controller.type);
const actions = Object.values(controller.actions); const actions = Object.values(controller.actions);
const imports = actions.reduce(createActionToImportsReducer(solution, namespace), []); 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); const methods = actions.map(mapActionToMethod);
sortMethods(methods);
return new Service({ apiName, imports, methods, name, namespace }); 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) { export function createActionToImportsReducer(solution: string, namespace: string) {
const mapTypeToImport = createTypeToImportMapper(solution, namespace); const mapTypeToImport = createTypeToImportMapper(solution, namespace);
@ -41,6 +59,7 @@ export function createActionToImportsReducer(solution: string, namespace: string
...new Set([...existingImport.specifiers, ...def.specifiers]), ...new Set([...existingImport.specifiers, ...def.specifiers]),
].sort(); ].sort();
}); });
return imports; return imports;
}; };
} }

Loading…
Cancel
Save