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.

utils.js 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const df = require( './defaults' );
  2. const chalk = require( 'chalk' );
  3. const { Writable } = require( 'stream' );
  4. function checkLogLevel( { levels, level }, method ) {
  5. return levels[level] >= levels[method];
  6. }
  7. function parseParams( str = '' ) {
  8. return str
  9. .replace( /[()"']*/g, '' )
  10. .split( ',' )
  11. .map( s => s.trim() )
  12. .filter( s => s !== '' )
  13. .map( s => isNaN( +s ) ? s : +s );
  14. }
  15. function generateConfig( options ) {
  16. if ( typeof options === 'string' ) {
  17. options = {
  18. format: df.dfFormat.replace( '$$', options )
  19. }
  20. }
  21. // Using Object.assign and not Object spread properties to support Node v6.4
  22. const config = Object.assign( {}, df, options || {}, {
  23. format: typeof options.format === 'undefined' ? df.dfFormat.replace( '$$', df.dfDateFormat ) : options.format,
  24. include: [...( new Set( [...( options.include || df.include ), ...Object.keys( options.extend || {} )] ) )],
  25. tokens: Object.assign( {}, df.tokens, options.tokens || {} ),
  26. levels: Object.assign( {}, df.levels, options.levels || {}, options.extend || {} ),
  27. stdout: options.stdout || process.stdout,
  28. stderr: options.stderr || options.stdout || process.stderr
  29. } );
  30. config.tokensKeys = Object.keys( config.tokens );
  31. config.defaultTokens = df.tokens;
  32. return config;
  33. }
  34. function generatePrefix( method, { tokens, defaultTokens, format: prefix, tokensKeys }, msg ) {
  35. tokensKeys
  36. .sort( ( a, b ) => b.length - a.length )
  37. .forEach( key => {
  38. const token = tokens[key];
  39. const re = new RegExp( `:${key}(\\([^)]*\\))?(\\.\\w+)*`, 'g' );
  40. prefix = prefix.replace( re, ( match, params ) => {
  41. let ret = token( { method, defaultTokens, params: parseParams( params ), tokens, msg } );
  42. match.replace( params, '' ).split( '.' ).slice( 1 ).forEach( decorator => {
  43. ret = chalk`{${decorator} ${ret}}`;
  44. } );
  45. return ret;
  46. } );
  47. } );
  48. // Color groups
  49. const rec = /(\([^)]*\))(\.\w+)+/g;
  50. prefix = prefix.replace( rec, ( match, text ) => {
  51. let ret = text.replace( /[\(\)]/g, '' );
  52. match.replace( text, '' ).split( '.' ).slice( 1 ).forEach( decorator => {
  53. ret = chalk`{${decorator} ${ret}}`;
  54. } );
  55. return ret;
  56. } )
  57. return prefix;
  58. }
  59. function selectOutputStream( method, { levels, stdout, stderr } ) {
  60. return levels[method] <= 2 ? stderr : stdout;
  61. }
  62. class FakeStream extends Writable {
  63. constructor() {
  64. super();
  65. this._last_message = '';
  66. }
  67. get last_msg() {
  68. return this._last_message.replace( /\n$/, '' );
  69. }
  70. _write( chunk, enc, cb ) {
  71. this._last_message = chunk.toString();
  72. cb();
  73. }
  74. }
  75. module.exports = {
  76. checkLogLevel,
  77. parseParams,
  78. generateConfig,
  79. generatePrefix,
  80. selectOutputStream,
  81. FakeStream
  82. };