Browse Source

feat: parse controller and create service path

pull/5137/head
Arman Ozak 6 years ago
parent
commit
5358d5b916
  1. 0
      npm/ng-packs/packages/schematics/src/commands/proxy/files-model/__sharedPath__/models/__name@kebab__.ts.template
  2. 0
      npm/ng-packs/packages/schematics/src/commands/proxy/files-service/__sharedPath__/services/__name@kebab__.service.ts.template
  3. 42
      npm/ng-packs/packages/schematics/src/commands/proxy/index.ts
  4. 4
      npm/ng-packs/packages/schematics/src/commands/proxy/schema.json
  5. 10
      npm/ng-packs/packages/schematics/src/commands/proxy/schema.ts
  6. 1
      npm/ng-packs/packages/schematics/src/utils/index.ts
  7. 26
      npm/ng-packs/packages/schematics/src/utils/rule.ts
  8. 26
      npm/ng-packs/packages/schematics/src/utils/workspace.ts

0
npm/ng-packs/packages/schematics/src/commands/proxy/files-model/__sharedPath__/enums/__name@kebab__.ts.template → npm/ng-packs/packages/schematics/src/commands/proxy/files-model/__sharedPath__/models/__name@kebab__.ts.template

0
npm/ng-packs/packages/schematics/src/commands/proxy/files-service/__sharedPath__/enums/__name@kebab__.service.ts.template → npm/ng-packs/packages/schematics/src/commands/proxy/files-service/__sharedPath__/services/__name@kebab__.service.ts.template

42
npm/ng-packs/packages/schematics/src/commands/proxy/index.ts

@ -1,8 +1,9 @@
import { strings } from '@angular-devkit/core'; import { normalize, strings } from '@angular-devkit/core';
import { chain, SchematicContext, Tree } from '@angular-devkit/schematics'; import { applyTemplates, branchAndMerge, chain, move, SchematicContext, Tree, url } from '@angular-devkit/schematics';
import { API_DEFINITION_ENDPOINT } from '../../constants'; import { API_DEFINITION_ENDPOINT } from '../../constants';
import { ApiDefinition } from '../../models'; import { ApiDefinition } from '../../models';
import { getSourceJson, getSourceUrl, resolveProject } from '../../utils'; import { applyWithOverwrite, buildDefaultPath, getSourceJson, getSourceUrl, isLibrary, resolveProject } from '../../utils';
import * as cases from '../../utils/text';
import type { Schema as GenerateProxySchema } from './schema'; import type { Schema as GenerateProxySchema } from './schema';
export default function(params: GenerateProxySchema) { export default function(params: GenerateProxySchema) {
@ -11,20 +12,35 @@ export default function(params: GenerateProxySchema) {
return chain([ return chain([
async (tree: Tree, _context: SchematicContext) => { async (tree: Tree, _context: SchematicContext) => {
const source = await resolveProject(tree, params.source!); const source = await resolveProject(tree, params.source!);
const url = getSourceUrl(tree, source, moduleName); const target = await resolveProject(tree, params.target!);
const data: ApiDefinition = await getSourceJson(url + API_DEFINITION_ENDPOINT); const isModule = isLibrary(target.definition);
const sourceUrl = getSourceUrl(tree, source, moduleName);
const targetPath = buildDefaultPath(target.definition);
const data: ApiDefinition = await getSourceJson(sourceUrl + API_DEFINITION_ENDPOINT);
const services = Object.entries(data.modules).map(([name, def]) => [ const controllers = Object.values(data.modules[moduleName]?.controllers || {});
name,
Object.values(def.controllers).map( const createServiceFiles = chain(
({controllerName, actions}) => [controllerName, Object.keys(actions)] controllers.map(controller => {
const {type} = controller;
const [namespace] = type.replace(/^Volo\.(Abp\.)?/, '').split('.');
return applyWithOverwrite(url('./files-service'), [
applyTemplates({
...cases,
name: controller.type.split('.').pop()!.replace('Controller', ''),
sharedPath: 'shared/' + strings.dasherize(namespace),
controller,
}),
move(normalize(targetPath)),
]);
}
), ),
]); );
const defs = services.filter(([name]) => name === moduleName); console.log(isModule);
console.log(defs); return branchAndMerge(chain([createServiceFiles]));
return chain([]);
}, },
]); ]);
} }

4
npm/ng-packs/packages/schematics/src/commands/proxy/schema.json

@ -24,8 +24,8 @@
}, },
"x-prompt": "Plese enter Angular project name to resolve API definition URL from. (default: workspace \"defaultProject\")" "x-prompt": "Plese enter Angular project name to resolve API definition URL from. (default: workspace \"defaultProject\")"
}, },
"destination": { "target": {
"alias": "d", "alias": "t",
"description": "Angular project to place the generated code in", "description": "Angular project to place the generated code in",
"type": "string", "type": "string",
"x-prompt": "Plese enter Angular project name to place generated code in. (default: workspace \"defaultProject\")" "x-prompt": "Plese enter Angular project name to place generated code in. (default: workspace \"defaultProject\")"

10
npm/ng-packs/packages/schematics/src/commands/proxy/schema.ts

@ -1,9 +1,4 @@
export interface Schema { export interface Schema {
/**
* Angular project to place the generated code in
*/
destination?: string;
/** /**
* The name of the backend module to generate code for * The name of the backend module to generate code for
*/ */
@ -13,4 +8,9 @@ export interface Schema {
* Angular project to resolve API definition URL from * Angular project to resolve API definition URL from
*/ */
source?: string; source?: string;
/**
* Angular project to place the generated code in
*/
target?: string;
} }

1
npm/ng-packs/packages/schematics/src/utils/index.ts

@ -1,6 +1,7 @@
export * from './angular'; export * from './angular';
export * from './ast'; export * from './ast';
export * from './common'; export * from './common';
export * from './rule';
export * from './source'; export * from './source';
export * from './text'; export * from './text';
export * from './workspace'; export * from './workspace';

26
npm/ng-packs/packages/schematics/src/utils/rule.ts

@ -0,0 +1,26 @@
import {
apply,
forEach,
mergeWith,
Rule,
SchematicContext,
Source,
Tree,
} from '@angular-devkit/schematics';
export function applyWithOverwrite(source: Source, rules: Rule[]): Rule {
return (tree: Tree, _context: SchematicContext) => {
const rule = mergeWith(apply(source, [...rules, overwriteFileIfExists(tree)]));
return rule(tree, _context);
};
}
export function overwriteFileIfExists(tree: Tree): Rule {
return forEach(fileEntry => {
if (!tree.exists(fileEntry.path)) return fileEntry;
tree.overwrite(fileEntry.path, fileEntry.content);
return null;
});
}

26
npm/ng-packs/packages/schematics/src/utils/workspace.ts

@ -1,4 +1,4 @@
import type { experimental, workspaces } from '@angular-devkit/core'; import { experimental, strings, workspaces } from '@angular-devkit/core';
import { SchematicsException } from '@angular-devkit/schematics'; import { SchematicsException } from '@angular-devkit/schematics';
import type { Tree } from '@angular-devkit/schematics'; import type { Tree } from '@angular-devkit/schematics';
import { Exception } from '../enums'; import { Exception } from '../enums';
@ -41,7 +41,29 @@ export async function resolveProject(
): Promise<Project> { ): Promise<Project> {
name = name || readWorkspaceSchema(tree).defaultProject!; name = name || readWorkspaceSchema(tree).defaultProject!;
const workspace = await getWorkspace(tree); const workspace = await getWorkspace(tree);
const definition = workspace.projects.get(name); let definition: Project['definition'] | undefined;
try {
definition = workspace.projects.get(name);
} catch (_) {}
if (!definition)
try {
name = strings.dasherize(name);
definition = workspace.projects.get(name);
} catch (_) {}
if (!definition)
try {
name = strings.camelize(name);
definition = workspace.projects.get(name);
} catch (_) {}
if (!definition)
try {
name = strings.classify(name);
definition = workspace.projects.get(name);
} catch (_) {}
if (!definition) throw new SchematicsException(Exception.NoProject); if (!definition) throw new SchematicsException(Exception.NoProject);

Loading…
Cancel
Save