monocart-coverage-reports 2.5.9 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,179 +1,179 @@
1
- const path = require('path');
2
- const { fileURLToPath, pathToFileURL } = require('url');
3
- const Util = require('./util.js');
4
-
5
- const filterPath = (str) => {
6
- str = decodeURI(str);
7
- // remove / of start, end or double, ./ ../
8
- const ls = str.split('/').map((it) => {
9
- it = it.trim();
10
- // remove illegal characters except /
11
- it = it.replace(/[\\:*?"<>|]/g, '');
12
- // space
13
- it = it.replace(/\s+/g, '-');
14
- return it;
15
- }).filter((item) => {
16
- if (!item || item === '.' || item === '..') {
17
- return false;
18
- }
19
- return true;
20
- });
21
- return ls.join('/');
22
- };
23
-
24
- const getSourcePath = (url, index = '', type = '') => {
25
-
26
- // no url for anonymous
27
- if (!url) {
28
- // anonymous scripts will have __playwright_evaluation_script__ as their URL.
29
- url = ['anonymous', index ? `-${index}` : '', type ? `.${type}` : ''].join('');
30
- return filterPath(url);
31
- }
32
-
33
- // already is file url
34
- if (url.startsWith('file:')) {
35
- const relPath = Util.relativePath(fileURLToPath(url));
36
- return filterPath(relPath);
37
- }
38
-
39
- // http/https url, other url
40
- const urlObj = Util.resolveUrl(url);
41
- if (urlObj) {
42
- const host = [urlObj.hostname, urlObj.port].filter((it) => it).join('-');
43
- // always start with '/'
44
- const pathname = urlObj.pathname;
45
-
46
- let p = host + pathname;
47
- // webpack://monocart-v8/packages/v8/src/app.vue?5cc4
48
- if (urlObj.search) {
49
- p += `/${urlObj.search}`;
50
- }
51
-
52
- return filterPath(p);
53
- }
54
-
55
- // could be path
56
- const fileUrl = pathToFileURL(url).toString();
57
- const relPath = Util.relativePath(fileURLToPath(fileUrl));
58
- return filterPath(relPath);
59
-
60
- };
61
-
62
- const getSourceType = (sourcePath) => {
63
- let ext = path.extname(sourcePath);
64
- let type = 'js';
65
- if (ext) {
66
- ext = ext.slice(1);
67
- const reg = /^[a-z0-9]+$/;
68
- if (reg.test(ext)) {
69
- type = ext;
70
- }
71
- }
72
- return type;
73
- };
74
-
75
-
76
- // ========================================================================================================
77
-
78
- const getSourcePathHandler = (options) => {
79
- const input = options.sourcePath;
80
- if (typeof input === 'function') {
81
- return input;
82
- }
83
- // object replace key with value
84
- if (input && typeof input === 'object') {
85
- const keys = Object.keys(input).filter((k) => typeof input[k] === 'string');
86
- if (keys.length) {
87
- return (filePath) => {
88
- keys.forEach((k) => {
89
- filePath = filePath.replace(k, input[k]);
90
- });
91
- return filePath;
92
- };
93
- }
94
- }
95
- };
96
-
97
- // ========================================================================================================
98
-
99
- const initIstanbulSourcePath = (coverageData, fileSources, sourcePathHandler) => {
100
- if (!sourcePathHandler) {
101
- return coverageData;
102
- }
103
-
104
- // console.log(fileSources);
105
-
106
- const newCoverage = {};
107
- Object.keys(coverageData).forEach((sourcePath) => {
108
- // previous coverage and source
109
- const item = coverageData[sourcePath];
110
- let source = fileSources[sourcePath];
111
-
112
- // source could be empty string ''
113
- if (typeof source !== 'string') {
114
- // read source with original source path first
115
- source = Util.readFileSync(sourcePath);
116
- if (typeof source === 'string') {
117
- fileSources[sourcePath] = source;
118
- }
119
- }
120
-
121
- // new source path, second is source url
122
- const newSourcePath = sourcePathHandler(sourcePath, '');
123
- if (typeof newSourcePath === 'string' && newSourcePath) {
124
- sourcePath = newSourcePath;
125
- }
126
-
127
- // update source path
128
- item.data.path = sourcePath;
129
- newCoverage[sourcePath] = item;
130
- // update source for the new path
131
- if (typeof source === 'string') {
132
- fileSources[sourcePath] = source;
133
- }
134
- });
135
-
136
- // console.log(Object.keys(fileSources));
137
- // console.log(newCoverage);
138
-
139
- return newCoverage;
140
- };
141
-
142
- // ========================================================================================================
143
-
144
- const mergeSourceRoot = (sourceRoot, sourceName) => {
145
- if (sourceName.startsWith(sourceRoot)) {
146
- return sourceName;
147
- }
148
- return sourceRoot + sourceName;
149
- };
150
-
151
- const initSourceMapSourcePath = (sourceMap, fileUrls, sourcePathHandler) => {
152
- // reset sourceRoot
153
- const sourceRoot = sourceMap.sourceRoot || '';
154
- sourceMap.sourceRoot = '';
155
-
156
- sourceMap.sources = sourceMap.sources.map((sourceName, i) => {
157
- const sourceUrl = mergeSourceRoot(sourceRoot, sourceName);
158
-
159
- let sourcePath = getSourcePath(sourceUrl, i + 1);
160
- if (sourcePathHandler) {
161
- const newSourcePath = sourcePathHandler(sourcePath, sourceUrl);
162
- if (typeof newSourcePath === 'string' && newSourcePath) {
163
- sourcePath = newSourcePath;
164
- }
165
- }
166
-
167
- fileUrls[sourcePath] = sourceUrl;
168
- return sourcePath;
169
- });
170
-
171
- };
172
-
173
- module.exports = {
174
- getSourcePath,
175
- getSourceType,
176
- getSourcePathHandler,
177
- initIstanbulSourcePath,
178
- initSourceMapSourcePath
179
- };
1
+ const path = require('path');
2
+ const { fileURLToPath, pathToFileURL } = require('url');
3
+ const Util = require('./util.js');
4
+
5
+ const filterPath = (str) => {
6
+ str = decodeURI(str);
7
+ // remove / of start, end or double, ./ ../
8
+ const ls = str.split('/').map((it) => {
9
+ it = it.trim();
10
+ // remove illegal characters except /
11
+ it = it.replace(/[\\:*?"<>|]/g, '');
12
+ // space
13
+ it = it.replace(/\s+/g, '-');
14
+ return it;
15
+ }).filter((item) => {
16
+ if (!item || item === '.' || item === '..') {
17
+ return false;
18
+ }
19
+ return true;
20
+ });
21
+ return ls.join('/');
22
+ };
23
+
24
+ const getSourcePath = (url, index = '', type = '') => {
25
+
26
+ // no url for anonymous
27
+ if (!url) {
28
+ // anonymous scripts will have __playwright_evaluation_script__ as their URL.
29
+ url = ['anonymous', index ? `-${index}` : '', type ? `.${type}` : ''].join('');
30
+ return filterPath(url);
31
+ }
32
+
33
+ // already is file url
34
+ if (url.startsWith('file:')) {
35
+ const relPath = Util.relativePath(fileURLToPath(url));
36
+ return filterPath(relPath);
37
+ }
38
+
39
+ // http/https url, other url
40
+ const urlObj = Util.resolveUrl(url);
41
+ if (urlObj) {
42
+ const host = [urlObj.hostname, urlObj.port].filter((it) => it).join('-');
43
+ // always start with '/'
44
+ const pathname = urlObj.pathname;
45
+
46
+ let p = host + pathname;
47
+ // webpack://monocart-v8/packages/v8/src/app.vue?5cc4
48
+ if (urlObj.search) {
49
+ p += `/${urlObj.search}`;
50
+ }
51
+
52
+ return filterPath(p);
53
+ }
54
+
55
+ // could be path
56
+ const fileUrl = pathToFileURL(url).toString();
57
+ const relPath = Util.relativePath(fileURLToPath(fileUrl));
58
+ return filterPath(relPath);
59
+
60
+ };
61
+
62
+ const getSourceType = (sourcePath) => {
63
+ let ext = path.extname(sourcePath);
64
+ let type = 'js';
65
+ if (ext) {
66
+ ext = ext.slice(1);
67
+ const reg = /^[a-z0-9]+$/;
68
+ if (reg.test(ext)) {
69
+ type = ext;
70
+ }
71
+ }
72
+ return type;
73
+ };
74
+
75
+
76
+ // ========================================================================================================
77
+
78
+ const getSourcePathHandler = (options) => {
79
+ const input = options.sourcePath;
80
+ if (typeof input === 'function') {
81
+ return input;
82
+ }
83
+ // object replace key with value
84
+ if (input && typeof input === 'object') {
85
+ const keys = Object.keys(input).filter((k) => typeof input[k] === 'string');
86
+ if (keys.length) {
87
+ return (filePath) => {
88
+ keys.forEach((k) => {
89
+ filePath = filePath.replace(k, input[k]);
90
+ });
91
+ return filePath;
92
+ };
93
+ }
94
+ }
95
+ };
96
+
97
+ // ========================================================================================================
98
+
99
+ const initIstanbulSourcePath = (coverageData, fileSources, sourcePathHandler) => {
100
+ if (!sourcePathHandler) {
101
+ return coverageData;
102
+ }
103
+
104
+ // console.log(fileSources);
105
+
106
+ const newCoverage = {};
107
+ Object.keys(coverageData).forEach((sourcePath) => {
108
+ // previous coverage and source
109
+ const item = coverageData[sourcePath];
110
+ let source = fileSources[sourcePath];
111
+
112
+ // source could be empty string ''
113
+ if (typeof source !== 'string') {
114
+ // read source with original source path first
115
+ source = Util.readFileSync(sourcePath);
116
+ if (typeof source === 'string') {
117
+ fileSources[sourcePath] = source;
118
+ }
119
+ }
120
+
121
+ // new source path, second is source url
122
+ const newSourcePath = sourcePathHandler(sourcePath, '');
123
+ if (typeof newSourcePath === 'string' && newSourcePath) {
124
+ sourcePath = newSourcePath;
125
+ }
126
+
127
+ // update source path
128
+ item.data.path = sourcePath;
129
+ newCoverage[sourcePath] = item;
130
+ // update source for the new path
131
+ if (typeof source === 'string') {
132
+ fileSources[sourcePath] = source;
133
+ }
134
+ });
135
+
136
+ // console.log(Object.keys(fileSources));
137
+ // console.log(newCoverage);
138
+
139
+ return newCoverage;
140
+ };
141
+
142
+ // ========================================================================================================
143
+
144
+ const mergeSourceRoot = (sourceRoot, sourceName) => {
145
+ if (sourceName.startsWith(sourceRoot)) {
146
+ return sourceName;
147
+ }
148
+ return sourceRoot + sourceName;
149
+ };
150
+
151
+ const initSourceMapSourcePath = (sourceMap, fileUrls, sourcePathHandler) => {
152
+ // reset sourceRoot
153
+ const sourceRoot = sourceMap.sourceRoot || '';
154
+ sourceMap.sourceRoot = '';
155
+
156
+ sourceMap.sources = sourceMap.sources.map((sourceName, i) => {
157
+ const sourceUrl = mergeSourceRoot(sourceRoot, sourceName);
158
+
159
+ let sourcePath = getSourcePath(sourceUrl, i + 1);
160
+ if (sourcePathHandler) {
161
+ const newSourcePath = sourcePathHandler(sourcePath, sourceUrl);
162
+ if (typeof newSourcePath === 'string' && newSourcePath) {
163
+ sourcePath = newSourcePath;
164
+ }
165
+ }
166
+
167
+ fileUrls[sourcePath] = sourceUrl;
168
+ return sourcePath;
169
+ });
170
+
171
+ };
172
+
173
+ module.exports = {
174
+ getSourcePath,
175
+ getSourceType,
176
+ getSourcePathHandler,
177
+ initIstanbulSourcePath,
178
+ initSourceMapSourcePath
179
+ };