Skip to content

Commit 90b9ce9

Browse files
committed
refactor(semver): 🛠 get rid of deprecated workspace def
1 parent 92aaf80 commit 90b9ce9

File tree

6 files changed

+43
-26
lines changed

6 files changed

+43
-26
lines changed

packages/semver/src/executors/version/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { _logStep } from './utils/logger';
1717
import { runPostTargets } from './utils/post-target';
1818
import { formatTag, formatTagPrefix } from './utils/tag';
1919
import { tryBump } from './utils/try-bump';
20-
import { getProjectRoot } from './utils/workspace';
20+
import { getProject } from './utils/workspace';
2121
import {
2222
versionProject,
2323
versionWorkspace,
@@ -76,7 +76,7 @@ export default async function version(
7676
projectName,
7777
syncVersions,
7878
});
79-
const projectRoot = getProjectRoot(context);
79+
const projectRoot = getProject(context).root;
8080
const newVersion$ = tryBump({
8181
preset,
8282
projectRoot,
@@ -132,7 +132,7 @@ export default async function version(
132132
commitMessage,
133133
dependencyUpdates,
134134
skipCommit,
135-
workspace: context.workspace,
135+
workspace: context.projectsConfigurations,
136136
};
137137

138138
const version$ = defer(() =>

packages/semver/src/executors/version/testing.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as rimraf from 'rimraf';
44
import * as tmp from 'tmp';
55
import { promisify } from 'util';
66

7-
import type {
7+
import {
88
ExecutorContext,
99
ProjectConfiguration,
1010
TargetConfiguration,
@@ -68,10 +68,11 @@ export function createFakeContext({
6868
}[];
6969
}): ExecutorContext {
7070
return {
71+
isVerbose: false,
7172
cwd: cwd,
7273
root: workspaceRoot,
7374
projectName: project,
74-
workspace: {
75+
projectsConfigurations: {
7576
version: 2,
7677
projects: {
7778
[project]: {
@@ -81,7 +82,7 @@ export function createFakeContext({
8182
...assembleAdditionalProjects(additionalProjects),
8283
},
8384
},
84-
} as ExecutorContext;
85+
} satisfies ExecutorContext;
8586
}
8687

8788
function assembleAdditionalProjects(
@@ -91,11 +92,13 @@ function assembleAdditionalProjects(
9192
targets?: Record<string, TargetConfiguration>;
9293
}[]
9394
) {
94-
return additionalProjects.reduce((acc, p) => {
95+
return additionalProjects.reduce<{
96+
[projectName: string]: ProjectConfiguration;
97+
}>((acc, p) => {
9598
acc[p.project] = {
9699
root: p.projectRoot,
97100
targets: p.targets || {},
98101
};
99102
return acc;
100-
}, {} as { [project: string]: ProjectConfiguration });
103+
}, {} satisfies { [project: string]: ProjectConfiguration });
101104
}

packages/semver/src/executors/version/utils/get-project-dependencies.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export async function getDependencyRoots({
2020
// Include any depended-upon libraries in determining the version bump.
2121
return (await getProjectDependencies(projectName)).map((name) => ({
2222
name,
23-
path: context.workspace.projects[name].root,
23+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
24+
path: context.projectsConfigurations!.projects[name].root,
2425
}));
2526
}
2627

packages/semver/src/executors/version/utils/post-target.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ export function _getTargetOptions({
7373
(optionsAccumulator, [option, value]) => {
7474
const resolvedValue = Array.isArray(value)
7575
? value.map((_element) =>
76-
typeof _element !== "object"
77-
? coerce(
78-
createTemplateString(
79-
(_element as number | string | boolean).toString(),
80-
context
76+
typeof _element !== 'object'
77+
? coerce(
78+
createTemplateString(
79+
(_element as number | string | boolean).toString(),
80+
context
81+
)
8182
)
82-
)
83-
: _getTargetOptions({ options: _element, context })
83+
: _getTargetOptions({ options: _element, context })
8484
)
8585
: typeof value === 'object'
8686
? _getTargetOptions({
@@ -105,14 +105,14 @@ export function _getTargetOptions({
105105

106106
/* istanbul ignore next */
107107
export function _checkTargetExist(target: Target, context: ExecutorContext) {
108-
const project = context.workspace.projects[target.project];
108+
const project = context.projectsConfigurations?.projects?.[target.project];
109109

110110
if (project === undefined) {
111111
throw new Error(
112112
`The target project "${
113113
target.project
114114
}" does not exist in your workspace. Available projects: ${Object.keys(
115-
context.workspace.projects
115+
context.projectsConfigurations?.projects ?? []
116116
).map((project) => `"${project}"`)}.`
117117
);
118118
}

packages/semver/src/executors/version/utils/workspace.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
import type {
22
ExecutorContext,
3-
NxJsonConfiguration,
3+
ProjectConfiguration,
44
ProjectsConfigurations,
55
} from '@nrwl/devkit';
66
import { resolve } from 'path';
77

88
/* istanbul ignore next */
9-
export function getProjectRoot(context: ExecutorContext): string {
10-
return context.workspace.projects[context.projectName as string].root;
9+
export function getProject(context: ExecutorContext): ProjectConfiguration {
10+
const project =
11+
context.projectsConfigurations?.projects[context.projectName as string];
12+
13+
if (!project) {
14+
throw new Error(`Project root not found for ${context.projectName}`);
15+
}
16+
17+
return project;
1118
}
1219

1320
/* istanbul ignore next */
1421
export function getProjectRoots(
1522
workspaceRoot: string,
16-
workspace: ProjectsConfigurations & NxJsonConfiguration<string[] | '*'>
23+
workspace: ProjectsConfigurations | undefined
1724
): string[] {
18-
return Object.values(workspace.projects).map((project) =>
25+
const projects = Object.values(workspace?.projects ?? {});
26+
27+
if (projects.length === 0) {
28+
throw new Error('No projects found in workspace');
29+
}
30+
31+
return projects.map((project) =>
1932
typeof project === 'string'
2033
? resolve(workspaceRoot, project)
2134
: resolve(workspaceRoot, project.root)

packages/semver/src/executors/version/version.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { NxJsonConfiguration, ProjectsConfigurations } from '@nrwl/devkit';
1+
import { ProjectsConfigurations } from '@nrwl/devkit';
22
import { forkJoin, Observable, of } from 'rxjs';
33
import { concatMap, map } from 'rxjs/operators';
4+
import { Preset } from './schema';
45
import {
56
insertChangelogDependencyUpdates,
67
updateChangelog,
@@ -10,7 +11,6 @@ import { addToStage, createTag, getLastCommitHash } from './utils/git';
1011
import { logStep } from './utils/logger';
1112
import { updatePackageJson } from './utils/project';
1213
import { getProjectRoots } from './utils/workspace';
13-
import { Preset } from './schema';
1414

1515
export type Version =
1616
| {
@@ -38,7 +38,7 @@ export interface CommonVersionOptions {
3838
skipProjectChangelog: boolean;
3939
dependencyUpdates: Version[];
4040
preset: Preset;
41-
workspace: ProjectsConfigurations & NxJsonConfiguration<string[] | '*'>;
41+
workspace: ProjectsConfigurations | undefined;
4242
}
4343

4444
export function versionWorkspace({

0 commit comments

Comments
 (0)