Skip to content

Commit

Permalink
Added package manager and root package json to execution state
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Apr 20, 2023
1 parent 32f27a2 commit 8318200
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
7 changes: 5 additions & 2 deletions cli/internal/turbostate/turbostate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package turbostate

import (
"fmt"
"github.com/vercel/turbo/cli/internal/fs"

"github.com/vercel/turbo/cli/internal/util"
)
Expand Down Expand Up @@ -99,8 +100,10 @@ type ParsedArgsFromRust struct {

// ExecutionState is the entire state of a turbo execution that is passed from the Rust shim.
type ExecutionState struct {
RemoteConfig RemoteConfig `json:"remote_config"`
CLIArgs ParsedArgsFromRust `json:"cli_args"`
RemoteConfig RemoteConfig `json:"remote_config"`
PackageManager string `json:"package_manager"`
RootPackageJson fs.PackageJSON `json:"root_package_json"`
CLIArgs ParsedArgsFromRust `json:"cli_args"`
}

// RemoteConfig holds the authentication and endpoint details for the API client
Expand Down
18 changes: 14 additions & 4 deletions crates/turborepo-lib/src/execution_state.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use serde::Serialize;
use turbopath::RelativeSystemPathBuf;

use crate::{
cli::Args, commands::CommandBase, package_manager::PackageManager, DryRunMode, LogPrefix,
RunArgs,
cli::{Args, RunArgs},
commands::CommandBase,
package_json::{read_package_json, PackageJson},
package_manager::PackageManager,
DryRunMode, LogPrefix, RunArgs,
};

#[derive(Debug, Serialize)]
pub struct ExecutionState<'a> {
pub remote_config: RemoteConfig<'a>,
package_manager: PackageManager,
root_package_json: PackageJson,
pub cli_args: &'a Args,
}

Expand All @@ -19,10 +25,10 @@ pub struct RemoteConfig<'a> {
pub api_url: &'a str,
}

impl<'a> TryFrom<&'a CommandBase> for ExecutionState<'a> {
impl<'a> TryFrom<&'a mut CommandBase> for ExecutionState<'a> {
type Error = anyhow::Error;

fn try_from(base: &'a CommandBase) -> Result<Self, Self::Error> {
fn try_from(base: &'a mut CommandBase) -> Result<Self, Self::Error> {
let repo_config = base.repo_config()?;
let user_config = base.user_config()?;

Expand All @@ -32,9 +38,13 @@ impl<'a> TryFrom<&'a CommandBase> for ExecutionState<'a> {
team_slug: repo_config.team_slug(),
api_url: repo_config.api_url(),
};
let root_package_json = PackageJson::load(base.repo_root().join("package.json"))?;
let package_manager = PackageManager::get_package_manager(base, &root_package_json)?;

Ok(ExecutionState {
remote_config,
package_manager,
root_package_json,
cli_args: base.args(),
})
}
Expand Down
10 changes: 6 additions & 4 deletions crates/turborepo-lib/src/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ pub struct PackageJson {
raw_json: HashMap<String, serde_json::Value>,
}

pub fn read_package_json(path: &AbsoluteSystemPathBuf) -> Result<PackageJson> {
let contents = std::fs::read_to_string(path)?;
let mut package_json: PackageJson = serde_json::from_str(&contents)?;
Ok(package_json)
impl PackageJson {
pub fn load(path: &AbsoluteSystemPathBuf) -> Result<PackageJson> {
let contents = std::fs::read_to_string(path)?;
let mut package_json: PackageJson = serde_json::from_str(&contents)?;
Ok(package_json)
}
}

0 comments on commit 8318200

Please sign in to comment.