Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

context.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use strict';
  2. /*
  3. Copyright 2012-2015, Yahoo Inc.
  4. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  5. */
  6. const fs = require('fs');
  7. const FileWriter = require('./file-writer');
  8. const XMLWriter = require('./xml-writer');
  9. const tree = require('./tree');
  10. const watermarks = require('./watermarks');
  11. const SummarizerFactory = require('./summarizer-factory');
  12. function defaultSourceLookup(path) {
  13. try {
  14. return fs.readFileSync(path, 'utf8');
  15. } catch (ex) {
  16. throw new Error(`Unable to lookup source: ${path} (${ex.message})`);
  17. }
  18. }
  19. function normalizeWatermarks(specified = {}) {
  20. Object.entries(watermarks.getDefault()).forEach(([k, value]) => {
  21. const specValue = specified[k];
  22. if (!Array.isArray(specValue) || specValue.length !== 2) {
  23. specified[k] = value;
  24. }
  25. });
  26. return specified;
  27. }
  28. /**
  29. * A reporting context that is passed to report implementations
  30. * @param {Object} [opts=null] opts options
  31. * @param {String} [opts.dir='coverage'] opts.dir the reporting directory
  32. * @param {Object} [opts.watermarks=null] opts.watermarks watermarks for
  33. * statements, lines, branches and functions
  34. * @param {Function} [opts.sourceFinder=fsLookup] opts.sourceFinder a
  35. * function that returns source code given a file path. Defaults to
  36. * filesystem lookups based on path.
  37. * @constructor
  38. */
  39. class Context {
  40. constructor(opts) {
  41. this.dir = opts.dir || 'coverage';
  42. this.watermarks = normalizeWatermarks(opts.watermarks);
  43. this.sourceFinder = opts.sourceFinder || defaultSourceLookup;
  44. this._summarizerFactory = new SummarizerFactory(
  45. opts.coverageMap,
  46. opts.defaultSummarizer
  47. );
  48. this.data = {};
  49. }
  50. /**
  51. * returns a FileWriter implementation for reporting use. Also available
  52. * as the `writer` property on the context.
  53. * @returns {Writer}
  54. */
  55. getWriter() {
  56. return this.writer;
  57. }
  58. /**
  59. * returns the source code for the specified file path or throws if
  60. * the source could not be found.
  61. * @param {String} filePath the file path as found in a file coverage object
  62. * @returns {String} the source code
  63. */
  64. getSource(filePath) {
  65. return this.sourceFinder(filePath);
  66. }
  67. /**
  68. * returns the coverage class given a coverage
  69. * types and a percentage value.
  70. * @param {String} type - the coverage type, one of `statements`, `functions`,
  71. * `branches`, or `lines`
  72. * @param {Number} value - the percentage value
  73. * @returns {String} one of `high`, `medium` or `low`
  74. */
  75. classForPercent(type, value) {
  76. const watermarks = this.watermarks[type];
  77. if (!watermarks) {
  78. return 'unknown';
  79. }
  80. if (value < watermarks[0]) {
  81. return 'low';
  82. }
  83. if (value >= watermarks[1]) {
  84. return 'high';
  85. }
  86. return 'medium';
  87. }
  88. /**
  89. * returns an XML writer for the supplied content writer
  90. * @param {ContentWriter} contentWriter the content writer to which the returned XML writer
  91. * writes data
  92. * @returns {XMLWriter}
  93. */
  94. getXMLWriter(contentWriter) {
  95. return new XMLWriter(contentWriter);
  96. }
  97. /**
  98. * returns a full visitor given a partial one.
  99. * @param {Object} partialVisitor a partial visitor only having the functions of
  100. * interest to the caller. These functions are called with a scope that is the
  101. * supplied object.
  102. * @returns {Visitor}
  103. */
  104. getVisitor(partialVisitor) {
  105. return new tree.Visitor(partialVisitor);
  106. }
  107. getTree(name = 'defaultSummarizer') {
  108. return this._summarizerFactory[name];
  109. }
  110. }
  111. Object.defineProperty(Context.prototype, 'writer', {
  112. enumerable: true,
  113. get() {
  114. if (!this.data.writer) {
  115. this.data.writer = new FileWriter(this.dir);
  116. }
  117. return this.data.writer;
  118. }
  119. });
  120. module.exports = Context;