1
+ #!/usr/bin/env node
2
+ const yargs = require ( "yargs" ) ;
3
+ const path = require ( "path" ) ;
4
+ const istanbul = require ( 'istanbul-lib-coverage' ) ;
5
+ const { existsSync, mkdirSync, rmSync, writeFileSync, readFileSync} = require ( "fs" ) ;
6
+
7
+ /**
8
+ * this taken from @cypress/code-coverage
9
+ * @param coverage object
10
+ */
11
+ function fixSourcePaths ( coverage ) {
12
+ Object . values ( coverage ) . forEach ( ( file ) => {
13
+ const { path : absolutePath , inputSourceMap } = file
14
+ const fileName = / ( [ ^ \/ \\ ] + ) $ / . exec ( absolutePath ) [ 1 ]
15
+ if ( ! inputSourceMap || ! fileName ) return
16
+
17
+ if ( inputSourceMap . sourceRoot ) inputSourceMap . sourceRoot = ''
18
+ inputSourceMap . sources = inputSourceMap . sources . map ( ( source ) =>
19
+ source . includes ( fileName ) ? absolutePath : source
20
+ )
21
+ } )
22
+ }
23
+
24
+ /**
25
+ * this partly taken from @cypress/code-coverage
26
+ * @param tempDir directory where to merge
27
+ * @param fileWithCoverage file containing coverage
28
+ */
29
+ function combineCoverage ( tempDir , fileWithCoverage ) {
30
+ const fileToSave = `${ tempDir } /combined.json` ;
31
+
32
+ const coverage = existsSync ( fileToSave )
33
+ ? JSON . parse ( readFileSync ( fileToSave , 'utf8' ) )
34
+ : { } ;
35
+
36
+ fixSourcePaths ( coverage )
37
+
38
+ const previousCoverage = existsSync ( fileWithCoverage )
39
+ ? JSON . parse ( readFileSync ( fileWithCoverage , 'utf8' ) )
40
+ : { }
41
+
42
+ const coverageMap = istanbul . createCoverageMap ( previousCoverage )
43
+ coverageMap . merge ( coverage )
44
+
45
+ writeFileSync ( fileToSave , JSON . stringify ( coverageMap , null , 2 ) )
46
+ console . log ( 'combined coverage from `%s` with %s' , fileToSave , fileWithCoverage )
47
+ }
48
+
49
+ /**
50
+ * Create report by NYC library,
51
+ * Will not read nyc config and temp dic from nyc.config.js
52
+ * @param tempDir dir where json files located
53
+ * @param reportDir dir where to put report
54
+ * @param reporterArr array with reporters like ['json', 'lcov', 'text']
55
+ */
56
+ function createReport ( tempDir , reportDir , reporterArr ) {
57
+ const NYC = require ( 'nyc' ) ;
58
+ const nycReportOptions = {
59
+ reportDir : reportDir ,
60
+ tempDir : tempDir ,
61
+ reporter : reporterArr ?? [ 'json' , 'lcov' , 'text' ] ,
62
+ } ;
63
+
64
+ const nyc = new NYC ( nycReportOptions )
65
+
66
+ nyc . report ( ) . then ( ( ) => {
67
+ console . log ( "Report created" ) ;
68
+ } )
69
+ }
70
+
71
+ /**
72
+ * Remove directory sync
73
+ * @param dir
74
+ */
75
+ const removeDir = ( dir ) => {
76
+ const pathResolved = path . resolve ( dir )
77
+ if ( existsSync ( pathResolved ) ) {
78
+ rmSync ( pathResolved , { recursive : true } ) ;
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Clear directory sync
84
+ * @param dir
85
+ */
86
+ const clearDir = ( dir ) => {
87
+ const pathResolved = path . resolve ( dir )
88
+ if ( existsSync ( pathResolved ) ) {
89
+ rmSync ( pathResolved , { recursive : true } ) ;
90
+ }
91
+ mkdirSync ( pathResolved , { recursive : true } ) ;
92
+ }
93
+
94
+ const argv = yargs ( process . argv . slice ( 2 ) )
95
+ . options ( {
96
+ cypress : {
97
+ type : 'string' ,
98
+ demandOption : true ,
99
+ default : 'reports/coverage-cypress' ,
100
+ describe : `Path to coverage reports directory (relative to current working directory)
101
+ Path with directories - each of them should contain coverage report (coverage-final.json)` ,
102
+ } ,
103
+ jest : {
104
+ type : 'string' ,
105
+ demandOption : true ,
106
+ default : 'reports/coverage-jest' ,
107
+ describe : `Path to jet coverage report, should contain coverage report (coverage-final.json)` ,
108
+ } ,
109
+ out : {
110
+ type : 'string' ,
111
+ demandOption : true ,
112
+ default : 'reports/coverage-temp' ,
113
+ describe : `Path to final report` ,
114
+ } ,
115
+ report : {
116
+ type : 'string' ,
117
+ demandOption : true ,
118
+ default : 'reports/coverage-full' ,
119
+ describe : `Path to final report` ,
120
+ } ,
121
+ } )
122
+ . alias ( 'c' , 'cypress' )
123
+ . alias ( 'j' , 'jest' )
124
+ . alias ( 'h' , 'help' )
125
+ . help ( 'help' )
126
+ . parseSync ( ) ;
127
+
128
+ console . log ( ' ======== MERGE COVERAGE REPORTS' ) ;
129
+
130
+ const { jest, cypress, out, report } = argv ;
131
+ const outDir = path . resolve ( out ) ;
132
+ const reportDir = path . resolve ( report ) ;
133
+
134
+ removeDir ( reportDir ) ;
135
+ clearDir ( outDir ) ;
136
+
137
+ combineCoverage ( outDir , `${ cypress } /coverage-final.json` ) ;
138
+ combineCoverage ( outDir , `${ jest } /coverage-final.json` ) ;
139
+ createReport ( outDir , reportDir , [ 'json' , 'lcov' , 'text' , 'cobertura' , 'clover' ] ) ;
0 commit comments