blob: 268fc478b43c51b2f0eec128d20df5af1eed5708 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2
3var path = require('path');
4var isNegated = require('is-negated-glob');
5var isAbsolute = require('is-absolute');
6
7module.exports = function(glob, options) {
8 // default options
9 var opts = options || {};
10
11 // ensure cwd is absolute
12 var cwd = path.resolve(opts.cwd ? opts.cwd : process.cwd());
13 cwd = unixify(cwd);
14
15 var rootDir = opts.root;
16 // if `options.root` is defined, ensure it's absolute
17 if (rootDir) {
18 rootDir = unixify(rootDir);
19 if (process.platform === 'win32' || !isAbsolute(rootDir)) {
20 rootDir = unixify(path.resolve(rootDir));
21 }
22 }
23
24 // trim starting ./ from glob patterns
25 if (glob.slice(0, 2) === './') {
26 glob = glob.slice(2);
27 }
28
29 // when the glob pattern is only a . use an empty string
30 if (glob.length === 1 && glob === '.') {
31 glob = '';
32 }
33
34 // store last character before glob is modified
35 var suffix = glob.slice(-1);
36
37 // check to see if glob is negated (and not a leading negated-extglob)
38 var ing = isNegated(glob);
39 glob = ing.pattern;
40
41 // make glob absolute
42 if (rootDir && glob.charAt(0) === '/') {
43 glob = join(rootDir, glob);
44 } else if (!isAbsolute(glob) || glob.slice(0, 1) === '\\') {
45 glob = join(cwd, glob);
46 }
47
48 // if glob had a trailing `/`, re-add it now in case it was removed
49 if (suffix === '/' && glob.slice(-1) !== '/') {
50 glob += '/';
51 }
52
53 // re-add leading `!` if it was removed
54 return ing.negated ? '!' + glob : glob;
55};
56
57function unixify(filepath) {
58 return filepath.replace(/\\/g, '/');
59}
60
61function join(dir, glob) {
62 if (dir.charAt(dir.length - 1) === '/') {
63 dir = dir.slice(0, -1);
64 }
65 if (glob.charAt(0) === '/') {
66 glob = glob.slice(1);
67 }
68 if (!glob) return dir;
69 return dir + '/' + glob;
70}