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

Commit 80110c2

Browse files
authored
Resolve bit components from package json (#125)
* resolve bit components by package.json only without resolving their paths * bump version and update CHANGELOG
1 parent 067a35b commit 80110c2

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
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.1.6-dev.1] - 2020-01-25
11+
12+
- fix components dependencies detection to resolve from package.json if not exist on the fs
13+
1014
## [2.1.5] - 2020-01-12
1115

1216
### Bug fixes

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.1.5",
3+
"version": "2.1.6-dev.1",
44
"scripts": {
55
"lint": "tsc && eslint \"src/**/*.ts\"",
66
"lint-circle": "eslint \"src/**/*.ts\" --format junit -o junit/eslint-results.xml",

src/dependency-builder/build-tree.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ function groupDependencyTree(tree, cwd, bindingPrefix) {
161161
}
162162

163163
/**
164-
* Get an import statement path to node package and return the package name
164+
* return the package name by the import statement path to node package
165165
*
166166
* @param {string} packagePath import statement path
167167
* @returns {string} name of the package
@@ -220,6 +220,10 @@ function findPackagesInPackageJson(packageJson: Record<string, any>, packagesNam
220220
}
221221
return { foundPackages: {}, missingPackages: packagesNames };
222222
}
223+
224+
type Missing = { [absolutePath: string]: string[] }; // e.g. { '/tmp/workspace': ['lodash', 'ramda'] };
225+
type MissingGroupItem = { originFile: string; packages?: string[]; bits?: string[]; files?: string[] };
226+
type FoundPackages = { [packageName: string]: string };
223227
/**
224228
* Run over each entry in the missing array and transform the missing from list of paths
225229
* to object with missing types
@@ -230,7 +234,12 @@ function findPackagesInPackageJson(packageJson: Record<string, any>, packagesNam
230234
* @param {string} bindingPrefix
231235
* @returns new object with grouped missing
232236
*/
233-
function groupMissing(missing, cwd, consumerPath, bindingPrefix) {
237+
function groupMissing(
238+
missing: Missing,
239+
cwd,
240+
consumerPath,
241+
bindingPrefix
242+
): { missingGroups: MissingGroupItem[]; foundPackages: FoundPackages } {
234243
// temporarily disable this functionality since it cause few bugs: explanation below (on using the packageJson)
235244
// const packageJson = PackageJson.findPackage(cwd);
236245

@@ -243,27 +252,26 @@ function groupMissing(missing, cwd, consumerPath, bindingPrefix) {
243252
if (item.startsWith(`${bindingPrefix}/`) || item.startsWith(`${DEFAULT_BINDINGS_PREFIX}/`)) return 'bits';
244253
return item.startsWith('.') ? 'files' : 'packages';
245254
});
246-
const groups = Object.keys(missing).map(key =>
255+
const groups: MissingGroupItem[] = Object.keys(missing).map(key =>
247256
Object.assign({ originFile: processPath(key, {}, cwd) }, byPathType(missing[key], bindingPrefix))
248257
);
249-
groups.forEach(group => {
258+
groups.forEach((group: MissingGroupItem) => {
250259
if (group.packages) group.packages = group.packages.map(resolvePackageNameByPath);
260+
if (group.bits) group.bits = group.bits.map(resolvePackageNameByPath);
251261
});
252262
// This is a hack to solve problems that madge has with packages for type script files
253263
// It see them as missing even if they are exists
254264
const foundPackages = {};
255265
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
256266
const packageJson = PackageJson.findPackage(cwd);
257-
258-
groups.forEach(group => {
259-
const missingPackages = [];
267+
groups.forEach((group: MissingGroupItem) => {
268+
const missingPackages: string[] = [];
260269
if (group.packages) {
261270
group.packages.forEach(packageName => {
262-
// Don't add the same package twice
271+
// Don't try to resolve the same package twice
263272
if (R.contains(packageName, missingPackages)) return;
264273
const resolvedPath = resolveModulePath(packageName, cwd, consumerPath);
265274
if (!resolvedPath) {
266-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
267275
missingPackages.push(packageName);
268276
return;
269277
}
@@ -279,6 +287,13 @@ function groupMissing(missing, cwd, consumerPath, bindingPrefix) {
279287
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
280288
groups.packages = result.missingPackages;
281289
Object.assign(foundPackages, result.foundPackages);
290+
291+
if (group.bits) {
292+
const foundBits = findPackagesInPackageJson(packageJson, group.bits);
293+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
294+
groups.bits = foundBits.missingPackages;
295+
Object.assign(foundPackages, foundBits.foundPackages);
296+
}
282297
}
283298
});
284299

@@ -358,16 +373,23 @@ function getResolveConfigAbsolute(
358373
return resolveConfigAbsolute;
359374
}
360375

361-
function mergeManuallyFoundPackagesToTree(foundPackages, groups, tree: Tree) {
376+
function mergeManuallyFoundPackagesToTree(foundPackages: FoundPackages, missingGroups: MissingGroupItem[], tree: Tree) {
362377
if (R.isEmpty(foundPackages)) return;
363378
// Merge manually found packages (by groupMissing()) with the packages found by Madge (generate-tree-madge)
364379
Object.keys(foundPackages).forEach(pkg => {
365380
// locate package in groups(contains missing)
366-
groups.forEach(fileDep => {
381+
missingGroups.forEach((fileDep: MissingGroupItem) => {
367382
if (fileDep.packages && fileDep.packages.includes(pkg)) {
368383
fileDep.packages = fileDep.packages.filter(packageName => packageName !== pkg);
369384
lset(tree[fileDep.originFile], ['packages', pkg], foundPackages[pkg]);
370385
}
386+
if (fileDep.bits && fileDep.bits.includes(pkg)) {
387+
fileDep.bits = fileDep.bits.filter(packageName => packageName !== pkg);
388+
if (!tree[fileDep.originFile]) tree[fileDep.originFile] = {};
389+
if (!tree[fileDep.originFile].bits) tree[fileDep.originFile].bits = [];
390+
// @ts-ignore
391+
tree[fileDep.originFile].bits.push(pkg);
392+
}
371393
});
372394
});
373395
}

0 commit comments

Comments
 (0)