Skip to content

Commit 1870935

Browse files
committed
Use existing helper functions to get bsc path
1 parent eb7da76 commit 1870935

File tree

3 files changed

+13
-32
lines changed

3 files changed

+13
-32
lines changed

rewatch/src/cli.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ pub enum Command {
177177
files: Vec<String>,
178178
#[command(flatten)]
179179
bsc_path: BscPathArg,
180+
#[command(flatten)]
181+
folder: FolderArg,
180182
},
181183
/// Alias to `legacy dump`.
182184
#[command(disable_help_flag = true)]

rewatch/src/format_cmd.rs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ use std::process::Command;
55
use std::io::{self, Read, Write};
66
use std::fs;
77
use rayon::prelude::*;
8+
use crate::helpers;
89
use num_cpus;
910

1011
pub fn run(
1112
stdin_path: Option<String>,
1213
all: bool,
1314
check: bool,
1415
files: Vec<String>,
15-
bsc_path_arg: Option<PathBuf>,
16+
bsc_path: Option<PathBuf>,
17+
path: PathBuf,
1618
) -> Result<()> {
17-
let bsc_exe = match bsc_path_arg {
18-
Some(path) => path,
19-
None => find_bsc_exe()?,
19+
let project_root = helpers::get_abs_path(&path);
20+
let workspace_root = helpers::get_workspace_root(&project_root);
21+
22+
let bsc_exe = match bsc_path {
23+
Some(path) => helpers::get_abs_path(&path),
24+
None => helpers::get_bsc(&project_root, &workspace_root),
2025
};
2126

2227
if check && stdin_path.is_some() {
@@ -37,34 +42,7 @@ pub fn run(
3742
Ok(())
3843
}
3944

40-
fn find_bsc_exe() -> Result<PathBuf> {
41-
let current_exe = std::env::current_exe()?;
42-
let mut current_dir = current_exe.parent().unwrap_or_else(|| Path::new("/"));
43-
44-
// Traverse up to find node_modules
45-
let node_modules_path = loop {
46-
let potential_path = current_dir.join("node_modules");
47-
if potential_path.exists() {
48-
break Some(potential_path);
49-
}
50-
if current_dir.parent().is_none() {
51-
break None;
52-
}
53-
current_dir = current_dir.parent().unwrap();
54-
}
55-
.ok_or_else(|| anyhow::anyhow!("Could not find node_modules directory"))?;
56-
57-
let target = format!("{}-{}", std::env::consts::OS, std::env::consts::ARCH);
58-
let bsc_path = node_modules_path
59-
.join("@rescript")
60-
.join(target)
61-
.join("bsc.exe");
6245

63-
if !bsc_path.exists() {
64-
bail!("bsc executable not found at {}", bsc_path.display());
65-
}
66-
Ok(bsc_path)
67-
}
6846

6947
fn format_all(bsc_exe: &Path, check: bool) -> Result<()> {
7048
let output = Command::new(std::env::current_exe()?)

rewatch/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ fn main() -> Result<()> {
119119
check,
120120
files,
121121
bsc_path,
122-
} => format_cmd::run(stdin, all, check, files, bsc_path.as_ref().map(|s| PathBuf::from(s.clone()))),
122+
folder: path,
123+
} => format_cmd::run(stdin, all, check, files, bsc_path.as_ref().map(|s| PathBuf::from(s.clone())), PathBuf::from(path.folder)),
123124
cli::Command::Dump { mut dump_args } => {
124125
dump_args.insert(0, "dump".into());
125126
let code = build::pass_through_legacy(dump_args);

0 commit comments

Comments
 (0)