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