Skip to content

Commit

Permalink
feat(core): support for async createNodes functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Nov 10, 2023
1 parent bdbed35 commit 1b8d206
Show file tree
Hide file tree
Showing 23 changed files with 394 additions and 330 deletions.
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>) => Promise<Record<string, string>>): object | null
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>) => Promise<Record<string, string>>): object
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>
}
120 changes: 2 additions & 118 deletions packages/nx/src/native/tests/workspace_files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,9 @@ describe('workspace files', () => {
let globs = ['project.json', '**/project.json', 'libs/*/package.json'];

const context = new WorkspaceContext(fs.tempDir);
<<<<<<< HEAD
let { projectFileMap, globalFiles } = context.getWorkspaceFiles(
let { projectFileMap, globalFiles } = await context.getWorkspaceFiles(
globs,
createParseConfigurationsFunction(fs.tempDir)
=======
let { projectFileMap, projectConfigurations, globalFiles } =
(await context.getWorkspaceFiles(
globs,
createParseConfigurationsFunction(fs.tempDir)
)) as any;

let sortedConfigs = Object.values(projectConfigurations).sort((a, b) =>
a['name'].localeCompare(b['name'])
>>>>>>> 6d080c8bf (feat(core): make createNodes async)
);

expect(projectFileMap).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -139,7 +128,6 @@ describe('workspace files', () => {
`);
});

<<<<<<< HEAD
it('should assign files to the root project if it exists', async () => {
const fs = new TempFs('workspace-files');
const nxJson: NxJsonConfiguration = {};
Expand All @@ -160,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 Expand Up @@ -191,110 +179,6 @@ describe('workspace files', () => {
]
`);
});
=======
// it('should assign files to the root project if it exists', async () => {
// const fs = new TempFs('workspace-files');
// const nxJson: NxJsonConfiguration = {};
// await fs.createFiles({
// './nx.json': JSON.stringify(nxJson),
// './package.json': JSON.stringify({
// name: 'repo-name',
// version: '0.0.0',
// dependencies: {},
// }),
// './project.json': JSON.stringify({
// name: 'repo-name',
// }),
// './src/index.js': '',
// './jest.config.js': '',
// });
//
// const context = new WorkspaceContext(fs.tempDir);
//
// const globs = ['project.json', '**/project.json', '**/package.json'];
// const { globalFiles, projectFileMap } = context.getWorkspaceFiles(
// globs,
// createParseConfigurationsFunction(fs.tempDir)
// );
//
// expect(globalFiles).toEqual([]);
// expect(projectFileMap['repo-name']).toMatchInlineSnapshot(`
// [
// {
// "file": "jest.config.js",
// "hash": "3244421341483603138",
// },
// {
// "file": "nx.json",
// "hash": "1389868326933519382",
// },
// {
// "file": "package.json",
// "hash": "14409636362330144230",
// },
// {
// "file": "project.json",
// "hash": "4357927788053707201",
// },
// {
// "file": "src/index.js",
// "hash": "3244421341483603138",
// },
// ]
// `);
// });
//
// it('should dedupe configuration files', async () => {
// const fs = new TempFs('workspace-files');
// const nxJson: NxJsonConfiguration = {};
// await fs.createFiles({
// './nx.json': JSON.stringify(nxJson),
// './package.json': JSON.stringify({
// name: 'repo-name',
// version: '0.0.0',
// dependencies: {},
// }),
// './project.json': JSON.stringify({
// name: 'repo-name',
// }),
// './libs/project1/project.json': JSON.stringify({
// name: 'project1',
// }),
// './libs/project1/package.json': JSON.stringify({
// name: 'project1',
// }),
// './libs/project1/index.js': '',
// });
//
// const context = new WorkspaceContext(fs.tempDir);
// let globs = ['project.json', '**/project.json', '**/package.json'];
//
// let nodes = context.getProjectConfigurations(globs, (filenames) => {
// const res = {};
// for (const filename of filenames) {
// const json = readJsonFile(join(fs.tempDir, filename));
// res[json.name] = {
// ...json,
// root: dirname(filename),
// };
// }
// return {
// externalNodes: {},
// projectNodes: res,
// };
// });
// expect(nodes.projectNodes).toEqual({
// project1: {
// name: 'project1',
// root: 'libs/project1',
// },
// 'repo-name': expect.objectContaining({
// name: 'repo-name',
// root: '.',
// }),
// });
// });
>>>>>>> 6d080c8bf (feat(core): make createNodes async)

// describe('errors', () => {
// it('it should infer names of configuration files without a name', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/native/workspace/config_files.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::native::utils::glob::build_glob_set;
use crate::native::utils::path::Normalize;
use std::collections::HashMap;
use napi::bindgen_prelude::{Object, Promise};
use napi::bindgen_prelude::Promise;

use crate::native::workspace::errors::{InternalWorkspaceErrors, WorkspaceErrors};
use rayon::prelude::*;
Expand Down
5 changes: 2 additions & 3 deletions packages/nx/src/native/workspace/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,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 @@ -149,7 +148,7 @@ impl WorkspaceContext {
}
}

#[napi]
#[napi(ts_return_type = "Promise<NxWorkspaceFiles>")]
pub fn get_workspace_files<ConfigurationParser>(
&self,
env: Env,
Expand All @@ -173,7 +172,7 @@ impl WorkspaceContext {
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,
Expand Down
3 changes: 1 addition & 2 deletions packages/nx/src/native/workspace/workspace_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use napi::bindgen_prelude::{Object, Promise};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use napi::{Env, JsObject};
use napi::Env;
use rayon::prelude::*;
use serde_json::Value;
use tracing::trace;

use crate::native::types::FileData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ import * as memfs from 'memfs';
import '../../../internal-testing-utils/mock-fs';

import { CreatePackageJsonProjectsNextToProjectJson } from './package-json-next-to-project-json';
import {
CreateNodesContext,
CreateNodesFunction,
} from '../../../utils/nx-plugin';
import { CreateNodesContext } from '../../../utils/nx-plugin';
const { createNodes } = CreatePackageJsonProjectsNextToProjectJson;

describe('nx project.json plugin', () => {
let context: CreateNodesContext;
let createNodesFunction: CreateNodesFunction;
let createNodesFunction = createNodes[1];

beforeEach(() => {
context = {
nxJsonConfiguration: {},
workspaceRoot: '/root',
};
createNodesFunction = createNodes[1];
});

it('should build projects from project.json', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { dirname, join } from 'node:path';
import { ProjectConfiguration } from '../../../config/workspace-json-project-json';
import { toProjectName } from '../../../config/workspaces';
import { readJsonFile } from '../../../utils/fileutils';
import { NxPluginV2 } from '../../../utils/nx-plugin';
import { CreateNodes, NxPluginV2 } from '../../../utils/nx-plugin';

export const CreateProjectJsonProjectsPlugin: NxPluginV2 = {
export const CreateProjectJsonProjectsPlugin: NxPluginV2<void, CreateNodes> = {
name: 'nx-core-build-project-json-nodes',
createNodes: [
'{project.json,**/project.json}',
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/project-graph/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from './project-graph';
import { toOldFormat } from '../adapter/angular-json';
import { getIgnoreObject } from '../utils/ignore';
import { retrieveProjectConfigurationsSync } from './utils/retrieve-workspace-files';
import { retrieveProjectConfigurationsSync } from './utils/retrieve-workspace-files.deprecated';

export interface Change {
type: string;
Expand Down
Loading

0 comments on commit 1b8d206

Please sign in to comment.