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