-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathplugin.ts
207 lines (178 loc) · 5.37 KB
/
plugin.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
import {
CreateDependencies,
CreateNodes,
CreateNodesContext,
joinPathFragments,
NxJsonConfiguration,
readJsonFile,
TargetConfiguration,
writeJsonFile,
} from '@nx/devkit';
import { dirname, join, relative, resolve } from 'path';
import { readTargetDefaultsForTarget } from 'nx/src/project-graph/utils/project-configuration-utils';
import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
import { existsSync, readdirSync, readFileSync } from 'fs';
import { readConfig } from 'jest-config';
import { projectGraphCacheDirectory } from 'nx/src/utils/cache-directory';
import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes';
import { getGlobPatternsFromPackageManagerWorkspaces } from 'nx/src/plugins/package-json-workspaces';
import { combineGlobPatterns } from 'nx/src/utils/globs';
import { minimatch } from 'minimatch';
export interface JestPluginOptions {
targetName?: string;
}
const cachePath = join(projectGraphCacheDirectory, 'jest.hash');
const targetsCache = existsSync(cachePath) ? readTargetsCache() : {};
const calculatedTargets: Record<
string,
Record<string, TargetConfiguration>
> = {};
function readTargetsCache(): Record<
string,
Record<string, TargetConfiguration>
> {
return readJsonFile(cachePath);
}
function writeTargetsToCache(
targets: Record<string, Record<string, TargetConfiguration>>
) {
writeJsonFile(cachePath, targets);
}
export const createDependencies: CreateDependencies = () => {
writeTargetsToCache(calculatedTargets);
return [];
};
export const createNodes: CreateNodes<JestPluginOptions> = [
'**/jest.config.{cjs,mjs,js,cts,mts,ts}',
async (configFilePath, options, context) => {
const projectRoot = dirname(configFilePath);
const packageManagerWorkspacesGlob = combineGlobPatterns(
getGlobPatternsFromPackageManagerWorkspaces(context.workspaceRoot)
);
// Do not create a project if package.json and project.json isn't there.
const siblingFiles = readdirSync(join(context.workspaceRoot, projectRoot));
if (
!siblingFiles.includes('package.json') &&
!siblingFiles.includes('project.json')
) {
return {};
} else if (
!siblingFiles.includes('project.json') &&
siblingFiles.includes('package.json')
) {
const path = joinPathFragments(projectRoot, 'package.json');
const isPackageJsonProject = minimatch(
path,
packageManagerWorkspacesGlob
);
if (!isPackageJsonProject) {
return {};
}
}
const jestConfigContent = readFileSync(
resolve(context.workspaceRoot, configFilePath),
'utf-8'
);
if (jestConfigContent.includes('getJestProjectsAsync()')) {
// The `getJestProjectsAsync` function uses the project graph, which leads to a
// circular dependency. We can skip this since it's no intended to be used for
// an Nx project.
return {};
}
options = normalizeOptions(options);
const hash = calculateHashForCreateNodes(projectRoot, options, context);
const targets =
targetsCache[hash] ??
(await buildJestTargets(configFilePath, projectRoot, options, context));
calculatedTargets[hash] = targets;
return {
projects: {
[projectRoot]: {
root: projectRoot,
targets: targets,
},
},
};
},
];
async function buildJestTargets(
configFilePath: string,
projectRoot: string,
options: JestPluginOptions,
context: CreateNodesContext
) {
const config = await readConfig(
{
_: [],
$0: undefined,
},
resolve(context.workspaceRoot, configFilePath)
);
const targetDefaults = readTargetDefaultsForTarget(
options.targetName,
context.nxJsonConfiguration.targetDefaults,
'nx:run-commands'
);
const namedInputs = getNamedInputs(projectRoot, context);
const targets: Record<string, TargetConfiguration> = {};
const target: TargetConfiguration = (targets[options.targetName] = {
command: 'jest',
options: {
cwd: projectRoot,
},
});
if (!targetDefaults?.cache) {
target.cache = true;
}
if (!targetDefaults?.inputs) {
target.inputs = getInputs(namedInputs);
}
if (!targetDefaults?.outputs) {
target.outputs = getOutputs(projectRoot, config, context);
}
return targets;
}
function getInputs(
namedInputs: NxJsonConfiguration['namedInputs']
): TargetConfiguration['inputs'] {
return [
...('production' in namedInputs
? ['default', '^production']
: ['default', '^default']),
{
externalDependencies: ['jest'],
},
];
}
function getOutputs(
projectRoot: string,
{ globalConfig }: Awaited<ReturnType<typeof readConfig>>,
context: CreateNodesContext
): string[] {
function getOutput(path: string): string {
const relativePath = relative(
join(context.workspaceRoot, projectRoot),
path
);
if (relativePath.startsWith('..')) {
return join('{workspaceRoot}', join(projectRoot, relativePath));
} else {
return join('{projectRoot}', relativePath);
}
}
const outputs = [];
for (const outputOption of [
globalConfig.coverageDirectory,
globalConfig.outputFile,
]) {
if (outputOption) {
outputs.push(getOutput(outputOption));
}
}
return outputs;
}
function normalizeOptions(options: JestPluginOptions): JestPluginOptions {
options ??= {};
options.targetName ??= 'test';
return options;
}