node.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import {promisify} from 'node:util';
  2. import {execFile as execFileCallback, execFileSync as execFileSyncOriginal} from 'node:child_process';
  3. import path from 'node:path';
  4. import {fileURLToPath} from 'node:url';
  5. const execFileOriginal = promisify(execFileCallback);
  6. export function toPath(urlOrPath) {
  7. return urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;
  8. }
  9. export function rootDirectory(pathInput) {
  10. return path.parse(toPath(pathInput)).root;
  11. }
  12. export function traversePathUp(startPath) {
  13. return {
  14. * [Symbol.iterator]() {
  15. let currentPath = path.resolve(toPath(startPath));
  16. let previousPath;
  17. while (previousPath !== currentPath) {
  18. yield currentPath;
  19. previousPath = currentPath;
  20. currentPath = path.resolve(currentPath, '..');
  21. }
  22. },
  23. };
  24. }
  25. const TEN_MEGABYTES_IN_BYTES = 10 * 1024 * 1024;
  26. export async function execFile(file, arguments_, options = {}) {
  27. return execFileOriginal(file, arguments_, {
  28. maxBuffer: TEN_MEGABYTES_IN_BYTES,
  29. ...options,
  30. });
  31. }
  32. export function execFileSync(file, arguments_ = [], options = {}) {
  33. return execFileSyncOriginal(file, arguments_, {
  34. maxBuffer: TEN_MEGABYTES_IN_BYTES,
  35. encoding: 'utf8',
  36. stdio: 'pipe',
  37. ...options,
  38. });
  39. }
  40. export * from './default.js';