2020
2121// MODULES //
2222
23- var resolve = require ( 'path' ) . resolve ;
2423var logger = require ( 'debug' ) ;
2524var CompactAdjacencyMatrix = require ( '@stdlib/utils/compact-adjacency-matrix' ) ;
26- var rootDir = require ( '@stdlib/_tools/utils/root-dir' ) ;
27- var getDeps = require ( '@stdlib/_tools/pkgs/deps' ) . sync ;
25+ var depList = require ( '@stdlib/_tools/pkgs/dep-list' ) ;
2826var indexOf = require ( '@stdlib/utils/index-of' ) ;
2927var contains = require ( '@stdlib/assert/contains' ) ;
28+ var startsWith = require ( '@stdlib/string/starts-with' ) ;
3029var format = require ( '@stdlib/string/format' ) ;
3130
3231
3332// VARIABLES //
3433
3534var debug = logger ( 'pkgs:toposort:sort' ) ;
3635var OPTS = {
37- 'dev' : false ,
38- 'dir' : resolve ( rootDir ( ) , 'lib' , 'node_modules' )
36+ 'dev' : false
3937} ;
4038
4139
40+ // FUNCTIONS //
41+
42+ /**
43+ * Checks whether a package is an ancestor of another package.
44+ *
45+ * @private
46+ * @param {string } candidate - potential ancestor package (e.g., `@stdlib/string/tools`)
47+ * @param {string } descendant - descendant package (e.g., `@stdlib/string/tools/grapheme-cluster-break`)
48+ * @returns {boolean } boolean indicating whether a package is an ancestor of another package
49+ */
50+ function isAncestorPackage ( candidate , descendant ) {
51+ return startsWith ( descendant , candidate + '/' ) ;
52+ }
53+
54+
4255// MAIN //
4356
4457/**
@@ -49,7 +62,6 @@ var OPTS = {
4962* @returns {(StringArray|EmptyArray|Error) } sorted package names
5063*/
5164function sort ( pkgs ) {
52- var pkgsDeps ;
5365 var deps ;
5466 var idx ;
5567 var out ;
@@ -63,17 +75,19 @@ function sort( pkgs ) {
6375 return pkgs ;
6476 }
6577 M = new CompactAdjacencyMatrix ( N ) ;
66- pkgsDeps = getDeps ( pkgs , OPTS ) ;
6778 for ( i = 0 ; i < N ; i ++ ) {
6879 debug ( 'Resolving dependencies for package: %s (%d of %d)' , pkgs [ i ] , i + 1 , N ) ;
69- deps = pkgsDeps [ i ] . deps ;
80+ deps = depList ( pkgs [ i ] , OPTS ) ;
7081
7182 debug ( 'Processing %d dependencies...' , deps . length ) ;
7283 for ( j = 0 ; j < deps . length ; j ++ ) {
7384 idx = indexOf ( pkgs , deps [ j ] ) ;
7485
75- // Ignore external dependencies and internal tools packages...
76- if ( idx >= 0 && ! contains ( deps [ j ] , '_tools' ) ) {
86+ if (
87+ idx >= 0 && // ignore external dependencies
88+ ! contains ( deps [ j ] , '_tools' ) && // ignore internal tools packages
89+ ! isAncestorPackage ( deps [ j ] , pkgs [ i ] ) // ignore dependencies which are parent packages
90+ ) {
7791 M . addEdge ( idx , i ) ;
7892 }
7993 }
0 commit comments