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

Commit 7c3b0c7

Browse files
committed
resolve-authored-dep-version
1 parent 7dfad45 commit 7c3b0c7

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/dependency-builder/build-tree.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export function resolveNodePackage(cwd: string, packageFullPath: string): Object
9696
if (packageJsonInfo) {
9797
// The +1 is for the / after the node_modules, we didn't enter it into the NODE_MODULES const because it makes problems on windows
9898
const packageRelativePath = packageFullPath.substring(packageFullPath.lastIndexOf(NODE_MODULES) + NODE_MODULES.length + 1, packageFullPath.length);
99+
99100
const packageName = resolvePackageNameByPath(packageRelativePath);
100101
const packageVersion = R.path(['dependencies', packageName], packageJsonInfo) ||
101102
R.path(['devDependencies', packageName], packageJsonInfo) ||
@@ -105,9 +106,20 @@ export function resolveNodePackage(cwd: string, packageFullPath: string): Object
105106
return result;
106107
}
107108
}
108-
// Get the package relative path to the node_modules dir
109109

110-
const packageInfo = PackageJson.findPackage(packageFullPath);
110+
// Get the package relative path to the node_modules dir
111+
let packageDir = packageFullPath;
112+
// Check if the full path is path to the index file and not only to the directory
113+
const stats = fs.statSync(packageFullPath);
114+
if (stats.isFile()) {
115+
packageDir = path.dirname(packageFullPath);
116+
}
117+
// don't propagate here since loading a package.json of another folder and taking the version from it will result wrong version
118+
// This for example happen in the following case:
119+
// if you have 2 authored component which one dependet on the other
120+
// we will look for the package.json on the dependency but won't find it
121+
// if we propagate we will take the version from the root's package json which has nothing with the component version
122+
const packageInfo = PackageJson.loadSync(packageDir, false);
111123
if (!packageInfo) return null; // when running 'bitjs get-dependencies' command, packageInfo is sometimes empty
112124
result[packageInfo.name] = packageInfo.version;
113125
return result;

src/package-json/package-json.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,18 @@ export default class PackageJson {
121121

122122
static async load(componentRootFolder: string, throwError: boolean = true): Promise<PackageJson> {
123123
const composedPath = composePath(componentRootFolder);
124-
if(!PackageJson.hasExisting(componentRootFolder, throwError)) return null;
124+
if (!PackageJson.hasExisting(componentRootFolder, throwError)) return null;
125125
const componentJsonObject = await fs.readJson(composedPath);
126126
return new PackageJson(componentRootFolder, componentJsonObject);
127127
}
128128

129+
static loadSync(componentRootFolder: string, throwError: boolean = true): PackageJson {
130+
const composedPath = composePath(componentRootFolder);
131+
if (!PackageJson.hasExisting(componentRootFolder, throwError)) return null;
132+
const componentJsonObject = fs.readJsonSync(composedPath);
133+
return new PackageJson(componentRootFolder, componentJsonObject);
134+
}
135+
129136
/**
130137
* Taken from this package (with some minor changes):
131138
* https://www.npmjs.com/package/find-package

0 commit comments

Comments
 (0)