Skip to content

Commit 586226a

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular/cli): infer schematic defaults correctly when using --project
Previously, the `--project` flag was ignored when gathering and merging the schematics defaults from the angular workspace configuration file. Closes #20666
1 parent d7ef0d0 commit 586226a

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

packages/angular/cli/models/schematic-command.ts

+14-16
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,17 @@ export abstract class SchematicCommand<
242242
schemaValidation: true,
243243
optionTransforms: [
244244
// Add configuration file defaults
245-
async (schematic, current) => ({
246-
...(await getSchematicDefaults(
247-
schematic.collection.name,
248-
schematic.name,
249-
getProjectName(),
250-
)),
251-
...current,
252-
}),
245+
async (schematic, current) => {
246+
const projectName =
247+
typeof (current as Record<string, unknown>).project === 'string'
248+
? ((current as Record<string, unknown>).project as string)
249+
: getProjectName();
250+
251+
return {
252+
...(await getSchematicDefaults(schematic.collection.name, schematic.name, projectName)),
253+
...current,
254+
};
255+
},
253256
],
254257
engineHostCreator: (options) => new SchematicEngineHost(options.resolvePaths),
255258
});
@@ -446,14 +449,9 @@ export abstract class SchematicCommand<
446449
}
447450

448451
const pathOptions = o ? this.setPathOptions(o, workingDir) : {};
449-
let input = { ...pathOptions, ...args };
450-
451-
// Read the default values from the workspace.
452-
const projectName = input.project !== undefined ? '' + input.project : null;
453-
const defaults = await getSchematicDefaults(collectionName, schematicName, projectName);
454-
input = {
455-
...defaults,
456-
...input,
452+
const input = {
453+
...pathOptions,
454+
...args,
457455
...options.additionalOptions,
458456
};
459457

packages/schematics/angular/app-shell/index_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('App Shell Schematic', () => {
1717
require.resolve('../collection.json'),
1818
);
1919
const defaultOptions: AppShellOptions = {
20-
clientProject: 'bar',
20+
project: 'bar',
2121
};
2222

2323
const workspaceOptions: WorkspaceOptions = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { ng } from '../../utils/process';
2+
import { updateJsonFile } from '../../utils/project';
3+
4+
export default async function () {
5+
await updateJsonFile('angular.json', (config) => {
6+
config.projects['test-project'].schematics = {
7+
'@schematics/angular:component': {
8+
style: 'scss',
9+
},
10+
};
11+
});
12+
13+
// Generate component in application to verify that it's minimal
14+
const { stdout } = await ng('generate', 'component', 'foo');
15+
if (!stdout.includes('foo.component.scss')) {
16+
console.log(stdout);
17+
throw new Error('Expected "foo.component.scss" to exist.');
18+
}
19+
20+
// Generate another project with different settings
21+
await ng('generate', 'application', 'test-project-two', '--no-minimal');
22+
23+
await updateJsonFile('angular.json', (config) => {
24+
config.projects['test-project-two'].schematics = {
25+
'@schematics/angular:component': {
26+
style: 'less',
27+
},
28+
};
29+
});
30+
31+
const { stdout: stdout2 } = await ng(
32+
'generate',
33+
'component',
34+
'foo',
35+
'--project',
36+
'test-project-two',
37+
);
38+
if (!stdout2.includes('foo.component.less')) {
39+
console.log(stdout2);
40+
throw new Error('Expected "foo.component.less" to exist.');
41+
}
42+
}

0 commit comments

Comments
 (0)