Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#### :nail_care: Polish

- Read package name from rescript.json if package.json is absent. https://github.com/rescript-lang/rescript/pull/7746

#### :house: Internal

- Add token viewer to `res_parser`. https://github.com/rescript-lang/rescript/pull/7751
Expand Down
22 changes: 18 additions & 4 deletions rewatch/src/build/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,27 @@ fn flatten_dependencies(dependencies: Vec<Dependency>) -> Vec<Dependency> {
}

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

let package_json_contents =
fs::read_to_string(&package_json_path).map_err(|e| anyhow!("Could not read package.json: {}", e))?;
let package_json_contents = if Path::exists(&package_json_path) {
fs::read_to_string(&package_json_path).map_err(|e| anyhow!("Could not read package.json: {}", e))?
} else {
let rescript_json_path = package_dir.join("rescript.json");
if Path::exists(&rescript_json_path) {
file_name = "rescript.json";
fs::read_to_string(&rescript_json_path)
.map_err(|e| anyhow!("Could not read rescript.json: {}", e))?
} else {
return Err(anyhow!(
"There is no package.json or rescript.json file in {}",
package_dir.to_string_lossy()
));
}
};

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

package_json["name"]
.as_str()
Expand Down
Loading