Browse Source

feat: modify schematics params according to new contract

pull/5137/head
Arman Ozak 6 years ago
parent
commit
ab5a5a46b4
  1. 3
      npm/ng-packs/apps/dev-app/src/environments/environment.ts
  2. 22
      npm/ng-packs/packages/schematics/src/commands/proxy/index.ts
  3. 23
      npm/ng-packs/packages/schematics/src/commands/proxy/schema.json
  4. 15
      npm/ng-packs/packages/schematics/src/commands/proxy/schema.ts
  5. 1
      npm/ng-packs/packages/schematics/src/constants/api.ts
  6. 1
      npm/ng-packs/packages/schematics/src/constants/index.ts
  7. 5
      npm/ng-packs/packages/schematics/src/enums/exception.ts
  8. 1
      npm/ng-packs/packages/schematics/src/models/index.ts
  9. 6
      npm/ng-packs/packages/schematics/src/models/project.ts
  10. 34
      npm/ng-packs/packages/schematics/src/utils/source.ts
  11. 11
      npm/ng-packs/packages/schematics/src/utils/workspace.ts

3
npm/ng-packs/apps/dev-app/src/environments/environment.ts

@ -21,5 +21,8 @@ export const environment = {
default: { default: {
url: 'https://localhost:44305', url: 'https://localhost:44305',
}, },
saas: {
url: 'https://localhost:44305',
},
}, },
} as Config.Environment; } as Config.Environment;

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

@ -1,15 +1,29 @@
import { strings } from '@angular-devkit/core';
import { chain, SchematicContext, Tree } from '@angular-devkit/schematics'; import { chain, SchematicContext, Tree } from '@angular-devkit/schematics';
import { API_DEFINITION_ENDPOINT } from '../../constants';
import { ApiDefinition } from '../../models';
import { getSourceJson, getSourceUrl, resolveProject } from '../../utils'; import { getSourceJson, getSourceUrl, resolveProject } from '../../utils';
import type { Schema as GenerateProxySchema } from './schema'; import type { Schema as GenerateProxySchema } from './schema';
export default function(params: GenerateProxySchema) { export default function(params: GenerateProxySchema) {
const moduleName = strings.camelize(params.module || 'app');
return chain([ return chain([
async (tree: Tree, _context: SchematicContext) => { async (tree: Tree, _context: SchematicContext) => {
const project = await resolveProject(tree, params.module!); const source = await resolveProject(tree, params.source!);
const url = params.source || getSourceUrl(tree, project.definition) + '/api/abp/api-definition'; const url = getSourceUrl(tree, source, moduleName);
const data = await getSourceJson(url); const data: ApiDefinition = await getSourceJson(url + API_DEFINITION_ENDPOINT);
const services = Object.entries(data.modules).map(([name, def]) => [
name,
Object.values(def.controllers).map(
({controllerName, actions}) => [controllerName, Object.keys(actions)]
),
]);
const defs = services.filter(([name]) => name === moduleName);
console.log(Object.keys(data.types)); console.log(defs);
return chain([]); return chain([]);
}, },
]); ]);

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

@ -6,36 +6,29 @@
"properties": { "properties": {
"module": { "module": {
"alias": "m", "alias": "m",
"description": "The name of the module to generate code for", "description": "The name of the backend module to generate code for",
"type": "string", "type": "string",
"$default": { "$default": {
"$source": "argv", "$source": "argv",
"index": 0 "index": 0
}, },
"x-prompt": "Please enter name of the module you wish to generate proxies for. (default: \"app\")" "x-prompt": "Please enter name of the backend module you wish to generate proxies for. (default: \"app\")"
}, },
"source": { "source": {
"alias": "s", "alias": "s",
"description": "The URL to get API definitions from", "description": "Angular project to resolve API definition URL from",
"type": "string", "type": "string",
"$default": { "$default": {
"$source": "argv", "$source": "argv",
"index": 1 "index": 1
}, },
"x-prompt": "Plese enter URL to get API definitions from. (default: resolved from environment)" "x-prompt": "Plese enter Angular project name to resolve API definition URL from. (default: workspace \"defaultProject\")"
}, },
"project": { "destination": {
"alias": "p", "alias": "d",
"description": "The project to place the generated code in", "description": "Angular project to place the generated code in",
"type": "string", "type": "string",
"x-prompt": "Please enter the project to place proxies in. (default: workspace \"defaultProject\")" "x-prompt": "Plese enter Angular project name to place generated code in. (default: workspace \"defaultProject\")"
},
"path": {
"description": "The path to place the generated code at",
"type": "string",
"format": "path",
"default": "",
"visible": false
} }
}, },
"required": [] "required": []

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

@ -1,21 +1,16 @@
export interface Schema { export interface Schema {
/** /**
* The name of the module to generate code for * Angular project to place the generated code in
*/ */
module?: string; destination?: string;
/** /**
* The project to place the generated code in * The name of the backend module to generate code for
*/ */
project?: string; module?: string;
/**
* The path to place the generated code at
*/
path?: string;
/** /**
* The URL to get API configuration from * Angular project to resolve API definition URL from
*/ */
source?: string; source?: string;
} }

1
npm/ng-packs/packages/schematics/src/constants/api.ts

@ -0,0 +1 @@
export const API_DEFINITION_ENDPOINT = '/api/abp/api-definition';

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

@ -0,0 +1 @@
export * from './api';

5
npm/ng-packs/packages/schematics/src/enums/exception.ts

@ -1,8 +1,9 @@
export const enum Exception { export const enum Exception {
FileNotFound = '[File Not Found] There is no file at "{0}" path.', FileNotFound = '[File Not Found] There is no file at "{0}" path.',
InvalidWorkspace = '[Invalid Workspace] The angular.json should be a valid JSON file.', InvalidWorkspace = '[Invalid Workspace] The angular.json should be a valid JSON file.',
NoApi = '[API Not Available] Please double-check the source url and make sure your application is up and running.', NoApi = '[API Not Available] Please double-check the URL in the source project environment and make sure your application is up and running.',
NoProject = '[Project Not Found] Either define a default project in your workspace or specify the project name in schematics options.', NoProject = '[Project Not Found] Either define a default project in your workspace or specify the project name in schematics options.',
NoWorkspace = '[Workspace Not Found] Make sure you are running schematics at the root directory of your workspace and it has an angular.json file.', NoWorkspace = '[Workspace Not Found] Make sure you are running schematics at the root directory of your workspace and it has an angular.json file.',
RequiredApiUrl = '[API URL Required] API URL cannot be resolved. Please re-run the schematics and enter the URL to get API definitions from.', NoEnvironment = '[Environment Not Found] An environment file cannot be located in "{0}" project.',
NoApiUrl = '[API URL Not Found] Cannot resolve API URL for "{1}" module from "{0}" project.',
} }

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

@ -1 +1,2 @@
export * from './api-definition'; export * from './api-definition';
export * from './project';

6
npm/ng-packs/packages/schematics/src/models/project.ts

@ -0,0 +1,6 @@
import { workspaces } from '@angular-devkit/core';
export interface Project {
name: string;
definition: workspaces.ProjectDefinition;
}

34
npm/ng-packs/packages/schematics/src/utils/source.ts

@ -1,8 +1,9 @@
import type { workspaces } from '@angular-devkit/core';
import { SchematicsException, Tree } from '@angular-devkit/schematics'; import { SchematicsException, Tree } from '@angular-devkit/schematics';
import got from 'got'; import got from 'got';
import { Exception } from '../enums'; import { Exception } from '../enums';
import { Project } from '../models';
import { getAssignedPropertyFromObjectliteral } from './ast'; import { getAssignedPropertyFromObjectliteral } from './ast';
import { interpolate } from './common';
import { readEnvironment } from './workspace'; import { readEnvironment } from './workspace';
export async function getSourceJson(url: string) { export async function getSourceJson(url: string) {
@ -14,28 +15,33 @@ export async function getSourceJson(url: string) {
searchParams: { includeTypes: true }, searchParams: { includeTypes: true },
https: { rejectUnauthorized: false }, https: { rejectUnauthorized: false },
})); }));
} catch (err) { } catch ({ response }) {
// handle redirects // handle redirects
try { if (response?.body && response.statusCode < 400) return response.body;
({ response: { body } } = err);
if (!body.types) throw Error(''); throw new SchematicsException(Exception.NoApi);
} catch (_) {
throw new SchematicsException(Exception.NoApi);
}
} }
return body; return body;
} }
export function getSourceUrl(tree: Tree, projectDefinition: workspaces.ProjectDefinition) { export function getSourceUrl(tree: Tree, project: Project, moduleName: string) {
const environmentExpr = readEnvironment(tree, projectDefinition); const environmentExpr = readEnvironment(tree, project.definition);
let assignment: string | undefined;
if (!environmentExpr)
throw new SchematicsException(interpolate(Exception.NoEnvironment, project.name));
if (environmentExpr) { let assignment = getAssignedPropertyFromObjectliteral(environmentExpr, [
'apis',
moduleName,
'url',
]);
if (!assignment)
assignment = getAssignedPropertyFromObjectliteral(environmentExpr, ['apis', 'default', 'url']); assignment = getAssignedPropertyFromObjectliteral(environmentExpr, ['apis', 'default', 'url']);
}
if (!assignment) throw new SchematicsException(Exception.RequiredApiUrl); if (!assignment)
throw new SchematicsException(interpolate(Exception.NoApiUrl, project.name, moduleName));
return assignment.replace(/[`'"]/g, ''); return assignment.replace(/[`'"]/g, '');
} }

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

@ -2,6 +2,7 @@ import type { experimental, 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';
import { Project } from '../models';
import { getWorkspace, ProjectType } from './angular'; import { getWorkspace, ProjectType } from './angular';
import { findEnvironmentExpression } from './ast'; import { findEnvironmentExpression } from './ast';
import { readFileInTree } from './common'; import { readFileInTree } from './common';
@ -37,14 +38,10 @@ export function readWorkspaceSchema(tree: Tree) {
export async function resolveProject( export async function resolveProject(
tree: Tree, tree: Tree,
name: string, name: string,
): Promise<{ name: string; definition: workspaces.ProjectDefinition }> { ): Promise<Project> {
name = name || readWorkspaceSchema(tree).defaultProject!;
const workspace = await getWorkspace(tree); const workspace = await getWorkspace(tree);
let definition = workspace.projects.get(name); const definition = workspace.projects.get(name);
if (!definition) {
name = readWorkspaceSchema(tree).defaultProject!;
definition = workspace.projects.get(name);
}
if (!definition) throw new SchematicsException(Exception.NoProject); if (!definition) throw new SchematicsException(Exception.NoProject);

Loading…
Cancel
Save