From a0e2f28d26c9e69d3523b5ea7ea501431e42988c Mon Sep 17 00:00:00 2001 From: timdeschryver <28659384+timdeschryver@users.noreply.github.com> Date: Sun, 19 Apr 2020 18:42:15 +0200 Subject: [PATCH] fix(@schematics/angular): don't create e2e script when createApplication is false Bugfix for the ng new --createApplication=false command. Currently, it creates an e2e script in package.json. This change will only add the script when the application is created. Closes #13412 --- packages/schematics/angular/e2e/index.ts | 13 +++++++++++++ packages/schematics/angular/e2e/index_spec.ts | 7 +++++++ .../angular/workspace/files/package.json.template | 3 +-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/schematics/angular/e2e/index.ts b/packages/schematics/angular/e2e/index.ts index cee30a5e37..2e6f56bb78 100644 --- a/packages/schematics/angular/e2e/index.ts +++ b/packages/schematics/angular/e2e/index.ts @@ -17,11 +17,23 @@ import { move, url, } from '@angular-devkit/schematics'; +import { JSONFile } from '../utility/json-file'; import { relativePathToWorkspaceRoot } from '../utility/paths'; import { getWorkspace, updateWorkspace } from '../utility/workspace'; import { Builders } from '../utility/workspace-models'; import { Schema as E2eOptions } from './schema'; +function addScriptsToPackageJson(): Rule { + return host => { + const pkgJson = new JSONFile(host, 'package.json'); + const e2eScriptPath = ['scripts', 'e2e']; + + if (!pkgJson.get(e2eScriptPath)) { + pkgJson.modify(e2eScriptPath, 'ng e2e', false); + } + }; +} + export default function (options: E2eOptions): Rule { return async (host: Tree) => { const appProject = options.relatedAppName; @@ -65,6 +77,7 @@ export default function (options: E2eOptions): Rule { }), move(root), ])), + addScriptsToPackageJson(), ]); }; } diff --git a/packages/schematics/angular/e2e/index_spec.ts b/packages/schematics/angular/e2e/index_spec.ts index 4ef92e8f64..fb63f66ade 100644 --- a/packages/schematics/angular/e2e/index_spec.ts +++ b/packages/schematics/angular/e2e/index_spec.ts @@ -99,4 +99,11 @@ describe('Application Schematic', () => { expect(e2eOptions.devServerTarget).toEqual('foo:serve'); }); }); + + it('should add an e2e script in package.json', async () => { + const tree = await schematicRunner.runSchematicAsync('e2e', defaultOptions, applicationTree) + .toPromise(); + const pkg = JSON.parse(tree.readContent('/package.json')); + expect(pkg.scripts['e2e']).toBe('ng e2e'); + }); }); diff --git a/packages/schematics/angular/workspace/files/package.json.template b/packages/schematics/angular/workspace/files/package.json.template index e8255f161a..b8537fd0aa 100644 --- a/packages/schematics/angular/workspace/files/package.json.template +++ b/packages/schematics/angular/workspace/files/package.json.template @@ -6,8 +6,7 @@ "start": "ng serve", "build": "ng build", "test": "ng test", - "lint": "ng lint", - "e2e": "ng e2e" + "lint": "ng lint" }, "private": true, "dependencies": {