transform.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * transform.js - browserify workaround for blessed
  3. * Copyright (c) 2013-2015, Christopher Jeffrey and contributors (MIT License).
  4. * https://github.com/chjj/blessed
  5. */
  6. var Transform = require('stream').Transform
  7. , path = require('path')
  8. , fs = require('fs');
  9. /**
  10. * Transformer
  11. */
  12. function transformer(code) {
  13. var stream = new Transform;
  14. stream._transform = function(chunk, encoding, callback) {
  15. return callback(null, chunk);
  16. };
  17. stream._flush = function(callback) {
  18. if (code) {
  19. stream.push(code);
  20. }
  21. return callback();
  22. };
  23. return stream;
  24. }
  25. /**
  26. * Explicitly require all widgets in widget.js
  27. */
  28. var widgets = fs.readdirSync(__dirname + '/../lib/widgets');
  29. var requireWidgets = widgets.reduce(function(out, name) {
  30. name = path.basename(name, '.js');
  31. out += '\nrequire(\'./widgets/' + name + '\');';
  32. return out;
  33. }, '');
  34. /**
  35. * Do not make filesystem calls in tput.js for
  36. * terminfo or termcap, just use xterm terminfo/cap.
  37. */
  38. var infoPath = path.resolve(__dirname, '..', 'usr', 'xterm-256color')
  39. , capPath = path.resolve(__dirname, '..', 'usr', 'xterm.termcap');
  40. var infoPathFake = path.resolve(
  41. path.sep, 'usr', 'share', 'terminfo',
  42. path.basename(infoPath)[0],
  43. path.basename(infoPath)
  44. );
  45. function readMethods() {
  46. Tput._infoBuffer = new Buffer(TERMINFO, 'base64');
  47. Tput.prototype.readTerminfo = function() {
  48. this.terminal = TERMINFO_NAME;
  49. return this.parseTerminfo(Tput._infoBuffer, TERMINFO_PATH);
  50. };
  51. Tput.cpaths = [];
  52. Tput.termcap = TERMCAP;
  53. Tput.prototype._readTermcap = Tput.prototype.readTermcap;
  54. Tput.prototype.readTermcap = function() {
  55. this.terminal = TERMCAP_NAME;
  56. return this._readTermcap(this.terminal);
  57. };
  58. Tput.prototype.detectUnicode = function() {
  59. return true;
  60. };
  61. }
  62. readMethods = readMethods.toString().slice(24, -2)
  63. .replace(/^ /gm, '')
  64. .replace('TERMINFO', JSON.stringify(fs.readFileSync(infoPath, 'base64')))
  65. .replace('TERMINFO_NAME', JSON.stringify(path.basename(infoPath)))
  66. .replace('TERMINFO_PATH', JSON.stringify(infoPathFake))
  67. .replace('TERMCAP', JSON.stringify(fs.readFileSync(capPath, 'utf8')))
  68. .replace('TERMCAP_NAME', JSON.stringify(path.basename(capPath, '.termcap')));
  69. /**
  70. * Helpers
  71. */
  72. function end(file, offset) {
  73. return file.split(path.sep).slice(-offset).join('/');
  74. }
  75. /**
  76. * Expose
  77. */
  78. module.exports = function(file) {
  79. if (end(file, 2) === 'lib/widget.js') {
  80. return transformer(requireWidgets);
  81. }
  82. if (end(file, 2) === 'lib/tput.js') {
  83. return transformer(readMethods);
  84. }
  85. return transformer();
  86. };