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

Commit d192a29

Browse files
amitgilad3David First
authored andcommitted
Feature/missing deps origin (#48)
* resolve missing deps origin * change nonExistent from array to map in order to support specifying origin of missing dependency * added support for yarn workspaces * add flag to load package.json and not throw exception if it dosent exsit * package.json fix * resolve package dependencies to files
1 parent 809529c commit d192a29

File tree

4 files changed

+55
-46
lines changed

4 files changed

+55
-46
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"test": "jest",
88
"test-watch": "jest --watch",
99
"clean": "rm -rf dist",
10-
"build": "babel src -d dist",
10+
"build": "babel src -d dist --source-maps",
1111
"watch": "babel --watch src -d dist --source-maps",
1212
"prepublishOnly": "npm run clean && npm run build",
1313
"build-debug": "babel src -d dist --source-maps"
@@ -49,6 +49,7 @@
4949
"is-relative-path": "^2.0.0",
5050
"lodash.flatten": "^4.4.0",
5151
"lodash.partition": "^4.6.0",
52+
"lodash.set": "^4.3.2",
5253
"module-definition": "^2.2.4",
5354
"module-lookup-amd": "^5.0.1",
5455
"node-source-walk": "^3.3.0",

src/dependency-builder/build-tree.js

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import R from 'ramda';
77
import generateTree from './generate-tree-madge';
88
import PackageJson from '../package-json/package-json';
99
import partition from 'lodash.partition';
10+
import lset from 'lodash.set';
1011
import { DEFAULT_BINDINGS_PREFIX } from '../constants';
1112

1213
/**
@@ -233,34 +234,42 @@ function groupMissing(missing, cwd, consumerPath, bindingPrefix) {
233234
if (item.startsWith(`${bindingPrefix}/`) || item.startsWith(`${DEFAULT_BINDINGS_PREFIX}/`)) return 'bits';
234235
return item.startsWith('.') ? 'files' : 'packages';
235236
});
236-
const groups = byPathType(missing, bindingPrefix);
237-
const packages = groups.packages ? groups.packages.map(resolvePackageNameByPath) : [];
237+
const groups = Object.keys(missing).map(key => Object.assign({ originFile: path.relative(cwd, key) }, byPathType(missing[key], bindingPrefix)));
238+
groups.forEach((group) => {
239+
if (group.packages) group.packages = group.packages.map(resolvePackageNameByPath);
240+
});
238241
// This is a hack to solve problems that madge has with packages for type script files
239242
// It see them as missing even if they are exists
240243
const foundPackages = {};
241-
const missingPackages = [];
242-
packages.forEach((packageName) => {
243-
// Don't add the same package twice
244-
if (R.contains(packageName, missingPackages)) return;
245-
const resolvedPath = resolveModulePath(packageName, cwd, consumerPath);
246-
if (!resolvedPath) {
247-
return missingPackages.push(packageName);
248-
}
249-
const packageWithVersion = resolveNodePackage(cwd, resolvedPath);
244+
const packageJson = PackageJson.findPackage(cwd);
245+
246+
groups.forEach((group) => {
247+
const missingPackages = [];
248+
if (group.packages) {
249+
group.packages.forEach((packageName) => {
250+
// Don't add the same package twice
251+
if (R.contains(packageName, missingPackages)) return;
252+
const resolvedPath = resolveModulePath(packageName, cwd, consumerPath);
253+
if (!resolvedPath) {
254+
return missingPackages.push(packageName);
255+
}
256+
const packageWithVersion = resolveNodePackage(cwd, resolvedPath);
250257

251-
return packageWithVersion ? Object.assign(foundPackages, packageWithVersion) :
252-
missingPackages.push(packageWithVersion);
258+
return packageWithVersion ? Object.assign(foundPackages, packageWithVersion) :
259+
missingPackages.push(packageWithVersion);
260+
});
261+
}
262+
if (packageJson) {
263+
const result = findPackagesInPackageJson(packageJson, missingPackages);
264+
groups.packages = result.missingPackages;
265+
Object.assign(foundPackages, result.foundPackages);
266+
}
253267
});
254-
groups.packages = missingPackages;
255268

256269
// temporarily disable this functionality since it cause this bugs:
257270
// https://github.com/teambit/bit/issues/635
258271
// https://github.com/teambit/bit/issues/690
259-
// if (packageJson) {
260-
// const result = findPackagesInPackageJson(packageJson, missingPackages);
261-
// groups.packages = result.missingPackages;
262-
// Object.assign(foundPackages, result.foundPackages)
263-
// }
272+
264273

265274
return { groups, foundPackages };
266275
}
@@ -362,23 +371,23 @@ function updateTreeWithLinkFilesAndImportSpecifiers(tree: Tree, pathMap: PathMap
362371
* @param bindingPrefix
363372
* @return {Promise<{missing, tree}>}
364373
*/
365-
export default async function getDependencyTree(baseDir: string, consumerPath: string, filePaths: string[], bindingPrefix: string):
366-
Promise<{ missing: Object, tree: Tree}> {
374+
export default async function getDependencyTree(baseDir: string, consumerPath: string, filePaths: string[], bindingPrefix: string): Promise<{ missing: Object, tree: Tree}> {
367375
const config = { baseDir, includeNpm: true, requireConfig: null, webpackConfig: null, visited: {}, nonExistent: [] };
368376
const result = generateTree(filePaths, config);
369377
const { groups, foundPackages } = groupMissing(result.skipped, baseDir, consumerPath, bindingPrefix);
370378
const tree: Tree = groupDependencyTree(result.tree, baseDir, bindingPrefix);
371-
const relativeFilePaths = filePaths.map(filePath => path.relative(baseDir, filePath));
379+
// const relativeFilePaths = filePaths.map(filePath => path.relative(baseDir, filePath));
372380
// Merge manually found packages with madge founded packages
373381
if (foundPackages && !R.isEmpty(foundPackages)) {
374382
// Madge found packages so we need to merge them with the manual
375-
relativeFilePaths.forEach((relativeFilePath) => {
376-
if (tree[relativeFilePath].packages) {
377-
Object.assign(tree[relativeFilePath].packages, foundPackages);
378-
// There is only manually found packages
379-
} else {
380-
tree[relativeFilePath].packages = foundPackages;
381-
}
383+
Object.keys(foundPackages).forEach((pkg) => {
384+
// locate package in groups(contains missing)
385+
groups.forEach((fileDep) => {
386+
if (fileDep.packages && fileDep.packages.includes(pkg)) {
387+
fileDep.packages = fileDep.packages.filter(packageName => packageName !== pkg);
388+
lset(tree[fileDep['originFile']],`packages.${pkg}`, foundPackages[pkg]);
389+
}
390+
});
382391
});
383392
}
384393

src/dependency-builder/dependency-tree/index.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ module.exports = function(options) {
3535
var results = traverse(config);
3636
debug('traversal complete', results);
3737

38-
dedupeNonExistent(config.nonExistent);
3938
debug('deduped list of nonExistent partials: ', config.nonExistent);
4039

4140
var tree;
@@ -100,13 +99,13 @@ module.exports._getDependencies = function(config) {
10099

101100
var resolvedDependencies = [];
102101
var pathMapDependencies = [];
103-
var pathMapFile = { file: config.filename };
102+
var pathMapFile = {file: config.filename};
104103

105104
for (var i = 0, l = dependencies.length; i < l; i++) {
106105
var dep = dependencies[i];
107106
const isVue = (!Array.isArray(dep) && dep.isScript);
108107
var result;
109-
if (!Array.isArray(dep) && dep.isScript!== undefined) { // used for vue - return array of objects
108+
if (!Array.isArray(dep) && dep.isScript !== undefined) { // used for vue - return array of objects
110109
result = cabinet({
111110
partial: dep.dep,
112111
filename: config.filename,
@@ -126,20 +125,29 @@ module.exports._getDependencies = function(config) {
126125
webpackConfig: config.webpackConfig
127126
});
128127
}
128+
const dependency = isVue ? dep.dep : dep;
129129
if (!result) {
130130
debug('skipping an empty filepath resolution for partial: ' + dep);
131-
config.nonExistent.push(isVue? dep.dep : dep);
131+
if (config.nonExistent[config.filename]) {
132+
config.nonExistent[config.filename].push(dependency);
133+
} else {
134+
config.nonExistent[config.filename] = [dependency];
135+
}
132136
continue;
133137
}
134138

135139
var exists = fs.existsSync(result);
136140

137141
if (!exists) {
138-
config.nonExistent.push(isVue? dep.dep : dep);
142+
if (config.nonExistent[config.filename]) {
143+
config.nonExistent[config.filename].push(dependency);
144+
} else {
145+
config.nonExistent[config.filename] = [dependency];
146+
}
139147
debug('skipping non-empty but non-existent resolution: ' + result + ' for partial: ' + dep);
140148
continue;
141149
}
142-
var pathMap = { dep: isVue ? dep.dep : dep , resolvedDep: result};
150+
var pathMap = {dep: dependency , resolvedDep: result};
143151
var importSpecifiersSupportedLang = ['es6', 'ts', 'stylable'];
144152
importSpecifiersSupportedLang.forEach((lang) => {
145153
if (precinctOptions[lang] && precinctOptions[lang].importSpecifiers && precinctOptions[lang].importSpecifiers[dep]) {
@@ -232,12 +240,3 @@ function removeDups(list) {
232240
return unique;
233241
}
234242

235-
// Mutate the list input to do a dereferenced modification of the user-supplied list
236-
function dedupeNonExistent(nonExistent) {
237-
var deduped = removeDups(nonExistent);
238-
nonExistent.length = deduped.length;
239-
240-
for (var i = 0, l = deduped.length; i < l; i++) {
241-
nonExistent[i] = deduped[i];
242-
}
243-
}

src/dependency-builder/generate-tree-madge.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function addRelativePathsToPathMap(pathMap, pathCache, baseDir) {
138138
export default function generateTree(files, config) {
139139
const depTree = {};
140140
const visited = {};
141-
const nonExistent = [];
141+
const nonExistent = {};
142142
const npmPaths = {};
143143
const pathCache = {};
144144
const pathMap = [];

0 commit comments

Comments
 (0)