Skip to content

Commit

Permalink
fix: detect nx packages inside apps and packages folder by default (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasholzer authored Jul 26, 2023
1 parent 5ec40f6 commit 388c66a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 31 deletions.
34 changes: 34 additions & 0 deletions packages/build-info/src/build-systems/nx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,40 @@ test('detects nx workspace packages in a nested folder structure', async ({ fs }
})
})

test('detects nx workspace packages in the default locations', async ({ fs }) => {
const cwd = mockFileSystem({
'nx.json': '{}', // no workspaceLayout specified
'package.json': JSON.stringify({ devDependencies: { nx: '^15.0.2' } }),
'packages/app-1/project.json': JSON.stringify({
name: 'app-1',
sourceRoot: 'packages/app-1',
projectType: 'application',
tags: [],
targets: { build: { executor: '@nrwl/next:build' } },
}),
'apps/app-2/project.json': JSON.stringify({
name: 'app-2',
sourceRoot: 'apps/app-2',
projectType: 'application',
tags: [],
targets: { build: { executor: '@nrwl/next:build' } },
}),
})
fs.cwd = cwd
const project = new Project(fs, cwd, cwd)
await project.getBuildSettings()
expect(project.buildSystems).toHaveLength(1)
expect(project.buildSystems[0]?.id).toBe('nx')
expect(project.workspace).toMatchObject({
isRoot: true,
packages: [
{ path: join('apps/app-2'), name: 'app-2', forcedFramework: 'next' },
{ path: join('packages/app-1'), name: 'app-1', forcedFramework: 'next' },
],
rootDir: cwd,
})
})

describe('getDist', () => {
beforeEach((ctx) => {
ctx.cwd = mockFileSystem({
Expand Down
66 changes: 35 additions & 31 deletions packages/build-info/src/build-systems/nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ export class Nx extends BaseBuildTool {
}
const target = this.targets.get(packagePath)?.find((t) => NPM_DEV_SCRIPTS.includes(t.name))
const executor = this.getExecutorFromTarget(target)
if (!executor) {
return null
}

switch (executor) {
case '@nxtensions/astro:dev':
Expand Down Expand Up @@ -227,44 +230,45 @@ export class Nx extends BaseBuildTool {
return []
}

/** detect workspace pacakges with the project.json files */
/** detect workspace packages with the project.json files */
private async detectProjectJson(): Promise<WorkspacePackage[]> {
const fs = this.project.fs
try {
const { workspaceLayout = { appsDir: 'apps' } } = await fs.readJSON<any>(
fs.join(this.project.jsWorkspaceRoot, 'nx.json'),
{
fail: true,
},
)
// if an apps dir is specified get it.
if (workspaceLayout?.appsDir?.length) {
const identifyPkg: identifyPackageFn = async ({ entry, directory, packagePath }) => {
// ignore e2e test applications as there is no need to deploy them
if (entry === 'project.json' && !packagePath.endsWith('-e2e')) {
try {
// we need to check the project json for application types (we don't care about libraries)
const { projectType, name, targets } = await fs.readJSON(fs.join(directory, entry))
if (projectType === 'application') {
const targetsWithName = Object.entries(targets || {}).map(([name, target]) => ({ ...target, name }))
const forcedFramework = await this.detectFramework(targetsWithName)
this.targets.set(fs.join(packagePath), targetsWithName)
return { name, path: fs.join(packagePath), forcedFramework } as WorkspacePackage
}
} catch {
// noop
const { workspaceLayout } = await fs.readJSON<any>(fs.join(this.project.jsWorkspaceRoot, 'nx.json'), {
fail: true,
})
const appDirs = workspaceLayout?.appsDir ? [workspaceLayout.appsDir] : ['apps', 'packages']
const identifyPkg: identifyPackageFn = async ({ entry, directory, packagePath }) => {
// ignore e2e test applications as there is no need to deploy them
if (entry === 'project.json' && !packagePath.endsWith('-e2e')) {
try {
// we need to check the project json for application types (we don't care about libraries)
const { projectType, name, targets } = await fs.readJSON(fs.join(directory, entry))
if (projectType === 'application') {
const targetsWithName = Object.entries(targets || {}).map(([name, target]) => ({ ...target, name }))
const forcedFramework = await this.detectFramework(targetsWithName)
this.targets.set(fs.join(packagePath), targetsWithName)
return { name, path: fs.join(packagePath), forcedFramework } as WorkspacePackage
}
} catch {
// noop
}
return null
}

return findPackages(
this.project,
workspaceLayout.appsDir,
identifyPkg,
'*', // only check for one level
)
return null
}

const pkgs = await Promise.all(
appDirs.map(async (appDir) =>
findPackages(
this.project,
appDir,
identifyPkg,
'*', // only check for one level
).catch(() => []),
),
)

return pkgs.flat()
} catch {
// noop
}
Expand Down

0 comments on commit 388c66a

Please sign in to comment.