Skip to content

Commit db9ada9

Browse files
authored
refactor: use NG CLI utility functions when getting entry module metadata (#123)
1 parent b91eb0f commit db9ada9

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

src/angular-project-parser.ts

+20-19
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import { join, dirname, basename } from 'path';
33
import { Tree, SchematicsException } from '@angular-devkit/schematics';
44
import { getWorkspace } from '@schematics/angular/utility/config';
55
import { getProjectTargets } from '@schematics/angular/utility/project-targets';
6+
import {
7+
getAppModulePath,
8+
findBootstrapModuleCall,
9+
findBootstrapModulePath,
10+
} from '@schematics/angular/utility/ng-ast-utils';
611

712
import { getSourceFile, safeGet } from './utils';
8-
import { findNode, getFunctionParams, findImportPath } from './ast-utils';
13+
import { findNode, findImportPath } from './ast-utils';
914

1015
export interface AngularProjectSettings {
1116
/** default: '' */
@@ -106,7 +111,7 @@ export function getAngularProjectSettings(tree: Tree, projectName: string): Angu
106111
};
107112
}
108113

109-
export function getCoreProjectSettings(tree: Tree, projectName: string): CoreProjectSettings {
114+
function getCoreProjectSettings(tree: Tree, projectName: string): CoreProjectSettings {
110115
const project = getProjectObject(tree, projectName);
111116
const targets = getProjectTargets(project);
112117
if (!targets) {
@@ -149,28 +154,24 @@ function getProjectObject(tree: Tree, projectName: string) {
149154
return project;
150155
}
151156

152-
// Step 2 - get entryModule and entryModulePath => open ${sourceRoot}/${main}.ts
153-
// - get entryModule from .bootstrapModule(__value__)
154-
// - get entryModulePath from import { ${entryModule} } from '__value__' -- might need to remove ./
155157
function getEntryModuleMetadata(tree: Tree, mainPath: string): ClassMetadata {
156-
const source = getSourceFile(tree, mainPath);
157-
158-
const params = getFunctionParams(source, 'bootstrapModule');
159-
const className = params[0];
160-
161-
const name = className.replace('Module', '');
162-
163-
const importPath: string = findImportPath(source, className);
164-
165-
const mainDir = dirname(mainPath);
166-
const path = join(mainDir, importPath) + '.ts';
158+
const bootstrapCall = findBootstrapModuleCall(tree, mainPath);
159+
if (!bootstrapCall) {
160+
throw new SchematicsException('Bootstrap call not found! Cannot build project data!');
161+
}
162+
const className = bootstrapCall.arguments[0].getText();
163+
const name = className.replace(/Module$/, '');
164+
const importPath = findBootstrapModulePath(tree, mainPath);
165+
const path = getAppModulePath(tree, mainPath);
167166

168-
return {
167+
const metadata = {
169168
className,
170169
name,
171170
importPath,
172-
path
173-
}
171+
path,
172+
};
173+
174+
return metadata;
174175
}
175176

176177
// Step 3 - get appComponent and appComponentPath => open ${appRoot}/${entryModulePath}

src/ast-utils.ts

-11
Original file line numberDiff line numberDiff line change
@@ -594,17 +594,6 @@ function checkNameForKind(node: ts.Node, searchParam: SearchParam): boolean {
594594
return child.getText() === searchParam.name;
595595
}
596596

597-
export function getFunctionParams(source: ts.Node, name: string) {
598-
const node = findNode<ts.CallExpression>(source, [
599-
{ kind: ts.SyntaxKind.CallExpression, name },
600-
]);
601-
602-
if (node.getChildCount() !== 4) {
603-
throw new SchematicsException(`Couldn't find function params for ${name} in ${node.getSourceFile().fileName}`);
604-
}
605-
return node.arguments.map(node => node.getText());
606-
}
607-
608597
export function findImportPath(source: ts.Node, name) {
609598
const node = findNode<ts.ImportDeclaration>(source, [
610599
{ kind: ts.SyntaxKind.ImportDeclaration, name },

0 commit comments

Comments
 (0)