mirror of https://github.com/abpframework/abp.git
5 changed files with 109 additions and 25 deletions
@ -0,0 +1,37 @@ |
|||
import * as ts from 'typescript'; |
|||
import { findNodes } from './angular/ast-utils'; |
|||
|
|||
export function findEnvironmentExpression(source: ts.SourceFile) { |
|||
const expressions = findNodes(source, ts.isObjectLiteralExpression); |
|||
return expressions.find(expr => expr.getText().includes('production')); |
|||
} |
|||
|
|||
export function getAssignedPropertyFromObjectliteral( |
|||
expression: ts.ObjectLiteralExpression, |
|||
variableSelector: string[], |
|||
) { |
|||
const expressions = findNodes(expression, isBooleanStringOrNumberLiteral); |
|||
|
|||
const literal = expressions.find(node => |
|||
Boolean( |
|||
variableSelector.reduceRight( |
|||
(acc: ts.PropertyAssignment, key) => |
|||
acc?.name?.getText() === key ? acc.parent.parent : undefined, |
|||
node.parent, |
|||
), |
|||
), |
|||
); |
|||
|
|||
return literal ? literal.getText() : undefined; |
|||
} |
|||
|
|||
export function isBooleanStringOrNumberLiteral( |
|||
node: ts.Node, |
|||
): node is ts.StringLiteral | ts.NumericLiteral | ts.BooleanLiteral { |
|||
return ( |
|||
ts.isStringLiteral(node) || |
|||
ts.isNumericLiteral(node) || |
|||
node.kind === ts.SyntaxKind.TrueKeyword || |
|||
node.kind === ts.SyntaxKind.FalseKeyword |
|||
); |
|||
} |
|||
@ -1,5 +1,6 @@ |
|||
export * from './angular'; |
|||
export * from './ast'; |
|||
export * from './common'; |
|||
export * from './project'; |
|||
export * from './source'; |
|||
export * from './text'; |
|||
export * from './workspace'; |
|||
|
|||
@ -1,22 +0,0 @@ |
|||
import { ProjectDefinition } from '@angular-devkit/core/src/workspace'; |
|||
import { SchematicsException, Tree } from '@angular-devkit/schematics'; |
|||
import { Exception } from '../enums'; |
|||
import { getWorkspace } from './angular/workspace'; |
|||
import { readWorkspaceSchema } from './workspace'; |
|||
|
|||
export async function resolveProject( |
|||
tree: Tree, |
|||
name: string, |
|||
): Promise<{ name: string; definition: ProjectDefinition }> { |
|||
const workspace = await getWorkspace(tree); |
|||
let definition = workspace.projects.get(name); |
|||
|
|||
if (!definition) { |
|||
name = readWorkspaceSchema(tree).defaultProject!; |
|||
definition = workspace.projects.get(name); |
|||
} |
|||
|
|||
if (!definition) throw new SchematicsException(Exception.NoProject); |
|||
|
|||
return { name, definition }; |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
import type { workspaces } from '@angular-devkit/core'; |
|||
import { SchematicsException, Tree } from '@angular-devkit/schematics'; |
|||
import got from 'got'; |
|||
import { Exception } from '../enums'; |
|||
import { getAssignedPropertyFromObjectliteral } from './ast'; |
|||
import { readEnvironment } from './workspace'; |
|||
|
|||
export async function getSourceJson(url: string) { |
|||
let body: any; |
|||
|
|||
try { |
|||
({ body } = await got(url, { |
|||
responseType: 'json', |
|||
https: { rejectUnauthorized: false }, |
|||
})); |
|||
} catch (e) { |
|||
throw new SchematicsException(Exception.NoApi); |
|||
} |
|||
|
|||
return body; |
|||
} |
|||
|
|||
export function getSourceUrl(tree: Tree, projectDefinition: workspaces.ProjectDefinition) { |
|||
const environmentExpr = readEnvironment(tree, projectDefinition); |
|||
let assignment: string | undefined; |
|||
|
|||
if (environmentExpr) { |
|||
assignment = getAssignedPropertyFromObjectliteral(environmentExpr, ['apis', 'default', 'url']); |
|||
} |
|||
|
|||
if (!assignment) throw new SchematicsException(Exception.RequiredApiUrl); |
|||
|
|||
return assignment.replace(/[`'"]/g, ''); |
|||
} |
|||
Loading…
Reference in new issue