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

Resolve bit components from package json #125

Merged
merged 2 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased]

## [2.1.6-dev.1] - 2020-01-25

- fix components dependencies detection to resolve from package.json if not exist on the fs

## [2.1.5] - 2020-01-12

### Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bit-javascript",
"version": "2.1.5",
"version": "2.1.6-dev.1",
"scripts": {
"lint": "tsc && eslint \"src/**/*.ts\"",
"lint-circle": "eslint \"src/**/*.ts\" --format junit -o junit/eslint-results.xml",
Expand Down
44 changes: 33 additions & 11 deletions src/dependency-builder/build-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ function groupDependencyTree(tree, cwd, bindingPrefix) {
}

/**
* Get an import statement path to node package and return the package name
* return the package name by the import statement path to node package
*
* @param {string} packagePath import statement path
* @returns {string} name of the package
Expand Down Expand Up @@ -220,6 +220,10 @@ function findPackagesInPackageJson(packageJson: Record<string, any>, packagesNam
}
return { foundPackages: {}, missingPackages: packagesNames };
}

type Missing = { [absolutePath: string]: string[] }; // e.g. { '/tmp/workspace': ['lodash', 'ramda'] };
type MissingGroupItem = { originFile: string; packages?: string[]; bits?: string[]; files?: string[] };
type FoundPackages = { [packageName: string]: string };
/**
* Run over each entry in the missing array and transform the missing from list of paths
* to object with missing types
Expand All @@ -230,7 +234,12 @@ function findPackagesInPackageJson(packageJson: Record<string, any>, packagesNam
* @param {string} bindingPrefix
* @returns new object with grouped missing
*/
function groupMissing(missing, cwd, consumerPath, bindingPrefix) {
function groupMissing(
missing: Missing,
cwd,
consumerPath,
bindingPrefix
): { missingGroups: MissingGroupItem[]; foundPackages: FoundPackages } {
// temporarily disable this functionality since it cause few bugs: explanation below (on using the packageJson)
// const packageJson = PackageJson.findPackage(cwd);

Expand All @@ -243,27 +252,26 @@ function groupMissing(missing, cwd, consumerPath, bindingPrefix) {
if (item.startsWith(`${bindingPrefix}/`) || item.startsWith(`${DEFAULT_BINDINGS_PREFIX}/`)) return 'bits';
return item.startsWith('.') ? 'files' : 'packages';
});
const groups = Object.keys(missing).map(key =>
const groups: MissingGroupItem[] = Object.keys(missing).map(key =>
Object.assign({ originFile: processPath(key, {}, cwd) }, byPathType(missing[key], bindingPrefix))
);
groups.forEach(group => {
groups.forEach((group: MissingGroupItem) => {
if (group.packages) group.packages = group.packages.map(resolvePackageNameByPath);
if (group.bits) group.bits = group.bits.map(resolvePackageNameByPath);
});
// This is a hack to solve problems that madge has with packages for type script files
// It see them as missing even if they are exists
const foundPackages = {};
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
const packageJson = PackageJson.findPackage(cwd);

groups.forEach(group => {
const missingPackages = [];
groups.forEach((group: MissingGroupItem) => {
const missingPackages: string[] = [];
if (group.packages) {
group.packages.forEach(packageName => {
// Don't add the same package twice
// Don't try to resolve the same package twice
if (R.contains(packageName, missingPackages)) return;
const resolvedPath = resolveModulePath(packageName, cwd, consumerPath);
if (!resolvedPath) {
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
missingPackages.push(packageName);
return;
}
Expand All @@ -279,6 +287,13 @@ function groupMissing(missing, cwd, consumerPath, bindingPrefix) {
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
groups.packages = result.missingPackages;
Object.assign(foundPackages, result.foundPackages);

if (group.bits) {
const foundBits = findPackagesInPackageJson(packageJson, group.bits);
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
groups.bits = foundBits.missingPackages;
Object.assign(foundPackages, foundBits.foundPackages);
}
}
});

Expand Down Expand Up @@ -358,16 +373,23 @@ function getResolveConfigAbsolute(
return resolveConfigAbsolute;
}

function mergeManuallyFoundPackagesToTree(foundPackages, groups, tree: Tree) {
function mergeManuallyFoundPackagesToTree(foundPackages: FoundPackages, missingGroups: MissingGroupItem[], tree: Tree) {
if (R.isEmpty(foundPackages)) return;
// Merge manually found packages (by groupMissing()) with the packages found by Madge (generate-tree-madge)
Object.keys(foundPackages).forEach(pkg => {
// locate package in groups(contains missing)
groups.forEach(fileDep => {
missingGroups.forEach((fileDep: MissingGroupItem) => {
if (fileDep.packages && fileDep.packages.includes(pkg)) {
fileDep.packages = fileDep.packages.filter(packageName => packageName !== pkg);
lset(tree[fileDep.originFile], ['packages', pkg], foundPackages[pkg]);
}
if (fileDep.bits && fileDep.bits.includes(pkg)) {
fileDep.bits = fileDep.bits.filter(packageName => packageName !== pkg);
if (!tree[fileDep.originFile]) tree[fileDep.originFile] = {};
if (!tree[fileDep.originFile].bits) tree[fileDep.originFile].bits = [];
// @ts-ignore
tree[fileDep.originFile].bits.push(pkg);
}
});
});
}
Expand Down