@@ -13,79 +13,54 @@ namespace Moduless
13
13
/** */
14
14
type CoverFn = ( ) => CoverReturn | Promise < CoverReturn > ;
15
15
16
- /** */
17
- export interface IRunInfo
18
- {
19
- cwd : string ;
20
- namespacePath : string [ ] ;
21
- functionPrefix ?: string ;
22
- functionName ?: string ;
23
- }
24
-
25
16
/**
26
- *
17
+ * An interface that stores information about the running
18
+ * of a single cover function.
27
19
*/
28
- export namespace IRunInfo
20
+ export interface IRunMeta
29
21
{
30
- /**
31
- * Creates an IRunInfo namespace, parsed from
32
- * the specified namespace path, and function prefix.
33
- */
34
- export function parsePrefix ( qualifiedName : string ) : IRunInfo
35
- {
36
- const parts = qualifiedName . split ( "." ) ;
37
- return {
38
- cwd : process . cwd ( ) ,
39
- namespacePath : parts . slice ( 0 , - 1 ) ,
40
- functionPrefix : parts . at ( - 1 ) || ""
41
- } ;
42
- }
22
+ functionNamespace : string [ ] ;
23
+ functionName : string ;
24
+ projectPath : string ;
43
25
44
26
/**
45
- * Creates an IRunInfo namespace, parsed from
46
- * the specified namespace path, and function name.
27
+ * Whether the "name" property refers to a prefix (true),
28
+ * or whether it's a fully qualified name (false) .
47
29
*/
48
- export function parseNamed ( qualified : string )
49
- {
50
- const parts = qualified . split ( "." ) ;
51
- return {
52
- cwd : process . cwd ( ) ,
53
- namespacePath : parts . slice ( 0 , - 1 ) ,
54
- functionName : parts . at ( - 1 ) || ""
55
- } ;
56
- }
30
+ prefix ?: boolean ;
57
31
}
58
32
59
33
/**
60
34
*
61
35
*/
62
- export async function run ( info : IRunInfo )
36
+ export async function run ( runInfo : IRunMeta )
63
37
{
64
38
// Wait 1600ms to give the debugger a chance to connect.
65
- // This can be a problem with larger projects.
66
- await new Promise ( r => setTimeout ( r , 1600 ) ) ;
39
+ // This can be a problem with larger projects when
40
+ if ( Moduless . inElectronMain || Moduless . inElectronRender )
41
+ await new Promise ( r => setTimeout ( r , 1600 ) ) ;
67
42
68
43
if ( Moduless . inElectronRender )
69
44
{
70
45
// Eliminate Electron's console generated garbage
71
46
console . clear ( ) ;
72
47
}
73
48
74
- if ( ! info . functionName )
49
+ if ( runInfo . prefix )
75
50
{
76
51
Util . log (
77
- `Running cover functions in ${ info . namespacePath . join ( "." ) } ` +
78
- `starting with ${ info . functionPrefix } .` ) ;
52
+ `Running cover functions in ${ runInfo . functionNamespace . join ( "." ) } ` +
53
+ `starting with ${ runInfo . functionName } .` ) ;
79
54
}
80
55
81
56
let hasRunOneFunction = false ;
82
57
83
- const coverResult = await runCovers ( info ) ;
58
+ const coverResult = await runCovers ( runInfo ) ;
84
59
if ( coverResult )
85
60
{
86
61
hasRunOneFunction = true ;
87
62
88
- if ( info . functionName )
63
+ if ( runInfo . functionName )
89
64
return ;
90
65
}
91
66
@@ -98,10 +73,9 @@ namespace Moduless
98
73
* only be found by traversing the TypeScript project structure.
99
74
* Returns null in the case when no functions were discovered.
100
75
*/
101
- function tryLoadCoversFromDependencies ( cwd = process . cwd ( ) )
76
+ function tryLoadCoversFromDependencies ( projectPath : string )
102
77
{
103
- //const coverNamespaces: Namespace[] = [];
104
- const graph = new ProjectGraph ( cwd ) ;
78
+ const graph = new ProjectGraph ( projectPath ) ;
105
79
const scriptFilePaths : string [ ] = [ ] ;
106
80
107
81
for ( const project of graph . eachProject ( ) )
@@ -138,20 +112,18 @@ namespace Moduless
138
112
* Runs the cover functions with the specified name, from the specified
139
113
* namespace. Intended for use with Node.js.
140
114
*/
141
- async function runCovers ( info : IRunInfo )
115
+ async function runCovers ( target : IRunMeta )
142
116
{
143
- const exports = [
144
- ...tryLoadCoversFromDependencies ( info . cwd ) ,
145
- globalThis ,
146
- ] ;
117
+ const dependencies = tryLoadCoversFromDependencies ( target . projectPath ) ;
118
+ const exports = [ ...dependencies , globalThis ] ;
147
119
148
120
const resolvedNamespace = ( ( ) =>
149
121
{
150
122
nextExport: for ( const exp of exports )
151
123
{
152
124
let current : any = exp ;
153
125
154
- for ( const identifier of info . namespacePath )
126
+ for ( const identifier of target . functionNamespace )
155
127
{
156
128
if ( ! ( identifier in current ) )
157
129
continue nextExport;
@@ -165,27 +137,27 @@ namespace Moduless
165
137
return current as Record < string , any > ;
166
138
}
167
139
140
+ const ns = target . functionNamespace . join ( "." ) ;
168
141
throw new Error (
169
- "Could not resolve: " + info . namespacePath +
170
- ". Not found or not an object." ) ;
142
+ `Could not resolve: ${ ns } \nNot found or not an object.` ) ;
171
143
} ) ( ) ;
172
144
173
145
const covers = ( ( ) =>
174
146
{
175
147
const out : [ string , CoverFn ] [ ] = [ ] ;
176
148
177
- if ( info . functionName )
149
+ if ( target . functionName )
178
150
{
179
- const fn = resolvedNamespace [ info . functionName ] ;
151
+ const fn = resolvedNamespace [ target . functionName ] ;
180
152
if ( typeof fn !== "function" )
181
- throw new Error ( info . functionName + " is not a function." ) ;
153
+ throw new Error ( target . functionName + " is not a function." ) ;
182
154
183
- out . push ( [ info . functionName , fn ] ) ;
155
+ out . push ( [ target . functionName , fn ] ) ;
184
156
}
185
- else if ( info . functionPrefix )
157
+ else if ( target . prefix )
186
158
for ( const [ functionName , maybeFunction ] of Object . entries ( resolvedNamespace ) )
187
159
if ( typeof maybeFunction === "function" )
188
- if ( functionName . startsWith ( info . functionPrefix ) )
160
+ if ( functionName . startsWith ( target . functionName ) )
189
161
out . push ( [ functionName , maybeFunction ] ) ;
190
162
191
163
return out ;
0 commit comments