index.d.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. type CommonOptions = {
  2. /**
  3. Working directory.
  4. @default process.cwd()
  5. */
  6. readonly cwd?: string | URL;
  7. /**
  8. The path to the current Node.js executable.
  9. This can be either an absolute path or a path relative to the `cwd` option.
  10. @default [process.execPath](https://nodejs.org/api/process.html#processexecpath)
  11. */
  12. readonly execPath?: string | URL;
  13. /**
  14. Whether to push the current Node.js executable's directory (`execPath` option) to the front of PATH.
  15. @default true
  16. */
  17. readonly addExecPath?: boolean;
  18. /**
  19. Whether to push the locally installed binaries' directory to the front of PATH.
  20. @default true
  21. */
  22. readonly preferLocal?: boolean;
  23. };
  24. export type RunPathOptions = CommonOptions & {
  25. /**
  26. PATH to be appended.
  27. Set it to an empty string to exclude the default PATH.
  28. @default [`PATH`](https://github.com/sindresorhus/path-key)
  29. */
  30. readonly path?: string;
  31. };
  32. export type ProcessEnv = Record<string, string | undefined>;
  33. export type EnvOptions = CommonOptions & {
  34. /**
  35. Accepts an object of environment variables, like `process.env`, and modifies the PATH using the correct [PATH key](https://github.com/sindresorhus/path-key). Use this if you're modifying the PATH for use in the `child_process` options.
  36. @default [process.env](https://nodejs.org/api/process.html#processenv)
  37. */
  38. readonly env?: ProcessEnv;
  39. };
  40. /**
  41. Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries.
  42. @returns The augmented path string.
  43. @example
  44. ```
  45. import childProcess from 'node:child_process';
  46. import {npmRunPath} from 'npm-run-path';
  47. console.log(process.env.PATH);
  48. //=> '/usr/local/bin'
  49. console.log(npmRunPath());
  50. //=> '/Users/sindresorhus/dev/foo/node_modules/.bin:/Users/sindresorhus/dev/node_modules/.bin:/Users/sindresorhus/node_modules/.bin:/Users/node_modules/.bin:/node_modules/.bin:/usr/local/bin'
  51. ```
  52. */
  53. export function npmRunPath(options?: RunPathOptions): string;
  54. /**
  55. Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries.
  56. @returns The augmented [`process.env`](https://nodejs.org/api/process.html#process_process_env) object.
  57. @example
  58. ```
  59. import childProcess from 'node:child_process';
  60. import {npmRunPathEnv} from 'npm-run-path';
  61. // `foo` is a locally installed binary
  62. childProcess.execFileSync('foo', {
  63. env: npmRunPathEnv()
  64. });
  65. ```
  66. */
  67. export function npmRunPathEnv(options?: EnvOptions): ProcessEnv;