-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.ts
144 lines (129 loc) · 4.61 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import fs from 'fs';
import path from 'path';
import os from 'os';
import { Readable } from 'stream';
import open from 'open';
import { parseChunked, stringifyStream } from '@discoveryjs/json-ext';
import {
FromItem,
makeReplacer,
transform,
waitFinished,
} from '@statoscope/report-writer/dist/utils';
import statsPackage from '@statoscope/stats/package.json';
import ExtensionValidationResultGenerator from '@statoscope/stats-extension-stats-validation-result/dist/generator';
import { Reporter } from '@statoscope/types/types/validation/reporter';
import { Result } from '@statoscope/types/types/validation/result';
import normalizeCompilation from '@statoscope/webpack-model/dist/normalizeCompilation';
import { StatoscopeMeta } from '@statoscope/webpack-model/webpack';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const version = require('../package.json');
export type Options = {
saveReportTo?: string;
saveStatsTo?: string;
open?: boolean;
disableReportCompression?: boolean;
};
export default class ConsoleReporter implements Reporter {
constructor(public options?: Options) {}
async run(result: Result): Promise<void> {
console.log(`Preparing data for Statoscope report...`);
const generator = new ExtensionValidationResultGenerator({
adapter: version.adapter,
author: version.author,
description: version.description,
homepage: version.homepage,
name: version.name,
version: version.version,
});
for (const rule of result.rules) {
const storage = rule.api.getStorage();
const ruleDescriptor = rule.api.getRuleDescriptor();
if (ruleDescriptor) {
generator.handleRule(rule.name, ruleDescriptor);
}
for (const entry of storage) {
generator.handleEntry(rule.name, entry);
}
}
const parsedInput = (await parseChunked(fs.createReadStream(result.files.input))) as {
__statoscope?: StatoscopeMeta;
};
const meta: StatoscopeMeta = {
descriptor: { name: statsPackage.name, version: statsPackage.version },
extensions: [],
};
parsedInput.__statoscope ??= meta;
parsedInput.__statoscope.extensions ??= [];
parsedInput.__statoscope.extensions.push(generator.get());
let parsedReference;
if (result.files.reference) {
parsedReference = await parseChunked(fs.createReadStream(result.files.reference));
}
const id = path.basename(result.files.input, '.json');
const reportPath =
this.options?.saveReportTo ||
path.join(os.tmpdir(), `statoscope-report-${id}-${Date.now()}.html`);
const statsPath = this.options?.saveStatsTo;
if (statsPath) {
console.log(`Generating stats...`);
const toDir = path.dirname(statsPath);
if (!fs.existsSync(toDir)) {
fs.mkdirSync(toDir, { recursive: true });
}
const statsFileStream = fs.createWriteStream(statsPath);
const statStream: Readable = stringifyStream(
parsedInput,
makeReplacer(parsedInput.__statoscope.context, '.', ['context', 'source']),
);
statStream.pipe(statsFileStream);
await waitFinished(statsFileStream);
console.log(`Stats saved into ${statsPath}`);
}
if (this.options?.saveReportTo || this.options?.open) {
console.log(`Generating Statoscope report...`);
normalizeCompilation(parsedInput);
if (parsedReference) {
normalizeCompilation(parsedReference);
}
const reportFilename = await transform(
{
writer: {
scripts: [{ type: 'path', path: require.resolve('@statoscope/webpack-ui') }],
init: `function (data) {
Statoscope.default(data.map((item) => ({ name: item.id, data: item.data })));
}`,
dataCompression: !this.options.disableReportCompression,
},
},
[
{
type: 'data',
filename: 'input.json',
data: parsedInput,
replacer: makeReplacer(parsedInput.__statoscope?.context, '.', [
'context',
'source',
]),
},
parsedReference
? {
type: 'data',
filename: 'reference.json',
data: parsedReference,
replacer: makeReplacer(parsedReference.__statoscope?.context, '.', [
'context',
'source',
]),
}
: null,
].filter(Boolean) as FromItem[],
reportPath,
);
console.log(`Statoscope report saved into ${reportFilename}`);
if (this.options?.open) {
open(reportFilename);
}
}
}
}