@@ -5,7 +5,7 @@ use crate::config;
5
5
use crate :: helpers;
6
6
use crate :: helpers:: emojis:: * ;
7
7
use ahash:: { AHashMap , AHashSet } ;
8
- use anyhow:: Result ;
8
+ use anyhow:: { anyhow , Result } ;
9
9
use console:: style;
10
10
use log:: { debug, error} ;
11
11
use rayon:: prelude:: * ;
@@ -374,6 +374,25 @@ fn flatten_dependencies(dependencies: Vec<Dependency>) -> Vec<Dependency> {
374
374
flattened
375
375
}
376
376
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
+
377
396
fn make_package ( config : config:: Config , package_path : & str , is_pinned_dep : bool , is_root : bool ) -> Package {
378
397
let source_folders = match config. sources . to_owned ( ) {
379
398
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,
397
416
} ;
398
417
399
418
Package {
400
- name : config . name . to_owned ( ) ,
419
+ name : read_package_name ( package_path ) . expect ( "Could not read package name" ) ,
401
420
config : config. to_owned ( ) ,
402
421
source_folders,
403
422
source_files : None ,
0 commit comments