@@ -19,14 +19,20 @@ const MAX_API_SCORE = 3;
1919/**
2020 * How much a module looks like a way in: +3 for an entry name (`index`,
2121 * `main`, `server`, `app`, `cli`, any extension), +1 per exported function
22- * nobody calls (at most 3), -1 per directory below a leading `src/`.
22+ * nobody calls (at most 3), -1 per directory below a leading `src/`, the
23+ * path taken from `root` (a package's directory; `""` for the repository).
2324 */
2425const entryScore = (
2526 module : ModuleNode ,
2627 graph : CodeGraph ,
27- called : ReadonlySet < string >
28+ called : ReadonlySet < string > ,
29+ root : string
2830) : number => {
29- const segments = module . id . split ( "/" ) ;
31+ const path =
32+ root !== "" && module . id . startsWith ( `${ root } /` )
33+ ? module . id . slice ( root . length + 1 )
34+ : module . id ;
35+ const segments = path . split ( "/" ) ;
3036 const basename = segments . at ( - 1 ) ?? "" ;
3137 const directories = segments . slice ( 0 , - 1 ) ;
3238 const depth =
@@ -54,14 +60,16 @@ const byPath = (a: ModuleNode, b: ModuleNode): number => {
5460 * The modules the entrance leads to, best first, among `modules` (all of
5561 * the graph's by default; a zone's for its hub), counting only imports and
5662 * calls between those. Every module nobody imports is one (nothing else
57- * leads there), ranked by `entryScore`, ties by path. Modules only cycles
58- * reach then add the best of what is still out of reach, one at a time,
59- * until imports from the entries reach every module. Never empty for a
60- * non-empty `modules`.
63+ * leads there), ranked by `entryScore` with paths taken from `root` (the
64+ * zone's package directory), ties by path. Modules only cycles reach then
65+ * add the best of what is still out of reach, one at a time, until imports
66+ * from the entries reach every module. Never empty for a non-empty
67+ * `modules`.
6168 */
6269const entryModules = (
6370 graph : CodeGraph ,
64- modules : readonly ModuleNode [ ] = graph . modules
71+ modules : readonly ModuleNode [ ] = graph . modules ,
72+ root = ""
6573) : readonly ModuleNode [ ] => {
6674 const inside = new Set ( modules . map ( ( module ) => module . id ) ) ;
6775 const links = new Map (
@@ -81,7 +89,10 @@ const entryModules = (
8189 )
8290 ) ;
8391 const scores = new Map (
84- modules . map ( ( module ) => [ module . id , entryScore ( module , graph , called ) ] )
92+ modules . map ( ( module ) => [
93+ module . id ,
94+ entryScore ( module , graph , called , root ) ,
95+ ] )
8596 ) ;
8697 const ranked = ( group : readonly ModuleNode [ ] ) : readonly ModuleNode [ ] =>
8798 group . toSorted (
0 commit comments