-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.ts
More file actions
195 lines (175 loc) · 7 KB
/
Copy pathindex.ts
File metadata and controls
195 lines (175 loc) · 7 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
Copyright 2012-2015, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
/**
* @module Exports
*/
import { isAbsolute } from "node:path";
import { pathToFileURL } from "node:url";
import Context from "./context";
import type { ContextOptions } from "./context";
import FileWriter from "./file-writer";
import ReportBase from "./report-base";
import CloverReport from "./reports/clover/index";
import type { CloverOptions } from "./reports/clover/index";
import CoberturaReport from "./reports/cobertura/index";
import type { CoberturaOptions } from "./reports/cobertura/index";
import HtmlSpaReport from "./reports/html-spa/index";
import type { HtmlSpaOptions } from "./reports/html-spa/index";
import HtmlReport from "./reports/html/index";
import type { HtmlOptions } from "./reports/html/index";
import JsonSummaryReport from "./reports/json-summary/index";
import type { JsonSummaryOptions } from "./reports/json-summary/index";
import JsonReport from "./reports/json/index";
import type { JsonOptions } from "./reports/json/index";
import LcovReport from "./reports/lcov/index";
import type { LcovOptions } from "./reports/lcov/index";
import LcovOnlyReport from "./reports/lcovonly/index";
import type { LcovOnlyOptions } from "./reports/lcovonly/index";
import NoneReport from "./reports/none/index";
import TeamcityReport from "./reports/teamcity/index";
import type { TeamcityOptions } from "./reports/teamcity/index";
import TextLcovReport from "./reports/text-lcov/index";
import type { TextLcovOptions } from "./reports/text-lcov/index";
import TextSummaryReport from "./reports/text-summary/index";
import type { TextSummaryOptions } from "./reports/text-summary/index";
import TextReport from "./reports/text/index";
import type { TextOptions } from "./reports/text/index";
import * as watermarks from "./watermarks";
import type { Watermarks } from "./watermarks";
export type { ContextOptions, SourceFinder } from "./context";
export type { Context };
export type { ContentWriter, ConsoleWriter, FileContentWriter } from "./file-writer";
export type { ReportBaseOptions } from "./report-base";
export type { CloverOptions } from "./reports/clover/index";
export type { CoberturaOptions } from "./reports/cobertura/index";
export type { HtmlSpaOptions } from "./reports/html-spa/index";
export type { HtmlOptions, LinkMapper } from "./reports/html/index";
export type { JsonSummaryOptions } from "./reports/json-summary/index";
export type { JsonOptions } from "./reports/json/index";
export type { LcovOptions } from "./reports/lcov/index";
export type { LcovOnlyOptions } from "./reports/lcovonly/index";
export type { TeamcityOptions } from "./reports/teamcity/index";
export type { TextLcovOptions } from "./reports/text-lcov/index";
export type { TextSummaryOptions } from "./reports/text-summary/index";
export type { TextOptions } from "./reports/text/index";
export type { ReportNode, ReportTree, Summarizers } from "./summarizer-factory";
export type { BaseNode, BaseTree, CompositeVisitor, PartialVisitor, Visitor } from "./tree";
export type { Watermark, Watermarks } from "./watermarks";
export type { XmlAttributes, default as XMLWriter } from "./xml-writer";
/**
* returns a reporting context for the supplied options
* @param {Object} [opts=null] opts
* @returns {Context}
*/
export function createContext(opts: ContextOptions): Context {
return new Context(opts);
}
/**
* returns the default watermarks that would be used when not
* overridden
* @returns {Object} an object with `statements`, `functions`, `branches`,
* and `line` keys. Each value is a 2 element array that has the low and
* high watermark as percentages.
*/
export function getDefaultWatermarks(): Watermarks {
return watermarks.getDefault();
}
/**
* Base class for all reports
*/
export { ReportBase };
/**
* Utility for writing files under a specific directory
*/
export { FileWriter };
const reports = {
clover: CloverReport,
cobertura: CoberturaReport,
html: HtmlReport,
"html-spa": HtmlSpaReport,
json: JsonReport,
"json-summary": JsonSummaryReport,
lcov: LcovReport,
lcovonly: LcovOnlyReport,
none: NoneReport,
teamcity: TeamcityReport,
text: TextReport,
"text-lcov": TextLcovReport,
"text-summary": TextSummaryReport,
};
/** options accepted by each built-in report, keyed by report name */
export interface ReportOptions {
clover: CloverOptions;
cobertura: CoberturaOptions;
html: HtmlOptions;
"html-spa": HtmlSpaOptions;
json: JsonOptions;
"json-summary": JsonSummaryOptions;
lcov: LcovOptions;
lcovonly: LcovOnlyOptions;
none: never;
teamcity: TeamcityOptions;
text: TextOptions;
"text-lcov": TextLcovOptions;
"text-summary": TextSummaryOptions;
}
/** names of the built-in reports */
export type ReportType = keyof ReportOptions;
type ReportConstructor = new (cfg: object) => ReportBase;
function getBuiltinReport(name: string): ReportConstructor | undefined {
return Object.hasOwn(reports, name)
? (reports as Record<string, ReportConstructor>)[name]
: undefined;
}
function unwrapDefault(mod: unknown): unknown {
return typeof mod === "object" && mod !== null && "default" in mod ? mod.default : mod;
}
/**
* creates an instance of a built-in report.
* @param name the report name, e.g. `html`, `json`, `text`
* @param cfg options for the report
*/
export function create<T extends ReportType>(
name: T,
cfg?: Partial<ReportOptions[T]>,
): InstanceType<(typeof reports)[T]> {
const Cons = getBuiltinReport(name);
if (!Cons) {
throw new Error(`Unknown report "${name}". Use createAsync() to load custom reports.`);
}
return new Cons(cfg ?? {}) as InstanceType<(typeof reports)[T]>;
}
/**
* creates an instance of a built-in or custom report.
*
* Custom reports are loaded with `import()`, so `name` may be a package name,
* an absolute path or a `file://` URL of an ES module or CommonJS module whose
* default export (or `module.exports`) is the report class.
* @param name the report name, package name or path
* @param cfg options for the report
*/
export async function createAsync<T extends ReportType>(
name: T,
cfg?: Partial<ReportOptions[T]>,
): Promise<InstanceType<(typeof reports)[T]>>;
export async function createAsync(name: string, cfg?: object): Promise<ReportBase>;
export async function createAsync(name: string, cfg?: object): Promise<ReportBase> {
let Cons = getBuiltinReport(name);
if (!Cons) {
const specifier = isAbsolute(name) ? pathToFileURL(name).href : name;
const mod: unknown = await import(specifier);
Cons = unwrapDefault(mod) as ReportConstructor;
// CommonJS transpiled from ESM: `module.exports = { __esModule: true, default: … }`
if (typeof Cons === "object" && Cons !== null && "__esModule" in Cons) {
Cons = unwrapDefault(Cons) as ReportConstructor;
}
if (typeof Cons !== "function") {
throw new TypeError(
`Custom report "${name}" must export a report class as its default export (ESM) or module.exports (CommonJS)`,
);
}
}
return new Cons!(cfg ?? {});
}