Skip to content

Commit ddade2e

Browse files
authored
chore: extract set_run_flags logic as named helper (#10012)
### Description Refactors the logic in `run` related to setting run flags into a named helper. If you're not dealing with `Watch` or `Run`, this logic is a barrier to understanding how `run` works, extracting this logic into a helper makes it easier to read `run`'s logic. The `set_run_flags` helper uses the same mutation of `command` that was previously present in the `run` function body, but future work could include removing the necessity for mutation.
1 parent 86d30c9 commit ddade2e

File tree

1 file changed

+48
-39
lines changed
  • crates/turborepo-lib/src/cli

1 file changed

+48
-39
lines changed

crates/turborepo-lib/src/cli/mod.rs

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,53 @@ fn should_print_version() -> bool {
11891189
print_version_state == PrintVersionState::Enabled && ci_state == CIState::Outside
11901190
}
11911191

1192+
fn set_run_flags<'a>(
1193+
command: &'a mut Command,
1194+
repo_state: &'a Option<RepoState>,
1195+
cli_args: &'a Args,
1196+
) -> Result<&'a mut Command, Error> {
1197+
match command {
1198+
Command::Run {
1199+
run_args: _,
1200+
execution_args,
1201+
}
1202+
| Command::Watch { execution_args, .. } => {
1203+
// Don't overwrite the flag if it's already been set for whatever reason
1204+
execution_args.single_package = execution_args.single_package
1205+
|| repo_state
1206+
.as_ref()
1207+
.map(|repo_state| matches!(repo_state.mode, RepoMode::SinglePackage))
1208+
.unwrap_or(false);
1209+
// If this is a run command, and we know the actual invocation path, set the
1210+
// inference root, as long as the user hasn't overridden the cwd
1211+
if cli_args.cwd.is_none() {
1212+
if let Ok(invocation_dir) = env::var(INVOCATION_DIR_ENV_VAR) {
1213+
// TODO: this calculation can probably be wrapped into the path library
1214+
// and made a little more robust or clear
1215+
let invocation_path = Utf8Path::new(&invocation_dir);
1216+
1217+
// If repo state doesn't exist, we're either local turbo running at the root
1218+
// (cwd), or inference failed.
1219+
// If repo state does exist, we're global turbo, and want to calculate
1220+
// package inference based on the repo root
1221+
let this_dir = AbsoluteSystemPathBuf::cwd()?;
1222+
let repo_root = repo_state.as_ref().map_or(&this_dir, |r| &r.root);
1223+
if let Ok(relative_path) = invocation_path.strip_prefix(repo_root) {
1224+
if !relative_path.as_str().is_empty() {
1225+
debug!("pkg_inference_root set to \"{}\"", relative_path);
1226+
execution_args.pkg_inference_root = Some(relative_path.to_string());
1227+
}
1228+
}
1229+
} else {
1230+
debug!("{} not set", INVOCATION_DIR_ENV_VAR);
1231+
}
1232+
}
1233+
}
1234+
_ => {}
1235+
}
1236+
Ok(command)
1237+
}
1238+
11921239
fn default_to_run_command(cli_args: &Args) -> Result<Command, Error> {
11931240
let run_args = cli_args.run_args.clone().unwrap_or_default();
11941241
let execution_args = cli_args
@@ -1259,45 +1306,7 @@ pub async fn run(
12591306
let mut command = get_command(&mut cli_args)?;
12601307

12611308
// Set some run flags if we have the data and are executing a Run
1262-
match &mut command {
1263-
Command::Run {
1264-
run_args: _,
1265-
execution_args,
1266-
}
1267-
| Command::Watch { execution_args, .. } => {
1268-
// Don't overwrite the flag if it's already been set for whatever reason
1269-
execution_args.single_package = execution_args.single_package
1270-
|| repo_state
1271-
.as_ref()
1272-
.map(|repo_state| matches!(repo_state.mode, RepoMode::SinglePackage))
1273-
.unwrap_or(false);
1274-
// If this is a run command, and we know the actual invocation path, set the
1275-
// inference root, as long as the user hasn't overridden the cwd
1276-
if cli_args.cwd.is_none() {
1277-
if let Ok(invocation_dir) = env::var(INVOCATION_DIR_ENV_VAR) {
1278-
// TODO: this calculation can probably be wrapped into the path library
1279-
// and made a little more robust or clear
1280-
let invocation_path = Utf8Path::new(&invocation_dir);
1281-
1282-
// If repo state doesn't exist, we're either local turbo running at the root
1283-
// (cwd), or inference failed.
1284-
// If repo state does exist, we're global turbo, and want to calculate
1285-
// package inference based on the repo root
1286-
let this_dir = AbsoluteSystemPathBuf::cwd()?;
1287-
let repo_root = repo_state.as_ref().map_or(&this_dir, |r| &r.root);
1288-
if let Ok(relative_path) = invocation_path.strip_prefix(repo_root) {
1289-
if !relative_path.as_str().is_empty() {
1290-
debug!("pkg_inference_root set to \"{}\"", relative_path);
1291-
execution_args.pkg_inference_root = Some(relative_path.to_string());
1292-
}
1293-
}
1294-
} else {
1295-
debug!("{} not set", INVOCATION_DIR_ENV_VAR);
1296-
}
1297-
}
1298-
}
1299-
_ => {}
1300-
}
1309+
set_run_flags(&mut command, &repo_state, &cli_args)?;
13011310

13021311
// TODO: make better use of RepoState, here and below. We've already inferred
13031312
// the repo root, we don't need to calculate it again, along with package

0 commit comments

Comments
 (0)