|
'use strict'; |
|
|
|
var path = require('path'); |
|
var isNegated = require('is-negated-glob'); |
|
|
|
module.exports = function (glob, options) { |
|
|
|
var opts = options || {}; |
|
|
|
|
|
var cwd = unescape(opts.cwd ? opts.cwd : process.cwd()); |
|
cwd = path.resolve(cwd); |
|
cwd = unixify(cwd); |
|
cwd = escape(cwd); |
|
|
|
var rootDir = opts.root; |
|
|
|
if (rootDir) { |
|
rootDir = unescape(rootDir); |
|
rootDir = unixify(rootDir); |
|
if (process.platform === 'win32' || !path.isAbsolute(rootDir)) { |
|
rootDir = unixify(path.resolve(rootDir)); |
|
} |
|
rootDir = escape(rootDir); |
|
} |
|
|
|
|
|
var suffix = glob.slice(-1); |
|
|
|
|
|
var ing = isNegated(glob); |
|
glob = ing.pattern; |
|
|
|
|
|
if (glob.slice(0, 2) === './') { |
|
glob = glob.slice(2); |
|
} |
|
|
|
|
|
if (glob.length === 1 && glob === '.') { |
|
glob = ''; |
|
} |
|
|
|
|
|
if (rootDir && glob.charAt(0) === '/') { |
|
glob = join(rootDir, glob); |
|
} else if (!path.isAbsolute(glob) || glob.slice(0, 1) === '\\') { |
|
glob = join(cwd, glob); |
|
} |
|
|
|
|
|
if (suffix === '/' && glob.slice(-1) !== '/') { |
|
glob += '/'; |
|
} |
|
|
|
|
|
return ing.negated ? '!' + glob : glob; |
|
}; |
|
|
|
function escape(path) { |
|
return path.replace(/([({[\]})*?!])/g, '\\$1'); |
|
} |
|
|
|
function unescape(path) { |
|
return path.replace(/\\([({[\]})*?!])/g, '$1'); |
|
} |
|
|
|
|
|
|
|
function unixify(filepath) { |
|
return filepath.replace(/\\/g, '/'); |
|
} |
|
|
|
function join(dir, glob) { |
|
if (dir.charAt(dir.length - 1) === '/') { |
|
dir = dir.slice(0, -1); |
|
} |
|
if (glob.charAt(0) === '/') { |
|
glob = glob.slice(1); |
|
} |
|
if (!glob) return dir; |
|
|
|
|
|
while (glob.slice(0, 3) === '../') { |
|
dir = dir.slice(0, dir.lastIndexOf('/')); |
|
glob = glob.slice(3); |
|
} |
|
|
|
return dir + '/' + glob; |
|
} |
|
|