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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. 'use strict';
  2. module.exports = wrap
  3. wrap.runMain = runMain
  4. const Module = require('module')
  5. const fs = require('fs')
  6. const cp = require('child_process')
  7. const ChildProcess = cp.ChildProcess
  8. const assert = require('assert')
  9. const crypto = require('crypto')
  10. const IS_WINDOWS = require('is-windows')()
  11. const makeDir = require('make-dir')
  12. const rimraf = require('rimraf')
  13. const path = require('path')
  14. const signalExit = require('signal-exit')
  15. const {IS_DEBUG, debug} = require("./lib/debug")
  16. const munge = require("./lib/munge")
  17. const homedir = require("./lib/homedir")
  18. const shebang = process.platform === 'os390' ?
  19. '#!/bin/env ' : '#!'
  20. const shim = shebang + process.execPath + '\n' +
  21. fs.readFileSync(path.join(__dirname, 'shim.js'))
  22. function wrap(argv, env, workingDir) {
  23. const spawnSyncBinding = process.binding('spawn_sync')
  24. // if we're passed in the working dir, then it means that setup
  25. // was already done, so no need.
  26. const doSetup = !workingDir
  27. if (doSetup) {
  28. workingDir = setup(argv, env)
  29. }
  30. const spawn = ChildProcess.prototype.spawn
  31. const spawnSync = spawnSyncBinding.spawn
  32. function unwrap() {
  33. if (doSetup && !IS_DEBUG) {
  34. rimraf.sync(workingDir)
  35. }
  36. ChildProcess.prototype.spawn = spawn
  37. spawnSyncBinding.spawn = spawnSync
  38. }
  39. spawnSyncBinding.spawn = wrappedSpawnFunction(spawnSync, workingDir)
  40. ChildProcess.prototype.spawn = wrappedSpawnFunction(spawn, workingDir)
  41. return unwrap
  42. }
  43. function wrappedSpawnFunction (fn, workingDir) {
  44. return wrappedSpawn
  45. function wrappedSpawn (options) {
  46. const mungedOptions = munge(workingDir, options)
  47. debug('WRAPPED', mungedOptions)
  48. return fn.call(this, mungedOptions)
  49. }
  50. }
  51. function setup(argv, env) {
  52. if (argv && typeof argv === 'object' && !env && !Array.isArray(argv)) {
  53. env = argv
  54. argv = []
  55. }
  56. if (!argv && !env) {
  57. throw new Error('at least one of "argv" and "env" required')
  58. }
  59. if (argv) {
  60. assert(Array.isArray(argv), 'argv must be an array')
  61. } else {
  62. argv = []
  63. }
  64. if (env) {
  65. assert(typeof env === 'object', 'env must be an object')
  66. } else {
  67. env = {}
  68. }
  69. debug('setup argv=%j env=%j', argv, env)
  70. // For stuff like --use_strict or --harmony, we need to inject
  71. // the argument *before* the wrap-main.
  72. const execArgv = []
  73. for (let i = 0; i < argv.length; i++) {
  74. if (argv[i].startsWith('-')) {
  75. execArgv.push(argv[i])
  76. if (argv[i] === '-r' || argv[i] === '--require') {
  77. execArgv.push(argv[++i])
  78. }
  79. } else {
  80. break
  81. }
  82. }
  83. if (execArgv.length) {
  84. if (execArgv.length === argv.length) {
  85. argv.length = 0
  86. } else {
  87. argv = argv.slice(execArgv.length)
  88. }
  89. }
  90. const key = process.pid + '-' + crypto.randomBytes(6).toString('hex')
  91. let workingDir = path.resolve(homedir, `.node-spawn-wrap-${key}`)
  92. const settings = JSON.stringify({
  93. module: __filename,
  94. deps: {
  95. foregroundChild: require.resolve('foreground-child'),
  96. signalExit: require.resolve('signal-exit'),
  97. debug: require.resolve('./lib/debug')
  98. },
  99. isWindows: IS_WINDOWS,
  100. key,
  101. workingDir,
  102. argv,
  103. execArgv,
  104. env,
  105. root: process.pid
  106. }, null, 2) + '\n'
  107. if (!IS_DEBUG) {
  108. signalExit(() => rimraf.sync(workingDir))
  109. }
  110. makeDir.sync(workingDir)
  111. workingDir = fs.realpathSync(workingDir)
  112. if (IS_WINDOWS) {
  113. const cmdShim =
  114. '@echo off\r\n' +
  115. 'SETLOCAL\r\n' +
  116. 'CALL :find_dp0\r\n' +
  117. 'SET PATHEXT=%PATHEXT:;.JS;=;%\r\n' +
  118. '"' + process.execPath + '" "%dp0%node" %*\r\n' +
  119. 'EXIT /b %errorlevel%\r\n'+
  120. ':find_dp0\r\n' +
  121. 'SET dp0=%~dp0\r\n' +
  122. 'EXIT /b\r\n'
  123. fs.writeFileSync(path.join(workingDir, 'node.cmd'), cmdShim)
  124. fs.chmodSync(path.join(workingDir, 'node.cmd'), '0755')
  125. }
  126. fs.writeFileSync(path.join(workingDir, 'node'), shim)
  127. fs.chmodSync(path.join(workingDir, 'node'), '0755')
  128. const cmdname = path.basename(process.execPath).replace(/\.exe$/i, '')
  129. if (cmdname !== 'node') {
  130. fs.writeFileSync(path.join(workingDir, cmdname), shim)
  131. fs.chmodSync(path.join(workingDir, cmdname), '0755')
  132. }
  133. fs.writeFileSync(path.join(workingDir, 'settings.json'), settings)
  134. return workingDir
  135. }
  136. function runMain () {
  137. process.argv.splice(1, 1)
  138. process.argv[1] = path.resolve(process.argv[1])
  139. delete require.cache[process.argv[1]]
  140. Module.runMain()
  141. }