forked from nrwl/nx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.ts
147 lines (126 loc) · 3.98 KB
/
init.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
import {
addDependenciesToPackageJson,
formatFiles,
readNxJson,
removeDependenciesFromPackageJson,
runTasksInSerial,
updateNxJson,
type GeneratorCallback,
type Tree,
} from '@nx/devkit';
import { updatePackageScripts } from '@nx/devkit/src/utils/update-package-scripts';
import { createNodes } from '../../plugins/plugin';
import { jestVersion, nxVersion } from '../../utils/versions';
import { isPresetCjs } from '../../utils/config/is-preset-cjs';
import type { JestInitSchema } from './schema';
function addPlugin(tree: Tree) {
const nxJson = readNxJson(tree);
nxJson.plugins ??= [];
if (
!nxJson.plugins.some((p) =>
typeof p === 'string'
? p === '@nx/jest/plugin'
: p.plugin === '@nx/jest/plugin'
)
) {
nxJson.plugins.push({
plugin: '@nx/jest/plugin',
options: {
targetName: 'test',
},
});
}
updateNxJson(tree, nxJson);
}
function updateProductionFileSet(tree: Tree, presetExt: 'cjs' | 'js') {
const nxJson = readNxJson(tree);
const productionFileSet = nxJson.namedInputs?.production;
if (productionFileSet) {
// This is one of the patterns in the default jest patterns
productionFileSet.push(
// Remove spec, test, and snapshots from the production fileset
'!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)',
// Remove tsconfig.spec.json
'!{projectRoot}/tsconfig.spec.json',
// Remove jest.config.js/ts
'!{projectRoot}/jest.config.[jt]s',
// Remove test-setup.js/ts
// TODO(meeroslav) this should be standardized
'!{projectRoot}/src/test-setup.[jt]s',
'!{projectRoot}/test-setup.[jt]s'
);
// Dedupe and set
nxJson.namedInputs.production = Array.from(new Set(productionFileSet));
}
updateNxJson(tree, nxJson);
}
function addJestTargetDefaults(tree: Tree, presetEnv: 'cjs' | 'js') {
const nxJson = readNxJson(tree);
nxJson.targetDefaults ??= {};
nxJson.targetDefaults['@nx/jest:jest'] ??= {};
const productionFileSet = nxJson.namedInputs?.production;
nxJson.targetDefaults['@nx/jest:jest'].cache ??= true;
// Test targets depend on all their project's sources + production sources of dependencies
nxJson.targetDefaults['@nx/jest:jest'].inputs ??= [
'default',
productionFileSet ? '^production' : '^default',
`{workspaceRoot}/jest.preset.${presetEnv}`,
];
nxJson.targetDefaults['@nx/jest:jest'].options ??= {
passWithNoTests: true,
};
nxJson.targetDefaults['@nx/jest:jest'].configurations ??= {
ci: {
ci: true,
codeCoverage: true,
},
};
updateNxJson(tree, nxJson);
}
function updateDependencies(tree: Tree, options: JestInitSchema) {
return addDependenciesToPackageJson(
tree,
{},
{
'@nx/jest': nxVersion,
jest: jestVersion,
},
undefined,
options.keepExistingVersions
);
}
export function jestInitGenerator(tree: Tree, options: JestInitSchema) {
return jestInitGeneratorInternal(tree, { addPlugin: false, ...options });
}
export async function jestInitGeneratorInternal(
tree: Tree,
options: JestInitSchema
): Promise<GeneratorCallback> {
const nxJson = readNxJson(tree);
const addPluginDefault =
process.env.NX_ADD_PLUGINS !== 'false' &&
nxJson.useInferencePlugins !== false;
options.addPlugin ??= addPluginDefault;
const presetExt = isPresetCjs(tree) ? 'cjs' : 'js';
if (!tree.exists('jest.preset.js') && !tree.exists('jest.preset.cjs')) {
updateProductionFileSet(tree, presetExt);
if (options.addPlugin) {
addPlugin(tree);
} else {
addJestTargetDefaults(tree, presetExt);
}
}
const tasks: GeneratorCallback[] = [];
if (!options.skipPackageJson) {
tasks.push(removeDependenciesFromPackageJson(tree, ['@nx/jest'], []));
tasks.push(updateDependencies(tree, options));
}
if (options.updatePackageScripts) {
await updatePackageScripts(tree, createNodes);
}
if (!options.skipFormat) {
await formatFiles(tree);
}
return runTasksInSerial(...tasks);
}
export default jestInitGenerator;