/
e2e.ts
112 lines (98 loc) · 3.06 KB
/
e2e.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
import {
formatFiles,
getWorkspaceLayout,
names,
convertNxGenerator,
readProjectConfiguration,
generateFiles,
addProjectConfiguration,
updateProjectConfiguration,
} from '@nrwl/devkit';
import type { Tree } from '@nrwl/devkit';
import type { Schema } from './schema';
import * as path from 'path';
import { jestProjectGenerator } from '@nrwl/jest';
import { getRelativePathToRootTsConfig } from '@nrwl/workspace/src/utilities/typescript';
interface NormalizedSchema extends Schema {
projectRoot: string;
projectName: string;
pluginPropertyName: string;
npmScope: string;
}
function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
const { npmScope, appsDir } = getWorkspaceLayout(host);
const projectName = `${options.pluginName}-e2e`;
const projectRoot = options.projectDirectory
? path.join(appsDir, `${options.projectDirectory}-e2e`)
: path.join(appsDir, projectName);
const pluginPropertyName = names(options.pluginName).propertyName;
return {
...options,
projectName,
pluginPropertyName,
projectRoot,
npmScope,
};
}
function validatePlugin(host: Tree, pluginName: string) {
try {
readProjectConfiguration(host, pluginName);
} catch {
throw new Error(`Project name "${pluginName}" doesn't not exist.`);
}
}
function addFiles(host: Tree, options: NormalizedSchema) {
generateFiles(host, path.join(__dirname, './files'), options.projectRoot, {
...options,
tmpl: '',
rootTsConfigPath: getRelativePathToRootTsConfig(host, options.projectRoot),
});
}
function updateWorkspaceConfiguration(host: Tree, options: NormalizedSchema) {
addProjectConfiguration(
host,
options.projectName,
{
root: options.projectRoot,
projectType: 'application',
sourceRoot: `${options.projectRoot}/src`,
targets: {
e2e: {
executor: '@nrwl/nx-plugin:e2e',
options: { target: `${options.pluginName}:build` },
},
},
tags: [],
implicitDependencies: [options.pluginName],
},
options.standaloneConfig
);
}
async function addJest(host: Tree, options: NormalizedSchema) {
await jestProjectGenerator(host, {
project: options.projectName,
setupFile: 'none',
supportTsx: false,
skipSerializers: true,
});
const project = readProjectConfiguration(host, options.projectName);
const testOptions = project.targets.test.options;
const e2eOptions = project.targets.e2e.options;
project.targets.e2e.options = {
...e2eOptions,
jestConfig: testOptions.jestConfig,
};
// remove the jest build target
delete project.targets.test;
updateProjectConfiguration(host, options.projectName, project);
}
export async function e2eProjectGenerator(host: Tree, schema: Schema) {
validatePlugin(host, schema.pluginName);
const options = normalizeOptions(host, schema);
addFiles(host, options);
updateWorkspaceConfiguration(host, options);
await addJest(host, options);
await formatFiles(host);
}
export default e2eProjectGenerator;
export const e2eProjectSchematic = convertNxGenerator(e2eProjectGenerator);