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

Commit 8e7968a

Browse files
committed
refactor(generators): add utility functions
1 parent e2aed27 commit 8e7968a

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

src/generators.ts

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import { Logger} from './logger/logger';
21
import { generateContext } from './util/config';
32
import * as Constants from './util/constants';
43
import { BuildContext } from './util/interfaces';
5-
import { readFileAsync, writeFileAsync } from './util/helpers';
6-
import { getTypescriptSourceFile, appendNgModuleDeclaration, insertNamedImportIfNeeded } from './util/typescript-utils';
7-
import { applyTemplates, filterOutTemplates, getNgModules, GeneratorOption, GeneratorRequest, hydrateRequest, readTemplates, writeGeneratedFiles } from './generators/util';
8-
import * as path from 'path';
4+
import { getNgModules, GeneratorOption, GeneratorRequest, nonPageFileManipulation, processNonTabRequest } from './generators/util';
95

106
export { getNgModules, GeneratorOption, GeneratorRequest };
117

@@ -19,31 +15,19 @@ export function processPageRequest(context: BuildContext, name: string) {
1915
}
2016

2117
export function processPipeRequest(context: BuildContext, name: string, ngModulePath: string) {
22-
const hydratedRequest = hydrateRequest(context, { type: 'pipe', name });
23-
return readFileAsync(ngModulePath).then((fileContent: string) => {
24-
fileContent = insertNamedImportIfNeeded(ngModulePath, fileContent, hydratedRequest.className, path.relative(path.dirname(ngModulePath), hydratedRequest.dirToWrite));
25-
fileContent = appendNgModuleDeclaration(ngModulePath, fileContent, hydratedRequest.className);
26-
return writeFileAsync(ngModulePath, fileContent);
27-
}).then(() => {
28-
return processNonTabRequest(context, hydratedRequest);
29-
}).then(() => {
30-
// TODO
31-
});
18+
return nonPageFileManipulation(context, name, ngModulePath, 'pipe');
3219
}
3320

34-
function processNonTabRequest(context: BuildContext, request: GeneratorRequest): Promise<string[]> {
35-
Logger.debug('[Generators] processNonTabRequest: Hydrating the request with project data ...');
36-
const hydratedRequest = hydrateRequest(context, request);
37-
Logger.debug('[Generators] processNonTabRequest: Reading templates ...');
38-
return readTemplates(hydratedRequest.dirToRead).then((map: Map<string, string>) => {
39-
Logger.debug('[Generators] processNonTabRequest: Filtering out NgModule and Specs if needed ...');
40-
return filterOutTemplates(hydratedRequest, map);
41-
}).then((filteredMap: Map<string, string>) => {
42-
Logger.debug('[Generators] processNonTabRequest: Applying tempaltes ...');
43-
const appliedTemplateMap = applyTemplates(hydratedRequest, filteredMap);
44-
Logger.debug('[Generators] processNonTabRequest: Writing generated files to disk ...');
45-
return writeGeneratedFiles(hydratedRequest, appliedTemplateMap);
46-
});
21+
export function processDirectiveRequest(context: BuildContext, name: string, ngModulePath: string) {
22+
return nonPageFileManipulation(context, name, ngModulePath, 'directive');
23+
}
24+
25+
export function processComponentRequest(context: BuildContext, name: string, ngModulePath: string) {
26+
return nonPageFileManipulation(context, name, ngModulePath, 'component');
27+
}
28+
29+
export function processProviderRequest(context: BuildContext, name: string, ngModulePath: string) {
30+
return nonPageFileManipulation(context, name, ngModulePath, 'provider');
4731
}
4832

4933
export function listOptions() {

src/generators/util.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { basename, dirname, join } from 'path';
2-
import { readdirSync} from 'fs';
1+
import { basename, dirname, join, relative } from 'path';
2+
import { readdirSync } from 'fs';
3+
import { Logger} from '../logger/logger';
34

45
import { paramCase, pascalCase, upperCaseFirst } from 'change-case';
56

@@ -9,6 +10,7 @@ import { getPropertyValue, getStringPropertyValue, mkDirpAsync, readFileAsync, r
910
import { BuildContext } from '../util/interfaces';
1011
import { globAll, GlobResult } from '../util/glob-util';
1112
import { ensureSuffix, removeSuffix } from '../util/helpers';
13+
import { appendNgModuleDeclaration, insertNamedImportIfNeeded } from '../util/typescript-utils';
1214

1315
export function hydrateRequest(context: BuildContext, request: GeneratorRequest) {
1416
const hydrated = Object.assign({ includeNgModule: true }, request) as HydratedGeneratorRequest;
@@ -107,6 +109,34 @@ export function getDirToWriteToByType(context: BuildContext, type: string) {
107109
throw new Error(`Unknown Generator Type: ${type}`);
108110
}
109111

112+
export function nonPageFileManipulation(context: BuildContext, name: string, ngModulePath: string, type: string) {
113+
const hydratedRequest = hydrateRequest(context, { type: type, name });
114+
return readFileAsync(ngModulePath).then((fileContent: string) => {
115+
fileContent = insertNamedImportIfNeeded(ngModulePath, fileContent, hydratedRequest.className, relative(dirname(ngModulePath), hydratedRequest.dirToWrite));
116+
fileContent = appendNgModuleDeclaration(ngModulePath, fileContent, hydratedRequest.className);
117+
return writeFileAsync(ngModulePath, fileContent);
118+
}).then(() => {
119+
return processNonTabRequest(context, hydratedRequest);
120+
}).then(() => {
121+
// TODO
122+
});
123+
}
124+
125+
export function processNonTabRequest(context: BuildContext, request: GeneratorRequest): Promise<string[]> {
126+
Logger.debug('[Generators] processNonTabRequest: Hydrating the request with project data ...');
127+
const hydratedRequest = hydrateRequest(context, request);
128+
Logger.debug('[Generators] processNonTabRequest: Reading templates ...');
129+
return readTemplates(hydratedRequest.dirToRead).then((map: Map<string, string>) => {
130+
Logger.debug('[Generators] processNonTabRequest: Filtering out NgModule and Specs if needed ...');
131+
return filterOutTemplates(hydratedRequest, map);
132+
}).then((filteredMap: Map<string, string>) => {
133+
Logger.debug('[Generators] processNonTabRequest: Applying tempaltes ...');
134+
const appliedTemplateMap = applyTemplates(hydratedRequest, filteredMap);
135+
Logger.debug('[Generators] processNonTabRequest: Writing generated files to disk ...');
136+
return writeGeneratedFiles(hydratedRequest, appliedTemplateMap);
137+
});
138+
}
139+
110140
export interface GeneratorOption {
111141
type: string;
112142
multiple: boolean;

src/util/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export function generateContext(context?: BuildContext): BuildContext {
9595
setProcessEnvVar(Constants.ENV_VAR_COMPONENTS_DIR, context.componentsDir);
9696
Logger.debug(`componentsDir set to ${context.componentsDir}`);
9797

98-
context.directivesDir = resolve(context.directivesDir || getConfigValue(context, '--directivesDir', null, Constants.ENV_VAR_DIRECTIVES_DIR, Constants.ENV_VAR_DIRECTIVES_DIR.toLowerCase(), join(context.srcDir, 'components')));
98+
context.directivesDir = resolve(context.directivesDir || getConfigValue(context, '--directivesDir', null, Constants.ENV_VAR_DIRECTIVES_DIR, Constants.ENV_VAR_DIRECTIVES_DIR.toLowerCase(), join(context.srcDir, 'directives')));
9999
setProcessEnvVar(Constants.ENV_VAR_DIRECTIVES_DIR, context.directivesDir);
100100
Logger.debug(`directivesDir set to ${context.directivesDir}`);
101101

0 commit comments

Comments
 (0)