Skip to content

Commit

Permalink
feat(turbo-repository): return additional workspace data (#7187)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmckeb authored Feb 6, 2024
1 parent 09c398b commit 3b460c6
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 52 deletions.
2 changes: 1 addition & 1 deletion packages/turbo-repository/__tests__/find.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("Workspace", () => {

it("finds a package manager", async () => {
const workspace = await Workspace.find();
const packageManager: PackageManager = workspace.packageManager();
const packageManager: PackageManager = workspace.packageManager;
expect(packageManager.name).toBe("pnpm");
});
// TODO: proper tests on real fixtures
Expand Down
26 changes: 20 additions & 6 deletions packages/turbo-repository/js/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@

/* auto-generated by NAPI-RS */

export class Workspace {
export class Package {
/** The absolute path to the package root. */
readonly absolutePath: string;
readonly isMultiPackage: boolean;
static find(path?: string | undefined | null): Promise<Workspace>;
packageManager(): PackageManager;
findPackages(): Promise<Array<Package>>;
/** The relative path from the workspace root to the package root. */
readonly relativePath: string;
}
export class PackageManager {
/** The package manager name (lower case). */
readonly name: string;
/** The package manager version (semver). */
readonly version: string;
}
export class Package {
export class Workspace {
/** The absolute path to the workspace root. */
readonly absolutePath: string;
/** `true` when the workspace is a multi-package workspace. */
readonly isMultiPackage: boolean;
/** The package manager used by the workspace. */
readonly packageManager: PackageManager;
/**
* Finds the workspace root from the given path, and returns a new
* Workspace.
*/
static find(path?: string | undefined | null): Promise<Workspace>;
/** Finds and returns packages within the workspace. */
findPackages(): Promise<Array<Package>>;
}
14 changes: 7 additions & 7 deletions packages/turbo-repository/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@turbo/repository",
"version": "0.0.1-canary.3",
"version": "0.0.1-canary.4",
"description": "",
"bugs": "https://github.com/vercel/turbo/issues",
"homepage": "https://turbo.build/repo",
Expand All @@ -15,11 +15,11 @@
],
"types": "dist/index.d.ts",
"optionalDependencies": {
"@turbo/repository-darwin-x64": "0.0.1-canary.3",
"@turbo/repository-darwin-arm64": "0.0.1-canary.3",
"@turbo/repository-linux-x64-gnu": "0.0.1-canary.3",
"@turbo/repository-linux-arm64-gnu": "0.0.1-canary.3",
"@turbo/repository-win32-x64-msvc": "0.0.1-canary.3",
"@turbo/repository-win32-arm64-msvc": "0.0.1-canary.3"
"@turbo/repository-darwin-x64": "0.0.1-canary.4",
"@turbo/repository-darwin-arm64": "0.0.1-canary.4",
"@turbo/repository-linux-x64-gnu": "0.0.1-canary.4",
"@turbo/repository-linux-arm64-gnu": "0.0.1-canary.4",
"@turbo/repository-win32-x64-msvc": "0.0.1-canary.4",
"@turbo/repository-win32-arm64-msvc": "0.0.1-canary.4"
}
}
2 changes: 1 addition & 1 deletion packages/turbo-repository/npm/darwin-arm64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@turbo/repository-darwin-arm64",
"version": "0.0.1-canary.3",
"version": "0.0.1-canary.4",
"repository": {
"type": "git",
"url": "https://github.com/vercel/turbo",
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-repository/npm/darwin-x64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@turbo/repository-darwin-x64",
"version": "0.0.1-canary.3",
"version": "0.0.1-canary.4",
"repository": {
"type": "git",
"url": "https://github.com/vercel/turbo",
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-repository/npm/linux-arm64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@turbo/repository-linux-arm64-gnu",
"version": "0.0.1-canary.3",
"version": "0.0.1-canary.4",
"repository": {
"type": "git",
"url": "https://github.com/vercel/turbo",
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-repository/npm/linux-x64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@turbo/repository-linux-x64-gnu",
"version": "0.0.1-canary.3",
"version": "0.0.1-canary.4",
"repository": {
"type": "git",
"url": "https://github.com/vercel/turbo",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@turbo/repository-win32-arm64-msvc",
"version": "0.0.1-canary.3",
"version": "0.0.1-canary.4",
"repository": {
"type": "git",
"url": "https://github.com/vercel/turbo",
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-repository/npm/win32-x64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@turbo/repository-win32-x64-msvc",
"version": "0.0.1-canary.3",
"version": "0.0.1-canary.4",
"repository": {
"type": "git",
"url": "https://github.com/vercel/turbo",
Expand Down
13 changes: 12 additions & 1 deletion packages/turbo-repository/rust/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use turborepo_repository::{
package_manager,
};

use crate::{Package, Workspace};
use crate::{Package, PackageManager, Workspace};

/// This module is used to isolate code with defined errors
/// from code in lib.rs that needs to have errors coerced to strings /
Expand Down Expand Up @@ -59,10 +59,21 @@ impl Workspace {
})?;
let workspace_state = WorkspaceState::infer(&reference_dir)?;
let is_multi_package = workspace_state.mode == WorkspaceType::MultiPackage;
let package_manager_name = workspace_state
.package_manager
.as_ref()
.map_err(|error| Error::PackageManager {
error: error.to_string(),
path: workspace_state.root.clone(),
})?
.clone();
Ok(Self {
absolute_path: workspace_state.root.to_string(),
workspace_state,
is_multi_package,
package_manager: PackageManager {
name: package_manager_name.to_string(),
},
})
}

Expand Down
54 changes: 23 additions & 31 deletions packages/turbo-repository/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,72 +1,64 @@
use napi_derive::napi;
use turbopath::AbsoluteSystemPath;
use turborepo_repository::{
inference::RepoState as WorkspaceState, package_manager::PackageManager as RustPackageManager,
};
use turborepo_repository::inference::RepoState as WorkspaceState;

mod internal;

#[napi]
pub struct Workspace {
workspace_state: WorkspaceState,
pub struct Package {
/// The absolute path to the package root.
#[napi(readonly)]
pub absolute_path: String,
/// The relative path from the workspace root to the package root.
#[napi(readonly)]
pub is_multi_package: bool,
pub relative_path: String,
}

#[derive(Clone)]
#[napi]

pub struct PackageManager {
#[allow(dead_code)]
package_manager: RustPackageManager,
/// The package manager name in lower case.
#[napi(readonly)]
pub name: String,
}

#[napi]
pub struct Package {
pub struct Workspace {
workspace_state: WorkspaceState,
/// The absolute path to the workspace root.
#[napi(readonly)]
pub absolute_path: String,
/// `true` when the workspace is a multi-package workspace.
#[napi(readonly)]
pub is_multi_package: bool,
/// The package manager used by the workspace.
#[napi(readonly)]
pub package_manager: PackageManager,
}

impl Package {
fn new(workspace_path: &AbsoluteSystemPath, package_path: &AbsoluteSystemPath) -> Self {
workspace_path
let relative_path = workspace_path
.anchor(package_path)
.expect("workspace is in the repo root");
.expect("Package path is within the workspace");
Self {
absolute_path: package_path.to_string(),
}
}
}

impl From<RustPackageManager> for PackageManager {
fn from(package_manager: RustPackageManager) -> Self {
Self {
name: package_manager.to_string(),
package_manager,
relative_path: relative_path.to_string(),
}
}
}

#[napi]
impl Workspace {
/// Finds the workspace root from the given path, and returns a new
/// Workspace.
#[napi(factory)]
pub async fn find(path: Option<String>) -> Result<Workspace, napi::Error> {
Self::find_internal(path).await.map_err(|e| e.into())
}

#[napi]
pub fn package_manager(&self) -> Result<PackageManager, napi::Error> {
// match rather than map/map_err due to only the Ok variant implementing "Copy"
// match lets us handle each case independently, rather than forcing the whole
// value to a reference or concrete value
match self.workspace_state.package_manager.as_ref() {
Ok(pm) => Ok((*pm).into()),
Err(e) => Err(napi::Error::from_reason(format!("{}", e))),
}
}

/// Finds and returns packages within the workspace.
#[napi]
pub async fn find_packages(&self) -> std::result::Result<Vec<Package>, napi::Error> {
self.packages_internal().await.map_err(|e| e.into())
Expand Down

0 comments on commit 3b460c6

Please sign in to comment.