Skip to content

Commit 58f9fcd

Browse files
meeroslavFrozenPandaz
authored andcommitted
cleanup(core): use isNpmProject and cleanup project graph utils (#6522)
* cleanup(core): use isNpmProject and cleanup project graph utils * chore(core): mark utils/buildable-libs-utils as deprecated
1 parent 863cb47 commit 58f9fcd

File tree

11 files changed

+92
-50
lines changed

11 files changed

+92
-50
lines changed

packages/angular/src/migrations/update-10-5-0/add-template-support-and-presets-to-eslint.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
} from '@nrwl/workspace';
1212
import { join } from 'path';
1313
import { offsetFromRoot } from '@nrwl/devkit';
14-
import { createProjectGraphAsync } from '@nrwl/workspace/src/core/project-graph';
14+
import {
15+
createProjectGraphAsync,
16+
isNpmProject,
17+
} from '@nrwl/workspace/src/core/project-graph';
1518

1619
/**
1720
* It was decided with Jason that we would do a simple replacement in this migration
@@ -67,7 +70,7 @@ async function updateProjectESLintConfigsAndBuilders(
6770
!graph.dependencies[projectName].some(
6871
(dependency) =>
6972
dependency.target.startsWith('npm:@angular/') &&
70-
graph.nodes[dependency.target].type === 'npm'
73+
isNpmProject(graph.nodes[dependency.target])
7174
)
7275
) {
7376
return;

packages/react/src/migrations/update-9-4-0/babelrc-9-4-0.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
} from '@angular-devkit/core/src/utils/literals';
1111
import { initRootBabelConfig } from '../utils/rules';
1212
import { addDepsToPackageJson, formatFiles } from '@nrwl/workspace';
13-
import { createProjectGraphAsync } from '@nrwl/workspace/src/core/project-graph';
13+
import {
14+
createProjectGraphAsync,
15+
isNpmProject,
16+
} from '@nrwl/workspace/src/core/project-graph';
1417

1518
let addedEmotionPreset = false;
1619

@@ -30,15 +33,15 @@ export default function update(): Rule {
3033
context.logger.info(
3134
`
3235
Found an existing babel.config.json file so we skipped creating it.
33-
36+
3437
You may want to update it to include the Nx preset "@nrwl/web/babel".
3538
`
3639
);
3740
} else if (host.exists('/babel.config.js')) {
3841
context.logger.info(
3942
`
4043
Found an existing babel.config.js file so we skipped creating it.
41-
44+
4245
You may want to update it to include the Nx preset "@nrwl/web/babel".
4346
`
4447
);
@@ -51,7 +54,7 @@ export default function update(): Rule {
5154
const deps = projectGraph.dependencies[name];
5255
const isReact = deps.some(
5356
(d) =>
54-
projectGraph.nodes[d.target].type === 'npm' &&
57+
isNpmProject(projectGraph.nodes[d.target]) &&
5558
d.target.indexOf('react') !== -1
5659
);
5760
if (isReact) {
@@ -69,11 +72,11 @@ export default function update(): Rule {
6972
if (conflicts.length > 0) {
7073
context.logger.info(stripIndent`
7174
The following projects already have .babelrc so we did not create them:
72-
75+
7376
${conflicts
7477
.map(([name, babelrc]) => `${name} - ${babelrc}`)
7578
.join('\n ')}
76-
79+
7780
You may want to update them to include the Nx preset "@nrwl/react/babel".
7881
`);
7982
}
@@ -109,7 +112,7 @@ function createBabelrc(host, context, babelrcPath, deps) {
109112
context.logger.warn(
110113
stripIndents`We created a babel config at ${babelrcPath} with both styled-components and emotion plugins.
111114
Only one should be used, please remove the unused plugin.
112-
115+
113116
For example, if you don't use styled-components, then remove that plugin from the .babelrc file.
114117
`
115118
);

packages/workspace/src/core/affected-project-graph/locators/npm-packages.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
JsonChange,
66
} from '../../../utilities/json-diff';
77
import { TouchedProjectLocator } from '../affected-project-graph-models';
8+
import { isNpmProject } from '../../project-graph/operators';
89

910
export const getTouchedNpmPackages: TouchedProjectLocator<
1011
WholeFileChange | JsonChange
@@ -21,9 +22,7 @@ export const getTouchedNpmPackages: TouchedProjectLocator<
2122
let touched = [];
2223
const changes = packageJsonChange.getChanges();
2324

24-
const npmPackages = Object.values(projectGraph.nodes).filter(
25-
(node) => node.type === 'npm'
26-
);
25+
const npmPackages = Object.values(projectGraph.nodes).filter(isNpmProject);
2726

2827
for (const c of changes) {
2928
if (

packages/workspace/src/core/file-graph/project-file-map.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ export function createProjectFileMap(
1010
// a chance to match prefix first.
1111
Object.keys(workspaceJson.projects)
1212
.sort((a, b) => {
13-
if (!workspaceJson.projects[a].root) return -1;
14-
if (!workspaceJson.projects[b].root) return -1;
15-
return workspaceJson.projects[a].root.length >
16-
workspaceJson.projects[b].root.length
17-
? -1
18-
: 1;
13+
const projectA = workspaceJson.projects[a];
14+
const projectB = workspaceJson.projects[b];
15+
if (!projectA.root) return -1;
16+
if (!projectB.root) return -1;
17+
return projectA.root.length > projectB.root.length ? -1 : 1;
1918
})
2019
.forEach((projectName) => {
2120
const p = workspaceJson.projects[projectName];
22-
fileMap[projectName] = fileMap[projectName] || [];
21+
fileMap[projectName] = [];
2322
files.forEach((f) => {
2423
if (seen.has(f.file)) {
2524
return;
Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { NxJsonConfiguration } from '@nrwl/devkit';
22

3+
/**
4+
* Normalize nx json by replacing wildcard `*` implicit dependencies
5+
* to an array of all project names
6+
* @param {NxJsonConfiguration} nxJson
7+
* @returns {NxJsonConfiguration<string[]>}
8+
*/
39
export function normalizeNxJson(
410
nxJson: NxJsonConfiguration
511
): NxJsonConfiguration<string[]> {
@@ -9,22 +15,31 @@ export function normalizeNxJson(
915
implicitDependencies: Object.entries(
1016
nxJson.implicitDependencies
1117
).reduce((acc, [key, val]) => {
12-
acc[key] = recur(val);
18+
acc[key] = recur(nxJson, val);
1319
return acc;
14-
15-
function recur(v: '*' | string[] | {}): string[] | {} {
16-
if (v === '*') {
17-
return Object.keys(nxJson.projects);
18-
} else if (Array.isArray(v)) {
19-
return v;
20-
} else {
21-
return Object.keys(v).reduce((xs, x) => {
22-
xs[x] = recur(v[x]);
23-
return xs;
24-
}, {});
25-
}
26-
}
2720
}, {}),
2821
}
2922
: (nxJson as NxJsonConfiguration<string[]>);
3023
}
24+
25+
/**
26+
* Map recursively wildcard `*` to project names
27+
* @param {NxJsonConfiguration} nxJson
28+
* @param {'*' | string[] | {}} v
29+
* @returns {string[] | {}}
30+
*/
31+
function recur(
32+
nxJson: NxJsonConfiguration,
33+
v: '*' | string[] | {}
34+
): string[] | {} {
35+
if (v === '*') {
36+
return Object.keys(nxJson.projects);
37+
} else if (Array.isArray(v)) {
38+
return v;
39+
} else {
40+
return Object.keys(v).reduce((acc, key) => {
41+
acc[key] = recur(nxJson, v[key]);
42+
return acc;
43+
}, {});
44+
}
45+
}

packages/workspace/src/core/project-graph/build-dependencies/explicit-project-dependencies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function buildExplicitTypeScriptDependencies(
2222
f.file,
2323
ctx.workspace.npmScope
2424
);
25-
if (source && target) {
25+
if (target) {
2626
builder.addExplicitDependency(source, f.file, target);
2727
}
2828
}

packages/workspace/src/utilities/buildable-libs-utils.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import { ProjectType } from '../core/project-graph';
1+
import { isNpmProject, ProjectType } from '../core/project-graph';
22
import { join, resolve, dirname, relative } from 'path';
3-
import {
4-
directoryExists,
5-
fileExists,
6-
readJsonFile,
7-
writeJsonFile,
8-
} from './fileutils';
3+
import { directoryExists, readJsonFile, writeJsonFile } from './fileutils';
94
import { stripIndents } from '@nrwl/devkit';
105
import type { ProjectGraph, ProjectGraphNode } from '@nrwl/devkit';
116
import { getOutputsForTargetAndConfiguration } from '../tasks-runner/utils';
@@ -66,7 +61,7 @@ export function calculateProjectDependencies(
6661
),
6762
node: depNode,
6863
};
69-
} else if (depNode.type === 'npm') {
64+
} else if (isNpmProject(depNode)) {
7065
return {
7166
name: depNode.data.packageName,
7267
outputs: [],
@@ -286,8 +281,9 @@ export function updateBuildableProjectPackageJsonDependencies(
286281

287282
let updatePackageJson = false;
288283
dependencies.forEach((entry) => {
289-
const packageName =
290-
entry.node.type === 'npm' ? entry.node.data.packageName : entry.name;
284+
const packageName = isNpmProject(entry.node)
285+
? entry.node.data.packageName
286+
: entry.name;
291287

292288
if (
293289
!hasDependency(packageJson, 'dependencies', packageName) &&
@@ -313,7 +309,7 @@ export function updateBuildableProjectPackageJsonDependencies(
313309
depVersion = readJsonFile(depPackageJsonPath).version;
314310

315311
packageJson[typeOfDependency][packageName] = depVersion;
316-
} else if (entry.node.type === 'npm') {
312+
} else if (isNpmProject(entry.node)) {
317313
// If an npm dep is part of the workspace devDependencies, do not include it the library
318314
if (
319315
!!workspacePackageJson.devDependencies?.[

packages/workspace/src/utilities/create-package-json.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ProjectGraph } from '@nrwl/devkit';
2+
import { isNpmProject } from '../core/project-graph';
23
import { readJsonFile } from './fileutils';
34

45
/**
@@ -58,7 +59,7 @@ function findAllNpmDeps(
5859

5960
const node = graph.nodes[projectName];
6061

61-
if (node.type === 'npm') {
62+
if (isNpmProject(node)) {
6263
list[node.data.packageName] = node.data.version;
6364
recursivelyCollectPeerDependencies(node.name, graph, list);
6465
}
@@ -77,7 +78,7 @@ function recursivelyCollectPeerDependencies(
7778
) {
7879
if (
7980
!graph.nodes[projectName] ||
80-
graph.nodes[projectName].type !== 'npm' ||
81+
!isNpmProject(graph.nodes[projectName]) ||
8182
seen.has(projectName)
8283
) {
8384
return list;

packages/workspace/src/utilities/project-graph-utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ export function getProjectNameFromDirPath(
6767
}
6868

6969
/**
70-
* Takes a filename and figures out the belonging app and from there
71-
* collects all dependent
72-
* @param filename name of a file in some workspace app / lib
70+
* Find all internal project dependencies.
71+
* All the external (npm) dependencies will be filtered out
72+
* @param {string} parentNodeName
73+
* @param {ProjectGraph} projectGraph
74+
* @returns {string[]}
7375
*/
7476
function findAllProjectNodeDependencies(
7577
parentNodeName: string,

packages/workspace/src/utils/buildable-libs-utils.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
} from '../core/project-graph';
1111
import { getMockContext } from './testing';
1212

13+
// TODO(v13): remove this deprecated file
14+
1315
describe('updatePaths', () => {
1416
const deps: DependentBuildableProjectNode[] = [
1517
{ name: '@proj/lib', node: {} as any, outputs: ['dist/libs/lib'] },

0 commit comments

Comments
 (0)