説明なし

index.js 951B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. var path = require('path');
  3. var pathExists = require('path-exists');
  4. var Promise = require('pinkie-promise');
  5. function splitPath(x) {
  6. return path.resolve(x || '').split(path.sep);
  7. }
  8. function join(parts, filename) {
  9. return path.resolve(parts.join(path.sep) + path.sep, filename);
  10. }
  11. module.exports = function (filename, opts) {
  12. opts = opts || {};
  13. var parts = splitPath(opts.cwd);
  14. return new Promise(function (resolve) {
  15. (function find() {
  16. var fp = join(parts, filename);
  17. pathExists(fp).then(function (exists) {
  18. if (exists) {
  19. resolve(fp);
  20. } else if (parts.pop()) {
  21. find();
  22. } else {
  23. resolve(null);
  24. }
  25. });
  26. })();
  27. });
  28. };
  29. module.exports.sync = function (filename, opts) {
  30. opts = opts || {};
  31. var parts = splitPath(opts.cwd);
  32. var len = parts.length;
  33. while (len--) {
  34. var fp = join(parts, filename);
  35. if (pathExists.sync(fp)) {
  36. return fp;
  37. }
  38. parts.pop();
  39. }
  40. return null;
  41. };