Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): support for async createNodes functions #20161

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/generated/devkit/CreateNodesAsync.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Type alias: CreateNodesAsync<T\>

Ƭ **CreateNodesAsync**<`T`\>: readonly [projectFilePattern: string, createNodesFunction: CreateNodesFunctionAsync<T\>]

#### Type parameters

| Name | Type |
| :--- | :-------- |
| `T` | `unknown` |
11 changes: 3 additions & 8 deletions docs/generated/devkit/CreateNodesFunction.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Type alias: CreateNodesFunction<T\>

Ƭ **CreateNodesFunction**<`T`\>: (`projectConfigurationFile`: `string`, `options`: `T` \| `undefined`, `context`: [`CreateNodesContext`](../../devkit/documents/CreateNodesContext)) => { `externalNodes?`: `Record`<`string`, [`ProjectGraphExternalNode`](../../devkit/documents/ProjectGraphExternalNode)\> ; `projects?`: `Record`<`string`, `Optional`<[`ProjectConfiguration`](../../devkit/documents/ProjectConfiguration), `"root"`\>\> }
Ƭ **CreateNodesFunction**<`T`\>: (`projectConfigurationFile`: `string`, `options`: `T` \| `undefined`, `context`: [`CreateNodesContext`](../../devkit/documents/CreateNodesContext)) => [`CreateNodesResult`](../../devkit/documents/CreateNodesResult)

#### Type parameters

Expand All @@ -10,7 +10,7 @@

#### Type declaration

▸ (`projectConfigurationFile`, `options`, `context`): `Object`
▸ (`projectConfigurationFile`, `options`, `context`): [`CreateNodesResult`](../../devkit/documents/CreateNodesResult)

A function which parses a configuration file into a set of nodes.
Used for creating nodes for the [ProjectGraph](../../devkit/documents/ProjectGraph)
Expand All @@ -25,9 +25,4 @@ Used for creating nodes for the [ProjectGraph](../../devkit/documents/ProjectGra

##### Returns

`Object`

| Name | Type | Description |
| :--------------- | :---------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------- |
| `externalNodes?` | `Record`<`string`, [`ProjectGraphExternalNode`](../../devkit/documents/ProjectGraphExternalNode)\> | A map of external node name -> external node. External nodes do not have a root, so the key is their name. |
| `projects?` | `Record`<`string`, `Optional`<[`ProjectConfiguration`](../../devkit/documents/ProjectConfiguration), `"root"`\>\> | A map of project root -> project configuration |
[`CreateNodesResult`](../../devkit/documents/CreateNodesResult)
24 changes: 24 additions & 0 deletions docs/generated/devkit/CreateNodesResult.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Interface: CreateNodesResult

## Table of contents

### Properties

- [externalNodes](../../devkit/documents/CreateNodesResult#externalnodes): Record&lt;string, ProjectGraphExternalNode&gt;
- [projects](../../devkit/documents/CreateNodesResult#projects): Record&lt;string, Optional&lt;ProjectConfiguration, &quot;root&quot;&gt;&gt;

## Properties

### externalNodes

• `Optional` **externalNodes**: `Record`<`string`, [`ProjectGraphExternalNode`](../../devkit/documents/ProjectGraphExternalNode)\>

A map of external node name -> external node. External nodes do not have a root, so the key is their name.

---

### projects

• `Optional` **projects**: `Record`<`string`, `Optional`<[`ProjectConfiguration`](../../devkit/documents/ProjectConfiguration), `"root"`\>\>

A map of project root -> project configuration
21 changes: 11 additions & 10 deletions docs/generated/devkit/NxPluginV2.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Type alias: NxPluginV2<T\>
# Type alias: NxPluginV2<TOptions, TCreateNodes\>

Ƭ **NxPluginV2**<`T`\>: `Object`
Ƭ **NxPluginV2**<`TOptions`, `TCreateNodes`\>: `Object`

A plugin for Nx which creates nodes and dependencies for the [ProjectGraph](../../devkit/documents/ProjectGraph)

#### Type parameters

| Name | Type |
| :--- | :-------- |
| `T` | `unknown` |
| Name | Type |
| :------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `TOptions` | `unknown` |
| `TCreateNodes` | extends [`CreateNodes`](../../devkit/documents/CreateNodes)<`TOptions`\> \| [`CreateNodesAsync`](../../devkit/documents/CreateNodesAsync)<`TOptions`\> = [`CreateNodes`](../../devkit/documents/CreateNodes)<`TOptions`\> \| [`CreateNodesAsync`](../../devkit/documents/CreateNodesAsync)<`TOptions`\> |

#### Type declaration

| Name | Type | Description |
| :-------------------- | :---------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------- |
| `createDependencies?` | [`CreateDependencies`](../../devkit/documents/CreateDependencies)<`T`\> | Provides a function to analyze files to create dependencies for the [ProjectGraph](../../devkit/documents/ProjectGraph) |
| `createNodes?` | [`CreateNodes`](../../devkit/documents/CreateNodes)<`T`\> | Provides a file pattern and function that retrieves configuration info from those files. e.g. { '\*_/_.csproj': buildProjectsFromCsProjFile } |
| `name` | `string` | - |
| Name | Type | Description |
| :-------------------- | :----------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------- |
| `createDependencies?` | [`CreateDependencies`](../../devkit/documents/CreateDependencies)<`TOptions`\> | Provides a function to analyze files to create dependencies for the [ProjectGraph](../../devkit/documents/ProjectGraph) |
| `createNodes?` | `TCreateNodes` | Provides a file pattern and function that retrieves configuration info from those files. e.g. { '\*_/_.csproj': buildProjectsFromCsProjFile } |
| `name` | `string` | - |
2 changes: 2 additions & 0 deletions docs/generated/devkit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ It only uses language primitives and immutable objects

- [CreateDependenciesContext](../../devkit/documents/CreateDependenciesContext)
- [CreateNodesContext](../../devkit/documents/CreateNodesContext)
- [CreateNodesResult](../../devkit/documents/CreateNodesResult)
- [DefaultTasksRunnerOptions](../../devkit/documents/DefaultTasksRunnerOptions)
- [ExecutorContext](../../devkit/documents/ExecutorContext)
- [ExecutorsJson](../../devkit/documents/ExecutorsJson)
Expand Down Expand Up @@ -64,6 +65,7 @@ It only uses language primitives and immutable objects

- [CreateDependencies](../../devkit/documents/CreateDependencies)
- [CreateNodes](../../devkit/documents/CreateNodes)
- [CreateNodesAsync](../../devkit/documents/CreateNodesAsync)
- [CreateNodesFunction](../../devkit/documents/CreateNodesFunction)
- [CustomHasher](../../devkit/documents/CustomHasher)
- [DynamicDependency](../../devkit/documents/DynamicDependency)
Expand Down
2 changes: 2 additions & 0 deletions docs/generated/packages/devkit/documents/nx_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ It only uses language primitives and immutable objects

- [CreateDependenciesContext](../../devkit/documents/CreateDependenciesContext)
- [CreateNodesContext](../../devkit/documents/CreateNodesContext)
- [CreateNodesResult](../../devkit/documents/CreateNodesResult)
- [DefaultTasksRunnerOptions](../../devkit/documents/DefaultTasksRunnerOptions)
- [ExecutorContext](../../devkit/documents/ExecutorContext)
- [ExecutorsJson](../../devkit/documents/ExecutorsJson)
Expand Down Expand Up @@ -64,6 +65,7 @@ It only uses language primitives and immutable objects

- [CreateDependencies](../../devkit/documents/CreateDependencies)
- [CreateNodes](../../devkit/documents/CreateNodes)
- [CreateNodesAsync](../../devkit/documents/CreateNodesAsync)
- [CreateNodesFunction](../../devkit/documents/CreateNodesFunction)
- [CustomHasher](../../devkit/documents/CustomHasher)
- [DynamicDependency](../../devkit/documents/DynamicDependency)
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/config/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { dirname } from 'path';
import type { NxJsonConfiguration } from './nx-json';
import { readNxJson } from './nx-json';
import { ProjectsConfigurations } from './workspace-json-project-json';
import { retrieveProjectConfigurationsSync } from '../project-graph/utils/retrieve-workspace-files';
import { retrieveProjectConfigurationsSync } from '../project-graph/utils/retrieve-workspace-files.deprecated';

// TODO(v18): remove this class
/**
Expand Down
2 changes: 2 additions & 0 deletions packages/nx/src/devkit-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export type {
NxPluginV2,
ProjectTargetConfigurator,
CreateNodes,
CreateNodesAsync,
CreateNodesFunction,
CreateNodesResult,
CreateNodesContext,
CreateDependencies,
CreateDependenciesContext,
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/native/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ export class Watcher {
export class WorkspaceContext {
workspaceRoot: string
constructor(workspaceRoot: string)
getWorkspaceFiles(globs: Array<string>, parseConfigurations: (arg0: Array<string>) => Record<string, string>): NxWorkspaceFiles
getWorkspaceFiles(globs: Array<string>, parseConfigurations: (arg0: Array<string>) => Promise<Record<string, string>>): Promise<NxWorkspaceFiles>
glob(globs: Array<string>): Array<string>
getProjectConfigurations(globs: Array<string>, parseConfigurations: (arg0: Array<string>) => Record<string, string>): Record<string, string>
getProjectConfigurations(globs: Array<string>, parseConfigurations: (arg0: Array<string>) => Promise<Record<string, string>>): Promise<Record<string, string>>
incrementalUpdate(updatedFiles: Array<string>, deletedFiles: Array<string>): Record<string, string>
allFileData(): Array<FileData>
}
6 changes: 3 additions & 3 deletions packages/nx/src/native/tests/workspace_files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { readJsonFile } from '../../utils/fileutils';

describe('workspace files', () => {
function createParseConfigurationsFunction(tempDir: string) {
return (filenames: string[]) => {
return async (filenames: string[]) => {
const res = {};
for (const filename of filenames) {
const json = readJsonFile(join(tempDir, filename));
Expand Down Expand Up @@ -51,7 +51,7 @@ describe('workspace files', () => {
let globs = ['project.json', '**/project.json', 'libs/*/package.json'];

const context = new WorkspaceContext(fs.tempDir);
let { projectFileMap, globalFiles } = context.getWorkspaceFiles(
let { projectFileMap, globalFiles } = await context.getWorkspaceFiles(
globs,
createParseConfigurationsFunction(fs.tempDir)
);
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('workspace files', () => {
const context = new WorkspaceContext(fs.tempDir);

const globs = ['project.json', '**/project.json', '**/package.json'];
const { globalFiles, projectFileMap } = context.getWorkspaceFiles(
const { globalFiles, projectFileMap } = await context.getWorkspaceFiles(
globs,
createParseConfigurationsFunction(fs.tempDir)
);
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/native/types/file_data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::cmp::Ordering;

#[napi(object)]
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct FileData {
pub file: String,
pub hash: String,
Expand All @@ -17,7 +17,7 @@ impl PartialEq<Self> for FileData {

impl PartialOrd<Self> for FileData {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.file.partial_cmp(&other.file)
Some(self.cmp(other))
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/nx/src/native/workspace/config_files.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::native::utils::glob::build_glob_set;
use crate::native::utils::path::Normalize;
use std::collections::HashMap;
use napi::bindgen_prelude::Promise;

use crate::native::workspace::errors::{InternalWorkspaceErrors, WorkspaceErrors};
use rayon::prelude::*;
Expand Down Expand Up @@ -29,9 +30,9 @@ pub(super) fn get_project_configurations<ConfigurationParser>(
globs: Vec<String>,
files: Option<&[(PathBuf, String)]>,
parse_configurations: ConfigurationParser,
) -> napi::Result<HashMap<String, String>>
) -> napi::Result<Promise<HashMap<String, String>>>
where
ConfigurationParser: Fn(Vec<String>) -> napi::Result<HashMap<String, String>>,
ConfigurationParser: Fn(Vec<String>) -> napi::Result<Promise<HashMap<String, String>>>,
{
let config_paths = glob_files(globs, files).map_err(anyhow::Error::from)?;

Expand Down
26 changes: 17 additions & 9 deletions packages/nx/src/native/workspace/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashMap;

use crate::native::types::FileData;
use crate::native::utils::path::Normalize;
use napi::bindgen_prelude::*;
use parking_lot::lock_api::MutexGuard;
use parking_lot::{Condvar, Mutex, RawMutex};
use rayon::prelude::*;
Expand All @@ -15,7 +16,6 @@ use xxhash_rust::xxh3;

use crate::native::walker::nx_walker;
use crate::native::workspace::errors::WorkspaceErrors;
use crate::native::workspace::workspace_files::NxWorkspaceFiles;
use crate::native::workspace::{config_files, workspace_files};

#[napi]
Expand Down Expand Up @@ -148,41 +148,49 @@ impl WorkspaceContext {
}
}

#[napi]
#[napi(ts_return_type = "Promise<NxWorkspaceFiles>")]
pub fn get_workspace_files<ConfigurationParser>(
&self,
env: Env,
globs: Vec<String>,
parse_configurations: ConfigurationParser,
) -> napi::Result<NxWorkspaceFiles, WorkspaceErrors>
) -> anyhow::Result<Option<Object>>
where
ConfigurationParser: Fn(Vec<String>) -> napi::Result<HashMap<String, String>>,
ConfigurationParser: Fn(Vec<String>) -> napi::Result<Promise<HashMap<String, String>>>,
{
workspace_files::get_files(
env,
globs,
parse_configurations,
self.files_worker.get_files().as_deref(),
)
.map_err(anyhow::Error::from)
}

#[napi]
pub fn glob(&self, globs: Vec<String>) -> napi::Result<Vec<String>, WorkspaceErrors> {
config_files::glob_files(globs, self.files_worker.get_files().as_deref())
}

#[napi]
#[napi(ts_return_type = "Promise<Record<string, string>>")]
pub fn get_project_configurations<ConfigurationParser>(
&self,
env: Env,
globs: Vec<String>,
parse_configurations: ConfigurationParser,
) -> napi::Result<HashMap<String, String>>
) -> napi::Result<Object>
where
ConfigurationParser: Fn(Vec<String>) -> napi::Result<HashMap<String, String>>,
ConfigurationParser: Fn(Vec<String>) -> napi::Result<Promise<HashMap<String, String>>>,
{
config_files::get_project_configurations(
let promise = config_files::get_project_configurations(
globs,
self.files_worker.get_files().as_deref(),
parse_configurations,
)
)?;
env.spawn_future(async move {
let result = promise.await?;
Ok(result)
})
}

#[napi]
Expand Down
Loading