Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad56f57

Browse files
authored
fix(linter): run migration for targets that are not lint (#3850)
1 parent 655143e commit ad56f57

File tree

8 files changed

+88
-81
lines changed

8 files changed

+88
-81
lines changed

packages/linter/src/migrations/update-10-3-0/add-json-ext-to-eslintrc.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,41 @@
11
import { basename } from '@angular-devkit/core';
2-
import { chain, Tree } from '@angular-devkit/schematics';
2+
import { chain } from '@angular-devkit/schematics';
33
import {
44
formatFiles,
5-
readJsonInTree,
6-
serializeJson,
5+
updateBuilderConfig,
76
updateWorkspace,
87
visitNotIgnoredFiles,
98
} from '@nrwl/workspace';
109

1110
function updateESLintConfigReferencesInWorkspace() {
12-
return updateWorkspace((workspace) => {
13-
workspace.projects.forEach((project) => {
14-
const lintTarget = project.targets.get('lint');
11+
return updateBuilderConfig(
12+
(options, target, project) => {
1513
if (
16-
lintTarget?.builder !== '@nrwl/linter:eslint' &&
17-
(lintTarget?.builder !== '@nrwl/linter:lint' ||
18-
lintTarget?.options?.linter === 'tslint')
14+
target.builder === '@nrwl/linter:lint' &&
15+
options?.linter === 'tslint'
1916
) {
20-
return;
17+
return options;
2118
}
2219

23-
if (lintTarget.builder === '@nrwl/linter:eslint') {
24-
if (!lintTarget.options.eslintConfig) {
25-
return;
20+
if (target.builder === '@nrwl/linter:eslint') {
21+
if (!options.eslintConfig) {
22+
return options;
2623
}
27-
lintTarget.options.eslintConfig = `${lintTarget.options.eslintConfig}.json`;
28-
return;
24+
options.eslintConfig = `${options.eslintConfig}.json`;
25+
return options;
2926
}
3027

31-
if (lintTarget.builder === '@nrwl/linter:lint') {
32-
if (!lintTarget.options.config) {
33-
return;
28+
if (target.builder === '@nrwl/linter:lint') {
29+
if (!options.config) {
30+
return options;
3431
}
35-
lintTarget.options.config = `${lintTarget.options.config}.json`;
36-
return;
32+
options.config = `${options.config}.json`;
33+
return options;
3734
}
38-
});
39-
});
35+
},
36+
'@nrwl/linter:eslint',
37+
'@nrwl/linter:lint'
38+
);
4039
}
4140

4241
function renameESLintConfigFiles() {

packages/linter/src/migrations/update-10-3-0/update-eslint-builder-and-config.ts

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { chain, Tree } from '@angular-devkit/schematics';
2-
import { formatFiles, readJsonInTree, updateWorkspace } from '@nrwl/workspace';
2+
import {
3+
formatFiles,
4+
readJsonInTree,
5+
updateBuilderConfig,
6+
} from '@nrwl/workspace';
37
import { join, normalize } from '@angular-devkit/core';
48

59
/**
@@ -9,55 +13,43 @@ import { join, normalize } from '@angular-devkit/core';
913
* the migration.
1014
*/
1115
function updateESLintBuilder(host: Tree) {
12-
return updateWorkspace((workspace) => {
13-
workspace.projects.forEach((project) => {
14-
const lintTarget = project.targets.get('lint');
15-
if (
16-
lintTarget?.builder !== '@nrwl/linter:lint' ||
17-
lintTarget?.options?.linter === 'tslint'
18-
) {
19-
return;
20-
}
21-
lintTarget.builder = '@nrwl/linter:eslint';
16+
return updateBuilderConfig((options, target, project) => {
17+
if (options.linter === 'tslint') {
18+
return options;
19+
}
20+
target.builder = '@nrwl/linter:eslint';
2221

23-
const tsconfigs = [];
22+
const tsconfigs = [];
2423

25-
try {
26-
tsconfigs.push(readJsonInTree(host, `${project.root}/tsconfig.json`));
27-
} catch {}
28-
try {
29-
tsconfigs.push(
30-
readJsonInTree(host, `${project.root}/tsconfig.app.json`)
31-
);
32-
} catch {}
33-
try {
34-
tsconfigs.push(
35-
readJsonInTree(host, `${project.root}/tsconfig.lib.json`)
36-
);
37-
} catch {}
38-
try {
39-
tsconfigs.push(
40-
readJsonInTree(host, `${project.root}/tsconfig.spec.json`)
41-
);
42-
} catch {}
24+
try {
25+
tsconfigs.push(readJsonInTree(host, `${project.root}/tsconfig.json`));
26+
} catch {}
27+
try {
28+
tsconfigs.push(readJsonInTree(host, `${project.root}/tsconfig.app.json`));
29+
} catch {}
30+
try {
31+
tsconfigs.push(readJsonInTree(host, `${project.root}/tsconfig.lib.json`));
32+
} catch {}
33+
try {
34+
tsconfigs.push(
35+
readJsonInTree(host, `${project.root}/tsconfig.spec.json`)
36+
);
37+
} catch {}
4338

44-
const defaultLintFilePatterns = [`${project.root}/**/*.ts`];
39+
const defaultLintFilePatterns = [`${project.root}/**/*.ts`];
4540

46-
// Merge any available `includes` and `files` from the tsconfig files
47-
const lintFilePatterns = !tsconfigs.length
48-
? defaultLintFilePatterns
49-
: tsconfigs
50-
.map((tsconfig) => {
51-
return [...(tsconfig.include || []), ...(tsconfig.files || [])];
52-
})
53-
.reduce((flat, val) => flat.concat(val), [])
54-
.map((pattern) => join(normalize(project.root), pattern));
41+
// Merge any available `includes` and `files` from the tsconfig files
42+
const lintFilePatterns = !tsconfigs.length
43+
? defaultLintFilePatterns
44+
: tsconfigs
45+
.map((tsconfig) => {
46+
return [...(tsconfig.include || []), ...(tsconfig.files || [])];
47+
})
48+
.reduce((flat, val) => flat.concat(val), [])
49+
.map((pattern) => join(normalize(project.root), pattern));
5550

56-
lintTarget.options = {
57-
lintFilePatterns,
58-
};
59-
});
60-
});
51+
return { lintFilePatterns };
52+
}, '@nrwl/linter:lint');
6153
}
6254

6355
export default function () {

packages/node/src/schematics/application/application.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,13 @@ describe('app', () => {
284284
displayName: 'my-node-app',
285285
preset: '../../jest.preset.js',
286286
transform: {
287-
'^.+\\\\\\\\.[tj]s$': [ 'babel-jest',
288-
{ cwd: __dirname, configFile: './babel-jest.config.json' }]
287+
'^.+\\\\\\\\.[tj]s$': [
288+
'babel-jest',
289+
{ cwd: __dirname, configFile: './babel-jest.config.json' },
290+
],
289291
},
290-
moduleFileExtensions: ['ts', 'js', 'html'],
291-
coverageDirectory: '../../coverage/apps/my-node-app'
292+
moduleFileExtensions: ['ts', 'js', 'html'],
293+
coverageDirectory: '../../coverage/apps/my-node-app',
292294
};
293295
"
294296
`);

packages/node/src/schematics/application/application.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
updateWorkspaceInTree,
1919
generateProjectLint,
2020
addLintFiles,
21+
formatFiles,
2122
} from '@nrwl/workspace';
2223
import { toFileName } from '@nrwl/workspace';
2324
import { getProjectConfig } from '@nrwl/workspace';
@@ -167,6 +168,7 @@ export default function (schema: Schema): Rule {
167168
})
168169
: noop(),
169170
options.frontendProject ? addProxy(options) : noop(),
171+
formatFiles(options),
170172
])(host, context);
171173
};
172174
}

packages/workspace/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ export {
6262
serializeTarget,
6363
} from './src/utils/cli-config-utils';
6464

65-
export { getWorkspace, updateWorkspace } from './src/utils/workspace';
65+
export {
66+
getWorkspace,
67+
updateWorkspace,
68+
updateBuilderConfig,
69+
} from './src/utils/workspace';
6670
export { addUpdateTask } from './src/utils/update-task';
6771
export { addLintFiles, generateProjectLint, Linter } from './src/utils/lint';
6872

packages/workspace/src/migrations/update-9-4-0/update-linters-exclude.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { chain, SchematicContext, Tree } from '@angular-devkit/schematics';
22
import { formatFiles } from '../../utils/rules/format-files';
3-
import { updateBuilderOptions, updateWorkspace } from '../../utils/workspace';
3+
import { updateBuilderConfig, updateWorkspace } from '../../utils/workspace';
44
import { JsonArray } from '@angular-devkit/core';
55

66
function updateExcludePattern(host: Tree, context: SchematicContext) {
77
const builders = [
88
'@nrwl/linter:lint',
99
'@angular-devkit/build-angular:tslint',
1010
];
11-
return updateBuilderOptions((options, project) => {
11+
return updateBuilderConfig((options, target, project) => {
1212
if (!options?.exclude) {
1313
return options;
1414
}

packages/workspace/src/utils/workspace.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Tree } from '@angular-devkit/schematics';
22
import { callRule } from '@nrwl/workspace/src/utils/testing';
33
import { readWorkspace, updateWorkspace } from '@nrwl/workspace';
4-
import { updateBuilderOptions } from '@nrwl/workspace/src/utils/workspace';
4+
import { updateBuilderConfig } from '@nrwl/workspace/src/utils/workspace';
55
import { createEmptyWorkspace } from '@nrwl/workspace/testing';
66

77
describe('workspace utils', () => {
@@ -53,7 +53,7 @@ describe('workspace utils', () => {
5353

5454
it('should update options', async () => {
5555
const result = await callRule(
56-
updateBuilderOptions((options) => {
56+
updateBuilderConfig((options) => {
5757
options.i = 99;
5858
return options;
5959
}, 'builder1'),
@@ -70,7 +70,7 @@ describe('workspace utils', () => {
7070

7171
it('should not update options of other builders', async () => {
7272
const result = await callRule(
73-
updateBuilderOptions((options) => {
73+
updateBuilderConfig((options) => {
7474
options.i = 99;
7575
return options;
7676
}, 'builder1'),

packages/workspace/src/utils/workspace.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Tree, Rule } from '@angular-devkit/schematics';
22
import { JsonArray, JsonObject, workspaces } from '@angular-devkit/core';
3-
import { ProjectDefinition } from '@angular-devkit/core/src/workspace';
3+
import {
4+
ProjectDefinition,
5+
TargetDefinition,
6+
} from '@angular-devkit/core/src/workspace';
47

58
function createHost(tree: Tree): workspaces.WorkspaceHost {
69
return {
@@ -65,12 +68,13 @@ export function updateWorkspace(
6568
/**
6669
* Updates builder options for options and configurations for given builder names
6770
*/
68-
export function updateBuilderOptions(
71+
export function updateBuilderConfig(
6972
updater: (
7073
currentValue: Record<
7174
string,
7275
string | number | boolean | JsonArray | JsonObject
7376
>,
77+
target?: TargetDefinition,
7478
project?: ProjectDefinition
7579
) => Record<string, string | number | boolean | JsonArray | JsonObject>,
7680
...builderNames: string[]
@@ -85,14 +89,18 @@ export function updateBuilderOptions(
8589
return;
8690
}
8791
if (target.options) {
88-
target.options = updater(target.options, project);
92+
target.options = updater(target.options, target, project);
8993
}
9094
if (!target.configurations) {
9195
return;
9296
}
9397
Object.entries(target.configurations).forEach(
9498
([configName, options]) => {
95-
target.configurations[configName] = updater(options, project);
99+
target.configurations[configName] = updater(
100+
options,
101+
target,
102+
project
103+
);
96104
}
97105
);
98106
});

0 commit comments

Comments
 (0)