Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | 1x 1288x 7x 7x 22x 7x 11x 11x 1286x 1285x 1x 99x 99x 96x 3x 109x 109x 103x 6x 3x 61x 1x 60x 5x 5x 33x 33x 2x 1x 1x 54x 1x 53x 53x 53x 21x 32x 53x 45x 37x 8x 1x 7x 7x 7x | /*
Copyright 2012-2015, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
import fs from "node:fs";
import path from "node:path";
/**
* Base class for writing content
* @class ContentWriter
* @constructor
*/
abstract class ContentWriter {
/**
* writes a string to the destination
* @param {String} str the string to write
*/
abstract write(str: string): void;
/**
* returns the colorized version of a string. Typically,
* content writers that write to files will return the
* same string and ones writing to a tty will wrap it in
* appropriate escape sequences.
* @param {String} str the string to colorize
* @param {String} clazz one of `high`, `medium` or `low`
* @returns {String} the colorized form of the string
*/
colorize(str: string, _clazz?: string): string {
return str;
}
/**
* writes a string appended with a newline to the destination
* @param {String} str the string to write
*/
println(str: string): void {
this.write(`${str}\n`);
}
/**
* closes this content writer. Should be called after all writes are complete.
*/
close(): void {}
}
/**
* a content writer that writes to a file
* @param {Number} fd - the file descriptor
* @extends ContentWriter
* @constructor
*/
class FileContentWriter extends ContentWriter {
declare fd: number;
constructor(fd: number) {
super();
this.fd = fd;
}
write(str: string): void {
fs.writeSync(this.fd, str);
}
close(): void {
fs.closeSync(this.fd);
}
}
// allow stdout to be captured for tests.
let capture = false;
let output = "";
/**
* a content writer that writes to the console
* @extends ContentWriter
* @constructor
*/
class ConsoleWriter extends ContentWriter {
write(str: string): void {
if (capture) {
output += str;
} else {
process.stdout.write(str);
}
}
colorize(str: string, clazz?: string): string {
const colors: Record<string, string> = {
low: "31;1",
medium: "33;1",
high: "32;1",
};
if (colors[clazz as string] && supportsColor()) {
return `\u001b[${colors[clazz as string]}m${str}\u001b[0m`;
}
return str;
}
}
/**
* Whether ANSI colour codes should be emitted on `stream`. Mirrors the
* `supports-color` semantics upstream istanbul relied on:
*
* - `FORCE_COLOR` wins over everything: `0`/`false` disables colours, any
* other value (including an empty string) enables them even when the
* stream is not a TTY, e.g. when a CI runner pipes the output.
* - `NO_COLOR` or `NODE_DISABLE_COLORS` being set disables colours.
* - Otherwise colours are used only for a TTY that reports colour support.
*/
export function supportsColor(
stream: NodeJS.WriteStream = process.stdout,
env: NodeJS.ProcessEnv = process.env,
): boolean {
const force = env.FORCE_COLOR;
if (force !== undefined) {
return force !== "0" && force !== "false";
}
if (env.NO_COLOR !== undefined || env.NODE_DISABLE_COLORS !== undefined) {
return false;
}
/* istanbul ignore next: depends on whether the test runner's stdout is a TTY */
return Boolean(stream.isTTY && stream.hasColors?.());
}
/**
* utility for writing files under a specific directory
* @class FileWriter
* @param {String} baseDir the base directory under which files should be written
* @constructor
*/
class FileWriter {
declare baseDir: string;
constructor(baseDir: string) {
if (!baseDir) {
throw new Error("baseDir must be specified");
}
this.baseDir = baseDir;
}
/**
* static helpers for capturing stdout report output;
* super useful for tests!
*/
static startCapture(): void {
capture = true;
}
static stopCapture(): void {
capture = false;
}
static getOutput(): string {
return output;
}
static resetOutput(): void {
output = "";
}
/**
* returns a FileWriter that is rooted at the supplied subdirectory
* @param {String} subdir the subdirectory under which to root the
* returned FileWriter
* @returns {FileWriter}
*/
writerForDir(subdir: string): FileWriter {
if (path.isAbsolute(subdir)) {
throw new Error(`Cannot create subdir writer for absolute path: ${subdir}`);
}
return new FileWriter(`${this.baseDir}/${subdir}`);
}
/**
* copies a file from a source directory to a destination name
* @param {String} source path to source file
* @param {String} dest relative path to destination file
* @param {String} [header=undefined] optional text to prepend to destination
* (e.g., an "this file is autogenerated" comment, copyright notice, etc.)
*/
copyFile(source: string, dest: string, header?: string): void {
if (path.isAbsolute(dest)) {
throw new Error(`Cannot write to absolute path: ${dest}`);
}
dest = path.resolve(this.baseDir, dest);
fs.mkdirSync(path.dirname(dest), { recursive: true });
let contents: string | Buffer;
if (header) {
contents = header + fs.readFileSync(source, "utf8");
} else {
contents = fs.readFileSync(source);
}
fs.writeFileSync(dest, contents);
}
/**
* returns a content writer for writing content to the supplied file.
* @param {String|null} file the relative path to the file or the special
* values `"-"` or `null` for writing to the console
* @returns {ContentWriter}
*/
writeFile(file: string | null): ContentWriter {
if (file === null || file === "-") {
return new ConsoleWriter();
}
if (path.isAbsolute(file)) {
throw new Error(`Cannot write to absolute path: ${file}`);
}
file = path.resolve(this.baseDir, file);
fs.mkdirSync(path.dirname(file), { recursive: true });
return new FileContentWriter(fs.openSync(file, "w"));
}
}
export default FileWriter;
export type { ContentWriter, FileContentWriter, ConsoleWriter };
|