@@ -41,20 +41,42 @@ func (i *Inspector) InspectProject(ctx context.Context) error {
4141
4242func ScanNpmProject (ctx context.Context ) ([]model.Module , error ) {
4343 dir := model .UseInspectionTask (ctx ).Dir ()
44- logger := logctx .Use (ctx )
4544 pkgFile := filepath .Join (dir , "package-lock.json" )
45+ module := model.Module {
46+ PackageManager : "npm" ,
47+ ModuleName : "" ,
48+ ModuleVersion : "" ,
49+ ModulePath : pkgFile ,
50+ }
51+ logger := logctx .Use (ctx )
4652 logger .Debug ("Read package-lock.json" , zap .String ("path" , pkgFile ))
4753 data , e := os .ReadFile (pkgFile )
4854 if e != nil {
4955 return nil , errors .WithMessage (e , "Errors when reading package-lock.json" )
5056 }
57+ lockfileVer , e := parseLockfileVersion (data )
58+ if e != nil {
59+ return nil , e
60+ }
61+ if lockfileVer == 3 {
62+ parsed , e := parseLockfileV3 (data )
63+ if e != nil {
64+ return nil , fmt .Errorf ("v3lockfile: %w" , e )
65+ }
66+ module .ModuleName = parsed .Name
67+ module .ModuleVersion = parsed .Version
68+ module .Dependencies = parsed .Deps
69+ return []model.Module {module }, nil
70+ }
5171 var lockfile NpmPkgLock
5272 if e := json .Unmarshal (data , & lockfile ); e != nil {
5373 return nil , e
5474 }
5575 if lockfile .LockfileVersion > 2 || lockfile .LockfileVersion < 1 {
5676 return nil , errors .New (fmt .Sprintf ("unsupported lockfileVersion: %d" , lockfile .LockfileVersion ))
5777 }
78+ module .ModuleName = lockfile .Name
79+ module .ModuleVersion = lockfile .Version
5880 for s := range lockfile .Dependencies {
5981 if strings .HasPrefix (s , "node_modules/" ) {
6082 delete (lockfile .Dependencies , s )
@@ -82,12 +104,7 @@ func ScanNpmProject(ctx context.Context) ([]model.Module, error) {
82104 if len (rootComp ) == 0 {
83105 logger .Warn ("Not found root component" )
84106 }
85- module := model.Module {
86- PackageManager : "npm" ,
87- ModuleName : lockfile .Name ,
88- ModuleVersion : lockfile .Version ,
89- ModulePath : filepath .Join (dir , "package-lock.json" ),
90- }
107+
91108 m := map [string ]int {}
92109 for _ , it := range rootComp {
93110 if d := _convDep (it , lockfile , m , 0 ); d != nil {
@@ -132,7 +149,7 @@ func _convDep(root string, m NpmPkgLock, visited map[string]int, deep int) *mode
132149type NpmPkgLock struct {
133150 Name string `json:"name"`
134151 Version string `json:"version"`
135- LockfileVersion int `json:"LockfileVersion "`
152+ LockfileVersion int `json:"lockfileVersion "`
136153 Dependencies map [string ]struct {
137154 Version string `json:"version"`
138155 Requires map [string ]interface {} `json:"requires"`
@@ -143,3 +160,14 @@ var EcoRepo = model.EcoRepo{
143160 Ecosystem : "npm" ,
144161 Repository : "" ,
145162}
163+
164+ func parseLockfileVersion (data []byte ) (int , error ) {
165+ type unknownVersionLockfile struct {
166+ LockfileVersion int `json:"lockfileVersion"`
167+ }
168+ var u unknownVersionLockfile
169+ if e := json .Unmarshal (data , & u ); e != nil {
170+ return 0 , fmt .Errorf ("parse lockfile version failed: %w" , e )
171+ }
172+ return u .LockfileVersion , nil
173+ }
0 commit comments