1
1
import { compile } from 'handlebars'
2
2
import fs from 'node:fs'
3
3
import path from 'node:path'
4
- import { AwaitedProp , record } from 'type-plus'
4
+ import { forEachKey , record } from 'type-plus'
5
5
import { CompileResult , PackageCompileResults , compileProject } from './logic/compile'
6
- import { getTestSubjects , toImportMap } from './logic/project'
6
+ import { TestSubjectsContext , getTestSubjects , toImportMap } from './logic/project'
7
7
import { RuntimeResult , runProject } from './logic/runtime'
8
8
import { reduceFlatMessage } from './logic/utils'
9
9
@@ -16,123 +16,97 @@ export function runCompile(ctx: {
16
16
}
17
17
}
18
18
19
+ type RunCompileContext = ReturnType < typeof runCompile >
20
+
19
21
export function runRuntime ( ctx : {
20
22
project : string ,
21
23
moduleTypes : string [ ] ,
22
24
projectPath : string ,
23
25
packageJson : any
24
26
} & ReturnType < typeof getTestSubjects > ) {
25
27
return {
26
- runtime : Promise . all ( ctx . moduleTypes . map ( moduleType => ( {
28
+ runtime : ctx . moduleTypes . map ( moduleType => ( {
27
29
moduleType,
28
30
results : runProject ( ctx , moduleType )
29
- } ) ) )
30
- }
31
- }
32
-
33
- export function runCompileAndRuntimeTests ( ctx : {
34
- project : string ,
35
- moduleTypes : string [ ] ,
36
- projectPath : string ,
37
- packageJson : any
38
- } & ReturnType < typeof getTestSubjects > ) {
39
- return {
40
- results : compileProject ( ctx )
41
- . then ( compileResults => Promise . all (
42
- ctx . moduleTypes . map ( moduleType =>
43
- runProject ( ctx , moduleType ) . then ( runtimeResults => ( {
44
- moduleType,
45
- compileResults : compileResults [ moduleType ] ,
46
- runtimeResults
47
- } ) ) ) )
48
- )
31
+ } ) )
49
32
}
50
33
}
51
34
52
- type TestSubjectsContext = ReturnType < typeof getTestSubjects >
53
- type TestResultsContext = AwaitedProp < ReturnType < typeof runCompileAndRuntimeTests > , 'results' >
35
+ type RunRuntimeContext = ReturnType < typeof runRuntime >
54
36
55
- export function genTestResults (
56
- ctx : TestSubjectsContext & TestResultsContext ) {
57
- const results = collectTestResults ( ctx )
37
+ export async function genTestResults (
38
+ ctx : { moduleTypes : string [ ] } & TestSubjectsContext & RunCompileContext & RunRuntimeContext ) {
39
+ const results = await collectTestResults ( ctx )
58
40
const template = compile ( fs . readFileSync ( path . join ( __dirname , '../templates/test-results.hbs' ) , 'utf8' ) )
59
41
return template ( results )
60
42
}
61
43
62
- function collectTestResults ( { subjects, results } : TestSubjectsContext & TestResultsContext ) {
44
+ async function collectTestResults ( { moduleTypes, subjects, compile, runtime } : { moduleTypes : string [ ] } & TestSubjectsContext & RunCompileContext & RunRuntimeContext ) {
45
+ const compileResults = await compile
46
+ const runtimeResults = await Promise . all ( runtime . map ( async r => ( {
47
+ moduleType : r . moduleType ,
48
+ results : await r . results
49
+ } ) ) )
50
+ const results = moduleTypes . map ( moduleType => ( {
51
+ moduleType,
52
+ compileResults : compileResults [ moduleType ] ,
53
+ runtimeResults : runtimeResults . find ( r => r . moduleType === moduleType ) ! . results
54
+ } ) )
63
55
const errors = toErrorRecord ( results )
56
+ // console.log('runtime errors', errors.runtime)
64
57
return {
65
58
results : results . flatMap ( ( { moduleType, compileResults, runtimeResults } ) => {
59
+ // console.log(moduleType, runtimeResults.map(r => ([r.subject, ...r.results.map(r => ([r.importType, extractRuntimeErrorMessage(r.error)]))])))
66
60
return subjects . flatMap ( ( s , i ) => {
67
- const compileResult = compileResults [ s . name ]
68
- const runtimeResult = runtimeResults . find ( r => r . name === s . name )
61
+ const compileResult : Array < CompileResult > = compileResults [ s . name ]
62
+ const runtimeResult = runtimeResults . find ( r => r . subject === s . name )
63
+ const compileImportMap = toImportMap ( compileResult ? compileResult . map ( c => {
64
+ const error = errors . compile . find ( e => e . message === c . messageText )
65
+ return {
66
+ importType : c . importType ,
67
+ transient : c . transient ,
68
+ value : error ?. key ?? ''
69
+ }
70
+ } ) : ( s . files . length === 0 ? [
71
+ { importType : 'default' , notApply : true } ,
72
+ { importType : 'default-as' , notApply : true } ,
73
+ { importType : 'star' , notApply : true } ,
74
+ ] : [
75
+ { importType : 'default' , } ,
76
+ { importType : 'default-as' } ,
77
+ { importType : 'star' } ,
78
+ ] ) ) // TypeError: m.default is not a function
79
+ const runtimeImportMap = toImportMap ( runtimeResult ? runtimeResult . results . map ( ( r , i ) => {
80
+ const error = errors . runtime . find ( e => e . message === extractRuntimeErrorMessage ( r . error ) )
81
+ return {
82
+ importType : r . importType ,
83
+ transient : false ,
84
+ value : error ?. key ?? ''
85
+ }
86
+ } ) : ( s . files . length === 0 ? [
87
+ { importType : 'default' , notApply : true } ,
88
+ { importType : 'default-as' , notApply : true } ,
89
+ { importType : 'star' , notApply : true } ,
90
+ ] : [
91
+ { importType : 'default' , } ,
92
+ { importType : 'default-as' } ,
93
+ { importType : 'star' } ,
94
+ ] ) )
95
+ forEachKey ( runtimeImportMap , ( k ) => {
96
+ if ( ! compileImportMap [ k ] . value && runtimeImportMap [ k ] . value ) {
97
+ runtimeImportMap [ k ] . icon = '❌'
98
+ }
99
+ } )
69
100
return [ {
70
101
moduleType : i === 0 ? moduleType : undefined ,
71
102
package : s . name ,
72
103
type : '💻 compile' ,
73
- ...toImportMap ( compileResult ? compileResult . map ( c => {
74
- const error = errors . compile . find ( e => e . message === c . messageText )
75
- return {
76
- importType : c . importType ,
77
- transient : c . transient ,
78
- value : error ?. key ?? ''
79
- }
80
- } ) : ( s . files . length === 0 ? [
81
- { importType : 'default' , notApply : true } ,
82
- { importType : 'default-as' , notApply : true } ,
83
- { importType : 'star' , notApply : true } ,
84
- ] : [
85
- { importType : 'default' , } ,
86
- { importType : 'default-as' } ,
87
- { importType : 'star' } ,
88
- ] ) )
104
+ ...compileImportMap
89
105
} , {
90
106
type : '🏃 runtime' ,
91
- ...toImportMap ( runtimeResult ? runtimeResult . results . map ( r => {
92
- const error = errors . runtime . find ( e => e . message === extractRuntimeErrorMessage ( r . error ) )
93
- return {
94
- importType : r . importType ,
95
- transient : false ,
96
- value : error ?. key ?? ''
97
- }
98
- } ) : ( s . files . length === 0 ? [
99
- { importType : 'default' , notApply : true } ,
100
- { importType : 'default-as' , notApply : true } ,
101
- { importType : 'star' , notApply : true } ,
102
- ] : [
103
- { importType : 'default' , } ,
104
- { importType : 'default-as' } ,
105
- { importType : 'star' } ,
106
- ] ) )
107
+ ...runtimeImportMap
107
108
} ]
108
109
} )
109
- // return runtimeResults.flatMap((r, i) => {
110
- // return [{
111
- // moduleType: i === 0 ? moduleType : undefined,
112
- // package: r.name,
113
- // type: '💻 compile',
114
- // ...toImportMap(compileResults[r.name]?.map(c => {
115
- // const error = errors.compile.find(e => e.message === c.messageText)
116
- // return {
117
- // importType: c.importType,
118
- // transient: c.transient,
119
- // value: error?.key ?? ''
120
- // }
121
- // }))
122
- // }, {
123
- // moduleType: undefined,
124
- // package: r.name,
125
- // type: '🏃 runtime',
126
- // ...toImportMap(r.results.map(r => {
127
- // const error = errors.runtime.find(e => e.message === extractRuntimeErrorMessage(r.error))
128
- // return {
129
- // importType: r.importType,
130
- // transient: false,
131
- // value: error?.key ?? ''
132
- // }
133
- // }))
134
- // }]
135
- // })
136
110
} ) ,
137
111
errors : [ ...errors . compile , ...errors . runtime ]
138
112
}
0 commit comments