Skip to content

Commit

Permalink
feat(@angular/cli): deprecated defaultProject option
Browse files Browse the repository at this point in the history
With this change we deprecate the angular.json `defaultProject` option.

DEPRECATED:

The `defaultProject` workspace option has been deprecated. The project to use will be determined from the current working directory.

Closes #20661
  • Loading branch information
alan-agius4 authored and dgp1130 committed Mar 21, 2022
1 parent e49220f commit 036327e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
3 changes: 2 additions & 1 deletion packages/angular/cli/lib/config/workspace-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"defaultProject": {
"type": "string",
"description": "Default project name used in commands."
"description": "Default project name used in commands.",
"x-deprecated": "The project to use will be determined from the current working directory."
},
"projects": {
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ export abstract class ArchitectCommandModule
// For multi target commands, we always list all projects that have the target.
return allProjectsForTargetName;
} else {
if (allProjectsForTargetName.length === 1) {
return allProjectsForTargetName;
}

const maybeProject = getProjectByCwd(workspace);
if (maybeProject && allProjectsForTargetName.includes(maybeProject)) {
return [maybeProject];
}

if (allProjectsForTargetName.length === 1) {
return allProjectsForTargetName;
}
}

return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ export abstract class SchematicsCommandModule
return 0;
}

private defaultProjectDeprecationWarningShown = false;
private getProjectName(): string | undefined {
const { workspace, logger } = this.context;
if (!workspace) {
Expand All @@ -354,6 +355,15 @@ export abstract class SchematicsCommandModule

const defaultProjectName = workspace.extensions['defaultProject'];
if (typeof defaultProjectName === 'string' && defaultProjectName) {
if (!this.defaultProjectDeprecationWarningShown) {
logger.warn(tags.oneLine`
DEPRECATED: The 'defaultProject' workspace option has been deprecated.
The project to use will be determined from the current working directory.
`);

this.defaultProjectDeprecationWarningShown = true;
}

return defaultProjectName;
}
}
Expand Down
5 changes: 1 addition & 4 deletions packages/angular/cli/src/commands/add/long-description.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
Adds the npm package for a published library to your workspace, and configures
the project in the current working directory (or the default project if you are
not in a project directory) to use that library, as specified by the library's schematic.
the project in the current working directory to use that library, as specified by the library's schematic.
For example, adding `@angular/pwa` configures your project for PWA support:

```bash
ng add @angular/pwa
```

The default project is the value of `defaultProject` in `angular.json`.
10 changes: 10 additions & 0 deletions packages/angular/cli/src/utilities/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ function findProjectByPath(workspace: AngularWorkspace, location: string): strin
return projects[0][1];
}

let defaultProjectDeprecationWarningShown = false;
export function getProjectByCwd(workspace: AngularWorkspace): string | null {
if (workspace.projects.size === 1) {
// If there is only one project, return that one.
Expand All @@ -277,6 +278,15 @@ export function getProjectByCwd(workspace: AngularWorkspace): string | null {
const defaultProject = workspace.extensions['defaultProject'];
if (defaultProject && typeof defaultProject === 'string') {
// If there is a default project name, return it.
if (!defaultProjectDeprecationWarningShown) {
console.warn(
`DEPRECATED: The 'defaultProject' workspace option has been deprecated. ` +
`The project to use will be determined from the current working directory.`,
);

defaultProjectDeprecationWarningShown = true;
}

return defaultProject;
}

Expand Down
4 changes: 0 additions & 4 deletions tests/legacy-cli/e2e/tests/misc/multiple-targets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { updateJsonFile } from '../../utils/project';
export default async function () {
await ng('generate', 'app', 'secondary-app');

await updateJsonFile('angular.json', workspaceJson => {
workspaceJson.defaultProject = undefined;
});

await ng('build', 'secondary-app', '--configuration=development');

expectFileToExist('dist/secondary-app/index.html');
Expand Down

1 comment on commit 036327e

@michaelfaith
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the behavior, if the current working directory is the project root?

Please sign in to comment.