Skip to content

fix(@angular/cli): infer schematic defaults correctly when using --project #20690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot validate the project name at this stage, since the project might now exist yet, example when using ng g lib or ng g app, when under the will invoke ng g c will an unknown project name.

...(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
12 changes: 6 additions & 6 deletions packages/schematics/angular/app-shell/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function addAppShellConfigToWorkspace(options: AppShellOptions): Rule {
}

return updateWorkspace((workspace) => {
const project = workspace.projects.get(options.clientProject);
const project = workspace.projects.get(options.project);
if (!project) {
return;
}
Expand Down Expand Up @@ -187,8 +187,8 @@ function addAppShellConfigToWorkspace(options: AppShellOptions): Rule {
}

configurations[key] = {
browserTarget: `${options.clientProject}:build:${key}`,
serverTarget: `${options.clientProject}:server:${key}`,
browserTarget: `${options.project}:build:${key}`,
serverTarget: `${options.project}:server:${key}`,
};
}

Expand Down Expand Up @@ -239,7 +239,7 @@ function addServerRoutes(options: AppShellOptions): Rule {
return async (host: Tree) => {
// The workspace gets updated so this needs to be reloaded
const workspace = await getWorkspace(host);
const clientProject = workspace.projects.get(options.clientProject);
const clientProject = workspace.projects.get(options.project);
if (!clientProject) {
throw new Error('Universal schematic removed client project.');
}
Expand Down Expand Up @@ -309,7 +309,7 @@ function addShellComponent(options: AppShellOptions): Rule {
const componentOptions: ComponentOptions = {
name: 'app-shell',
module: options.rootModuleFileName,
project: options.clientProject,
project: options.project,
};

return schematic('component', componentOptions);
Expand All @@ -318,7 +318,7 @@ function addShellComponent(options: AppShellOptions): Rule {
export default function (options: AppShellOptions): Rule {
return async (tree) => {
const workspace = await getWorkspace(tree);
const clientProject = workspace.projects.get(options.clientProject);
const clientProject = workspace.projects.get(options.project);
if (!clientProject || clientProject.extensions.projectType !== 'application') {
throw new SchematicsException(`A client project type of "application" is required.`);
}
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
4 changes: 2 additions & 2 deletions packages/schematics/angular/app-shell/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"additionalProperties": false,
"long-description": "./app-shell-long.md",
"properties": {
"clientProject": {
"project": {
"type": "string",
"description": "The name of the related client app.",
"$default": {
Expand Down Expand Up @@ -50,5 +50,5 @@
"default": "AppServerModule"
}
},
"required": ["clientProject"]
"required": ["project"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ describe('Migration to version 9', () => {
require.resolve('../../collection.json'),
'universal',
{
clientProject: 'migration-test',
project: 'migration-test',
},
tree,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Migration to version 9', () => {
require.resolve('../../collection.json'),
'universal',
{
clientProject: 'migration-test',
project: 'migration-test',
},
tree,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ describe('Migration to version 9', () => {
require.resolve('../../collection.json'),
'universal',
{
clientProject: 'migration-test',
project: 'migration-test',
},
tree,
)
Expand Down
8 changes: 4 additions & 4 deletions packages/schematics/angular/universal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { Schema as UniversalOptions } from './schema';

function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): Rule {
return updateWorkspace((workspace) => {
const clientProject = workspace.projects.get(options.clientProject);
const clientProject = workspace.projects.get(options.project);

if (clientProject) {
// In case the browser builder hashes the assets
Expand All @@ -59,7 +59,7 @@ function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): R

const buildTarget = clientProject.targets.get('build');
if (buildTarget?.options) {
buildTarget.options.outputPath = `dist/${options.clientProject}/browser`;
buildTarget.options.outputPath = `dist/${options.project}/browser`;
}

const buildConfigurations = buildTarget?.configurations;
Expand All @@ -77,7 +77,7 @@ function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): R
builder: Builders.Server,
defaultConfiguration: 'production',
options: {
outputPath: `dist/${options.clientProject}/server`,
outputPath: `dist/${options.project}/server`,
main: join(
normalize(clientProject.root),
'src',
Expand Down Expand Up @@ -226,7 +226,7 @@ export default function (options: UniversalOptions): Rule {
return async (host: Tree, context: SchematicContext) => {
const workspace = await getWorkspace(host);

const clientProject = workspace.projects.get(options.clientProject);
const clientProject = workspace.projects.get(options.project);
if (!clientProject || clientProject.extensions.projectType !== 'application') {
throw new SchematicsException(`Universal requires a project type of "application".`);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/schematics/angular/universal/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ describe('Universal Schematic', () => {
require.resolve('../collection.json'),
);
const defaultOptions: UniversalOptions = {
clientProject: 'bar',
project: 'bar',
};
const workspaceUniversalOptions: UniversalOptions = {
clientProject: 'workspace',
project: 'workspace',
};

const workspaceOptions: WorkspaceOptions = {
Expand Down
9 changes: 6 additions & 3 deletions packages/schematics/angular/universal/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
"additionalProperties": false,
"description": "Pass this schematic to the \"run\" command to set up server-side rendering for an app.",
"properties": {
"clientProject": {
"project": {
"type": "string",
"description": "The name of the related client app. Required in place of \"project\"."
"description": "The name of the project.",
"$default": {
"$source": "projectName"
}
},
"appId": {
"type": "string",
Expand Down Expand Up @@ -45,5 +48,5 @@
"default": false
}
},
"required": ["clientProject"]
"required": ["project"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const snapshots = require('../../ng-snapshot/package.json');

export default async function () {
await appendToFile('src/app/app.component.html', '<router-outlet></router-outlet>');
await ng('generate', 'appShell', '--client-project', 'test-project');
await ng('generate', 'appShell', '--project', 'test-project');

const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots'];
if (isSnapshotBuild) {
Expand Down
7 changes: 5 additions & 2 deletions tests/legacy-cli/e2e/tests/build/platform-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { updateJsonFile } from '../../utils/project';
const snapshots = require('../../ng-snapshot/package.json');

export default async function () {
await ng('generate', 'universal', '--client-project', 'test-project');
await ng('generate', 'universal', '--project', 'test-project');

const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots'];
if (isSnapshotBuild) {
Expand Down Expand Up @@ -48,7 +48,10 @@ export default async function () {

await ng('run', 'test-project:server', '--optimization', 'false');

await expectFileToMatch('dist/test-project/server/main.js', /exports.*AppServerModule|"AppServerModule":/);
await expectFileToMatch(
'dist/test-project/server/main.js',
/exports.*AppServerModule|"AppServerModule":/,
);
await exec(normalize('node'), 'dist/test-project/server/main.js');
await expectFileToMatch(
'dist/test-project/server/index.html',
Expand Down
40 changes: 40 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,40 @@
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')) {
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')) {
throw new Error('Expected "foo.component.less" to exist.');
}
}
13 changes: 5 additions & 8 deletions tests/legacy-cli/e2e/tests/i18n/ivy-localize-app-shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ export default async function () {

const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots'];

await updateJsonFile('package.json', packageJson => {
await updateJsonFile('package.json', (packageJson) => {
const dependencies = packageJson['dependencies'];
dependencies['@angular/localize'] = isSnapshotBuild
? snapshots.dependencies['@angular/localize']
: readNgVersion();
});

await appendToFile('src/app/app.component.html', '<router-outlet></router-outlet>');
await ng('generate', 'appShell', '--client-project', 'test-project');
await ng('generate', 'appShell', '--project', 'test-project');

if (isSnapshotBuild) {
await updateJsonFile('package.json', packageJson => {
await updateJsonFile('package.json', (packageJson) => {
const dependencies = packageJson['dependencies'];
dependencies['@angular/platform-server'] = snapshots.dependencies['@angular/platform-server'];
dependencies['@angular/router'] = snapshots.dependencies['@angular/router'];
Expand All @@ -49,7 +49,7 @@ export default async function () {
{ lang: 'fr', translation: 'Bonjour i18n!' },
];

await updateJsonFile('angular.json', workspaceJson => {
await updateJsonFile('angular.json', (workspaceJson) => {
const appProject = workspaceJson.projects['test-project'];
const appArchitect = appProject.architect || appProject.targets;
const buildOptions = appArchitect['build'].options;
Expand Down Expand Up @@ -111,10 +111,7 @@ export default async function () {

// Clean up app.component.html so that we can easily
// find the translation text
await writeFile(
'src/app/app.component.html',
'<router-outlet></router-outlet>',
);
await writeFile('src/app/app.component.html', '<router-outlet></router-outlet>');

for (const { lang, translation } of langTranslations) {
if (lang != 'en-US') {
Expand Down