Skip to content

Commit eb949e2

Browse files
committed
chore(packages): Address comments #7515
1 parent 454aba7 commit eb949e2

File tree

2 files changed

+28
-43
lines changed

2 files changed

+28
-43
lines changed

projects/igniteui-angular/schematics/ng-add/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function propertyExistsInWorkspace(targetProp: string, workspace: WorkspaceSchem
2020
function enablePolyfills(tree: Tree, context: SchematicContext): string {
2121
const workspace = getWorkspace(tree);
2222
const project = workspace.projects[workspace.defaultProject];
23-
const targetFile = getConfigFile(project, 'polyfills', 'build');
23+
const targetFile = getConfigFile(project, 'polyfills');
2424
if (!tree.exists(targetFile)) {
2525
context.logger.warn(`${targetFile} not found. You may need to update polyfills.ts manually.`);
2626
return;
@@ -54,7 +54,7 @@ function readInput(options: Options): Rule {
5454
const workspace = getWorkspace(tree);
5555
const targetProperty = 'es5BrowserSupport';
5656
const project = workspace.projects[workspace.defaultProject];
57-
const polyfillsFile = getConfigFile(project, 'polyfills', 'build');
57+
const polyfillsFile = getConfigFile(project, 'polyfills');
5858
const propertyExists = propertyExistsInWorkspace(targetProperty, workspace);
5959
let polyfillsData = tree.read(polyfillsFile).toString();
6060
if (propertyExists) {

projects/igniteui-angular/schematics/utils/dependency-handler.ts

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,18 @@ function getTargetedProjectOptions(project: WorkspaceProject<ProjectType>, targe
5959
throw new SchematicsException(`Cannot determine the project's configuration for: ${target}`);
6060
}
6161

62-
export function getConfigFile(project: WorkspaceProject<ProjectType>, option: string, configSection: string): string {
63-
switch (configSection) {
64-
case 'build':
65-
const buildOptions = getTargetedProjectOptions(project, 'build');
66-
if (!buildOptions[option]) {
67-
throw new SchematicsException(`Could not find the project ${option} file inside of the ` +
68-
`workspace config ${project.sourceRoot}`);
69-
}
70-
return buildOptions[option];
71-
72-
case 'test':
73-
const testOptions = getTargetedProjectOptions(project, 'test');
74-
if (!testOptions[option]) {
75-
throw new SchematicsException(`Could not find the project ${option} file inside of the ` +
76-
`workspace config ${project.sourceRoot}`);
77-
}
78-
return testOptions[option];
79-
default:
80-
throw new SchematicsException(`Could not find matching ${configSection} section` +
62+
export function getConfigFile(project: WorkspaceProject<ProjectType>, option: string, configSection: string = 'build'): string {
63+
const options = getTargetedProjectOptions(project, configSection);
64+
if (!options) {
65+
throw new SchematicsException(`Could not find matching ${configSection} section` +
8166
`inside of the workspace config ${project.sourceRoot} `);
8267
}
68+
if (!options[option]) {
69+
throw new SchematicsException(`Could not find the project ${option} file inside of the ` +
70+
`workspace config ${project.sourceRoot}`);
71+
}
72+
return options[option];
73+
8374
}
8475
export function overwriteJsonFile(tree: Tree, targetFile: string, data: any) {
8576
tree.overwrite(targetFile, JSON.stringify(data, null, 2) + '\n');
@@ -145,6 +136,20 @@ export function getPropertyFromWorkspace(targetProp: string, workspace: any, cur
145136
return null;
146137
}
147138

139+
function addHammerToConfig(project: WorkspaceProject<ProjectType>, tree: Tree, config: string) {
140+
const projectOptions = getTargetedProjectOptions(project, config);
141+
const tsPath = getConfigFile(project, 'main', config);
142+
const hammerImport = 'import \'hammerjs\';\n';
143+
const tsContent = tree.read(tsPath).toString();
144+
// if there are no elements in the architect[config]options.scripts array that contain hammerjs
145+
// and the "main" file does not contain an import with hammerjs
146+
if (!projectOptions.scripts.some(el => el.includes('hammerjs')) && !tsContent.includes(hammerImport)) {
147+
// import hammerjs in the specified by config main file
148+
const mainContents = hammerImport + tsContent;
149+
tree.overwrite(tsPath, mainContents);
150+
}
151+
}
152+
148153
function includeDependencies(pkgJson: any, context: SchematicContext, tree: Tree) {
149154
Object.keys(pkgJson.dependencies).forEach(pkg => {
150155
const version = pkgJson.dependencies[pkg];
@@ -156,30 +161,10 @@ function includeDependencies(pkgJson: any, context: SchematicContext, tree: Tree
156161
case 'hammerjs':
157162
logIncludingDependency(context, pkg, version);
158163
addPackageToPkgJson(tree, pkg, version, entry.target);
159-
160164
const workspace = getWorkspace(tree);
161165
const project = workspace.projects[workspace.defaultProject];
162-
const projectOptions = getTargetedProjectOptions(project, 'build');
163-
const projectTestOptions = getTargetedProjectOptions(project, 'test');
164-
const mainTsPath = getConfigFile(project, 'main', 'build');
165-
const testTsPath = getConfigFile(project, 'main', 'test');
166-
const hammerImport = 'import \'hammerjs\';\n';
167-
const mainTsContent = tree.read(mainTsPath).toString();
168-
const testTsContent = tree.read(testTsPath).toString();
169-
// if there are no elements in the architect.build.options.scripts array that contain hammerjs
170-
// and main.ts does not contain an import with hammerjs
171-
if (!projectOptions.scripts.some(el => el.includes('hammerjs')) && !mainTsContent.includes(hammerImport)) {
172-
// import hammerjs in the main.ts file
173-
const mainContents = hammerImport + mainTsContent;
174-
tree.overwrite(mainTsPath, mainContents);
175-
}
176-
// make sure test.ts has hammerjs import even for projects already containing it in main.ts
177-
// if there are no elements in the architect.test.options.scripts array that contain hammerjs
178-
// and test.ts does not contain an import with hammerjs
179-
if (!projectTestOptions.scripts.some(el => el.includes('hammerjs')) && !testTsContent.includes(hammerImport)) {
180-
const testContents = hammerImport + testTsContent;
181-
tree.overwrite(testTsPath, testContents);
182-
}
166+
addHammerToConfig(project, tree, 'build');
167+
addHammerToConfig(project, tree, 'test');
183168
break;
184169
default:
185170
logIncludingDependency(context, pkg, version);

0 commit comments

Comments
 (0)