Skip to content

Commit f6d8303

Browse files
committed
Also resolve rescript legacy via oxc_resolver
1 parent d409a79 commit f6d8303

File tree

3 files changed

+23
-33
lines changed

3 files changed

+23
-33
lines changed

rewatch/src/build.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -556,23 +556,17 @@ pub fn build(
556556
}
557557
}
558558

559-
pub fn pass_through_legacy(mut args: Vec<OsString>) -> i32 {
559+
pub fn pass_through_legacy(mut args: Vec<OsString>) -> anyhow::Result<i32> {
560560
let project_root = helpers::get_abs_path(Path::new("."));
561-
let project_context = ProjectContext::new(&project_root).unwrap();
562-
let rescript_legacy_path = helpers::get_rescript_legacy(&project_context);
561+
let project_context = ProjectContext::new(&project_root)?;
562+
let rescript_legacy_path = helpers::get_rescript_legacy_path(&project_context)?;
563563

564564
args.insert(0, rescript_legacy_path.into());
565565
let status = std::process::Command::new("node")
566566
.args(args)
567567
.stdout(Stdio::inherit())
568568
.stderr(Stdio::inherit())
569-
.status();
569+
.status()?;
570570

571-
match status {
572-
Ok(s) => s.code().unwrap_or(0),
573-
Err(err) => {
574-
eprintln!("Error running the legacy build system: {err}");
575-
1
576-
}
577-
}
571+
Ok(status.code().unwrap_or(0))
578572
}

rewatch/src/helpers.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -272,26 +272,19 @@ pub fn get_runtime_path(project_context: &ProjectContext) -> anyhow::Result<Path
272272
Ok(canonicalized.to_stripped_verbatim_path())
273273
}
274274

275-
pub fn get_rescript_legacy(project_context: &ProjectContext) -> PathBuf {
275+
pub fn get_rescript_legacy_path(project_context: &ProjectContext) -> anyhow::Result<PathBuf> {
276+
let resolver = Resolver::new(ResolveOptions::default());
276277
let root_path = project_context.get_root_path();
277-
let node_modules_rescript = root_path.join("node_modules").join("rescript");
278-
let rescript_legacy_path = if node_modules_rescript.exists() {
279-
node_modules_rescript
280-
.join("cli")
281-
.join("rescript-legacy.js")
282-
.canonicalize()
283-
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
284-
} else {
285-
// If the root folder / node_modules doesn't exist, something is wrong.
286-
// The only way this can happen is if we are inside the rescript repository.
287-
root_path
288-
.join("cli")
289-
.join("rescript-legacy.js")
290-
.canonicalize()
291-
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
292-
};
293278

294-
rescript_legacy_path.unwrap_or_else(|_| panic!("Could not find rescript-legacy.exe"))
279+
let rescript_resolved: oxc_resolver::Resolution = resolver.resolve(root_path, "rescript/package.json")?;
280+
let rescript_package_json_path = rescript_resolved.full_path();
281+
let rescript_path = rescript_package_json_path
282+
.parent()
283+
.ok_or_else(|| anyhow!("Failed to get parent directory of rescript package.json"))?;
284+
let rescript_legacy_path = rescript_path.join("cli").join("rescript-legacy.js");
285+
286+
let canonicalized = rescript_legacy_path.to_path_buf().canonicalize()?;
287+
Ok(canonicalized.to_stripped_verbatim_path())
295288
}
296289

297290
pub fn string_ends_with_any(s: &Path, suffixes: &[&str]) -> bool {

rewatch/src/main.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ fn main() -> Result<()> {
8787
dev.dev,
8888
)
8989
}
90-
cli::Command::Legacy { legacy_args } => {
91-
let code = build::pass_through_legacy(legacy_args);
92-
std::process::exit(code);
93-
}
90+
cli::Command::Legacy { legacy_args } => match build::pass_through_legacy(legacy_args) {
91+
Ok(code) => std::process::exit(code),
92+
Err(e) => {
93+
println!("{e}");
94+
std::process::exit(1)
95+
}
96+
},
9497
cli::Command::Format {
9598
stdin,
9699
check,

0 commit comments

Comments
 (0)