Skip to content
This repository was archived by the owner on Dec 4, 2022. It is now read-only.

Commit 74336ce

Browse files
author
David First
authored
support scoped packages when resolving package.json directory of a package (#95)
1 parent e5c4819 commit 74336ce

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [unreleased]
99

10+
## [2.0.4-dev.1] - 2019-03-01
11+
12+
- support scoped packages when resolving package.json directory of a package
13+
1014
## [2.0.3] - 2019-02-24
1115

1216
- upgrade to babel 7

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bit-javascript",
3-
"version": "2.0.3",
3+
"version": "2.0.4-dev.1",
44
"scripts": {
55
"flow": "flow; test $? -eq 0 -o $? -eq 2",
66
"lint": "eslint src && flow check || true",

src/dependency-builder/build-tree.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)