monocart-reporter 2.2.2 → 2.2.3

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.
package/README.md CHANGED
@@ -174,12 +174,27 @@ Separated metadata file (Already included in the above HTML and compressed, it c
174
174
  > The [Trace Viewer](https://trace.playwright.dev/) requires that the trace file must be loaded over the http:// or https:// protocols without [CORS](https://developer.mozilla.org/en-US/docs/Glossary/CORS) issue, try following start a local web server:
175
175
  ```sh
176
176
  # serve and open report
177
- npx monocart show-report <your-outputFile-path>
177
+ npx monocart show-report <path-to-report>
178
178
 
179
179
  # serve report
180
- npx monocart serve-report <your-outputFile-path>
180
+ npx monocart serve-report <path-to-report>
181
+ ```
182
+ It works with `http://localhost:port/` or `http://127.0.0.1:port/`
183
+ But to successfully load from other `IP` or `domain`, you must start web server with `https`:
184
+ ```sh
185
+ npx monocart show-report <path-to-report> --ssl <path-to-key,path-to-cert>
186
+ # For local debug, you can create and install local CA with 'mkcert', see: https://mkcert.dev
187
+ ```
188
+ Or customize your own trace viewer url with option `traceViewerUrl`
189
+ ```js
190
+ // reporter options
191
+ {
192
+ name: "My Test Report",
193
+ outputFile: './test-results/report.html',
194
+ // defaults to 'https://trace.playwright.dev/?trace={traceUrl}'
195
+ traceViewerUrl: 'https://your-own-trace-viewer-url/?trace={traceUrl}'
196
+ }
181
197
  ```
182
- Or customize your own trace viewer url with option `traceViewerUrl` defaults to `https://trace.playwright.dev/?trace={traceUrl}`
183
198
 
184
199
  ## Custom Fields Report
185
200
  You can add custom fields to the report. for example: Owner, JIRA Key etc.
package/lib/cli.js CHANGED
@@ -3,12 +3,37 @@
3
3
  const fs = require('fs');
4
4
  const path = require('path');
5
5
  const http = require('http');
6
+ const https = require('https');
6
7
  const net = require('net');
8
+ const os = require('os');
7
9
  const EC = require('eight-colors');
8
10
  const KSR = require('koa-static-resolver');
9
11
  const Koa = require('koa');
12
+ const CG = require('console-grid');
10
13
 
14
+ const { program } = require('./packages/monocart-vendor.js');
11
15
  const defaultOptions = require('./default/options.js');
16
+ const version = require('../package.json').version;
17
+
18
+ const getInternalIps = () => {
19
+ const n = os.networkInterfaces();
20
+ // console.log(n);
21
+ const list = [];
22
+ for (const k in n) {
23
+ const inter = n[k];
24
+ for (const j in inter) {
25
+ const item = inter[j];
26
+ if (item.family === 'IPv4' && !item.internal) {
27
+ const a = item.address;
28
+ if (a.startsWith('192.') || a.startsWith('10.')) {
29
+ list.push(a);
30
+ }
31
+ }
32
+ }
33
+ }
34
+ return list;
35
+ };
36
+
12
37
 
13
38
  const generatePort = (startPort) => {
14
39
  return new Promise((resolve) => {
@@ -29,36 +54,71 @@ const generatePort = (startPort) => {
29
54
  });
30
55
  };
31
56
 
57
+ const showIpInfo = (protocol, port) => {
58
+ const ips = getInternalIps();
59
+ CG({
60
+ options: {
61
+ headerVisible: false
62
+ },
63
+ columns: [{
64
+ id: 'type'
65
+ }, {
66
+ id: 'url',
67
+ formatter: (v) => {
68
+ return EC.green(v);
69
+ }
70
+ }],
71
+ rows: [{
72
+ url: `${protocol}://localhost:${port}`,
73
+ type: 'Local'
74
+ }, ... ips.map((ip) => {
75
+ return {
76
+ url: `${protocol}://${ip}:${port}`,
77
+ type: 'Internal'
78
+ };
79
+ })]
80
+ });
81
+ };
82
+
32
83
  const openUrl = async (p) => {
33
84
  const open = await import('open');
34
85
  await open.default(p);
35
86
  };
36
87
 
88
+ const createServer = (app, options) => {
89
+
90
+ if (options.ssl) {
91
+ const [keyPath, certPath] = options.ssl.split(',');
92
+ const serverOptions = {
93
+ key: fs.readFileSync(path.resolve(keyPath)),
94
+ cert: fs.readFileSync(path.resolve(certPath))
95
+ };
96
+ return https.createServer(serverOptions, app.callback());
97
+ }
98
+
99
+ return http.createServer(app.callback());
100
+
101
+ };
102
+
103
+ const serveReport = async (p, options) => {
37
104
 
38
- const serveReport = async (list, openReport) => {
39
- if (!list.length) {
40
- list.push(defaultOptions.outputFile);
105
+ if (!p) {
106
+ p = defaultOptions.outputFile;
41
107
  }
42
108
 
109
+ const dirs = [];
43
110
  let filename = '';
44
- const dirs = list.filter((item) => {
45
- if (fs.existsSync(item)) {
46
- return true;
47
- }
48
- EC.logRed(`the path does not exists: ${item}`);
49
- return false;
50
- }).map((item) => {
51
- const stat = fs.statSync(item);
111
+ if (fs.existsSync(p)) {
112
+ const stat = fs.statSync(p);
52
113
  if (stat.isDirectory()) {
53
- return item;
114
+ dirs.push(p);
115
+ } else if (stat.isFile()) {
116
+ filename = path.basename(p);
117
+ dirs.push(path.dirname(p));
54
118
  }
55
-
56
- if (!filename) {
57
- filename = path.basename(item);
58
- }
59
-
60
- return path.dirname(item);
61
- });
119
+ } else {
120
+ EC.logRed(`The path does not exists: ${p}`);
121
+ }
62
122
 
63
123
  dirs.push('./');
64
124
 
@@ -75,36 +135,53 @@ const serveReport = async (list, openReport) => {
75
135
  maxAge: 1
76
136
  }));
77
137
 
78
- const server = http.createServer(app.callback());
138
+ const server = createServer(app, options);
79
139
 
80
140
  const port = await generatePort(8090);
141
+ const protocol = options.ssl ? 'https' : 'http';
81
142
 
82
- const url = `http://localhost:${port}/${filename}`;
143
+ const url = `${protocol}://localhost:${port}/${filename}`;
83
144
 
84
145
  server.listen(port, function() {
85
146
  EC.logCyan(`${new Date().toLocaleString()} server listening on ${url}`);
86
- if (openReport) {
147
+ if (protocol === 'https') {
148
+ showIpInfo(protocol, port);
149
+ }
150
+ if (options.open) {
87
151
  openUrl(url);
88
152
  }
89
153
  });
90
154
 
91
155
  };
92
156
 
93
- const start = function() {
94
- const args = process.argv.slice(2);
95
- const command = args.shift();
96
- // console.log(command, args);
97
- if (command === 'show' || command === 'show-report') {
98
- serveReport(args, true);
99
- return;
100
- }
157
+ program
158
+ .name('monocart')
159
+ .description('CLI to serve monocart reporter')
160
+ .version(version);
161
+
162
+ program.command('show-report')
163
+ .alias('show')
164
+ .description('Show report')
165
+ .argument('<path-to-report>', 'Report dir or html path')
166
+ .option('-s, --ssl <path-to-key,path-to-cert>', 'Start https server')
167
+ .action((str, options) => {
168
+ options.open = true;
169
+ serveReport(str, options);
170
+ });
101
171
 
102
- if (command === 'serve' || command === 'serve-report') {
103
- serveReport(args, false);
104
- return;
105
- }
172
+ program.command('serve-report')
173
+ .alias('serve')
174
+ .description('Serve report')
175
+ .argument('<path-to-report>', 'Report dir or html path')
176
+ .option('-s, --ssl <path-to-key,path-to-cert>', 'Start https server')
177
+ .action((str, options) => {
178
+ serveReport(str, options);
179
+ });
106
180
 
107
- EC.logRed(`Not found command: ${command}`);
108
- };
181
+ program.addHelpText('after', `
182
+ Starts ${EC.cyan('https')} with option --ssl:
183
+ npx monocart show-report <path-to-report> ${EC.cyan('--ssl <path-to-key,path-to-cert>')}
184
+ # Create and install local CA with 'mkcert', see: https://mkcert.dev
185
+ `);
109
186
 
110
- start();
187
+ program.parse();