Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 32ceda1

Browse files
committed
chore(generators): wip: NgModule manip
1 parent aac2a38 commit 32ceda1

File tree

6 files changed

+57
-24
lines changed

6 files changed

+57
-24
lines changed

src/deep-linking/util.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@ import * as Constants from '../util/constants';
1919
import { FileCache } from '../util/file-cache';
2020
import { changeExtension, getStringPropertyValue, replaceAll } from '../util/helpers';
2121
import { BuildContext, ChangedFile, DeepLinkConfigEntry, DeepLinkDecoratorAndClass, DeepLinkPathInfo, File } from '../util/interfaces';
22-
import { appendAfter, getNgModuleClassName, getNgModuleDecorator, getClassDeclarations, getTypescriptSourceFile, getNodeStringContent, replaceNode } from '../util/typescript-utils';
22+
import {
23+
appendAfter,
24+
getClassDeclarations,
25+
getNgModuleClassName,
26+
getNgModuleDecorator,
27+
getNgModuleObjectLiteralArg,
28+
getTypescriptSourceFile,
29+
getNodeStringContent,
30+
replaceNode,
31+
} from '../util/typescript-utils';
2332

2433
import { transpileTsString } from '../transpile';
2534

@@ -188,14 +197,6 @@ export function hasExistingDeepLinkConfig(appNgModuleFilePath: string, appNgModu
188197
return deepLinkConfigArg.kind === SyntaxKind.ObjectLiteralExpression;
189198
}
190199

191-
function getNgModuleObjectLiteralArg(decorator: Decorator) {
192-
const ngModuleArgs = (decorator.expression as CallExpression).arguments;
193-
if (!ngModuleArgs || ngModuleArgs.length === 0 || ngModuleArgs.length > 1) {
194-
throw new Error(`Invalid NgModule Argument`);
195-
}
196-
return ngModuleArgs[0] as ObjectLiteralExpression;
197-
}
198-
199200
function getIonicModuleForRootCall(decorator: Decorator) {
200201
const argument = getNgModuleObjectLiteralArg(decorator);
201202
const properties = argument.properties.filter((property: PropertyAssignment) => {

src/generators.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Logger} from './logger/logger';
22
import { generateContext } from './util/config';
33
import * as Constants from './util/constants';
44
import { BuildContext } from './util/interfaces';
5+
import { readFileAsync } from './util/helpers';
6+
import { getTypescriptSourceFile, appendNgModuleDeclaration, insertNamedImportIfNeeded } from './util/typescript-utils';
57
import { applyTemplates, filterOutTemplates, getNgModules, GeneratorOption, GeneratorRequest, hydrateRequest, readTemplates, writeGeneratedFiles } from './generators/util';
68

79
export { getNgModules, GeneratorOption, GeneratorRequest };
@@ -15,9 +17,17 @@ export function processPageRequest(context: BuildContext, name: string) {
1517
return processNonTabRequest(context, { type: 'page', name });
1618
}
1719

18-
export function processPipeRequest(context: BuildContext, name: string, ngModulePaths: string[]) {
19-
return processNonTabRequest(context, { type: 'pipe', name }).then(() => {
20-
// TODO
20+
export function processPipeRequest(context: BuildContext, name: string, ngModulePath: string) {
21+
const hydratedRequest = hydrateRequest(context, { type: 'pipe', name });
22+
23+
return readFileAsync(ngModulePath).then((fileContent: string) => {
24+
fileContent = insertNamedImportIfNeeded(ngModulePath, fileContent, name, `./${name}`);
25+
fileContent = appendNgModuleDeclaration(ngModulePath, fileContent, name);
26+
// TODO: write file
27+
28+
return processNonTabRequest(context, hydratedRequest).then(() => {
29+
// TODO
30+
});
2131
});
2232
}
2333

src/generators/util.spec.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,6 @@ export class $CLASSNAMEModule {}
341341
context = { componentsDir, directivesDir, pagesDir, pipesDir, providersDir };
342342
});
343343

344-
function genMockGlobResults(types: string[]): GlobResult[] {
345-
return types.map((type) => {
346-
return { absolutePath: `/path/to/${type}`, base: '/path' }
347-
});
348-
}
349-
350344
it('should return an empty list of glob results', () => {
351345
const globAllSpy = spyOn(globUtils, globUtils.globAll.name);
352346
util.getNgModules(context, []);

src/util/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ export function generateContext(context?: BuildContext): BuildContext {
155155
setProcessEnvVar(Constants.ENV_APP_ENTRY_POINT, appEntryPointPathValue);
156156
Logger.debug(`appEntryPoint set to ${appEntryPointPathValue}`);
157157

158-
const appNgModulePath = resolve(getConfigValue(context, '--appNgModulePath', null, Constants.ENV_APP_NG_MODULE_PATH, Constants.ENV_APP_NG_MODULE_PATH.toLowerCase(), join(context.srcDir, 'app', 'app.module.ts')));
159-
setProcessEnvVar(Constants.ENV_APP_NG_MODULE_PATH, appNgModulePath);
160-
Logger.debug(`appNgModulePath set to ${appNgModulePath}`);
158+
context.appNgModulePath = resolve(getConfigValue(context, '--appNgModulePath', null, Constants.ENV_APP_NG_MODULE_PATH, Constants.ENV_APP_NG_MODULE_PATH.toLowerCase(), join(context.srcDir, 'app', 'app.module.ts')));
159+
setProcessEnvVar(Constants.ENV_APP_NG_MODULE_PATH, context.appNgModulePath);
160+
Logger.debug(`appNgModulePath set to ${context.appNgModulePath}`);
161161

162162
const appNgModuleClass = getConfigValue(context, '--appNgModuleClass', null, Constants.ENV_APP_NG_MODULE_CLASS, Constants.ENV_APP_NG_MODULE_CLASS.toLowerCase(), 'AppModule');
163163
setProcessEnvVar(Constants.ENV_APP_NG_MODULE_CLASS, appNgModuleClass);

src/util/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface BuildContext {
2020
nodeModulesDir?: string;
2121
ionicAngularDir?: string;
2222
moduleFiles?: string[];
23+
appNgModulePath?: string;
2324
isProd?: boolean;
2425
isWatch?: boolean;
2526
runAot?: boolean;

src/util/typescript-utils.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
import { Logger } from '../logger/logger';
1+
import * as path from 'path';
22

33
import {
44
CallExpression,
55
ClassDeclaration,
6-
createSourceFile,
76
Decorator,
87
Identifier,
98
ImportClause,
109
ImportDeclaration,
1110
ImportSpecifier,
1211
NamedImports,
1312
Node,
13+
NodeArray,
14+
ObjectLiteralElement,
15+
ObjectLiteralExpression,
16+
PropertyAssignment,
1417
ScriptTarget,
1518
SourceFile,
1619
StringLiteral,
17-
SyntaxKind
20+
SyntaxKind,
21+
createSourceFile,
1822
} from 'typescript';
1923

2024
import { rangeReplace, stringSplice } from './helpers';
25+
import { Logger } from '../logger/logger';
2126

2227
export function getTypescriptSourceFile(filePath: string, fileContent: string, languageVersion: ScriptTarget = ScriptTarget.Latest, setParentNodes: boolean = false): SourceFile {
2328
return createSourceFile(filePath, fileContent, languageVersion, setParentNodes);
@@ -215,4 +220,26 @@ export function getNgModuleDecorator(fileName: string, sourceFile: SourceFile) {
215220
return ngModuleDecorators[0];
216221
}
217222

223+
export function getNgModuleObjectLiteralArg(decorator: Decorator) {
224+
const ngModuleArgs = (decorator.expression as CallExpression).arguments;
225+
if (!ngModuleArgs || ngModuleArgs.length === 0 || ngModuleArgs.length > 1) {
226+
throw new Error(`Invalid NgModule Argument`);
227+
}
228+
return ngModuleArgs[0] as ObjectLiteralExpression;
229+
}
230+
231+
export function findObjectLiteralElementByName(properties: NodeArray<ObjectLiteralElement>, identifierToLookFor: string) {
232+
return properties.filter((propertyNode) => {
233+
return propertyNode && (propertyNode as PropertyAssignment).name && ((propertyNode as PropertyAssignment).name as Identifier).text === identifierToLookFor;
234+
})[0];
235+
}
236+
237+
export function appendNgModuleDeclaration(filePath: string, fileContent: string, declaration: string): string {
238+
const sourceFile = getTypescriptSourceFile(filePath, fileContent, ScriptTarget.Latest, false);
239+
const decorator = getNgModuleDecorator(path.basename(filePath), sourceFile);
240+
const obj = getNgModuleObjectLiteralArg(decorator);
241+
const declarations = (findObjectLiteralElementByName(obj.properties, 'declarations') as PropertyAssignment).initializer.elements;
242+
return appendAfter(fileContent, declarations[declarations.length - 1], `,\n ${declaration}`);
243+
}
244+
218245
const NG_MODULE_DECORATOR_TEXT = 'NgModule';

0 commit comments

Comments
 (0)