/
cypress-project.ts
260 lines (239 loc) · 8.21 KB
/
cypress-project.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import {
addDependenciesToPackageJson,
addProjectConfiguration,
readProjectConfiguration,
convertNxGenerator,
formatFiles,
generateFiles,
getWorkspaceLayout,
joinPathFragments,
names,
offsetFromRoot,
toJS,
Tree,
updateJson,
ProjectConfiguration,
stripIndents,
} from '@nrwl/devkit';
import { Linter, lintProjectGenerator } from '@nrwl/linter';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';
import { getRelativePathToRootTsConfig } from '@nrwl/workspace/src/utilities/typescript';
import { join } from 'path';
// app
import { Schema } from './schema';
import {
cypressVersion,
eslintPluginCypressVersion,
} from '../../utils/versions';
import { filePathPrefix } from '../../utils/project-name';
import { installedCypressVersion } from '../../utils/cypress-version';
export interface CypressProjectSchema extends Schema {
projectName: string;
projectRoot: string;
}
function createFiles(tree: Tree, options: CypressProjectSchema) {
generateFiles(tree, join(__dirname, './files'), options.projectRoot, {
tmpl: '',
...options,
project: options.project || 'Project',
ext: options.js ? 'js' : 'ts',
offsetFromRoot: offsetFromRoot(options.projectRoot),
rootTsConfigPath: getRelativePathToRootTsConfig(tree, options.projectRoot),
});
const cypressVersion = installedCypressVersion();
if (!cypressVersion || cypressVersion >= 7) {
tree.delete(join(options.projectRoot, 'src/plugins/index.js'));
} else {
updateJson(tree, join(options.projectRoot, 'cypress.json'), (json) => {
json.pluginsFile = './src/plugins/index';
return json;
});
}
if (options.js) {
toJS(tree);
}
}
function addProject(tree: Tree, options: CypressProjectSchema) {
let e2eProjectConfig: ProjectConfiguration;
if (options.baseUrl) {
e2eProjectConfig = {
root: options.projectRoot,
sourceRoot: joinPathFragments(options.projectRoot, 'src'),
projectType: 'application',
targets: {
e2e: {
executor: '@nrwl/cypress:cypress',
options: {
cypressConfig: joinPathFragments(
options.projectRoot,
'cypress.json'
),
baseUrl: options.baseUrl,
},
},
},
tags: [],
implicitDependencies: options.project ? [options.project] : undefined,
};
} else if (options.project) {
const project = readProjectConfiguration(tree, options.project);
if (!project.targets?.serve) {
throw new Error(
`Unable to find a serve target for the provided project, "${options.project}". Please define a serve target or provide a baseUrl via the --baseUrl flag.`
);
}
const devServerTarget =
project.targets.serve && project.targets.serve.defaultConfiguration
? `${options.project}:serve:${project.targets.serve.defaultConfiguration}`
: `${options.project}:serve`;
e2eProjectConfig = {
root: options.projectRoot,
sourceRoot: joinPathFragments(options.projectRoot, 'src'),
projectType: 'application',
targets: {
e2e: {
executor: '@nrwl/cypress:cypress',
options: {
cypressConfig: joinPathFragments(
options.projectRoot,
'cypress.json'
),
devServerTarget,
},
configurations: {
production: {
devServerTarget: `${options.project}:serve:production`,
},
},
},
},
tags: [],
implicitDependencies: options.project ? [options.project] : undefined,
};
} else {
throw new Error(`Either project or baseUrl should be specified.`);
}
const detectedCypressVersion = installedCypressVersion() ?? cypressVersion;
if (detectedCypressVersion < 7) {
e2eProjectConfig.targets.e2e.options.tsConfig = joinPathFragments(
options.projectRoot,
'tsconfig.json'
);
}
addProjectConfiguration(
tree,
options.projectName,
e2eProjectConfig,
options.standaloneConfig
);
}
export async function addLinter(host: Tree, options: CypressProjectSchema) {
if (options.linter === Linter.None) {
return () => {};
}
const installTask = await lintProjectGenerator(host, {
project: options.projectName,
linter: options.linter,
skipFormat: true,
tsConfigPaths: [joinPathFragments(options.projectRoot, 'tsconfig.json')],
eslintFilePatterns: [
`${options.projectRoot}/**/*.${options.js ? 'js' : '{js,ts}'}`,
],
setParserOptionsProject: options.setParserOptionsProject,
skipPackageJson: options.skipPackageJson,
});
if (!options.linter || options.linter !== Linter.EsLint) {
return installTask;
}
const installTask2 = !options.skipPackageJson
? addDependenciesToPackageJson(
host,
{},
{ 'eslint-plugin-cypress': eslintPluginCypressVersion }
)
: () => {};
updateJson(host, join(options.projectRoot, '.eslintrc.json'), (json) => {
json.extends = ['plugin:cypress/recommended', ...json.extends];
json.overrides = [
/**
* In order to ensure maximum efficiency when typescript-eslint generates TypeScript Programs
* behind the scenes during lint runs, we need to make sure the project is configured to use its
* own specific tsconfigs, and not fall back to the ones in the root of the workspace.
*/
{
files: ['*.ts', '*.tsx', '*.js', '*.jsx'],
/**
* NOTE: We no longer set parserOptions.project by default when creating new projects.
*
* We have observed that users rarely add rules requiring type-checking to their Nx workspaces, and therefore
* do not actually need the capabilites which parserOptions.project provides. When specifying parserOptions.project,
* typescript-eslint needs to create full TypeScript Programs for you. When omitting it, it can perform a simple
* parse (and AST tranformation) of the source files it encounters during a lint run, which is much faster and much
* less memory intensive.
*
* In the rare case that users attempt to add rules requiring type-checking to their setup later on (and haven't set
* parserOptions.project), the executor will attempt to look for the particular error typescript-eslint gives you
* and provide feedback to the user.
*/
parserOptions: !options.setParserOptionsProject
? undefined
: {
project: `${options.projectRoot}/tsconfig.*?.json`,
},
/**
* Having an empty rules object present makes it more obvious to the user where they would
* extend things from if they needed to
*/
rules: {},
},
];
if (installedCypressVersion() < 7) {
/**
* We need this override because we enabled allowJS in the tsconfig to allow for JS based Cypress tests.
* That however leads to issues with the CommonJS Cypress plugin file.
*/
json.overrides.push({
files: ['src/plugins/index.js'],
rules: {
'@typescript-eslint/no-var-requires': 'off',
'no-undef': 'off',
},
});
}
return json;
});
return runTasksInSerial(installTask, installTask2);
}
export async function cypressProjectGenerator(host: Tree, schema: Schema) {
const options = normalizeOptions(host, schema);
createFiles(host, options);
addProject(host, options);
const installTask = await addLinter(host, options);
if (!options.skipFormat) {
await formatFiles(host);
}
return installTask;
}
function normalizeOptions(host: Tree, options: Schema): CypressProjectSchema {
const { appsDir } = getWorkspaceLayout(host);
const projectName = filePathPrefix(
options.directory ? `${options.directory}-${options.name}` : options.name
);
const projectRoot = options.directory
? joinPathFragments(
appsDir,
names(options.directory).fileName,
options.name
)
: joinPathFragments(appsDir, options.name);
options.linter = options.linter || Linter.EsLint;
return {
...options,
projectName,
projectRoot,
};
}
export default cypressProjectGenerator;
export const cypressProjectSchematic = convertNxGenerator(
cypressProjectGenerator
);