Skip to content

Commit 53e81a8

Browse files
authored
Fix when package name is different than rescript config name (#156)
1 parent fd7930f commit 53e81a8

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/build/packages.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::config;
55
use crate::helpers;
66
use crate::helpers::emojis::*;
77
use ahash::{AHashMap, AHashSet};
8-
use anyhow::Result;
8+
use anyhow::{anyhow, Result};
99
use console::style;
1010
use log::{debug, error};
1111
use rayon::prelude::*;
@@ -374,6 +374,25 @@ fn flatten_dependencies(dependencies: Vec<Dependency>) -> Vec<Dependency> {
374374
flattened
375375
}
376376

377+
fn read_package_name(package_dir: &str) -> Result<String> {
378+
let package_json_path = if package_dir.is_empty() {
379+
"package.json".to_string()
380+
} else {
381+
format!("{}/package.json", package_dir)
382+
};
383+
384+
let package_json_contents =
385+
fs::read_to_string(&package_json_path).map_err(|e| anyhow!("Could not read package.json: {}", e))?;
386+
387+
let package_json: serde_json::Value = serde_json::from_str(&package_json_contents)
388+
.map_err(|e| anyhow!("Could not parse package.json: {}", e))?;
389+
390+
package_json["name"]
391+
.as_str()
392+
.map(|s| s.to_string())
393+
.ok_or_else(|| anyhow!("No name field found in package.json"))
394+
}
395+
377396
fn make_package(config: config::Config, package_path: &str, is_pinned_dep: bool, is_root: bool) -> Package {
378397
let source_folders = match config.sources.to_owned() {
379398
Some(config::OneOrMore::Single(source)) => get_source_dirs(source, None),
@@ -397,7 +416,7 @@ fn make_package(config: config::Config, package_path: &str, is_pinned_dep: bool,
397416
};
398417

399418
Package {
400-
name: config.name.to_owned(),
419+
name: read_package_name(package_path).expect("Could not read package name"),
401420
config: config.to_owned(),
402421
source_folders,
403422
source_files: None,

0 commit comments

Comments
 (0)