Skip to content

Commit 388c66a

Browse files
author
Lukas Holzer
authored
fix: detect nx packages inside apps and packages folder by default (#5207)
1 parent 5ec40f6 commit 388c66a

File tree

2 files changed

+69
-31
lines changed

2 files changed

+69
-31
lines changed

packages/build-info/src/build-systems/nx.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,40 @@ test('detects nx workspace packages in a nested folder structure', async ({ fs }
9696
})
9797
})
9898

99+
test('detects nx workspace packages in the default locations', async ({ fs }) => {
100+
const cwd = mockFileSystem({
101+
'nx.json': '{}', // no workspaceLayout specified
102+
'package.json': JSON.stringify({ devDependencies: { nx: '^15.0.2' } }),
103+
'packages/app-1/project.json': JSON.stringify({
104+
name: 'app-1',
105+
sourceRoot: 'packages/app-1',
106+
projectType: 'application',
107+
tags: [],
108+
targets: { build: { executor: '@nrwl/next:build' } },
109+
}),
110+
'apps/app-2/project.json': JSON.stringify({
111+
name: 'app-2',
112+
sourceRoot: 'apps/app-2',
113+
projectType: 'application',
114+
tags: [],
115+
targets: { build: { executor: '@nrwl/next:build' } },
116+
}),
117+
})
118+
fs.cwd = cwd
119+
const project = new Project(fs, cwd, cwd)
120+
await project.getBuildSettings()
121+
expect(project.buildSystems).toHaveLength(1)
122+
expect(project.buildSystems[0]?.id).toBe('nx')
123+
expect(project.workspace).toMatchObject({
124+
isRoot: true,
125+
packages: [
126+
{ path: join('apps/app-2'), name: 'app-2', forcedFramework: 'next' },
127+
{ path: join('packages/app-1'), name: 'app-1', forcedFramework: 'next' },
128+
],
129+
rootDir: cwd,
130+
})
131+
})
132+
99133
describe('getDist', () => {
100134
beforeEach((ctx) => {
101135
ctx.cwd = mockFileSystem({

packages/build-info/src/build-systems/nx.ts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ export class Nx extends BaseBuildTool {
113113
}
114114
const target = this.targets.get(packagePath)?.find((t) => NPM_DEV_SCRIPTS.includes(t.name))
115115
const executor = this.getExecutorFromTarget(target)
116+
if (!executor) {
117+
return null
118+
}
116119

117120
switch (executor) {
118121
case '@nxtensions/astro:dev':
@@ -227,44 +230,45 @@ export class Nx extends BaseBuildTool {
227230
return []
228231
}
229232

230-
/** detect workspace pacakges with the project.json files */
233+
/** detect workspace packages with the project.json files */
231234
private async detectProjectJson(): Promise<WorkspacePackage[]> {
232235
const fs = this.project.fs
233236
try {
234-
const { workspaceLayout = { appsDir: 'apps' } } = await fs.readJSON<any>(
235-
fs.join(this.project.jsWorkspaceRoot, 'nx.json'),
236-
{
237-
fail: true,
238-
},
239-
)
240-
// if an apps dir is specified get it.
241-
if (workspaceLayout?.appsDir?.length) {
242-
const identifyPkg: identifyPackageFn = async ({ entry, directory, packagePath }) => {
243-
// ignore e2e test applications as there is no need to deploy them
244-
if (entry === 'project.json' && !packagePath.endsWith('-e2e')) {
245-
try {
246-
// we need to check the project json for application types (we don't care about libraries)
247-
const { projectType, name, targets } = await fs.readJSON(fs.join(directory, entry))
248-
if (projectType === 'application') {
249-
const targetsWithName = Object.entries(targets || {}).map(([name, target]) => ({ ...target, name }))
250-
const forcedFramework = await this.detectFramework(targetsWithName)
251-
this.targets.set(fs.join(packagePath), targetsWithName)
252-
return { name, path: fs.join(packagePath), forcedFramework } as WorkspacePackage
253-
}
254-
} catch {
255-
// noop
237+
const { workspaceLayout } = await fs.readJSON<any>(fs.join(this.project.jsWorkspaceRoot, 'nx.json'), {
238+
fail: true,
239+
})
240+
const appDirs = workspaceLayout?.appsDir ? [workspaceLayout.appsDir] : ['apps', 'packages']
241+
const identifyPkg: identifyPackageFn = async ({ entry, directory, packagePath }) => {
242+
// ignore e2e test applications as there is no need to deploy them
243+
if (entry === 'project.json' && !packagePath.endsWith('-e2e')) {
244+
try {
245+
// we need to check the project json for application types (we don't care about libraries)
246+
const { projectType, name, targets } = await fs.readJSON(fs.join(directory, entry))
247+
if (projectType === 'application') {
248+
const targetsWithName = Object.entries(targets || {}).map(([name, target]) => ({ ...target, name }))
249+
const forcedFramework = await this.detectFramework(targetsWithName)
250+
this.targets.set(fs.join(packagePath), targetsWithName)
251+
return { name, path: fs.join(packagePath), forcedFramework } as WorkspacePackage
256252
}
253+
} catch {
254+
// noop
257255
}
258-
return null
259256
}
260-
261-
return findPackages(
262-
this.project,
263-
workspaceLayout.appsDir,
264-
identifyPkg,
265-
'*', // only check for one level
266-
)
257+
return null
267258
}
259+
260+
const pkgs = await Promise.all(
261+
appDirs.map(async (appDir) =>
262+
findPackages(
263+
this.project,
264+
appDir,
265+
identifyPkg,
266+
'*', // only check for one level
267+
).catch(() => []),
268+
),
269+
)
270+
271+
return pkgs.flat()
268272
} catch {
269273
// noop
270274
}

0 commit comments

Comments
 (0)