@@ -74,9 +74,7 @@ export function resolveNodePackage(cwd: string, packageFullPath: string): Object
7474 }
7575
7676 // Get the package relative path to the node_modules dir
77- const indexOfLastNodeModules = packageFullPath . lastIndexOf ( NODE_MODULES ) + NODE_MODULES . length + 1 ;
78- const indexOfPackageFolderEnd = packageFullPath . indexOf ( path . sep , indexOfLastNodeModules ) ;
79- const packageDir = packageFullPath . substring ( 0 , indexOfPackageFolderEnd ) ;
77+ const packageDir = resolvePackageDirFromFilePath ( packageFullPath ) ;
8078
8179 // don't propagate here since loading a package.json of another folder and taking the version from it will result wrong version
8280 // This for example happen in the following case:
@@ -92,6 +90,27 @@ export function resolveNodePackage(cwd: string, packageFullPath: string): Object
9290 return result ;
9391}
9492
93+ /**
94+ * given the full path of a package file, returns the root dir of the package, so then we could
95+ * find the package.json in that directory.
96+ *
97+ * example of a normal package:
98+ * absolutePackageFilePath: /user/workspace/node_modules/lodash.isboolean/index.js
99+ * returns: /user/workspace/node_modules/lodash.isboolean
100+ *
101+ * example of a scoped package:
102+ * absolutePackageFilePath: /user/workspace/node_modules/@babel/core/lib/index.js
103+ * returns: /user/workspace/node_modules/@babel/core
104+ */
105+ function resolvePackageDirFromFilePath ( absolutePackageFilePath : string ) : string {
106+ const NODE_MODULES = 'node_modules' ;
107+ const indexOfLastNodeModules = absolutePackageFilePath . lastIndexOf ( NODE_MODULES ) + NODE_MODULES . length + 1 ;
108+ const pathInsideNodeModules = absolutePackageFilePath . substring ( indexOfLastNodeModules ) ;
109+ const packageName = resolvePackageNameByPath ( pathInsideNodeModules ) ;
110+ const pathUntilNodeModules = absolutePackageFilePath . substring ( 0 , indexOfLastNodeModules ) ;
111+ return pathUntilNodeModules + packageName ;
112+ }
113+
95114/**
96115 * Gets a list of dependencies and group them by types (files, bits, packages)
97116 * It's also transform the node package dependencies from array of paths to object in this format:
0 commit comments