Skip to content

Commit

Permalink
feat(angular): prompt users for standalone components in application
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Feb 17, 2023
1 parent 2a76e20 commit c6245f3
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
"standalone": {
"description": "Generate an application that is setup to use standalone components. _Note: This is only supported in Angular versions >= 14.1.0_",
"type": "boolean",
"default": false
"x-priority": "important"
},
"rootProject": {
"description": "Create an application at the root of the workspace.",
Expand Down
3 changes: 2 additions & 1 deletion packages/angular/ng-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"semver",
"webpack",
"http-server",
"magic-string"
"magic-string",
"enquirer"
],
"keepLifecycleScripts": true
}
3 changes: 2 additions & 1 deletion packages/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"ts-node": "10.9.1",
"tsconfig-paths": "^4.1.2",
"webpack": "^5.75.0",
"webpack-merge": "5.7.3"
"webpack-merge": "5.7.3",
"enquirer": "^2.3.6"
},
"peerDependencies": {
"@angular-devkit/build-angular": ">= 14.0.0 < 16.0.0",
Expand Down
50 changes: 49 additions & 1 deletion packages/angular/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@ import {
} from '../../utils/versions';
import { applicationGenerator } from './application';
import type { Schema } from './schema';

import * as enquirer from 'enquirer';
// need to mock cypress otherwise it'll use the nx installed version from package.json
// which is v9 while we are testing for the new v10 version
jest.mock('@nrwl/cypress/src/utils/cypress-version');
jest.mock('enquirer');
describe('app', () => {
let appTree: Tree;
let mockedInstalledCypressVersion: jest.Mock<
ReturnType<typeof installedCypressVersion>
> = installedCypressVersion as never;

beforeEach(() => {
mockedInstalledCypressVersion.mockReturnValue(10);
// @ts-ignore
enquirer.prompt = jest
.fn()
.mockReturnValue(Promise.resolve({ 'standalone-components': true }));
appTree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
});

Expand Down Expand Up @@ -819,6 +825,48 @@ describe('app', () => {
appTree.read('apps/standalone/src/app/nx-welcome.component.ts', 'utf-8')
).toContain('standalone: true');
});

it('should prompt for standalone components and not use them when the user selects false', async () => {
// ARRANGE
process.env.NX_INTERACTIVE = 'true';
// @ts-ignore
enquirer.prompt = jest
.fn()
.mockReturnValue(Promise.resolve({ 'standalone-components': false }));

// ACT
await generateApp(appTree, 'nostandalone');

// ASSERT
expect(
appTree.exists('apps/nostandalone/src/app/app.module.ts')
).toBeTruthy();
expect(enquirer.prompt).toHaveBeenCalled();

// CLEANUP
process.env.NX_INTERACTIVE = undefined;
});

it('should prompt for standalone components and use them when the user selects true', async () => {
// ARRANGE
process.env.NX_INTERACTIVE = 'true';
// @ts-ignore
enquirer.prompt = jest
.fn()
.mockReturnValue(Promise.resolve({ 'standalone-components': true }));

// ACT
await generateApp(appTree, 'nostandalone');

// ASSERT
expect(
appTree.exists('apps/nostandalone/src/app/app.module.ts')
).not.toBeTruthy();
expect(enquirer.prompt).toHaveBeenCalled();

// CLEANUP
process.env.NX_INTERACTIVE = undefined;
});
});

it('should generate correct main.ts', async () => {
Expand Down
15 changes: 14 additions & 1 deletion packages/angular/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
updateNxComponentTemplate,
} from './lib';
import type { Schema } from './schema';
import { lt } from 'semver';
import { gte, lt } from 'semver';
import { prompt } from 'enquirer';

export async function applicationGenerator(
tree: Tree,
Expand All @@ -47,6 +48,18 @@ export async function applicationGenerator(
You can resolve this error by removing the "standalone" option or by migrating to Angular 14.1.0.`);
}

if (
gte(installedAngularVersionInfo.version, '14.1.0') &&
schema.standalone === undefined &&
process.env.NX_INTERACTIVE === 'true'
) {
schema.standalone = await prompt({
name: 'standalone-components',
message: 'Would you like to use Standalone Components?',
type: 'confirm',
}).then((a) => a['standalone-components']);
}

const generatorDirectory =
getGeneratorDirectoryForInstalledAngularVersion(tree);
if (generatorDirectory) {
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
"standalone": {
"description": "Generate an application that is setup to use standalone components. _Note: This is only supported in Angular versions >= 14.1.0_",
"type": "boolean",
"default": false
"x-priority": "important"
},
"rootProject": {
"description": "Create an application at the root of the workspace.",
Expand Down
1 change: 1 addition & 0 deletions packages/angular/src/generators/host/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function host(tree: Tree, options: Schema) {

const appInstallTask = await applicationGenerator(tree, {
...options,
standalone: options.standalone ?? false,
routing: true,
port: 4200,
skipFormat: true,
Expand Down
1 change: 1 addition & 0 deletions packages/angular/src/generators/remote/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export async function remote(tree: Tree, options: Schema) {

const appInstallTask = await applicationGenerator(tree, {
...options,
standalone: options.standalone ?? false,
routing: true,
port,
});
Expand Down

0 comments on commit c6245f3

Please sign in to comment.