file-url.js 960 B

12345678910111213141516171819202122232425
  1. import {fileURLToPath} from 'node:url';
  2. // Allow some arguments/options to be either a file path string or a file URL
  3. export const safeNormalizeFileUrl = (file, name) => {
  4. const fileString = normalizeFileUrl(normalizeDenoExecPath(file));
  5. if (typeof fileString !== 'string') {
  6. throw new TypeError(`${name} must be a string or a file URL: ${fileString}.`);
  7. }
  8. return fileString;
  9. };
  10. // In Deno node:process execPath is a special object, not just a string:
  11. // https://github.com/denoland/deno/blob/f460188e583f00144000aa0d8ade08218d47c3c1/ext/node/polyfills/process.ts#L344
  12. const normalizeDenoExecPath = file => isDenoExecPath(file)
  13. ? file.toString()
  14. : file;
  15. export const isDenoExecPath = file => typeof file !== 'string'
  16. && file
  17. && Object.getPrototypeOf(file) === String.prototype;
  18. // Same but also allows other values, e.g. `boolean` for the `shell` option
  19. export const normalizeFileUrl = file => file instanceof URL ? fileURLToPath(file) : file;