Skip to content

Commit

Permalink
fix(@angular/cli): infer schematic defaults correctly when using `--p…
Browse files Browse the repository at this point in the history
…roject`

Previously, the `--project` flag was ignored when gathering and merging the schematics defaults from the angular workspace configuration file.

Closes #20666

(cherry picked from commit 586226a)
  • Loading branch information
alan-agius4 authored and clydin committed May 5, 2021
1 parent 95cb13e commit bc01593
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
30 changes: 14 additions & 16 deletions packages/angular/cli/models/schematic-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,17 @@ export abstract class SchematicCommand<
schemaValidation: true,
optionTransforms: [
// Add configuration file defaults
async (schematic, current) => ({
...(await getSchematicDefaults(
schematic.collection.name,
schematic.name,
getProjectName(),
)),
...current,
}),
async (schematic, current) => {
const projectName =
typeof (current as Record<string, unknown>).project === 'string'
? ((current as Record<string, unknown>).project as string)
: getProjectName();

return {
...(await getSchematicDefaults(schematic.collection.name, schematic.name, projectName)),
...current,
};
},
],
engineHostCreator: (options) => new SchematicEngineHost(options.resolvePaths),
});
Expand Down Expand Up @@ -446,14 +449,9 @@ export abstract class SchematicCommand<
}

const pathOptions = o ? this.setPathOptions(o, workingDir) : {};
let input = { ...pathOptions, ...args };

// Read the default values from the workspace.
const projectName = input.project !== undefined ? '' + input.project : null;
const defaults = await getSchematicDefaults(collectionName, schematicName, projectName);
input = {
...defaults,
...input,
const input = {
...pathOptions,
...args,
...options.additionalOptions,
};

Expand Down
2 changes: 1 addition & 1 deletion packages/schematics/angular/app-shell/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('App Shell Schematic', () => {
require.resolve('../collection.json'),
);
const defaultOptions: AppShellOptions = {
clientProject: 'bar',
project: 'bar',
};

const workspaceOptions: WorkspaceOptions = {
Expand Down
42 changes: 42 additions & 0 deletions tests/legacy-cli/e2e/tests/generate/schematic-defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';

export default async function () {
await updateJsonFile('angular.json', (config) => {
config.projects['test-project'].schematics = {
'@schematics/angular:component': {
style: 'scss',
},
};
});

// Generate component in application to verify that it's minimal
const { stdout } = await ng('generate', 'component', 'foo');
if (!stdout.includes('foo.component.scss')) {
console.log(stdout);
throw new Error('Expected "foo.component.scss" to exist.');
}

// Generate another project with different settings
await ng('generate', 'application', 'test-project-two', '--no-minimal');

await updateJsonFile('angular.json', (config) => {
config.projects['test-project-two'].schematics = {
'@schematics/angular:component': {
style: 'less',
},
};
});

const { stdout: stdout2 } = await ng(
'generate',
'component',
'foo',
'--project',
'test-project-two',
);
if (!stdout2.includes('foo.component.less')) {
console.log(stdout2);
throw new Error('Expected "foo.component.less" to exist.');
}
}

0 comments on commit bc01593

Please sign in to comment.