index.js 802 B

123456789101112131415161718192021222324252627282930
  1. var path = require('path');
  2. module.exports = function (basedir, relfiles) {
  3. if (relfiles) {
  4. var files = relfiles.map(function (r) {
  5. return path.resolve(basedir, r);
  6. });
  7. }
  8. else {
  9. var files = basedir;
  10. }
  11. var res = files.slice(1).reduce(function (ps, file) {
  12. if (!file.match(/^([A-Za-z]:)?\/|\\/)) {
  13. throw new Error('relative path without a basedir');
  14. }
  15. var xs = file.split(/\/+|\\+/);
  16. for (
  17. var i = 0;
  18. ps[i] === xs[i] && i < Math.min(ps.length, xs.length);
  19. i++
  20. );
  21. return ps.slice(0, i);
  22. }, files[0].split(/\/+|\\+/));
  23. // Windows correctly handles paths with forward-slashes
  24. return res.length > 1 ? res.join('/') : '/'
  25. };