forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
106 lines (96 loc) · 2.97 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {
Rule,
SchematicsException,
apply,
applyTemplates,
chain,
mergeWith,
move,
strings,
url,
} from '@angular-devkit/schematics';
import { posix as path } from 'node:path';
import { DependencyType, ExistingBehavior, addDependency } from '../utility/dependency';
import { JSONFile } from '../utility/json-file';
import { latestVersions } from '../utility/latest-versions';
import { addRootProvider } from '../utility/standalone';
import { updateWorkspace } from '../utility/workspace';
import { Builders as AngularBuilder } from '../utility/workspace-models';
import { Schema as E2eOptions } from './schema';
/**
* The list of development dependencies used by the E2E protractor-based builder.
* The versions are sourced from the latest versions `../utility/latest-versions/package.json`
* file which is automatically updated via renovate.
*/
const E2E_DEV_DEPENDENCIES = Object.freeze([
'protractor',
'jasmine-spec-reporter',
'ts-node',
'@types/node',
]);
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 {
const { relatedAppName } = options;
return updateWorkspace((workspace) => {
const project = workspace.projects.get(relatedAppName);
if (!project) {
throw new SchematicsException(`Project name "${relatedAppName}" doesn't not exist.`);
}
const e2eRootPath = path.join(project.root, 'e2e');
project.targets.add({
name: 'e2e',
builder: AngularBuilder.Protractor,
defaultConfiguration: 'development',
options: {
protractorConfig: path.join(e2eRootPath, 'protractor.conf.js'),
},
configurations: {
production: {
devServerTarget: `${relatedAppName}:serve:production`,
},
development: {
devServerTarget: `${relatedAppName}:serve:development`,
},
},
});
return chain([
mergeWith(
apply(url('./files'), [
applyTemplates({
utils: strings,
...options,
relativePathToWorkspaceRoot: path.relative(path.join('/', e2eRootPath), '/'),
}),
move(e2eRootPath),
]),
),
addRootProvider(
relatedAppName,
({ code, external }) =>
code`${external('provideProtractorTestingSupport', '@angular/platform-browser')}()`,
),
...E2E_DEV_DEPENDENCIES.map((name) =>
addDependency(name, latestVersions[name], {
type: DependencyType.Dev,
existing: ExistingBehavior.Skip,
}),
),
addScriptsToPackageJson(),
]);
});
}