File size: 4,969 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
const path = require( 'path' );
const debug = require( 'debug' )( 'effective-module-tree' );
const treeify = require( 'object-treeify' );
/**
* Generator that yields all places were node will look for modules
* For a path like : a/node_modules/b/node_modules/c
* It will generate the candidates:
* - a/node_modules/b/node_modules/c/node_modules
* - a/node_modules/b/node_modules
* - a/node_modules
* - node_modules
* @param {string} packagePath Package path used as seed for the traversal
* @yields Path to `node_modules`.
*/
const candidates = function* ( packagePath ) {
const parts = packagePath.split( path.sep );
for ( let i = parts.length; i >= 0; i-- ) {
// This avoids generating .../node_modules/node_modules/...
if ( parts[ i - 1 ] === 'node_modules' ) {
continue;
}
// Need to prepend path.sep manually because path.join ignores empty path segments,
// so it removes the "empty" segment at the beginning of an absolute path.
yield path.join( path.sep, ...parts.slice( 0, i ), 'node_modules' );
}
};
/**
* Recursively finds the dependency tree for a package and its dependencies
* @param {Object} packageJson package.json content of the package
* @param {string} packagePath Location of package.json, used to decide where to look for deps
* @param {string[]} parents List of parent dependencies already visited, used to avoid circular loops
* @param {Map} cache Map used to cache already resolved paths of the dependency tree
*/
const findTree = ( packageJson, packagePath, parents, cache ) => {
const name = `${ packageJson.name }@${ packageJson.version }`;
if ( parents.includes( name ) ) {
debug( `Package ${ name } at ${ packagePath } seems to be a circular dependency!` );
return { tree: { [ name ]: '[Circular]' }, isCacheable: false };
}
// We alredy solved this part of the tree
if ( cache.has( packagePath ) ) {
debug(
`Package ${ name } at ${ packagePath } was already resolved, returning info from cache`
);
return {
tree: cache.get( packagePath ),
isCacheable: true,
};
}
// For each dependency...
debug( `Finding dependencies for ${ name } at ${ packagePath }` );
let treeIsCacheable = true;
const dependencies = Object.keys( packageJson.dependencies || [] ).reduce(
( accumulated, dependency ) => {
let dependencyPath;
let dependencyJson;
// Loop over all possible locations of the dependency's package.json
for ( const candidatePath of candidates( packagePath ) ) {
dependencyPath = path.join( candidatePath, dependency, 'package.json' );
debug( ` Trying ${ dependencyPath }` );
try {
dependencyJson = require( dependencyPath );
debug( ` Found!!!` );
break;
} catch ( e ) {
debug( ` Not found` );
// Path doesn't exists. That's fine, continue with the next candidate.
continue;
}
}
if ( ! dependencyJson ) {
console.warn( `Can't find a candidate for ${ dependency } in ${ packagePath }` );
return accumulated;
}
// Continue finding dependencies recursively.
const { tree, isCacheable } = findTree(
dependencyJson,
dependencyPath,
[ ...parents, name ],
cache
);
// Propagate 'cacheability': if the package is not cacheable, none of the parents should be.
treeIsCacheable = treeIsCacheable && isCacheable;
return {
...accumulated,
...tree,
};
},
{}
);
const result = { [ name ]: dependencies };
if ( treeIsCacheable ) {
cache.set( packagePath, result );
}
return { tree: result, isCacheable: treeIsCacheable };
};
/**
* Finds the effective dependencies tree (aka the logical tree) of a given package
* givent its package.json
* @param {string} root Path to package.json
*/
const generateEffectiveTree = ( root ) => {
const packagePath = path.resolve( root );
const packageJson = require( path.join( packagePath ) );
const { tree } = findTree( packageJson, path.dirname( packagePath ), [], new Map() );
return tree;
};
const getEffectiveTreeAsTree = ( root ) => {
const tree = generateEffectiveTree( root );
return treeify( tree, {
sortFn: ( a, b ) => a.localeCompare( b ),
} );
};
const getEffectiveTreeAsList = ( root ) => {
const tree = generateEffectiveTree( root );
function print( branch, prefix = [] ) {
return (
Object.entries( branch )
// Ensure deps are listed in alphabetical order
.sort( ( a, b ) => a[ 0 ].localeCompare( b[ 0 ] ) )
// For each dep, create a new array with the dep name + all its deps recursively
.map( ( [ depName, nestedDependencies ] ) => {
const newPrefix = [ ...prefix, depName ];
if ( nestedDependencies === '[Circular]' ) {
return [ [ ...newPrefix, nestedDependencies ] ];
}
return [ newPrefix, ...print( nestedDependencies, newPrefix ) ];
} )
.flat()
);
}
return print( tree )
.map( ( line ) => line.join( ' ' ) )
.join( '\n' );
};
module.exports = { getEffectiveTreeAsTree, getEffectiveTreeAsList, candidates };
|