Skip to content

Commit 921f1c1

Browse files
feat: improve schematics (#8411)
1 parent be3366e commit 921f1c1

File tree

6 files changed

+51
-202
lines changed

6 files changed

+51
-202
lines changed

schematics/ng-add/index.spec.ts

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
addModuleImportToRootModule,
3-
getProjectFromWorkspace,
4-
getProjectTargetOptions,
5-
addImportToModule
6-
} from '@angular/cdk/schematics';
1+
import { getProjectFromWorkspace, getProjectTargetOptions } from '@angular/cdk/schematics';
72

83
import { normalize } from '@angular-devkit/core';
94
import { WorkspaceDefinition } from '@angular-devkit/core/src/workspace';
@@ -129,58 +124,28 @@ describe('ng-add schematic', () => {
129124
expect(assetsString).toContain(iconPathSegment);
130125
});
131126

132-
it('should required modules', async () => {
127+
it('should required modules and providers', async () => {
133128
const options = { ...defaultOptions };
134129
const tree = await runner.runSchematic('ng-add-setup-project', options, appTree);
135130
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.module.ts');
136131

132+
expect(fileContent).toContain('provideHttpClient()');
137133
expect(fileContent).toContain('FormsModule');
138-
expect(fileContent).toContain('HttpClientModule');
139134
});
140135

141-
it('should add browserAnimationsModuleName if animations is enable', async () => {
136+
it('should add provideAnimationsAsync() function call if animations is enable', async () => {
142137
const options = { ...defaultOptions, animations: true };
143138
const tree = await runner.runSchematic('ng-add-setup-project', options, appTree);
144139
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.module.ts');
145-
expect(fileContent).toContain('BrowserAnimationsModule');
140+
expect(fileContent).toContain('provideAnimationsAsync()');
146141
});
147142

148-
it('should add noopAnimationsModuleName if animations is disable', async () => {
143+
it(`should add provideAnimationsAsync('noop') function call if animations is disable`, async () => {
149144
const options = { ...defaultOptions, animations: false };
150145
const tree = await runner.runSchematic('ng-add-setup-project', options, appTree);
151146
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.module.ts');
152147

153-
expect(fileContent).toContain('NoopAnimationsModule');
154-
});
155-
156-
it('should not add BrowserAnimationsModule if NoopAnimationsModule is set up', async () => {
157-
const options = { ...defaultOptions, animations: true };
158-
159-
const workspace = await getWorkspace(appTree);
160-
const project = getProjectFromWorkspace(workspace, defaultOptions.project);
161-
162-
addModuleImportToRootModule(appTree, 'NoopAnimationsModule', '@angular/platform-browser/animations', project);
163-
164-
const tree = await runner.runSchematic('ng-add-setup-project', options, appTree);
165-
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.module.ts');
166-
167-
expect(fileContent).toContain('NoopAnimationsModule');
168-
expect(fileContent).not.toContain('BrowserAnimationsModule');
169-
});
170-
171-
it('should not add NoopAnimationsModule if BrowserAnimationsModule is set up', async () => {
172-
const options = { ...defaultOptions, animations: false };
173-
174-
const workspace = await getWorkspace(appTree);
175-
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition, defaultOptions.project);
176-
177-
addModuleImportToRootModule(appTree, 'BrowserAnimationsModule', '@angular/platform-browser/animations', project);
178-
179-
const tree = await runner.runSchematic('ng-add-setup-project', options, appTree);
180-
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.module.ts');
181-
182-
expect(fileContent).not.toContain('NoopAnimationsModule');
183-
expect(fileContent).toContain('BrowserAnimationsModule');
148+
expect(fileContent).toContain(`provideAnimationsAsync('noop')`);
184149
});
185150

186151
it('should register default locale id', async () => {

schematics/ng-add/setup-project/add-animations-module.ts

Lines changed: 0 additions & 107 deletions
This file was deleted.

schematics/ng-add/setup-project/add-required-modules.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ import { addRootImport } from '@schematics/angular/utility';
99
import { Schema } from '../schema';
1010

1111
const modulesMap = {
12-
FormsModule: '@angular/forms',
13-
HttpClientModule: '@angular/common/http'
12+
FormsModule: '@angular/forms'
1413
};
1514

1615
export function addRequiredModules(options: Schema): Rule {
1716
const rules = Object.entries(modulesMap).map(([symbolName, moduleName]) => {
18-
return addRootImport(options.project, ({code, external}) => {
17+
return addRootImport(options.project, ({ code, external }) => {
1918
return code`${external(symbolName, moduleName)}`;
2019
});
2120
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Use of this source code is governed by an MIT-style license that can be
3+
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
4+
*/
5+
6+
import { Rule, chain } from '@angular-devkit/schematics';
7+
import { addRootProvider } from '@schematics/angular/utility';
8+
9+
import { Schema } from '../schema';
10+
11+
export function addRequiredProviders(options: Schema): Rule {
12+
return chain([
13+
addAnimations(options),
14+
addHttpClient(options)
15+
]);
16+
}
17+
18+
function addAnimations(options: Schema): Rule {
19+
return addRootProvider(options.project, ({ code, external }) => {
20+
return code`${external(
21+
'provideAnimationsAsync',
22+
'@angular/platform-browser/animations/async',
23+
)}(${options.animations ? '' : `'noop'`})`;
24+
});
25+
}
26+
27+
function addHttpClient(options: Schema): Rule {
28+
return addRootProvider(options.project, ({ code, external }) => {
29+
return code`${external(
30+
'provideHttpClient',
31+
'@angular/common/http',
32+
)}()`;
33+
});
34+
}

schematics/ng-add/setup-project/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import { chain, noop, Rule } from '@angular-devkit/schematics';
77

88
import { Schema } from '../schema';
9-
import { addAnimationsModule } from './add-animations-module';
109
import { addIconToAssets } from './add-icon-assets';
1110
import { addRequiredModules } from './add-required-modules';
11+
import { addRequiredProviders } from './add-required-providers';
1212
import { hammerjsImport } from './hammerjs-import';
1313
import { registerLocale } from './register-locale';
1414
import { addThemeToAppStyles } from './theming';
@@ -17,7 +17,7 @@ export default function (options: Schema): Rule {
1717
return chain([
1818
registerLocale(options),
1919
addRequiredModules(options),
20-
addAnimationsModule(options),
20+
addRequiredProviders(options),
2121
addThemeToAppStyles(options),
2222
options.dynamicIcon ? addIconToAssets(options) : noop(),
2323
options.gestures ? hammerjsImport(options) : noop()

schematics/ng-add/standalone.spec.ts

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
*/
55

66
import { getProjectFromWorkspace, getProjectTargetOptions } from '@angular/cdk/schematics';
7-
import { firstValueFrom } from 'rxjs';
87

98
import { normalize } from '@angular-devkit/core';
109
import { WorkspaceDefinition } from '@angular-devkit/core/src/workspace';
1110
import { Tree } from '@angular-devkit/schematics';
1211
import { NodePackageName } from '@angular-devkit/schematics/tasks/package-manager/options';
1312
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
14-
import { addRootImport } from '@schematics/angular/utility';
1513
import { getWorkspace } from '@schematics/angular/utility/workspace';
1614

1715
import { join } from 'path';
@@ -132,69 +130,29 @@ describe('[standalone] ng-add schematic', () => {
132130
expect(assetsString).toContain(iconPathSegment);
133131
});
134132

135-
it('should required modules', async () => {
133+
it('should required modules and providers', async () => {
136134
const options = { ...defaultOptions };
137135
const tree = await runner.runSchematic('ng-add-setup-project', options, appTree);
138136
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.config.ts');
139137

138+
expect(fileContent).toContain('provideHttpClient()');
140139
expect(fileContent).toContain('FormsModule');
141-
expect(fileContent).toContain('HttpClientModule');
142140
});
143141

144-
it('should add browserAnimationsModuleName if animations is enable', async () => {
142+
it('should add provideAnimationsAsync() call function if animations is enable', async () => {
145143
const options = { ...defaultOptions, animations: true };
146144
const tree = await runner.runSchematic('ng-add-setup-project', options, appTree);
147145
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.config.ts');
148146

149-
expect(fileContent).toContain('provideAnimations');
147+
expect(fileContent).toContain('provideAnimationsAsync()');
150148
});
151149

152-
it('should add noopAnimationsModuleName if animations is disable', async () => {
150+
it(`should add provideAnimationsAsync('noop') function call if animations is disable`, async () => {
153151
const options = { ...defaultOptions, animations: false };
154152
const tree = await runner.runSchematic('ng-add-setup-project', options, appTree);
155153
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.config.ts');
156154

157-
expect(fileContent).toContain('provideNoopAnimations');
158-
});
159-
160-
it('should not add BrowserAnimationsModule if NoopAnimationsModule is set up', async () => {
161-
const options = { ...defaultOptions, animations: true };
162-
163-
// Add NoopAnimationsModule
164-
await firstValueFrom(
165-
runner.callRule(
166-
addRootImport(options.project, ({ code, external }) => {
167-
return code`${external('NoopAnimationsModule', '@angular/platform-browser/animations')}`;
168-
}),
169-
appTree
170-
)
171-
);
172-
173-
const tree = await runner.runSchematic('ng-add-setup-project', options, appTree);
174-
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.config.ts');
175-
176-
expect(fileContent).toContain('NoopAnimationsModule');
177-
expect(fileContent).not.toContain('BrowserAnimationsModule');
178-
});
179-
180-
it('should not add NoopAnimationsModule if BrowserAnimationsModule is set up', async () => {
181-
const options = { ...defaultOptions, animations: false };
182-
183-
// import BrowserAnimationsModule
184-
await firstValueFrom(
185-
runner.callRule(
186-
addRootImport(options.project, ({ code, external }) => {
187-
return code`${external('BrowserAnimationsModule', '@angular/platform-browser/animations')}`;
188-
}),
189-
appTree
190-
)
191-
);
192-
193-
const tree = await runner.runSchematic('ng-add-setup-project', options, appTree);
194-
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.config.ts');
195-
196-
expect(fileContent).not.toContain('NoopAnimationsModule');
197-
expect(fileContent).toContain('BrowserAnimationsModule');
155+
expect(fileContent).toContain(`provideAnimationsAsync('noop')`);
198156
});
199157

200158
it('should register default locale id', async () => {

0 commit comments

Comments
 (0)