Skip to content

Commit 1a8b3f4

Browse files
authored
feat(monorepo): option to build with nx (#834)
feat(monorepo): build with nx
1 parent 9d47974 commit 1a8b3f4

9 files changed

+197
-44
lines changed

API.md

+50-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projenrc/yarn-monorepo-options.ts

+20
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ export function generateYarnMonorepoOptions(project: typescript.TypeScriptProjec
4444
default: 'false',
4545
},
4646
},
47+
{
48+
name: 'buildWithNx',
49+
optional: true,
50+
type: { primitive: PrimitiveType.Boolean },
51+
docs: {
52+
summary: 'When Nx is enabled, always build the monorepo using Nx',
53+
remarks: 'Will build projects in parallel and can improve build performance',
54+
default: 'false',
55+
},
56+
},
4757
{
4858
name: 'release',
4959
optional: true,
@@ -131,6 +141,16 @@ export function generateYarnMonorepoOptions(project: typescript.TypeScriptProjec
131141
default: 'true',
132142
},
133143
},
144+
{
145+
name: 'buildWithNx',
146+
optional: true,
147+
type: { primitive: PrimitiveType.Boolean },
148+
docs: {
149+
summary: 'Build the monorepo using Nx during the release.',
150+
remarks: 'Will build projects in parallel and can improve build performance',
151+
default: 'false',
152+
},
153+
},
134154
],
135155
});
136156

src/yarn/monorepo-options.ts

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/yarn/monorepo-release-options.ts

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/yarn/monorepo-release.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class MonorepoRelease extends Component {
2323
private readonly branchName: string;
2424
private readonly github: github.GitHub;
2525
private readonly releaseTrigger: projenRelease.ReleaseTrigger;
26+
private readonly buildWithNx: boolean;
2627
private readonly packagesToRelease = new Array<{
2728
readonly workspaceDirectory: string;
2829
readonly release: {
@@ -46,6 +47,7 @@ export class MonorepoRelease extends Component {
4647
}
4748
this.github = gh;
4849
this.releaseTrigger = options.releaseTrigger ?? projenRelease.ReleaseTrigger.continuous();
50+
this.buildWithNx = options.buildWithNx ?? false;
4951
}
5052

5153
public workspaceRelease(project: TypeScriptWorkspace) {
@@ -200,7 +202,12 @@ export class MonorepoRelease extends Component {
200202
// time so that the dependency versions in all 'package.json's are correct.
201203
this.releaseTask.exec('yarn workspaces run shx rm -rf dist');
202204
this.releaseTask.exec('yarn workspaces run bump');
203-
this.releaseTask.exec('yarn workspaces run build');
205+
if (this.buildWithNx) {
206+
this.releaseTask.exec('nx run-many -t build');
207+
this.releaseTask.env('NX_SKIP_NX_CACHE', 'true');
208+
} else {
209+
this.releaseTask.exec('yarn workspaces run build');
210+
}
204211
this.releaseTask.exec('yarn workspaces run unbump');
205212
// anti-tamper check (fails if there were changes to committed files)
206213
// this will identify any non-committed files generated during build (e.g. test snapshots)

src/yarn/monorepo.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as pathPosix from 'node:path/posix';
2-
import { JsonFile, Project, javascript, typescript, github, DependencyType } from 'projen';
2+
import { JsonFile, Project, javascript, typescript, github, DependencyType, JsonPatch } from 'projen';
33
import { MonorepoOptions } from './monorepo-options';
44
import { MonorepoRelease } from './monorepo-release';
55
import { Nx } from './nx';
@@ -15,6 +15,9 @@ export class Monorepo extends typescript.TypeScriptProject {
1515
return Boolean(x && typeof x === 'object' && MONOREPO_SYM in x);
1616
}
1717

18+
/**
19+
* The Monorepo Release component
20+
*/
1821
public readonly monorepoRelease?: MonorepoRelease;
1922

2023
/**
@@ -38,6 +41,7 @@ export class Monorepo extends typescript.TypeScriptProject {
3841
Object.defineProperty(this, MONOREPO_SYM, { value: true });
3942

4043
this.repositoryUrl = options.repository;
44+
const buildWithNx = Boolean(options.nx && options.buildWithNx);
4145

4246
/**
4347
* Prettier formatting
@@ -121,7 +125,14 @@ export class Monorepo extends typescript.TypeScriptProject {
121125
if (fmtTask) {
122126
buildTask.spawn(fmtTask);
123127
}
124-
buildTask.exec('yarn workspaces run build');
128+
if (buildWithNx) {
129+
buildTask.exec('nx run-many -t build');
130+
this.github?.tryFindWorkflow('build')?.file?.patch(
131+
JsonPatch.add('/jobs/build/env/NX_SKIP_NX_CACHE', 'true'),
132+
);
133+
} else {
134+
buildTask.exec('yarn workspaces run build');
135+
}
125136

126137
// Run in all workspaces tasks
127138
this.tasks.tryFind('compile')?.reset('yarn workspaces run compile');
@@ -166,6 +177,7 @@ export class Monorepo extends typescript.TypeScriptProject {
166177
if (options.release) {
167178
this.monorepoRelease = new MonorepoRelease(this, {
168179
workflowRunsOn: options.workflowRunsOn,
180+
buildWithNx,
169181
...options.releaseOptions,
170182
});
171183
}

src/yarn/nx.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,12 @@ export class Nx extends Component {
5656
project.addPackageIgnore('/.nx');
5757
new JsonFile(project, 'nx.json', {
5858
obj: {
59-
tasksRunnerOptions: {
60-
default: {
61-
runner: 'nx/tasks-runners/default',
62-
options: {
63-
cacheableOperations: () =>
64-
cachableTasks.map(({ name }) => name).filter((name) => project.tasks.tryFind(name)),
65-
},
66-
},
67-
},
6859
targetDefaults: () =>
6960
cachableTasks.reduce((targetDefaults, { name, outputs, inputs }) => {
7061
const task = project.tasks.tryFind(name);
7162
if (task) {
7263
targetDefaults[name] = {
64+
cache: true,
7365
dependsOn: [`^${name}`],
7466
outputs,
7567
inputs,

test/__snapshots__/cdklabs-monorepo.test.ts.snap

+46-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)