-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(core): move calculateDefaultProjectName out of workspaces (#18225)
- Loading branch information
Showing
9 changed files
with
160 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/nx/src/config/calculate-default-project-name.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
calculateDefaultProjectName, | ||
findMatchingProjectInCwd, | ||
} from './calculate-default-project-name'; | ||
|
||
describe('calculateDefaultProjectName', () => { | ||
describe('findMatchingProjectInCwd', () => { | ||
it('should return matching project if it is found', () => { | ||
const projects = { | ||
'demo-app': { | ||
name: 'demo-app', | ||
root: 'apps/demo-app', | ||
}, | ||
}; | ||
expect(findMatchingProjectInCwd(projects, 'apps/demo-app')).toEqual( | ||
'demo-app' | ||
); | ||
expect( | ||
findMatchingProjectInCwd(projects, 'apps/demo-app/src/main.tsx') | ||
).toEqual('demo-app'); | ||
}); | ||
|
||
it('should return if it is root project and cwd is also root', () => { | ||
const projects = { | ||
'demo-app': { | ||
name: 'demo-app', | ||
root: '.', | ||
}, | ||
}; | ||
expect(findMatchingProjectInCwd(projects, '')).toEqual('demo-app'); | ||
expect(findMatchingProjectInCwd(projects, '.')).toEqual('demo-app'); | ||
}); | ||
|
||
it('should return undefined if no matching project is found', () => { | ||
expect( | ||
findMatchingProjectInCwd( | ||
{ | ||
'demo-app': { | ||
name: 'demo-app', | ||
root: 'apps/demo-app', | ||
}, | ||
}, | ||
'demo-app2' | ||
) | ||
).toEqual(undefined); | ||
}); | ||
}); | ||
|
||
it('should return default project if cwd is root', () => { | ||
expect( | ||
calculateDefaultProjectName( | ||
'.', | ||
'.', | ||
{ projects: { 'demo-app': { root: 'apps/demo-app' } }, version: 2 }, | ||
{ cli: { defaultProjectName: 'demo-app2' } } | ||
) | ||
).toEqual('demo-app2'); | ||
}); | ||
|
||
it('should return matching app if cwd is inside an app', () => { | ||
expect( | ||
calculateDefaultProjectName( | ||
'apps/demo-app', | ||
'.', | ||
{ projects: { 'demo-app': { root: 'apps/demo-app' } }, version: 2 }, | ||
{ cli: { defaultProjectName: 'demo-app2' } } | ||
) | ||
).toEqual('demo-app'); | ||
}); | ||
|
||
it('should return matching app if cwd is at workspace root', () => { | ||
expect( | ||
calculateDefaultProjectName( | ||
'demo-app', | ||
'demo-app', | ||
{ projects: { 'demo-app': { root: '.' } }, version: 2 }, | ||
{ cli: { defaultProjectName: 'demo-app2' } } | ||
) | ||
).toEqual('demo-app'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { NxJsonConfiguration } from './nx-json'; | ||
import { | ||
ProjectConfiguration, | ||
ProjectsConfigurations, | ||
} from './workspace-json-project-json'; | ||
import { | ||
findProjectForPath, | ||
normalizeProjectRoot, | ||
} from '../project-graph/utils/find-project-for-path'; | ||
import { relative } from 'path'; | ||
|
||
export function calculateDefaultProjectName( | ||
cwd: string, | ||
root: string, | ||
{ projects }: ProjectsConfigurations, | ||
nxJson: NxJsonConfiguration | ||
) { | ||
const relativeCwd = relative(root, cwd).replace(/\\/g, '/') ?? null; | ||
if (relativeCwd !== null) { | ||
const matchingProject = findMatchingProjectInCwd(projects, relativeCwd); | ||
// We have found a project | ||
if (matchingProject) { | ||
// That is not at the root | ||
if ( | ||
projects[matchingProject].root !== '.' && | ||
projects[matchingProject].root !== '' | ||
) { | ||
return matchingProject; | ||
// But its at the root, and NX_DEFAULT_PROJECT is set | ||
} else if (process.env.NX_DEFAULT_PROJECT) { | ||
return process.env.NX_DEFAULT_PROJECT; | ||
// Its root, and NX_DEFAULT_PROJECT is not set | ||
} else { | ||
return matchingProject; | ||
} | ||
} | ||
} | ||
// There was no matching project in cwd. | ||
return ( | ||
process.env.NX_DEFAULT_PROJECT ?? | ||
(nxJson.cli as { defaultProjectName: string })?.defaultProjectName ?? | ||
nxJson?.defaultProject | ||
); | ||
} | ||
|
||
export function findMatchingProjectInCwd( | ||
projects: { [projectName: string]: ProjectConfiguration }, | ||
relativeCwd: string | ||
): string | undefined { | ||
const projectRootMappings = new Map<string, string>(); | ||
for (const projectName of Object.keys(projects)) { | ||
const { root } = projects[projectName]; | ||
projectRootMappings.set(normalizeProjectRoot(root), projectName); | ||
} | ||
const matchingProject = findProjectForPath(relativeCwd, projectRootMappings); | ||
return matchingProject; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2f59796
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nx-dev – ./
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev