node.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import {execPath, execArgv} from 'node:process';
  2. import path from 'node:path';
  3. import {safeNormalizeFileUrl} from '../arguments/file-url.js';
  4. // `execaNode()` is a shortcut for `execa(..., {node: true})`
  5. export const mapNode = ({options}) => {
  6. if (options.node === false) {
  7. throw new TypeError('The "node" option cannot be false with `execaNode()`.');
  8. }
  9. return {options: {...options, node: true}};
  10. };
  11. // Applies the `node: true` option, and the related `nodePath`/`nodeOptions` options.
  12. // Modifies the file commands/arguments to ensure the same Node binary and flags are re-used.
  13. // Also adds `ipc: true` and `shell: false`.
  14. export const handleNodeOption = (file, commandArguments, {
  15. node: shouldHandleNode = false,
  16. nodePath = execPath,
  17. nodeOptions = execArgv.filter(nodeOption => !nodeOption.startsWith('--inspect')),
  18. cwd,
  19. execPath: formerNodePath,
  20. ...options
  21. }) => {
  22. if (formerNodePath !== undefined) {
  23. throw new TypeError('The "execPath" option has been removed. Please use the "nodePath" option instead.');
  24. }
  25. const normalizedNodePath = safeNormalizeFileUrl(nodePath, 'The "nodePath" option');
  26. const resolvedNodePath = path.resolve(cwd, normalizedNodePath);
  27. const newOptions = {
  28. ...options,
  29. nodePath: resolvedNodePath,
  30. node: shouldHandleNode,
  31. cwd,
  32. };
  33. if (!shouldHandleNode) {
  34. return [file, commandArguments, newOptions];
  35. }
  36. if (path.basename(file, '.exe') === 'node') {
  37. throw new TypeError('When the "node" option is true, the first argument does not need to be "node".');
  38. }
  39. return [
  40. resolvedNodePath,
  41. [...nodeOptions, file, ...commandArguments],
  42. {ipc: true, ...newOptions, shell: false},
  43. ];
  44. };