source: trip-planner-front/node_modules/istanbul-reports/lib/html-spa/index.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.2 KB
Line 
1'use strict';
2/*
3 Copyright 2012-2015, Yahoo Inc.
4 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
5 */
6const fs = require('fs');
7const path = require('path');
8const { ReportBase } = require('istanbul-lib-report');
9const HtmlReport = require('../html');
10
11const standardLinkMapper = {
12 getPath(node) {
13 if (typeof node === 'string') {
14 return node;
15 }
16 let filePath = node.getQualifiedName();
17 if (node.isSummary()) {
18 if (filePath !== '') {
19 filePath += '/index.html';
20 } else {
21 filePath = 'index.html';
22 }
23 } else {
24 filePath += '.html';
25 }
26 return filePath;
27 },
28
29 relativePath(source, target) {
30 const targetPath = this.getPath(target);
31 const sourcePath = path.dirname(this.getPath(source));
32 return path.relative(sourcePath, targetPath);
33 },
34
35 assetPath(node, name) {
36 return this.relativePath(this.getPath(node), name);
37 }
38};
39
40class HtmlSpaReport extends ReportBase {
41 constructor(opts = {}) {
42 super({
43 // force the summarizer to nested for html-spa
44 summarizer: 'nested'
45 });
46
47 this.verbose = opts.verbose || false;
48 this.linkMapper = opts.linkMapper || standardLinkMapper;
49 this.subdir = opts.subdir || '';
50 this.date = Date();
51 this.skipEmpty = opts.skipEmpty;
52 this.htmlReport = new HtmlReport(opts);
53 this.htmlReport.getBreadcrumbHtml = function() {
54 return '<a href="javascript:history.back()">Back</a>';
55 };
56
57 this.metricsToShow = opts.metricsToShow || [
58 'lines',
59 'branches',
60 'functions'
61 ];
62 }
63
64 getWriter(context) {
65 if (!this.subdir) {
66 return context.writer;
67 }
68 return context.writer.writerForDir(this.subdir);
69 }
70
71 onStart(root, context) {
72 this.htmlReport.onStart(root, context);
73
74 const writer = this.getWriter(context);
75 const srcDir = path.resolve(__dirname, './assets');
76 fs.readdirSync(srcDir).forEach(f => {
77 const resolvedSource = path.resolve(srcDir, f);
78 const resolvedDestination = '.';
79 const stat = fs.statSync(resolvedSource);
80 let dest;
81
82 if (stat.isFile()) {
83 dest = resolvedDestination + '/' + f;
84 if (this.verbose) {
85 console.log('Write asset: ' + dest);
86 }
87 writer.copyFile(resolvedSource, dest);
88 }
89 });
90 }
91
92 onDetail(node, context) {
93 this.htmlReport.onDetail(node, context);
94 }
95
96 getMetric(metric, type, context) {
97 const isEmpty = metric.total === 0;
98 return {
99 total: metric.total,
100 covered: metric.covered,
101 skipped: metric.skipped,
102 missed: metric.total - metric.covered,
103 pct: isEmpty ? 0 : metric.pct,
104 classForPercent: isEmpty
105 ? 'empty'
106 : context.classForPercent(type, metric.pct)
107 };
108 }
109
110 toDataStructure(node, context) {
111 const coverageSummary = node.getCoverageSummary();
112 const metrics = {
113 statements: this.getMetric(
114 coverageSummary.statements,
115 'statements',
116 context
117 ),
118 branches: this.getMetric(
119 coverageSummary.branches,
120 'branches',
121 context
122 ),
123 functions: this.getMetric(
124 coverageSummary.functions,
125 'functions',
126 context
127 ),
128 lines: this.getMetric(coverageSummary.lines, 'lines', context)
129 };
130
131 return {
132 file: node.getRelativeName(),
133 isEmpty: coverageSummary.isEmpty(),
134 metrics,
135 children:
136 node.isSummary() &&
137 node
138 .getChildren()
139 .map(child => this.toDataStructure(child, context))
140 };
141 }
142
143 onEnd(rootNode, context) {
144 const data = this.toDataStructure(rootNode, context);
145
146 const cw = this.getWriter(context).writeFile(
147 this.linkMapper.getPath(rootNode)
148 );
149
150 cw.write(
151 `<!doctype html>
152 <html lang="en">
153 <head>
154 <link rel="stylesheet" href="spa.css" />
155 <meta name="viewport" content="width=device-width, initial-scale=1">
156 </head>
157 <body>
158 <div id="app" class="app"></div>
159 <script>
160 window.data = ${JSON.stringify(data)};
161 window.generatedDatetime = ${JSON.stringify(
162 String(Date())
163 )};
164 window.metricsToShow = ${JSON.stringify(
165 this.metricsToShow
166 )};
167 </script>
168 <script src="bundle.js"></script>
169 </body>
170 </html>`
171 );
172 cw.close();
173 }
174}
175
176module.exports = HtmlSpaReport;
Note: See TracBrowser for help on using the repository browser.