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.

shim.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict'
  2. // This module should *only* be loaded as a main script
  3. // by child processes wrapped by spawn-wrap. It sets up
  4. // argv to include the injected argv (including the user's
  5. // wrapper script) and any environment variables specified.
  6. //
  7. // If any argv were passed in (ie, if it's used to force
  8. // a wrapper script, and not just ensure that an env is kept
  9. // around through all the child procs), then we also set up
  10. // a require('spawn-wrap').runMain() function that will strip
  11. // off the injected arguments and run the main file.
  12. // wrap in iife for babylon to handle module-level return
  13. ;(function () {
  14. if (module !== require.main) {
  15. throw new Error('spawn-wrap: cli wrapper invoked as non-main script')
  16. }
  17. const Module = require('module')
  18. const path = require('path')
  19. const settings = require('./settings.json')
  20. const {debug} = require(settings.deps.debug)
  21. debug('shim', [process.argv[0]].concat(process.execArgv, process.argv.slice(1)))
  22. const foregroundChild = require(settings.deps.foregroundChild)
  23. const IS_WINDOWS = settings.isWindows
  24. const argv = settings.argv
  25. const nargs = argv.length
  26. const env = settings.env
  27. const key = settings.key
  28. const node = process.env['SW_ORIG_' + key] || process.execPath
  29. Object.assign(process.env, env)
  30. const needExecArgv = settings.execArgv || []
  31. // If the user added their OWN wrapper pre-load script, then
  32. // this will pop that off of the argv, and load the "real" main
  33. function runMain () {
  34. debug('runMain pre', process.argv)
  35. process.argv.splice(1, nargs)
  36. process.argv[1] = path.resolve(process.argv[1])
  37. delete require.cache[process.argv[1]]
  38. debug('runMain post', process.argv)
  39. Module.runMain()
  40. debug('runMain after')
  41. }
  42. // Argv coming in looks like:
  43. // bin shim execArgv main argv
  44. //
  45. // Turn it into:
  46. // bin settings.execArgv execArgv settings.argv main argv
  47. //
  48. // If we don't have a main script, then just run with the necessary
  49. // execArgv
  50. let hasMain = false
  51. for (let a = 2; !hasMain && a < process.argv.length; a++) {
  52. switch (process.argv[a]) {
  53. case '-i':
  54. case '--interactive':
  55. case '--eval':
  56. case '-e':
  57. case '-p':
  58. case '-pe':
  59. hasMain = false
  60. a = process.argv.length
  61. continue
  62. case '-r':
  63. case '--require':
  64. a += 1
  65. continue
  66. default:
  67. // TODO: Double-check what's going on
  68. if (process.argv[a].startsWith('-')) {
  69. continue
  70. } else {
  71. hasMain = a
  72. a = process.argv.length
  73. break
  74. }
  75. }
  76. }
  77. debug('after argv parse hasMain=%j', hasMain)
  78. if (hasMain > 2) {
  79. // if the main file is above #2, then it means that there
  80. // was a --exec_arg *before* it. This means that we need
  81. // to slice everything from 2 to hasMain, and pass that
  82. // directly to node. This also splices out the user-supplied
  83. // execArgv from the argv.
  84. const addExecArgv = process.argv.splice(2, hasMain - 2)
  85. needExecArgv.push(...addExecArgv)
  86. }
  87. if (!hasMain) {
  88. // we got loaded by mistake for a `node -pe script` or something.
  89. const args = process.execArgv.concat(needExecArgv, process.argv.slice(2))
  90. debug('no main file!', args)
  91. foregroundChild(node, args)
  92. return
  93. }
  94. // If there are execArgv, and they're not the same as how this module
  95. // was executed, then we need to inject those. This is for stuff like
  96. // --harmony or --use_strict that needs to be *before* the main
  97. // module.
  98. if (needExecArgv.length) {
  99. const pexec = process.execArgv
  100. if (JSON.stringify(pexec) !== JSON.stringify(needExecArgv)) {
  101. debug('need execArgv for this', pexec, '=>', needExecArgv)
  102. const sargs = pexec.concat(needExecArgv).concat(process.argv.slice(1))
  103. foregroundChild(node, sargs)
  104. return
  105. }
  106. }
  107. // At this point, we've verified that we got the correct execArgv,
  108. // and that we have a main file, and that the main file is sitting at
  109. // argv[2]. Splice this shim off the list so it looks like the main.
  110. process.argv.splice(1, 1, ...argv)
  111. // Unwrap the PATH environment var so that we're not mucking
  112. // with the environment. It'll get re-added if they spawn anything
  113. if (IS_WINDOWS) {
  114. for (const i in process.env) {
  115. if (/^path$/i.test(i)) {
  116. process.env[i] = process.env[i].replace(__dirname + ';', '')
  117. }
  118. }
  119. } else {
  120. process.env.PATH = process.env.PATH.replace(__dirname + ':', '')
  121. }
  122. const spawnWrap = require(settings.module)
  123. if (nargs) {
  124. spawnWrap.runMain = function (original) {
  125. return function () {
  126. spawnWrap.runMain = original
  127. runMain()
  128. }
  129. }(spawnWrap.runMain)
  130. }
  131. spawnWrap(argv, env, __dirname)
  132. debug('shim runMain', process.argv)
  133. delete require.cache[process.argv[1]]
  134. Module.runMain()
  135. // end iife wrapper for babylon
  136. })()