Sin descripción
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.

runtime.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. -------------------------------------
  3. Introduction: Runtime Scripts
  4. -------------------------------------
  5. These functions are not invoked in the main firepit runtime
  6. but are written to the filesystem (via Function.toString())
  7. and then invoked from platform-specific .bat or .sh scripts
  8. Each of these scripts is designed to mimic a specific command
  9. which the Firebase CLI shells out to. It takes the same arguments
  10. and routes them to the correct place based on what the
  11. top-level command is.
  12. */
  13. /*
  14. -------------------------------------
  15. "node" Command
  16. -------------------------------------
  17. This function, when placed into a script with the function
  18. wrapper will take a command like "node ./script.js --foo'
  19. and correctly spawn "./script.js" while preserving the
  20. "--foo" argument.
  21. */
  22. exports.Script_NodeJS = function() {
  23. const execArgv = [];
  24. let script = "";
  25. const scriptArgv = [];
  26. /*
  27. When invoked, this script is passed arguments like...
  28. node {optional node args starting with --} script {args to the script}
  29. We loop through the args to split them properly for when we call.fork()
  30. */
  31. process.argv.slice(2).forEach((arg) => {
  32. if (!script) {
  33. if (arg.startsWith("--")) {
  34. execArgv.push(arg);
  35. } else {
  36. script = arg;
  37. }
  38. } else {
  39. scriptArgv.push(arg);
  40. }
  41. });
  42. require("child_process")
  43. .fork(script, scriptArgv, {
  44. env: process.env,
  45. cwd: process.cwd(),
  46. stdio: "inherit",
  47. execArgv
  48. })
  49. .on("exit", code => {
  50. process.exit(code);
  51. });
  52. };
  53. /*
  54. -------------------------------------
  55. "sh" Command
  56. -------------------------------------
  57. This function, when placed into a script with the function
  58. wrapper replicates the behavior of the system shell.
  59. The main change is that it adds locations onto the
  60. environment's PATH so it can locate our other shimmed
  61. tools. It finds references to "node" and ensures that
  62. they be redirected back into Firepit as well.
  63. */
  64. exports.Script_ShellJS = async function() {
  65. const path = require("path");
  66. const child_process = require("child_process");
  67. const isWin = process.platform === "win32";
  68. const args = process.argv.slice(2);
  69. appendToPath(isWin, [
  70. __dirname,
  71. path.join(process.cwd(), "node_modules/.bin")
  72. ]);
  73. let index;
  74. if ((index = args.indexOf("-c")) !== -1) {
  75. args.splice(index, 1);
  76. }
  77. args[0] = args[0].replace(process.execPath, "node");
  78. let [cmdRuntime, cmdScript, ...otherArgs] = args[0].split(" ");
  79. if (cmdRuntime === process.execPath) {
  80. cmdRuntime = "node";
  81. }
  82. let cmd;
  83. if (cmdRuntime === "node") {
  84. if ([".", "/"].indexOf(cmdScript[0]) === -1) {
  85. cmdScript = await getSafeCrossPlatformPath(
  86. isWin,
  87. path.join(process.cwd(), cmdScript)
  88. );
  89. }
  90. cmd = child_process.fork(cmdScript, otherArgs, {
  91. env: process.env,
  92. cwd: process.cwd(),
  93. stdio: "inherit"
  94. });
  95. } else {
  96. cmd = child_process.spawn(cmdRuntime, [cmdScript, ...otherArgs], {
  97. env: process.env,
  98. cwd: process.cwd(),
  99. stdio: "inherit",
  100. shell: true
  101. });
  102. }
  103. cmd.on("exit", code => {
  104. process.exit(code);
  105. });
  106. };