Skip to content

Commit ead028b

Browse files
authored
Try and read package name from rescript.json if package.json is not present (#7746)
* Try and read package name from rescript.json if package.json is not present. * Add changelog entry * Improve error messages
1 parent c738f75 commit ead028b

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
#### :nail_care: Polish
3232

33+
- Read package name from rescript.json if package.json is absent. https://github.com/rescript-lang/rescript/pull/7746
34+
3335
#### :house: Internal
3436

3537
- Add token viewer to `res_parser`. https://github.com/rescript-lang/rescript/pull/7751

rewatch/src/build/packages.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,27 @@ fn flatten_dependencies(dependencies: Vec<Dependency>) -> Vec<Dependency> {
397397
}
398398

399399
pub fn read_package_name(package_dir: &Path) -> Result<String> {
400-
let package_json_path = package_dir.join("package.json");
400+
let mut file_name = "package.json";
401+
let package_json_path = package_dir.join(file_name);
401402

402-
let package_json_contents =
403-
fs::read_to_string(&package_json_path).map_err(|e| anyhow!("Could not read package.json: {}", e))?;
403+
let package_json_contents = if Path::exists(&package_json_path) {
404+
fs::read_to_string(&package_json_path).map_err(|e| anyhow!("Could not read package.json: {}", e))?
405+
} else {
406+
let rescript_json_path = package_dir.join("rescript.json");
407+
if Path::exists(&rescript_json_path) {
408+
file_name = "rescript.json";
409+
fs::read_to_string(&rescript_json_path)
410+
.map_err(|e| anyhow!("Could not read rescript.json: {}", e))?
411+
} else {
412+
return Err(anyhow!(
413+
"There is no package.json or rescript.json file in {}",
414+
package_dir.to_string_lossy()
415+
));
416+
}
417+
};
404418

405419
let package_json: serde_json::Value = serde_json::from_str(&package_json_contents)
406-
.map_err(|e| anyhow!("Could not parse package.json: {}", e))?;
420+
.map_err(|e| anyhow!("Could not parse {}: {}", file_name, e))?;
407421

408422
package_json["name"]
409423
.as_str()

0 commit comments

Comments
 (0)