resolveFilePath.cjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. const node_path = require('node:path');
  5. const node_fs = require('node:fs');
  6. /**
  7. * Return the resolved file path, based on whether the provided parameter is
  8. * a directory or looks like a directory (ends in `path.sep`), in which case the file
  9. * name will be `location/defaultFileName`.
  10. *
  11. * If location points to a file or looks like a file, then it will just use that file.
  12. *
  13. * @param {string} location - The name of file or directory to be used
  14. * @param {string} cwd - Current working directory. Used for tests
  15. * @param {string} defaultFileName - Default filename to use when location is a directory
  16. * @returns {string} Resolved path to the file
  17. */
  18. function resolveFilePath(location, cwd, defaultFileName) {
  19. /*
  20. * Make sure path separators are normalized for environment/os.
  21. * Also, keep trailing path separator if present.
  22. */
  23. const normalizedLocation = node_path.normalize(location);
  24. const resolvedLocation = node_path.resolve(cwd, normalizedLocation);
  25. // If the location path ends with a separator, we assume is a directory.
  26. const looksLikeADirectory = location.endsWith(node_path.sep);
  27. if (
  28. looksLikeADirectory ||
  29. node_fs.lstatSync(resolvedLocation, { throwIfNoEntry: false })?.isDirectory()
  30. ) {
  31. // Return path to provided directory with the specified file name.
  32. return node_path.join(resolvedLocation, defaultFileName);
  33. }
  34. // Return normalized path to file.
  35. return resolvedLocation;
  36. }
  37. module.exports = resolveFilePath;