is-local-path.js 686 B

12345678910111213141516171819
  1. "use strict";
  2. const fs = require("fs");
  3. const path = require("path");
  4. /**
  5. * Attempts to detect whether the string is a local path regardless of its
  6. * existence by checking its format. The point is to distinguish between
  7. * paths and modules on the npm registry. This will fail for non-existent
  8. * local Windows paths that begin with a drive letter, e.g. C:..\generator.js,
  9. * but will succeed for any existing files and any absolute paths.
  10. *
  11. * @param {String} str - string to check
  12. * @returns {Boolean} whether the string could be a path to a local file or directory
  13. */
  14. module.exports = function(str) {
  15. return path.isAbsolute(str) || /^\./.test(str) || fs.existsSync(str);
  16. };