-
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
- Loading branch information
Showing
8 changed files
with
68 additions
and
92 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
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) { | ||
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 | ||
); | ||
} | ||
|
||
function findMatchingProjectInCwd( | ||
projects: { [projectName: string]: ProjectConfiguration }, | ||
relativeCwd: string | ||
) { | ||
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