/
generator.ts
259 lines (234 loc) · 7.75 KB
/
generator.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
import {
addDependenciesToPackageJson,
joinPathFragments,
logger,
ProjectConfiguration,
readJson,
readProjectConfiguration,
TargetConfiguration,
Tree,
updateJson,
updateProjectConfiguration,
writeJson,
} from '@nrwl/devkit';
import type { Linter as ESLint } from 'eslint';
import { Schema as EsLintExecutorOptions } from '@nrwl/linter/src/executors/eslint/schema';
import { jsoncEslintParserVersion } from '../../utils/versions';
import { PluginLintChecksGeneratorSchema } from './schema';
import { NX_PREFIX } from 'nx/src/utils/logger';
import { PackageJson, readNxMigrateConfig } from 'nx/src/utils/package-json';
export default async function pluginLintCheckGenerator(
host: Tree,
options: PluginLintChecksGeneratorSchema
) {
const project = readProjectConfiguration(host, options.projectName);
const packageJson = readJson<PackageJson>(
host,
joinPathFragments(project.root, 'package.json')
);
// This rule is eslint **only**
if (projectIsEsLintEnabled(project)) {
updateRootEslintConfig(host);
updateProjectEslintConfig(host, project, packageJson);
updateProjectTarget(host, options, packageJson);
// Project is setup for vscode
if (host.exists('.vscode')) {
setupVsCodeLintingForJsonFiles(host);
}
// Project contains migrations.json
const migrationsPath = readNxMigrateConfig(packageJson).migrations;
if (
migrationsPath &&
host.exists(joinPathFragments(project.root, migrationsPath))
) {
addMigrationJsonChecks(host, options, packageJson);
}
} else {
logger.error(
`${NX_PREFIX} plugin lint checks can only be added to plugins which use eslint for linting`
);
}
const installTask = addDependenciesToPackageJson(
host,
{},
{ 'jsonc-eslint-parser': jsoncEslintParserVersion }
);
return () => installTask;
}
export function addMigrationJsonChecks(
host: Tree,
options: PluginLintChecksGeneratorSchema,
packageJson: PackageJson
) {
const projectConfiguration = readProjectConfiguration(
host,
options.projectName
);
if (!projectIsEsLintEnabled(projectConfiguration)) {
return;
}
const [eslintTarget, eslintTargetConfiguration] =
getEsLintOptions(projectConfiguration);
const relativeMigrationsJsonPath =
readNxMigrateConfig(packageJson).migrations;
if (!relativeMigrationsJsonPath) {
return;
}
const migrationsJsonPath = joinPathFragments(
projectConfiguration.root,
relativeMigrationsJsonPath
);
if (
eslintTarget &&
!eslintTargetConfiguration.options?.lintFilePatterns?.includes(
migrationsJsonPath
)
) {
// Add to lintFilePatterns
eslintTargetConfiguration.options.lintFilePatterns.push(migrationsJsonPath);
updateProjectConfiguration(host, options.projectName, projectConfiguration);
// Update project level eslintrc
updateJson<ESLint.Config>(
host,
`${projectConfiguration.root}/.eslintrc.json`,
(c) => {
const override = c.overrides.find((o) =>
Object.keys(o.rules ?? {})?.includes('@nrwl/nx/nx-plugin-checks')
);
if (
Array.isArray(override?.files) &&
!override.files.includes(relativeMigrationsJsonPath)
) {
override.files.push(relativeMigrationsJsonPath);
}
return c;
}
);
}
}
function updateProjectTarget(
host: Tree,
options: PluginLintChecksGeneratorSchema,
packageJson: PackageJson
) {
const project = readProjectConfiguration(host, options.projectName);
if (!project.targets) {
return;
}
for (const [target, configuration] of Object.entries(project.targets)) {
if (configuration.executor === '@nrwl/linter:eslint') {
const opts: EsLintExecutorOptions = configuration.options ?? {};
opts.lintFilePatterns ??= [];
if (packageJson.generators) {
opts.lintFilePatterns.push(
joinPathFragments(project.root, packageJson.generators)
);
}
if (
packageJson.schematics &&
packageJson.schematics !== packageJson.generators
) {
opts.lintFilePatterns.push(
joinPathFragments(project.root, packageJson.schematics)
);
}
if (packageJson.executors) {
opts.lintFilePatterns.push(
joinPathFragments(project.root, packageJson.executors)
);
}
if (
packageJson.builders &&
packageJson.builders !== packageJson.executors
) {
opts.lintFilePatterns.push(
joinPathFragments(project.root, packageJson.builders)
);
}
opts.lintFilePatterns.push(`${project.root}/package.json`);
opts.lintFilePatterns = [...new Set(opts.lintFilePatterns)];
project.targets[target].options = opts;
}
}
updateProjectConfiguration(host, options.projectName, project);
}
function updateProjectEslintConfig(
host: Tree,
options: ProjectConfiguration,
packageJson: PackageJson
) {
// Update the project level lint configuration to specify
// the plugin schema rule for generated files
const eslintPath = `${options.root}/.eslintrc.json`;
const eslintConfig = readJson<ESLint.Config>(host, eslintPath);
eslintConfig.overrides ??= [];
if (
!eslintConfig.overrides.some((x) =>
Object.keys(x.rules ?? {}).includes('@nrwl/nx/nx-plugin-checks')
)
) {
eslintConfig.overrides.push({
files: [
'./package.json',
packageJson.generators,
packageJson.executors,
packageJson.schematics,
packageJson.builders,
].filter((f) => !!f),
parser: 'jsonc-eslint-parser',
rules: {
'@nrwl/nx/nx-plugin-checks': 'error',
},
});
}
writeJson(host, eslintPath, eslintConfig);
}
// Update the root eslint to specify a parser for json files
// This is required, otherwise every json file that is not overriden
// will display false errors in the IDE
function updateRootEslintConfig(host: Tree) {
const rootESLint = readJson<ESLint.Config>(host, '.eslintrc.json');
rootESLint.overrides ??= [];
if (!eslintConfigContainsJsonOverride(rootESLint)) {
rootESLint.overrides.push({
files: '*.json',
parser: 'jsonc-eslint-parser',
rules: {},
});
writeJson(host, '.eslintrc.json', rootESLint);
}
}
function setupVsCodeLintingForJsonFiles(host: Tree) {
let existing: Record<string, unknown> = {};
if (host.exists('.vscode/settings.json')) {
existing = readJson<Record<string, unknown>>(host, '.vscode/settings.json');
} else {
logger.info(
`${NX_PREFIX} We've updated the vscode settings for this repository to ensure that plugin lint checks show up inside your IDE. This created .vscode/settings.json. To read more about this file, check vscode's documentation. It is frequently not commited, so other developers may need to add similar settings if they'd like to see the lint checks in the IDE rather than only during linting.`
);
}
// setup eslint validation for json files
const eslintValidate = (existing['eslint.validate'] as string[]) ?? [];
if (!eslintValidate.includes('json')) {
existing['eslint.validate'] = [...eslintValidate, 'json'];
}
writeJson(host, '.vscode/settings.json', existing);
}
function eslintConfigContainsJsonOverride(eslintConfig: ESLint.Config) {
return eslintConfig.overrides.some((x) => {
if (typeof x.files === 'string' && x.files.includes('.json')) {
return true;
}
return Array.isArray(x.files) && x.files.some((f) => f.includes('.json'));
});
}
function projectIsEsLintEnabled(project: ProjectConfiguration) {
return !!getEsLintOptions(project);
}
export function getEsLintOptions(
project: ProjectConfiguration
): [target: string, configuration: TargetConfiguration<EsLintExecutorOptions>] {
return Object.entries(project.targets || {}).find(
([, x]) => x.executor === '@nrwl/linter:eslint'
);
}