log-to-test-harness.js 779 B

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * MIT License http://opensource.org/licenses/MIT
  3. * Author: Ben Holloway @bholloway
  4. */
  5. 'use strict';
  6. var stream = require('stream');
  7. var hasLogged = false;
  8. function logToTestHarness(maybeStream, options) {
  9. var doLogging =
  10. !hasLogged &&
  11. !!maybeStream &&
  12. (typeof maybeStream === 'object') &&
  13. (maybeStream instanceof stream.Writable);
  14. if (doLogging) {
  15. hasLogged = true; // ensure we log only once
  16. Object.keys(options).forEach(eachOptionKey);
  17. }
  18. function eachOptionKey(key) {
  19. maybeStream.write(key + ': ' + stringify(options[key]) + '\n');
  20. }
  21. function stringify(value) {
  22. try {
  23. return JSON.stringify(value) || String(value);
  24. } catch (e) {
  25. return '-unstringifyable-';
  26. }
  27. }
  28. }
  29. module.exports = logToTestHarness;