-
Notifications
You must be signed in to change notification settings - Fork 766
Expand file tree
/
Copy pathpaths.ts
More file actions
28 lines (24 loc) · 1.08 KB
/
paths.ts
File metadata and controls
28 lines (24 loc) · 1.08 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
import * as Path from 'path';
import { isArray } from 'util';
import { Minimatch, IMinimatch } from 'minimatch';
function convertToMinimatch(pattern: string): IMinimatch {
// Ensure uniform absolute path cross OS
// (POSIX would resolve c:/path to /path/to/repo/c:/path without this check)
if (Path.isAbsolute(pattern) && Path.sep === '/') {
pattern = pattern.replace(/^\w:/, '');
}
if (pattern[0] !== '*') {
// pattern path is resolved even if it is an absolute path,
// to ensure correct format for the current OS
pattern = Path.resolve(pattern);
}
// Unify the path slashes before creating the minimatch, for more relyable matching
return new Minimatch(pattern.replace(/[\\]/g, '/'), { dot: true });
}
export function createMinimatch(pattern: string[]): IMinimatch[];
export function createMinimatch(pattern: string): IMinimatch;
export function createMinimatch(pattern: string | string[]): IMinimatch | IMinimatch[] {
return isArray(pattern)
? pattern.map(convertToMinimatch)
: convertToMinimatch(pattern);
}