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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. const signalExit = require('signal-exit')
  2. /* istanbul ignore next */
  3. const spawn = process.platform === 'win32' ? require('cross-spawn') : require('child_process').spawn
  4. /**
  5. * Normalizes the arguments passed to `foregroundChild`.
  6. *
  7. * See the signature of `foregroundChild` for the supported arguments.
  8. *
  9. * @param fgArgs Array of arguments passed to `foregroundChild`.
  10. * @return Normalized arguments
  11. * @internal
  12. */
  13. function normalizeFgArgs(fgArgs) {
  14. let program, args, cb;
  15. let processArgsEnd = fgArgs.length;
  16. const lastFgArg = fgArgs[fgArgs.length - 1];
  17. if (typeof lastFgArg === "function") {
  18. cb = lastFgArg;
  19. processArgsEnd -= 1;
  20. } else {
  21. cb = (done) => done();
  22. }
  23. if (Array.isArray(fgArgs[0])) {
  24. [program, ...args] = fgArgs[0];
  25. } else {
  26. program = fgArgs[0];
  27. args = Array.isArray(fgArgs[1]) ? fgArgs[1] : fgArgs.slice(1, processArgsEnd);
  28. }
  29. return {program, args, cb};
  30. }
  31. /**
  32. *
  33. * Signatures:
  34. * ```
  35. * (program: string | string[], cb?: CloseHandler);
  36. * (program: string, args: string[], cb?: CloseHandler);
  37. * (program: string, ...args: string[], cb?: CloseHandler);
  38. * ```
  39. */
  40. function foregroundChild (...fgArgs) {
  41. const {program, args, cb} = normalizeFgArgs(fgArgs);
  42. const spawnOpts = { stdio: [0, 1, 2] }
  43. if (process.send) {
  44. spawnOpts.stdio.push('ipc')
  45. }
  46. const child = spawn(program, args, spawnOpts)
  47. const unproxySignals = proxySignals(process, child)
  48. process.on('exit', childHangup)
  49. function childHangup () {
  50. child.kill('SIGHUP')
  51. }
  52. child.on('close', (code, signal) => {
  53. // Allow the callback to inspect the child’s exit code and/or modify it.
  54. process.exitCode = signal ? 128 + signal : code
  55. let done = false;
  56. const doneCB = () => {
  57. if (done) {
  58. return
  59. }
  60. done = true
  61. unproxySignals()
  62. process.removeListener('exit', childHangup)
  63. if (signal) {
  64. // If there is nothing else keeping the event loop alive,
  65. // then there's a race between a graceful exit and getting
  66. // the signal to this process. Put this timeout here to
  67. // make sure we're still alive to get the signal, and thus
  68. // exit with the intended signal code.
  69. /* istanbul ignore next */
  70. setTimeout(function () {}, 200)
  71. process.kill(process.pid, signal)
  72. } else {
  73. process.exit(process.exitCode)
  74. }
  75. };
  76. const result = cb(doneCB);
  77. if (result && result.then) {
  78. result.then(doneCB);
  79. }
  80. })
  81. if (process.send) {
  82. process.removeAllListeners('message')
  83. child.on('message', (message, sendHandle) => {
  84. process.send(message, sendHandle)
  85. })
  86. process.on('message', (message, sendHandle) => {
  87. child.send(message, sendHandle)
  88. })
  89. }
  90. return child
  91. }
  92. /**
  93. * Starts forwarding signals to `child` through `parent`.
  94. *
  95. * @param parent Parent process.
  96. * @param child Child Process.
  97. * @return `unproxy` function to stop the forwarding.
  98. * @internal
  99. */
  100. function proxySignals (parent, child) {
  101. const listeners = new Map()
  102. for (const sig of signalExit.signals()) {
  103. const listener = () => child.kill(sig)
  104. listeners.set(sig, listener)
  105. parent.on(sig, listener)
  106. }
  107. return function unproxySignals () {
  108. for (const [sig, listener] of listeners) {
  109. parent.removeListener(sig, listener)
  110. }
  111. }
  112. }
  113. module.exports = foregroundChild