-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): export detectProjects utility function (#3740)
- Loading branch information
Showing
9 changed files
with
82 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ProjectFileMap, createProjectFileMapUsingProjectGraph, createProjectGraphAsync } from "@nx/devkit"; | ||
import { ProjectGraphWithPackages } from "../project-graph-with-packages"; | ||
import { createProjectGraphWithPackages } from "./create-project-graph-with-packages"; | ||
|
||
export async function detectProjects(packageConfigs: string[]): Promise<{ | ||
projectGraph: ProjectGraphWithPackages; | ||
projectFileMap: ProjectFileMap; | ||
}> { | ||
const _projectGraph = await createProjectGraphAsync(); | ||
|
||
let projectFileMap: ProjectFileMap; | ||
|
||
// if we are using Nx >= 16.3.1, we can use the helper function to create the file map | ||
// otherwise, we need to use the old "files" property on the node data | ||
if (createProjectFileMapUsingProjectGraph) { | ||
projectFileMap = await createProjectFileMapUsingProjectGraph(_projectGraph); | ||
} else { | ||
projectFileMap = Object.entries(_projectGraph.nodes).reduce( | ||
(map, [key, node]) => ({ | ||
...map, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
[key]: (node.data as any).files, | ||
}), | ||
{} | ||
); | ||
} | ||
|
||
const projectGraph = await createProjectGraphWithPackages(_projectGraph, projectFileMap, packageConfigs); | ||
|
||
return { | ||
projectGraph, | ||
projectFileMap, | ||
}; | ||
} |
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,7 @@ | ||
import { Project, detectProjects as _detectProjects } from "@lerna/core"; | ||
import { workspaceRoot } from "@nx/devkit"; | ||
|
||
export async function detectProjects() { | ||
const project = new Project(workspaceRoot); | ||
return _detectProjects(project.packageConfigs); | ||
} |
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 @@ | ||
export * from "./detect-projects"; |
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,29 @@ | ||
--- | ||
id: utilities | ||
title: Utilities | ||
type: reference | ||
--- | ||
|
||
# Utilities | ||
|
||
Lerna ships some utility functions that can be used in creating your own tools within a Lerna monorepo. | ||
|
||
```js | ||
const utils = require("lerna/utils"); | ||
``` | ||
|
||
## `detectProjects()` | ||
|
||
The `detectProjects()` function creates the same project graph file mapping that Lerna uses under the covers to execute its commands. This is useful for writing your own scripts that need to operate on the same set of packages that Lerna would. | ||
|
||
```js | ||
const { detectProjects } = require("lerna/utils"); | ||
|
||
const { projectGraph, projectFileMap } = await detectProjects(); | ||
``` | ||
|
||
The `projectGraph` that is returned will be a `ProjectGraphWithPackages`, which is an extension of the `ProjectGraph` type from `@nx/devkit`. It contains additional metadata about projects that have `package.json` files. It also has a `localPackageDependencies` property that tracks internal npm dependencies between projects (as opposed to external npm dependencies that are downloaded from the registry). | ||
|
||
The `projectFileMap` is a mapping of project names to the files within them. This is used to determine which project needs to be versioned when a file changes. | ||
|
||
See [Lerna's TypeScript source code](https://github.com/lerna/lerna/blob/main/libs/core/src/lib/project-graph-with-packages.ts) for specific type details. |
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