Skip to content

Commit

Permalink
feat(core): make createNodes async
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli authored and AgentEnder committed Nov 13, 2023
1 parent 5857561 commit b13c137
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 62 deletions.
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>>): object | null
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>>): object
incrementalUpdate(updatedFiles: Array<string>, deletedFiles: Array<string>): Record<string, string>
allFileData(): Array<FileData>
}
118 changes: 117 additions & 1 deletion 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,9 +51,20 @@ describe('workspace files', () => {
let globs = ['project.json', '**/project.json', 'libs/*/package.json'];

const context = new WorkspaceContext(fs.tempDir);
<<<<<<< HEAD
let { projectFileMap, globalFiles } = 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 @@ -128,6 +139,7 @@ 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 Down Expand Up @@ -179,6 +191,110 @@ 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
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::glob::build_glob_set;
use crate::native::utils::path::Normalize;
use std::collections::HashMap;
use napi::bindgen_prelude::{Object, 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
21 changes: 15 additions & 6 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 Down Expand Up @@ -151,17 +152,20 @@ impl WorkspaceContext {
#[napi]
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]
Expand All @@ -172,17 +176,22 @@ impl WorkspaceContext {
#[napi]
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

0 comments on commit b13c137

Please sign in to comment.