Skip to content

Commit c5dc5a3

Browse files
authored
Merge branch 'master' into nalipiev/update-10-schematics
2 parents f336c9f + eff6f98 commit c5dc5a3

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

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

+28-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ describe('ng-add schematics', () => {
2525
polyfills: `${sourceRoot}/polyfills.ts`,
2626
scripts: []
2727
}
28-
}
28+
},
29+
test: {
30+
options: {
31+
main: `${sourceRoot}/test.ts`,
32+
polyfills: `${sourceRoot}/polyfills.ts`,
33+
scripts: []
34+
}
35+
},
2936
}
3037
}
3138
}
@@ -46,6 +53,7 @@ describe('ng-add schematics', () => {
4653
tree.create('/angular.json', JSON.stringify(ngJsonConfig));
4754
tree.create('/package.json', JSON.stringify(pkgJsonConfig));
4855
tree.create(`${sourceRoot}/main.ts`, '// test comment');
56+
tree.create(`${sourceRoot}/test.ts`, '// test comment');
4957
});
5058

5159
it('should create the needed files correctly', () => {
@@ -101,7 +109,7 @@ describe('ng-add schematics', () => {
101109
expect(mainTs).toContain('import \'hammerjs\';');
102110
});
103111

104-
it('should not add hammer.js if it exists in angular.json', async () => {
112+
it('should not add hammer.js if it exists in angular.json build options', async () => {
105113
const workspace = getWorkspace(tree) as any;
106114
const currentProjectName = workspace.defaultProject;
107115
workspace.projects[currentProjectName].architect.build.options.scripts.push('./node_modules/hammerjs/hammer.min.js');
@@ -112,7 +120,25 @@ describe('ng-add schematics', () => {
112120
expect(newContent.split('import \'hammerjs\';\n// test comment').length).toEqual(1);
113121
});
114122

123+
it('should add hammer.js to the test.ts file', async () => {
124+
await runner.runSchematicAsync('ng-add', { normalizeCss: false }, tree).toPromise();
125+
const testTs = tree.read(`${sourceRoot}/test.ts`).toString();
126+
expect(testTs).toContain('import \'hammerjs\';');
127+
});
128+
129+
it('should not add hammer.js if it exists in angular.json test options', async () => {
130+
const workspace = getWorkspace(tree) as any;
131+
const currentProjectName = workspace.defaultProject;
132+
workspace.projects[currentProjectName].architect.test.options.scripts.push('./node_modules/hammerjs/hammer.min.js');
133+
tree.overwrite('angular.json', JSON.stringify(workspace));
134+
await runner.runSchematicAsync('ng-add', { normalizeCss: false }, tree).toPromise();
135+
136+
const testTs = tree.read(`${sourceRoot}/test.ts`).toString();
137+
expect(testTs).toMatch('// test comment');
138+
});
139+
115140
it('should not add hammer.js if it exists in main.ts', async () => {
141+
116142
const mainTsPath = `${sourceRoot}/main.ts`;
117143
const content = tree.read(mainTsPath).toString();
118144
tree.overwrite(mainTsPath, 'import \'hammerjs\';\n' + content);

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

+25-18
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,19 @@ function getTargetedProjectOptions(project: WorkspaceProject<ProjectType>, targe
6060
throw new SchematicsException(`Cannot determine the project's configuration for: ${target}`);
6161
}
6262

63-
export function getConfigFile(project: WorkspaceProject<ProjectType>, option: string): string {
64-
const buildOptions = getTargetedProjectOptions(project, 'build');
65-
if (!buildOptions[option]) {
63+
export function getConfigFile(project: WorkspaceProject<ProjectType>, option: string, configSection: string = 'build'): string {
64+
const options = getTargetedProjectOptions(project, configSection);
65+
if (!options) {
66+
throw new SchematicsException(`Could not find matching ${configSection} section` +
67+
`inside of the workspace config ${project.sourceRoot} `);
68+
}
69+
if (!options[option]) {
6670
throw new SchematicsException(`Could not find the project ${option} file inside of the ` +
67-
`workspace config (${project.sourceRoot})`);
71+
`workspace config ${project.sourceRoot}`);
6872
}
73+
return options[option];
6974

70-
return buildOptions[option];
7175
}
72-
7376
export function overwriteJsonFile(tree: Tree, targetFile: string, data: any) {
7477
tree.overwrite(targetFile, JSON.stringify(data, null, 2) + '\n');
7578
}
@@ -134,6 +137,20 @@ export function getPropertyFromWorkspace(targetProp: string, workspace: any, cur
134137
return null;
135138
}
136139

140+
function addHammerToConfig(project: WorkspaceProject<ProjectType>, tree: Tree, config: string) {
141+
const projectOptions = getTargetedProjectOptions(project, config);
142+
const tsPath = getConfigFile(project, 'main', config);
143+
const hammerImport = 'import \'hammerjs\';\n';
144+
const tsContent = tree.read(tsPath).toString();
145+
// if there are no elements in the architect[config]options.scripts array that contain hammerjs
146+
// and the "main" file does not contain an import with hammerjs
147+
if (!projectOptions.scripts.some(el => el.includes('hammerjs')) && !tsContent.includes(hammerImport)) {
148+
// import hammerjs in the specified by config main file
149+
const mainContents = hammerImport + tsContent;
150+
tree.overwrite(tsPath, mainContents);
151+
}
152+
}
153+
137154
function includeDependencies(pkgJson: any, context: SchematicContext, tree: Tree) {
138155
Object.keys(pkgJson.dependencies).forEach(pkg => {
139156
const version = pkgJson.dependencies[pkg];
@@ -145,20 +162,10 @@ function includeDependencies(pkgJson: any, context: SchematicContext, tree: Tree
145162
case 'hammerjs':
146163
logIncludingDependency(context, pkg, version);
147164
addPackageToPkgJson(tree, pkg, version, entry.target);
148-
149165
const workspace = getWorkspace(tree);
150166
const project = workspace.projects[workspace.defaultProject];
151-
const projectOptions = getTargetedProjectOptions(project, 'build');
152-
const mainTsPath = getConfigFile(project, 'main');
153-
const hammerImport = 'import \'hammerjs\';\n';
154-
const mainTsContent = tree.read(mainTsPath).toString();
155-
// if there are no elements in the architect.build.options.scripts array that contain hammerjs
156-
// and main.ts does not contain an import with hammerjs
157-
if (!projectOptions.scripts.some(el => el.includes('hammerjs')) && !mainTsContent.includes(hammerImport)) {
158-
// import hammerjs in the main.ts file
159-
const contents = hammerImport + mainTsContent;
160-
tree.overwrite(mainTsPath, contents);
161-
}
167+
addHammerToConfig(project, tree, 'build');
168+
addHammerToConfig(project, tree, 'test');
162169
break;
163170
default:
164171
logIncludingDependency(context, pkg, version);

0 commit comments

Comments
 (0)