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.

munge.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. const {isCmd, isNode, isNpm, isSh, getExeBasename} = require("./exe-type")
  3. const mungeCmd = require("./mungers/cmd")
  4. const mungeEnv = require("./mungers/env")
  5. const mungeNode = require("./mungers/node")
  6. const mungeNpm = require("./mungers/npm")
  7. const mungeSh = require("./mungers/sh")
  8. const mungeShebang = require("./mungers/shebang")
  9. /**
  10. * @typedef {object} InternalSpawnOptions Options for the internal spawn functions
  11. * `childProcess.ChildProcess.prototype.spawn` and `process.binding('spawn_sync').spawn`.
  12. * These are the options mapped by the `munge` function to intercept spawned processes and
  13. * handle the wrapping logic.
  14. *
  15. * @property {string} file File to execute: either an absolute system-dependent path or a
  16. * command name.
  17. * @property {string[]} args Command line arguments passed to the spawn process, including argv0.
  18. * @property {string | undefined} cwd Optional path to the current working directory passed to the
  19. * spawned process. Default: `process.cwd()`
  20. * @property {boolean} windowsHide Boolean controlling if the process should be spawned as
  21. * hidden (no GUI) on Windows.
  22. * @property {boolean} windowsVerbatimArguments Boolean controlling if Node should preprocess
  23. * the CLI arguments on Windows.
  24. * @property {boolean} detached Boolean controlling if the child process should keep its parent
  25. * alive or not.
  26. * @property {string[]} envPairs Array of serialized environment variable key/value pairs. The
  27. * variables serialized as `key + "=" + value`.
  28. * @property {import("child_process").StdioOptions} stdio Stdio options, with the same semantics
  29. * as the `stdio` parameter from the public API.
  30. * @property {number | undefined} uid User id for the spawn process, same as the `uid` parameter
  31. * from the public API.
  32. * @property {number | undefined} gid Group id for the spawn process, same as the `gid` parameter
  33. * from the public API.
  34. *
  35. * @property {string | undefined} originalNode Custom property only used by `spawn-wrap`. It is
  36. * used to remember the original Node executable that was intended to be spawned by the user.
  37. */
  38. /**
  39. * Returns updated internal spawn options to redirect the process through the shim and wrapper.
  40. *
  41. * This works on the options passed to `childProcess.ChildProcess.prototype.spawn` and
  42. * `process.binding('spawn_sync').spawn`.
  43. *
  44. * This function works by trying to identify the spawn process and map the options accordingly.
  45. * `spawn-wrap` recognizes most shells, Windows `cmd.exe`, Node and npm invocations; when spawn
  46. * either directly or through a script with a shebang line.
  47. * It also unconditionally updates the environment variables so bare `node` commands execute
  48. * the shim script instead of Node's binary.
  49. *
  50. * @param workingDir {string} Absolute system-dependent path to the directory containing the shim files.
  51. * @param options {InternalSpawnOptions} Original internal spawn options.
  52. * @return {InternalSpawnOptions} Updated internal spawn options.
  53. */
  54. function munge(workingDir, options) {
  55. const basename = getExeBasename(options.file);
  56. // XXX: dry this
  57. if (isSh(basename)) {
  58. options = mungeSh(workingDir, options)
  59. } else if (isCmd(basename)) {
  60. options = mungeCmd(workingDir, options)
  61. } else if (isNode(basename)) {
  62. options = mungeNode(workingDir, options)
  63. } else if (isNpm(basename)) {
  64. // XXX unnecessary? on non-windows, npm is just another shebang
  65. options = mungeNpm(workingDir, options)
  66. } else {
  67. options = mungeShebang(workingDir, options)
  68. }
  69. // now the options are munged into shape.
  70. // whether we changed something or not, we still update the PATH
  71. // so that if a script somewhere calls `node foo`, it gets our
  72. // wrapper instead.
  73. options = mungeEnv(workingDir, options)
  74. return options
  75. }
  76. module.exports = munge