123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- 'use strict';
- const fs = require('fs');
- const FileWriter = require('./file-writer');
- const XMLWriter = require('./xml-writer');
- const tree = require('./tree');
- const watermarks = require('./watermarks');
- const SummarizerFactory = require('./summarizer-factory');
- function defaultSourceLookup(path) {
- try {
- return fs.readFileSync(path, 'utf8');
- } catch (ex) {
- throw new Error(`Unable to lookup source: ${path} (${ex.message})`);
- }
- }
- function normalizeWatermarks(specified = {}) {
- Object.entries(watermarks.getDefault()).forEach(([k, value]) => {
- const specValue = specified[k];
- if (!Array.isArray(specValue) || specValue.length !== 2) {
- specified[k] = value;
- }
- });
- return specified;
- }
- class Context {
- constructor(opts) {
- this.dir = opts.dir || 'coverage';
- this.watermarks = normalizeWatermarks(opts.watermarks);
- this.sourceFinder = opts.sourceFinder || defaultSourceLookup;
- this._summarizerFactory = new SummarizerFactory(
- opts.coverageMap,
- opts.defaultSummarizer
- );
- this.data = {};
- }
-
- getWriter() {
- return this.writer;
- }
-
- getSource(filePath) {
- return this.sourceFinder(filePath);
- }
-
- classForPercent(type, value) {
- const watermarks = this.watermarks[type];
- if (!watermarks) {
- return 'unknown';
- }
- if (value < watermarks[0]) {
- return 'low';
- }
- if (value >= watermarks[1]) {
- return 'high';
- }
- return 'medium';
- }
-
- getXMLWriter(contentWriter) {
- return new XMLWriter(contentWriter);
- }
-
- getVisitor(partialVisitor) {
- return new tree.Visitor(partialVisitor);
- }
- getTree(name = 'defaultSummarizer') {
- return this._summarizerFactory[name];
- }
- }
- Object.defineProperty(Context.prototype, 'writer', {
- enumerable: true,
- get() {
- if (!this.data.writer) {
- this.data.writer = new FileWriter(this.dir);
- }
- return this.data.writer;
- }
- });
- module.exports = Context;
|