Skip to content
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

feat(angular): remove decorate-angular-cli #14226

Merged
merged 1 commit into from
Jan 9, 2023
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
3 changes: 0 additions & 3 deletions docs/shared/migration/migration-angular.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ This will enable you to use the Nx CLI in your existing Angular CLI workspace wh

- The `nx`, `@nrwl/workspace` and `prettier` packages will be installed.
- An `nx.json` file will be created in the root of your workspace.
- A `decorate-angular-cli.js` file will be added to the root of your workspace, and a `postinstall` script will be added to your `package.json` to run this script when your dependencies are updated. (The script forwards the `ng` commands to the Nx CLI (`nx`) to enable features such as [Computation Caching](/concepts/how-caching-works).)
- For an Angular 15+ repo, the `angular.json` file is split into separate `project.json` files for each project.

After the process completes, you can continue using the same `serve/build/lint/test` commands you are used to.
Expand Down Expand Up @@ -104,7 +103,6 @@ After the changes are applied, your workspace file structure should look similar
├── .gitignore
├── .prettierignore
├── .prettierrc
├── decorate-angular-cli.js
├── karma.conf.js
├── nx.json
├── package.json
Expand Down Expand Up @@ -359,7 +357,6 @@ The script will make the following changes:
- If you opted into Nx Cloud, `@nrwl/nx-cloud` will be installed as well.
- If your project's Angular version is greater than or equal to version 13, then the `@nrwl/angular` package will be installed as well.
- Creates an `nx.json` file in the root of your workspace.
- Adds a `decorate-angular-cli.js` to the root of your workspace, and a `postinstall` script in your `package.json` to run the script when your dependencies are updated. The script forwards the `ng` commands to the Nx CLI (`nx`) to enable features such as [Computation Caching](/concepts/how-caching-works).

By running this command and accepting Nx Cloud, Nx distributed caching is now enabled.

Expand Down
3 changes: 1 addition & 2 deletions e2e/angular-core/src/ng-add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,11 @@ describe('convert Angular CLI workspace to an Nx workspace', () => {
const updatedPackageJson = readJson('package.json');
expect(updatedPackageJson.description).toEqual('some description');
expect(updatedPackageJson.scripts).toEqual({
ng: 'nx',
Copy link
Member

Choose a reason for hiding this comment

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

Should we instead change this to something like 'nx': 'nx' or simply remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I'm not too certain!

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is from what @angular/cli generates?

I think we should leave it alone.

ng: 'ng',
start: 'nx serve',
build: 'nx build',
watch: 'nx build --watch --configuration development',
test: 'nx test',
postinstall: 'node ./decorate-angular-cli.js',
});
expect(updatedPackageJson.devDependencies['@nrwl/workspace']).toBeDefined();
expect(updatedPackageJson.devDependencies['@angular/cli']).toBeDefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,33 +148,14 @@ describe('workspace', () => {
await migrateFromAngularCli(tree, {});

expect(readJson(tree, 'package.json').scripts).toStrictEqual({
ng: 'nx',
ng: 'ng',
start: 'nx serve',
build: 'nx build',
watch: 'nx build --watch --configuration development',
test: 'nx test',
postinstall: 'node ./decorate-angular-cli.js',
});
});

it('should handle existing postinstall script', async () => {
tree.write(
'package.json',
JSON.stringify({
name: '@my-org/my-monorepo',
scripts: {
postinstall: 'node some-awesome-script.js',
},
})
);

await migrateFromAngularCli(tree, {});

expect(readJson(tree, 'package.json').scripts.postinstall).toEqual(
'node some-awesome-script.js && node ./decorate-angular-cli.js'
);
});

it('should remove the angular.json file', async () => {
tree.write(
'/angular.json',
Expand Down Expand Up @@ -586,14 +567,6 @@ describe('workspace', () => {
expect(readJson(tree, 'nx.json')).toMatchSnapshot();
});

it('should create decorate-angular-cli.js', async () => {
await migrateFromAngularCli(tree, { preserveAngularCliLayout: true });

expect(tree.exists('/decorate-angular-cli.js')).toBe(true);
const { scripts } = readJson(tree, 'package.json');
expect(scripts.postinstall).toBe('node ./decorate-angular-cli.js');
});

it('should support multiple projects', async () => {
const angularJson = {
$schema: './node_modules/@angular/cli/lib/config/schema.json',
Expand Down Expand Up @@ -663,11 +636,9 @@ describe('workspace', () => {
await migrateFromAngularCli(tree, { preserveAngularCliLayout: true });

expect(tree.exists('angular.json')).toBe(false);
expect(tree.exists('decorate-angular-cli.js')).toBe(true);
expect(tree.exists('.prettierignore')).toBe(true);
expect(tree.exists('.prettierrc')).toBe(true);
const { scripts } = readJson(tree, 'package.json');
expect(scripts.postinstall).toBe('node ./decorate-angular-cli.js');
expect(readJson(tree, 'nx.json')).toMatchSnapshot();
expect(readJson(tree, 'project.json')).toMatchSnapshot();
expect(readJson(tree, 'projects/app2/project.json')).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
createNxJson,
createRootKarmaConfig,
createWorkspaceFiles,
decorateAngularCli,
deleteAngularJson,
deleteGitKeepFilesIfNotNeeded,
formatFilesTask,
Expand Down Expand Up @@ -53,7 +52,6 @@ export async function migrateFromAngularCli(
}
);
createNxJson(tree, options, angularJson.defaultProject);
decorateAngularCli(tree);
updateVsCodeRecommendedExtensions(tree);
await updatePrettierConfig(tree);

Expand Down Expand Up @@ -96,7 +94,6 @@ export async function migrateFromAngularCli(
updateWorkspaceConfigDefaults(tree);
updateRootTsConfig(tree);
updatePackageJson(tree);
decorateAngularCli(tree);
await createWorkspaceFiles(tree);

// migrate all projects
Expand Down
34 changes: 0 additions & 34 deletions packages/angular/src/generators/ng-add/utilities/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import { deduceDefaultBase } from '@nrwl/workspace/src/utilities/default-base';
import { resolveUserExistingPrettierConfig } from '@nrwl/workspace/src/utilities/prettier';
import { getRootTsConfigPathInTree } from '@nrwl/workspace/src/utilities/typescript';
import { prettierVersion } from '@nrwl/workspace/src/utils/versions';
import { readFileSync } from 'fs';
import { readModulePackageJson } from 'nx/src/utils/package-json';
import { dirname, join } from 'path';
import { angularDevkitVersion, nxVersion } from '../../../utils/versions';
import type { ProjectMigrator } from '../migrators';
import type { GeneratorOptions } from '../schema';
Expand Down Expand Up @@ -136,37 +133,6 @@ function getWorkspaceCommonTargets(tree: Tree): {
return targets;
}

export function decorateAngularCli(tree: Tree): void {
const nrwlWorkspacePath = readModulePackageJson('@nrwl/workspace').path;
const decorateCli = readFileSync(
join(
dirname(nrwlWorkspacePath),
'src/generators/utils/decorate-angular-cli.js__tmpl__'
),
'utf-8'
);
tree.write('decorate-angular-cli.js', decorateCli);

updateJson(tree, 'package.json', (json) => {
if (
json.scripts &&
json.scripts.postinstall &&
!json.scripts.postinstall.includes('decorate-angular-cli.js')
) {
// if exists, add execution of this script
json.scripts.postinstall += ' && node ./decorate-angular-cli.js';
} else {
json.scripts ??= {};
// if doesn't exist, set to execute this script
json.scripts.postinstall = 'node ./decorate-angular-cli.js';
}
if (json.scripts.ng) {
json.scripts.ng = 'nx';
}
return json;
});
}

export function updateWorkspaceConfigDefaults(tree: Tree): void {
const nxJson = readNxJson(tree);
delete (nxJson as any).newProjectRoot;
Expand Down
1 change: 0 additions & 1 deletion packages/cli/lib/decorate-cli.ts

This file was deleted.

16 changes: 0 additions & 16 deletions packages/nx/src/adapter/decorate-cli.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/workspace/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@
"ignore": ["**/tsconfig*.json", "project.json", ".eslintrc.json"],
"output": "/"
},
{
"input": "packages/workspace",
"glob": "**/**/decorate-angular-cli.js__tmpl__",
"output": "/"
},
{
"input": "packages/workspace",
"glob": "**/*.{js,css,html,svg}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ Object {
"license": "MIT",
"name": "my-workspace",
"private": true,
"scripts": Object {
"ng": "nx",
"postinstall": "node ./decorate-angular-cli.js",
},
"scripts": Object {},
"version": "0.0.0",
}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,37 +198,6 @@ describe('@nrwl/workspace:generateWorkspaceFiles', () => {
expect(recommendations).toMatchSnapshot();
});

it('should add decorate-angular-cli when used with angular cli', async () => {
await generateWorkspaceFiles(tree, {
name: 'proj',
directory: 'proj',
preset: Preset.AngularMonorepo,
defaultBase: 'main',
});
expect(tree.exists('/proj/decorate-angular-cli.js')).toBe(true);

const { scripts } = readJson(tree, '/proj/package.json');
expect(scripts).toMatchInlineSnapshot(`
Object {
"ng": "nx",
"postinstall": "node ./decorate-angular-cli.js",
}
`);
});

it('should not add decorate-angular-cli when used with nx cli', async () => {
await generateWorkspaceFiles(tree, {
name: 'proj',
directory: 'proj',
preset: Preset.Empty,
defaultBase: 'main',
});
expect(tree.exists('/proj/decorate-angular-cli.js')).toBe(false);

const { scripts } = readJson(tree, '/proj/package.json');
expect(scripts).toMatchInlineSnapshot(`Object {}`);
});

it('should create a workspace using NPM preset (npm package manager)', async () => {
tree.write('/proj/package.json', JSON.stringify({}));
await generateWorkspaceFiles(tree, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
prettierVersion,
typescriptVersion,
} from '../../utils/versions';
import { readFileSync } from 'fs';
import { join, join as pathJoin } from 'path';
import { Preset } from '../utils/presets';
import { deduceDefaultBase } from '../../utilities/default-base';
Expand All @@ -37,9 +36,7 @@ export async function generateWorkspaceFiles(
createFiles(tree, options);
createNxJson(tree, options);
createPrettierrc(tree, options);
if (options.preset === Preset.AngularMonorepo) {
decorateAngularClI(tree, options);
}

const [packageMajor] = getPackageManagerVersion(
options.packageManager as PackageManager
).split('.');
Expand All @@ -56,13 +53,6 @@ export async function generateWorkspaceFiles(
await formatFiles(tree);
}

function decorateAngularClI(tree: Tree, options: NormalizedSchema) {
const decorateCli = readFileSync(
pathJoin(__dirname as any, '..', 'utils', 'decorate-angular-cli.js__tmpl__')
).toString();
tree.write(join(options.directory, 'decorate-angular-cli.js'), decorateCli);
}

function setPresetProperty(tree: Tree, options: NormalizedSchema) {
updateJson(tree, join(options.directory, 'nx.json'), (json) => {
if (options.preset === Preset.Core || options.preset === Preset.NPM) {
Expand Down Expand Up @@ -204,16 +194,6 @@ function createYarnrcYml(tree: Tree, options: NormalizedSchema) {
}

function addNpmScripts(tree: Tree, options: NormalizedSchema) {
if (options.preset === Preset.AngularMonorepo) {
updateJson(tree, join(options.directory, 'package.json'), (json) => {
Object.assign(json.scripts, {
ng: 'nx',
postinstall: 'node ./decorate-angular-cli.js',
});
return json;
});
}

if (
options.preset === Preset.AngularStandalone ||
options.preset === Preset.ReactStandalone
Expand Down

This file was deleted.