-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
131 lines (106 loc) · 2.06 KB
/
index.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Created by user on 2018/3/29/029.
*/
import { SymGlobTree } from '@lazy-glob/util';
import {
join,
normalize,
sep,
dirname as _dirname,
basename as _basename,
} from 'upath2';
import { ITree } from '@lazy-glob/util/lib/types';
export type { ITree }
export function globToTree(data: string[]): ITree
{
return data.reduce(function (a, b)
{
b = normalize(b);
let dirname = _dirname(b);
let basename = _basename(b);
const isdir = b.slice(-1) === sep;
if (isdir)
{
basename += sep;
}
//console.log([dirname, basename]);
if (dirname === '.')
{
const f = a;
f[basename] = isdir ? null : basename;
}
else
{
const c = dirname
.split(sep)
;
if (c[0] === '.')
{
c.shift();
}
let f = a;
c.forEach(function (e)
{
e += sep;
// @ts-ignore
f[e] = f[e] || {};
// @ts-ignore
f = f[e];
});
f[basename] = isdir ? (f[basename] || {}) : basename;
if (isdir)
{
// @ts-ignore
f[basename][SymGlobTree] = true;
//console.dir({ b, basename, f })
}
}
return a;
}, {} as ITree);
}
export function treeToGlob(a: ITree, d: string[] = []): string[]
{
return Object.entries(a).reduce(function (a, b)
{
//console.log(b);
if (b[1] === null || typeof b[1] === 'string')
{
const k = (b[1] === null ? b[0] : b[1]) as string;
if (d.length)
{
a.push(join(...d as [string], k));
}
else
{
a.push(k);
}
}
else
{
const ls = treeToGlob(b[1], d.concat(b[0]));
if (b[1][SymGlobTree])
{
let k = b[0];
if (d.length)
{
a.push(join(...d as [string], k));
}
else
{
a.push(k);
}
}
a = a.concat(ls);
}
return a;
}, [] as string[]);
}
// @ts-ignore
if (process.env.TSDX_FORMAT !== 'esm')
{
Object.defineProperty(globToTree, "__esModule", { value: true });
Object.defineProperty(globToTree, "globToTree", { value: globToTree });
Object.defineProperty(globToTree, "treeToGlob", { value: treeToGlob });
Object.defineProperty(globToTree, "default", { value: globToTree });
}
export default globToTree