index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. var pathPlatform = require('path-platform');
  2. module.exports = function (cwd, opts) {
  3. if (cwd === undefined) cwd = process.cwd();
  4. if (!opts) opts = {};
  5. var platform = opts.platform || process.platform;
  6. var isWindows = /^win/.test(platform);
  7. var path = isWindows ? pathPlatform.win32 : pathPlatform;
  8. var normalize = !isWindows ? path.normalize :
  9. path.normalize('c:') === 'c:.' ? fixNormalize(path.normalize) :
  10. path.normalize;
  11. var sep = isWindows ? /[\\\/]/ : '/';
  12. var init = isWindows ? '' : '/';
  13. var join = function (x, y) {
  14. var ps = [ x, y ].filter(function (p) {
  15. return p && typeof p === 'string'
  16. });
  17. return normalize(ps.join(isWindows ? '\\' : '/'));
  18. };
  19. var res = normalize(cwd)
  20. .split(sep)
  21. .reduce(function (acc,dir,ix) {
  22. return acc.concat(join(acc[ix], dir))
  23. }, [init])
  24. .slice(1)
  25. .reverse()
  26. ;
  27. if (res[0] === res[1]) return [ res[0] ];
  28. if (isWindows && /^\\/.test(cwd)) {
  29. return res.slice(0,-1).map(function (d) {
  30. var ch = d.charAt(0)
  31. return ch === '\\' ? d :
  32. ch === '.' ? '\\' + d.slice(1) :
  33. '\\' + d
  34. });
  35. }
  36. return res;
  37. function fixNormalize(fn) {
  38. return function(p) {
  39. return fn(p).replace(/:\.$/, ':')
  40. }
  41. }
  42. }