@@ -5,7 +5,7 @@ use crate::config;
55use crate :: helpers;
66use crate :: helpers:: emojis:: * ;
77use ahash:: { AHashMap , AHashSet } ;
8- use anyhow:: Result ;
8+ use anyhow:: { anyhow , Result } ;
99use console:: style;
1010use log:: { debug, error} ;
1111use 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+
377396fn 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