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

AST-based modification of turbo.json #5509

Merged
merged 2 commits into from
Aug 14, 2023
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/turborepo-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ humantime = "2.1.0"
indicatif = { workspace = true }
itertools = { workspace = true }
json_comments = "0.2.1"
jsonc-parser = { version = "0.21.0" }
lazy_static = { workspace = true }
libc = "0.2.140"
notify = "5.1"
Expand Down
30 changes: 9 additions & 21 deletions crates/turborepo-lib/src/commands/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
cli::LinkTarget,
commands::CommandBase,
config::{RawTurboJSON, SpacesJson},
rewrite_json,
};

#[derive(Clone)]
Expand Down Expand Up @@ -443,29 +444,16 @@ fn add_turbo_to_gitignore(base: &CommandBase) -> Result<()> {

fn add_space_id_to_turbo_json(base: &CommandBase, space_id: &str) -> Result<()> {
let turbo_json_path = base.repo_root.join_component("turbo.json");
let turbo_json = fs::read_to_string(&turbo_json_path)?;
let space_id_json_value = format!("\"{}\"", space_id);

if !turbo_json_path.exists() {
return Err(anyhow!("turbo.json not found."));
}

let turbo_json_file = File::open(&turbo_json_path)?;
let mut turbo_json: RawTurboJSON = serde_json::from_reader(turbo_json_file)?;
match turbo_json.experimental_spaces {
Some(mut spaces_config) => {
spaces_config.id = Some(space_id.to_string());
turbo_json.experimental_spaces = Some(spaces_config);
}
None => {
turbo_json.experimental_spaces = Some(SpacesJson {
id: Some(space_id.to_string()),
other: None,
});
}
}
let output = rewrite_json::set_path(
&turbo_json,
&["experimentalSpaces", "id"],
&space_id_json_value,
)?;

// write turbo_json back to file
let config_file = File::create(&turbo_json_path)?;
serde_json::to_writer_pretty(&config_file, &turbo_json)?;
fs::write(turbo_json_path, output)?;

Ok(())
}
Expand Down
30 changes: 9 additions & 21 deletions crates/turborepo-lib/src/commands/unlink.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{fs, fs::File};
use std::fs;

use anyhow::{Context, Result};
use turborepo_ui::GREY;

use crate::{cli::LinkTarget, commands::CommandBase, config::RawTurboJSON};
use crate::{cli::LinkTarget, commands::CommandBase, config::RawTurboJSON, rewrite_json};

enum UnlinkSpacesResult {
Unlinked,
Expand Down Expand Up @@ -55,25 +55,13 @@ pub fn unlink(base: &mut CommandBase, target: LinkTarget) -> Result<()> {

fn remove_spaces_from_turbo_json(base: &CommandBase) -> Result<UnlinkSpacesResult> {
let turbo_json_path = base.repo_root.join_component("turbo.json");
let turbo_json = fs::read_to_string(&turbo_json_path)?;

let turbo_json_contents =
fs::read_to_string(&turbo_json_path).context("unable to open turbo.json file")?;
let mut turbo_json: RawTurboJSON = serde_json::from_str(&turbo_json_contents)?;
let has_spaces_id = turbo_json
.experimental_spaces
.unwrap_or_default()
.id
.is_some();
// remove the spaces config
// TODO: in the future unlink should possible just remove the spaces id
turbo_json.experimental_spaces = None;

// write turbo_json back to file
let config_file = File::create(&turbo_json_path)?;
serde_json::to_writer_pretty(&config_file, &turbo_json)?;

match has_spaces_id {
true => Ok(UnlinkSpacesResult::Unlinked),
false => Ok(UnlinkSpacesResult::NoSpacesFound),
let output = rewrite_json::unset_path(&turbo_json, &["experimentalSpaces", "id"])?;
if let Some(output) = output {
fs::write(turbo_json_path, output)?;
Ok(UnlinkSpacesResult::Unlinked)
} else {
Ok(UnlinkSpacesResult::NoSpacesFound)
}
}
1 change: 1 addition & 0 deletions crates/turborepo-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod opts;
mod package_graph;
mod package_json;
mod package_manager;
mod rewrite_json;
mod run;
mod shim;
mod task_graph;
Expand Down
Loading