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
This commit is contained in:
timdeschryver 2020-04-19 18:42:15 +02:00 committed by Alan Agius
parent 155707a1ba
commit a0e2f28d26
3 changed files with 21 additions and 2 deletions

View File

@ -17,11 +17,23 @@ import {
move, move,
url, url,
} from '@angular-devkit/schematics'; } from '@angular-devkit/schematics';
import { JSONFile } from '../utility/json-file';
import { relativePathToWorkspaceRoot } from '../utility/paths'; import { relativePathToWorkspaceRoot } from '../utility/paths';
import { getWorkspace, updateWorkspace } from '../utility/workspace'; import { getWorkspace, updateWorkspace } from '../utility/workspace';
import { Builders } from '../utility/workspace-models'; import { Builders } from '../utility/workspace-models';
import { Schema as E2eOptions } from './schema'; 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 { export default function (options: E2eOptions): Rule {
return async (host: Tree) => { return async (host: Tree) => {
const appProject = options.relatedAppName; const appProject = options.relatedAppName;
@ -65,6 +77,7 @@ export default function (options: E2eOptions): Rule {
}), }),
move(root), move(root),
])), ])),
addScriptsToPackageJson(),
]); ]);
}; };
} }

View File

@ -99,4 +99,11 @@ describe('Application Schematic', () => {
expect(e2eOptions.devServerTarget).toEqual('foo:serve'); 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');
});
}); });

View File

@ -6,8 +6,7 @@
"start": "ng serve", "start": "ng serve",
"build": "ng build", "build": "ng build",
"test": "ng test", "test": "ng test",
"lint": "ng lint", "lint": "ng lint"
"e2e": "ng e2e"
}, },
"private": true, "private": true,
"dependencies": { "dependencies": {