bun.ts 881 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { dirname } from 'path';
  2. import fs from 'fs';
  3. import fsp from 'fs/promises';
  4. interface Peek {
  5. <T = undefined>(promise: T | Promise<T>): Promise<T> | T,
  6. status<T = undefined>(
  7. promise: T | Promise<T>,
  8. ): 'pending' | 'fulfilled' | 'rejected' | 'unknown'
  9. }
  10. const noopPeek = <T = undefined>(_: Promise<T>) => _;
  11. noopPeek.status = () => 'unknown';
  12. export const peek: Peek = typeof Bun !== 'undefined'
  13. ? Bun.peek
  14. : noopPeek as Peek;
  15. interface Write {
  16. (
  17. destination: string,
  18. input: NodeJS.TypedArray | string,
  19. ): Promise<unknown>
  20. }
  21. export const writeFile: Write = typeof Bun !== 'undefined'
  22. ? Bun.write
  23. : (async (destination: string, input) => {
  24. const dir = dirname(destination);
  25. if (!fs.existsSync(dir)) {
  26. await fsp.mkdir(dir, { recursive: true });
  27. }
  28. return fsp.writeFile(destination, input, { encoding: 'utf-8' });
  29. });