@@ -69,43 +69,38 @@ namespace Moduless
69
69
}
70
70
71
71
/**
72
- * Finds cover functions tucked away in TypeScript files that can
73
- * only be found by traversing the TypeScript project structure.
74
- * Returns null in the case when no functions were discovered.
72
+ * Scans the entire project graph, calls require() on each
73
+ * outFile defined in each project, and returns an array of
74
+ * objects containing the the Project objects and the export
75
+ * value acquired from the require() call.
75
76
*/
76
- function tryLoadCoversFromDependencies ( projectPath : string )
77
+ function getGraphFromDependencies ( projectPath : string )
77
78
{
78
79
const graph = new ProjectGraph ( projectPath ) ;
79
- const scriptFilePaths : string [ ] = [ ] ;
80
+ const out : { project : Project ; export : object ; } [ ] = [ ] ;
80
81
81
82
for ( const project of graph . eachProject ( ) )
82
- if ( project . outFile !== "" )
83
- scriptFilePaths . push ( project . outFile ) ;
84
-
85
- const exports : object [ ] = [ ] ;
86
-
87
- for ( const scriptFilePath of scriptFilePaths )
88
83
{
89
- if ( ! Fs . existsSync ( scriptFilePath ) )
84
+ if ( ! Fs . existsSync ( project . outFile ) )
90
85
{
91
- Util . error ( "File not found: " + scriptFilePath ) ;
86
+ Util . error ( "File not found: " + project . outFile ) ;
92
87
continue ;
93
88
}
94
89
95
90
try
96
91
{
97
- const exp = require ( scriptFilePath ) ;
92
+ const exp = require ( project . outFile ) ;
98
93
99
94
if ( exp && typeof exp === "object" && ! Array . isArray ( exp ) )
100
- exports . push ( exp ) ;
95
+ out . push ( { project , export : exp } ) ;
101
96
}
102
97
catch ( e )
103
98
{
104
99
console . log ( e ) ;
105
100
}
106
101
}
107
102
108
- return exports ;
103
+ return out ;
109
104
}
110
105
111
106
/**
@@ -114,51 +109,50 @@ namespace Moduless
114
109
*/
115
110
async function runCovers ( target : IRunMeta )
116
111
{
117
- const dependencies = tryLoadCoversFromDependencies ( target . projectPath ) ;
118
- const exports = [ ...dependencies , globalThis ] ;
112
+ const ns = target . functionNamespace . join ( "." ) ;
113
+ const graph = getGraphFromDependencies ( target . projectPath ) ;
114
+ const startingProject = graph . at ( - 1 ) ;
115
+
116
+ if ( ! startingProject )
117
+ throw new Error ( "No projects found at location: " + target . projectPath ) ;
119
118
120
- const resolvedNamespace = ( ( ) =>
119
+ const tryResolveNamepace = ( root : object ) =>
121
120
{
122
- nextExport: for ( const exp of exports )
121
+ let current : any = root ;
122
+
123
+ for ( const identifier of target . functionNamespace )
123
124
{
124
- let current : any = exp ;
125
+ if ( ! ( identifier in current ) )
126
+ return null ;
125
127
126
- for ( const identifier of target . functionNamespace )
127
- {
128
- if ( ! ( identifier in current ) )
129
- continue nextExport;
130
-
131
- current = current [ identifier ] ;
132
- }
133
-
134
- if ( ! current || typeof current !== "object" )
135
- continue nextExport;
136
-
137
- return current as Record < string , any > ;
128
+ current = current [ identifier ] ;
138
129
}
139
130
140
- const ns = target . functionNamespace . join ( "." ) ;
141
- throw new Error (
142
- `Could not resolve: ${ ns } \nNot found or not an object.` ) ;
143
- } ) ( ) ;
131
+ return current ;
132
+ } ;
133
+
134
+ const namespaceObject =
135
+ tryResolveNamepace ( startingProject . export ) ||
136
+ globalThis ;
144
137
145
138
const covers = ( ( ) =>
146
139
{
147
140
const out : [ string , CoverFn ] [ ] = [ ] ;
148
141
149
142
if ( target . functionName )
150
143
{
151
- const fn = resolvedNamespace [ target . functionName ] ;
144
+ const fn = namespaceObject [ target . functionName ] ;
145
+
152
146
if ( typeof fn !== "function" )
153
- throw new Error ( target . functionName + " is not a function." ) ;
147
+ throw new Error ( ` ${ ns } . ${ target . functionName } is not a function.` ) ;
154
148
155
149
out . push ( [ target . functionName , fn ] ) ;
156
150
}
157
151
else if ( target . prefix )
158
- for ( const [ functionName , maybeFunction ] of Object . entries ( resolvedNamespace ) )
152
+ for ( const [ functionName , maybeFunction ] of Object . entries ( namespaceObject ) )
159
153
if ( typeof maybeFunction === "function" )
160
154
if ( functionName . startsWith ( target . functionName ) )
161
- out . push ( [ functionName , maybeFunction ] ) ;
155
+ out . push ( [ functionName , maybeFunction as any ] ) ;
162
156
163
157
return out ;
164
158
} ) ( ) ;
0 commit comments