Skip to content

Commit de8a6b7

Browse files
authored
fix(module:schematics): cannot generate files and add default builders (#8176)
1 parent 14e5cdc commit de8a6b7

11 files changed

Lines changed: 138 additions & 164 deletions

File tree

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
16.14.0
1+
18.17.0

schematics/ng-add/index.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,51 @@
33
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
44
*/
55

6-
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
6+
import { getProjectFromWorkspace } from '@angular/cdk/schematics';
7+
8+
import { chain, noop, Rule, schematic, SchematicContext, Tree } from '@angular-devkit/schematics';
79
import { NodePackageInstallTask, RunSchematicTask } from '@angular-devkit/schematics/tasks';
10+
import { getWorkspace } from '@schematics/angular/utility/workspace';
811

912
import { addPackageToPackageJson } from '../utils/package-config';
13+
import { getProjectStyle } from '../utils/project-style';
14+
// generated by scripts/schematics/set-version.ts
1015
// @ts-ignore
1116
import { hammerjsVersion, zorroVersion } from '../utils/version-names';
1217
import { Schema } from './schema';
1318

1419
export default function (options: Schema): Rule {
15-
return (host: Tree, context: SchematicContext) => {
16-
// The CLI inserts `ng-zorro-antd` into the `package.json` before this schematic runs.
17-
// This means that we do not need to insert Angular Material into `package.json` files again.
18-
// In some cases though, it could happen that this schematic runs outside of the CLI `ng add`
19-
// command, or Material is only listed a dev dependency. If that is the case, we insert a
20-
// version based on the current build version (substituted version placeholder).
21-
if (!options.skipPackageJson) {
22-
addPackageToPackageJson(host, 'ng-zorro-antd', zorroVersion);
23-
if (options.gestures) {
24-
addPackageToPackageJson(host, 'hammerjs', hammerjsVersion);
20+
return chain([
21+
(host: Tree, context: SchematicContext) => {
22+
// The CLI inserts `ng-zorro-antd` into the `package.json` before this schematic runs.
23+
// This means that we do not need to insert Angular Material into `package.json` files again.
24+
// In some cases though, it could happen that this schematic runs outside of the CLI `ng add`
25+
// command, or Material is only listed a dev dependency. If that is the case, we insert a
26+
// version based on the current build version (substituted version placeholder).
27+
if (!options.skipPackageJson) {
28+
addPackageToPackageJson(host, 'ng-zorro-antd', zorroVersion);
29+
if (options.gestures) {
30+
addPackageToPackageJson(host, 'hammerjs', hammerjsVersion);
31+
}
2532
}
26-
}
2733

28-
// Since the Angular Material schematics depend on the schematic utility functions from the
29-
// CDK, we need to install the CDK before loading the schematic files that import from the CDK.
30-
if (!options.skipInstall) {
31-
const installTaskId = context.addTask(new NodePackageInstallTask());
32-
context.addTask(new RunSchematicTask('ng-add-setup-project', options), [installTaskId]);
33-
}
34-
};
34+
// Since the Angular Material schematics depend on the schematic utility functions from the
35+
// CDK, we need to install the CDK before loading the schematic files that import from the CDK.
36+
if (!options.skipInstall) {
37+
const installTaskId = context.addTask(new NodePackageInstallTask());
38+
context.addTask(new RunSchematicTask('ng-add-setup-project', options), [installTaskId]);
39+
}
40+
},
41+
options.template ? applyTemplate(options) : noop(),
42+
]);
3543
}
44+
45+
function applyTemplate(options: Schema): Rule {
46+
return async (host: Tree) => {
47+
const workspace = await getWorkspace(host);
48+
const project = getProjectFromWorkspace(workspace, options.project);
49+
const style = getProjectStyle(project);
50+
51+
return schematic(options.template, {...options, style});
52+
}
53+
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ const defaultCustomThemeFilename = 'theme.less';
2222

2323
/** Object that maps a CLI target to its default builder name. */
2424
const defaultTargetBuilders = {
25-
build: '@angular-devkit/build-angular:application',
26-
test: '@angular-devkit/build-angular:karma'
25+
build: [
26+
'@angular-devkit/build-angular:application', // for esbuild
27+
'@angular-devkit/build-angular:browser' // for webpack
28+
],
29+
test: ['@angular-devkit/build-angular:karma']
2730
};
2831

2932
/** Add pre-built styles to the main project style file. */
@@ -144,7 +147,7 @@ function validateDefaultTargetBuilder(
144147
): boolean {
145148
const defaultBuilder = defaultTargetBuilders[targetName];
146149
const targetConfig = project.targets && project.targets.get(targetName);
147-
const isDefaultBuilder = targetConfig && targetConfig.builder === defaultBuilder;
150+
const isDefaultBuilder = targetConfig && defaultBuilder.includes(targetConfig.builder);
148151

149152
if (!isDefaultBuilder && targetName === 'build') {
150153
throw new SchematicsException(

schematics/ng-add/standalone.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('[standalone] ng-add schematic', () => {
3131

3232
beforeEach(async () => {
3333
runner = new SchematicTestRunner('schematics', require.resolve('../collection.json'));
34-
appTree = await createTestApp(runner, { standalone: true });
34+
appTree = await createTestApp(runner);
3535
});
3636

3737
it('should update package.json', async () => {

schematics/ng-component/index.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
33
import { ChangeDetection, Style } from '@schematics/angular/component/schema';
44

55
import { createTestApp } from '../testing/test-app';
6-
import {getFileContent} from "../utils/get-file-content";
76

87
const appOptions = {
98
name: 'ng-zorro',
@@ -34,7 +33,7 @@ describe('ng-component schematic', () => {
3433

3534
beforeEach(async () => {
3635
runner = new SchematicTestRunner('schematics', require.resolve('../collection.json'));
37-
appTree = await createTestApp(runner, { ...appOptions, standalone: true });
36+
appTree = await createTestApp(runner, { ...appOptions });
3837
});
3938

4039
it('should create a component', async () => {

schematics/ng-generate/blank/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export default function(options: Schema): Rule {
2727
const buffer = host.read(appHTMLFile);
2828

2929
if (!buffer) {
30-
3130
context.logger.error(
3231
`Could not find the project ${appHTMLFile} file inside of the ` + `workspace config`
3332
);

schematics/ng-generate/side-menu/index.spec.ts

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,71 +16,56 @@ describe('side-menu schematic', () => {
1616

1717
beforeEach(async () => {
1818
runner = new SchematicTestRunner('schematics', require.resolve('../../collection.json'));
19-
appTree = await createTestApp(runner, { standalone: true });
19+
appTree = await createTestApp(runner, { standalone: false });
2020
});
2121

2222
it('should create side-menu files', async () => {
2323
const options = { ...defaultOptions };
2424
const tree = await runner.runSchematic('sidemenu', options, appTree);
25-
const files = tree.files;
26-
expect(files).toEqual(
25+
26+
expect(tree.files).toEqual(
2727
jasmine.arrayContaining([
2828
'/projects/ng-zorro/src/app/app.component.html',
2929
'/projects/ng-zorro/src/app/app.component.css',
3030
'/projects/ng-zorro/src/app/app.component.ts',
31-
'/projects/ng-zorro/src/app/app.routes.ts',
32-
'/projects/ng-zorro/src/app/pages/welcome/welcome.routes.ts',
31+
'/projects/ng-zorro/src/app/app-routing.module.ts',
32+
'/projects/ng-zorro/src/app/pages/welcome/welcome-routing.module.ts',
3333
'/projects/ng-zorro/src/app/pages/welcome/welcome.component.ts',
3434
'/projects/ng-zorro/src/app/pages/welcome/welcome.component.css',
3535
'/projects/ng-zorro/src/app/pages/welcome/welcome.component.html'
3636
])
3737
);
3838
});
3939

40-
it('should set the style preprocessor correctly', async () => {
41-
const options = { ...defaultOptions, style: Style.Less };
42-
const tree = await runner.runSchematic('sidemenu', options, appTree);
43-
const files = tree.files;
44-
const appContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.component.ts');
45-
const welcomeContent = getFileContent(tree, '/projects/ng-zorro/src/app/pages/welcome/welcome.component.ts');
46-
expect(appContent).toContain('app.component.less');
47-
expect(welcomeContent).toContain('welcome.component.less');
48-
49-
expect(files).toEqual(
50-
jasmine.arrayContaining([
51-
'/projects/ng-zorro/src/app/app.component.less',
52-
'/projects/ng-zorro/src/app/pages/welcome/welcome.component.less'
53-
])
54-
);
55-
});
56-
57-
xit('should fall back to the @schematics/angular:component option value', async () => {
58-
const options = { ...defaultOptions, template: 'sidemenu' };
59-
appTree = await createTestApp(runner, { style: Style.Less });
60-
const tree = await runner.runSchematic('ng-add', options, appTree);
61-
62-
expect(tree.files).toEqual(
63-
jasmine.arrayContaining([
64-
'/projects/ng-zorro/src/app/app.component.less',
65-
'/projects/ng-zorro/src/app/pages/welcome/welcome.component.less'
66-
])
67-
);
68-
});
69-
70-
xit('should fall back to the @schematics/angular:component option value', async () => {
71-
const options = { ...defaultOptions, template: 'sidemenu' };
72-
appTree = await createTestApp(runner, { inlineStyle: true });
73-
const tree = await runner.runSchematic('ng-add', options, appTree);
74-
75-
expect(tree.files).not.toEqual('/projects/ng-zorro/src/app/pages/welcome/welcome.component.css');
76-
});
77-
78-
xit('should fall back to the @schematics/angular:component option value', async () => {
79-
const options = { ...defaultOptions, template: 'sidemenu' };
80-
appTree = await createTestApp(runner, { inlineTemplate: true });
81-
const tree = await runner.runSchematic('ng-add', options, appTree);
82-
83-
expect(tree.files).not.toEqual('/projects/ng-zorro/src/app/pages/welcome/welcome.component.html');
40+
describe('style option', () => {
41+
it('should set the style preprocessor correctly', async () => {
42+
const options = { ...defaultOptions, style: Style.Less };
43+
const tree = await runner.runSchematic('sidemenu', options, appTree);
44+
const appContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.component.ts');
45+
const welcomeContent = getFileContent(tree, '/projects/ng-zorro/src/app/pages/welcome/welcome.component.ts');
46+
expect(appContent).toContain('app.component.less');
47+
expect(welcomeContent).toContain('welcome.component.less');
48+
49+
expect(tree.files).toEqual(
50+
jasmine.arrayContaining([
51+
'/projects/ng-zorro/src/app/app.component.less',
52+
'/projects/ng-zorro/src/app/pages/welcome/welcome.component.less'
53+
])
54+
);
55+
});
56+
57+
it('should fall back to the @schematics/angular:component option value', async () => {
58+
const options = { ...defaultOptions, template: 'sidemenu' };
59+
appTree = await createTestApp(runner, { style: Style.Less, standalone: false });
60+
const tree = await runner.runSchematic('ng-add', options, appTree);
61+
62+
expect(tree.files).toEqual(
63+
jasmine.arrayContaining([
64+
'/projects/ng-zorro/src/app/app.component.less',
65+
'/projects/ng-zorro/src/app/pages/welcome/welcome.component.less'
66+
])
67+
);
68+
});
8469
});
8570

8671
it('should set the prefix correctly', async () => {

schematics/ng-generate/side-menu/index.ts

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,12 @@ export default function (options: Schema): Rule {
3232
const project = getProjectFromWorkspace(workspace, options.project);
3333
const mainFile = getProjectMainFile(project);
3434
const prefix = options.prefix || project.prefix;
35-
36-
if (isStandaloneApp(host, mainFile)) {
37-
return chain([
38-
mergeWith(
39-
apply(url('./standalone/src'), [
40-
applyTemplates({
41-
...strings,
42-
...options,
43-
prefix
44-
}),
45-
move(project.sourceRoot as string),
46-
forEach((fileEntry: FileEntry) => {
47-
if (host.exists(fileEntry.path)) {
48-
host.overwrite(fileEntry.path, fileEntry.content);
49-
}
50-
return fileEntry;
51-
})
52-
]),
53-
MergeStrategy.Overwrite
54-
),
55-
addRootProvider(options.project, ({ code, external }) => {
56-
return code`${external('provideNzIcons', './icons-provider')}()`;
57-
})
58-
]);
59-
}
35+
const isStandalone = isStandaloneApp(host, mainFile);
36+
const templateSourcePath = isStandalone ? './standalone' : './files';
6037

6138
return chain([
6239
mergeWith(
63-
apply(url('./files/src'), [
40+
apply(url(`${templateSourcePath}/src`), [
6441
applyTemplates({
6542
...strings,
6643
...options,
@@ -76,10 +53,17 @@ export default function (options: Schema): Rule {
7653
]),
7754
MergeStrategy.Overwrite
7855
),
79-
addModule('AppRoutingModule', './app-routing.module', options.project),
80-
addModule('IconsProviderModule', './icons-provider.module', options.project),
81-
addModule('NzLayoutModule', 'ng-zorro-antd/layout', options.project),
82-
addModule('NzMenuModule', 'ng-zorro-antd/menu', options.project)
56+
isStandalone ?
57+
addRootProvider(options.project, ({ code, external }) => {
58+
return code`${external('provideNzIcons', './icons-provider')}()`;
59+
})
60+
:
61+
chain([
62+
addModule('AppRoutingModule', './app-routing.module', options.project),
63+
addModule('IconsProviderModule', './icons-provider.module', options.project),
64+
addModule('NzLayoutModule', 'ng-zorro-antd/layout', options.project),
65+
addModule('NzMenuModule', 'ng-zorro-antd/menu', options.project)
66+
])
8367
]);
8468
};
8569
}

0 commit comments

Comments
 (0)