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.

index.js 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const vm_1 = __importDefault(require("vm"));
  7. const repl_1 = __importDefault(require("repl"));
  8. const utils_1 = require("@wdio/utils");
  9. const constants_1 = require("./constants");
  10. class WDIORepl {
  11. constructor(config) {
  12. this._isCommandRunning = false;
  13. this._config = Object.assign(constants_1.DEFAULT_CONFIG, { eval: this.eval.bind(this) }, config);
  14. }
  15. eval(cmd, context, filename, callback) {
  16. if (this._isCommandRunning) {
  17. return;
  18. }
  19. if (cmd && constants_1.STATIC_RETURNS[cmd.trim()]) {
  20. return callback(null, constants_1.STATIC_RETURNS[cmd.trim()]);
  21. }
  22. vm_1.default.createContext(context);
  23. this._isCommandRunning = true;
  24. if (utils_1.hasWdioSyncSupport) {
  25. return utils_1.runFnInFiberContext(() => this._runCmd(cmd, context, callback))();
  26. }
  27. return this._runCmd(cmd, context, callback);
  28. }
  29. _runCmd(cmd, context, callback) {
  30. try {
  31. const result = vm_1.default.runInContext(cmd, context);
  32. return this._handleResult(result, callback);
  33. }
  34. catch (e) {
  35. this._isCommandRunning = false;
  36. return callback(e, undefined);
  37. }
  38. }
  39. _handleResult(result, callback) {
  40. if (!result || typeof result.then !== 'function') {
  41. this._isCommandRunning = false;
  42. return callback(null, result);
  43. }
  44. let timeoutCalled = false;
  45. const timeout = setTimeout(() => {
  46. callback(new Error('Command execution timed out'), undefined);
  47. this._isCommandRunning = false;
  48. timeoutCalled = true;
  49. }, this._config.commandTimeout);
  50. result.then((res) => {
  51. if (timeoutCalled) {
  52. return;
  53. }
  54. this._isCommandRunning = false;
  55. clearTimeout(timeout);
  56. return callback(null, res);
  57. }, (e) => {
  58. if (timeoutCalled) {
  59. return;
  60. }
  61. this._isCommandRunning = false;
  62. clearTimeout(timeout);
  63. const errorMessage = e ? e.message : 'Command execution timed out';
  64. const commandError = new Error(errorMessage);
  65. delete commandError.stack;
  66. return callback(commandError, undefined);
  67. });
  68. }
  69. start(context) {
  70. if (this._replServer) {
  71. throw new Error('a repl was already initialised');
  72. }
  73. if (context) {
  74. const evalFn = this._config.eval;
  75. this._config.eval = function (cmd, _, filename, callback) {
  76. return evalFn.call(this, cmd, context, filename, callback);
  77. };
  78. }
  79. this._replServer = repl_1.default.start(this._config);
  80. return new Promise((resolve) => {
  81. return this._replServer.on('exit', resolve);
  82. });
  83. }
  84. }
  85. exports.default = WDIORepl;
  86. WDIORepl.introMessage = constants_1.INTRO_MESSAGE;