node.d.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {
  2. type ExecFileOptionsWithStringEncoding,
  3. type ExecFileSyncOptionsWithStringEncoding,
  4. type PromiseWithChild,
  5. } from 'node:child_process';
  6. /**
  7. Converts a `URL` or path to a path.
  8. __Not available in browsers.__
  9. @example
  10. ```
  11. import path from 'node:path';
  12. import {toPath} from 'unicorn-magic';
  13. // `cwd` can be `URL` or a path string.
  14. const getUnicornPath = cwd => path.join(toPath(cwd), 'unicorn');
  15. ```
  16. */
  17. export function toPath(urlOrPath: URL | string): string;
  18. /**
  19. Finds the root directory of the given path.
  20. __Not available in browsers.__
  21. On Unix-based systems, the root is always `'/'`.
  22. On Windows, the root varies and includes the drive letter (e.g., `'C:\\'`).
  23. This function operates purely on paths and does not interact with the file system.
  24. @param path - The path or URL to check.
  25. @returns The root directory of the path.
  26. @example
  27. ```
  28. import {rootDirectory} from 'unicorn-magic';
  29. console.log(rootDirectory('/Users/x/y/z'));
  30. //=> '/'
  31. console.log(rootDirectory('C:\\Users\\x\\y\\z'));
  32. //=> 'C:\\'
  33. ```
  34. */
  35. export function rootDirectory(path: string | URL): string;
  36. /**
  37. Creates an iterable for traversing from a given start path up to the root directory.
  38. __Not available in browsers.__
  39. This function operates purely on paths and does not interact with the file system.
  40. @param startPath - The starting path. Can be relative.
  41. @returns An iterable that iterates over each parent directory up to the root.
  42. Tip: To stop iteration before reaching the root, use a `break` statement within a conditional check.
  43. @example
  44. ```
  45. import {traversePathUp} from 'unicorn-magic';
  46. for (const directory of traversePathUp('/Users/x/y/z')) {
  47. console.log(directory);
  48. //=> '/Users/x/y/z'
  49. //=> '/Users/x/y'
  50. //=> '/Users/x'
  51. //=> '/Users'
  52. //=> '/'
  53. }
  54. ```
  55. */
  56. export function traversePathUp(startPath: string | URL): Iterable<string>;
  57. /**
  58. Executes a file.
  59. Same as the built-in `execFile` but with:
  60. - Promise API
  61. - 10 MB `maxBuffer` instead of 1 MB
  62. @example
  63. ```
  64. import {execFile} from 'unicorn-magic';
  65. console.log(await execFile('ls', ['-l']));
  66. ```
  67. __Not available in browsers.__
  68. */
  69. export function execFile(
  70. file: string,
  71. arguments_: readonly string[],
  72. options?: ExecFileOptionsWithStringEncoding
  73. ): PromiseWithChild<{
  74. stdout: string;
  75. stderr: string;
  76. }>;
  77. /**
  78. Executes a file synchronously.
  79. Same as the built-in `execFileSync` but with:
  80. - String output instead of buffer (same as `execFile`)
  81. - Does not output `stderr` to the terminal by default (same as `execFile`)
  82. - 10 MB `maxBuffer` instead of 1 MB
  83. @example
  84. ```
  85. import {execFileSync} from 'unicorn-magic';
  86. console.log(execFileSync('ls', ['-l']));
  87. ```
  88. __Not available in browsers.__
  89. */
  90. export function execFileSync(
  91. file: string,
  92. arguments_?: readonly string[],
  93. options?: ExecFileSyncOptionsWithStringEncoding
  94. ): string;
  95. export * from './default.js';