-
Notifications
You must be signed in to change notification settings - Fork 0
/
rootpath.ts
162 lines (125 loc) · 3.13 KB
/
rootpath.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* eslint complexity: [ 2, 6 ] */
// TODO: type naming overhaul
// TODO: consider fixing glob double negation on rootpath's side
export type Rootpath$Segment = (string | $Rootpath)
export type Rootpath$Path = (Rootpath$Segment | Rootpath$Path[])
export interface Rootpath$Resolver
{
(...args: Rootpath$Path[]): string,
}
export interface Rootpath$Constructor
{
new (...args: Rootpath$Path[]): $Rootpath,
(...args: Rootpath$Path[]): $Rootpath,
}
export interface $Rootpath extends Rootpath$Resolver
{
path: string,
resolve: Rootpath$Resolver,
relative (to: Rootpath$Segment): string,
partial: Rootpath$Constructor,
contains (it: Rootpath$Segment): boolean,
guard (inside: Rootpath$Segment, fn_error?: Function): void,
over (each: Iterable<Rootpath$Path>): string[],
toString (): string,
}
import def from 'def-prop'
import val from 'def-prop/val'
import path from 'path'
function Rootpath (...args: Rootpath$Path[]): $Rootpath
{
const base = determine(args)
const rootpath = function rootpath (...args: Rootpath$Path[])
{
return resolve(base, args)
}
def(rootpath, 'path', val(base, ':enum'))
def(rootpath, 'resolve', val(function resolve (...args: Rootpath$Path[])
{
return rootpath(args)
}))
def(rootpath, 'relative', val(function relative (to: Rootpath$Segment)
{
return path.relative(base, String(to))
}))
def(rootpath, 'partial', val(function partial (...args: Rootpath$Path[])
{
return Rootpath(rootpath(args))
}))
function contains (it: Rootpath$Segment)
{
it = resolve(it)
if (it === base) return true
return (it.indexOf(base) === 0) && (it.slice(base.length)[0] === path.sep)
}
def(rootpath, 'contains', val(contains))
def(rootpath, 'guard', val(function guard (inside: Rootpath$Segment, fn_error?: Function)
{
if (contains(inside)) return
const e = fn_error?.() ?? new TypeError(`rootpath/must_be_contained`)
throw e
}))
def(rootpath, 'over', val(function over (each: Iterable<Rootpath$Path>)
{
return [ ...each ].map(path => rootpath(path))
}))
def(rootpath, 'toString', val(function toString ()
{
return rootpath()
}))
return rootpath as $Rootpath
}
export default Rootpath as Rootpath$Constructor
import find_root from 'find-root'
function determine (args: Rootpath$Path[])
{
if (! args.length)
{
try
{
return find_root(process.cwd())
}
catch (e)
{
return resolve()
}
}
if (args.length === 1)
{
const arg = String(args[0])
if (arg.startsWith('file://'))
{
return path.dirname(arg.slice(7))
}
}
return resolve(args)
}
import flatten from 'lodash.flattendeep'
import globjoin from 'globjoin'
function resolve (...args: Rootpath$Path[]): string
{
args = (flatten(args) as Rootpath$Path[])
let resolved = ''
while (args.length && ! is_absolute(resolved))
{
const arg = String(args.pop())
resolved = globjoin(arg, resolved)
}
if (! is_absolute(resolved))
{
resolved = globjoin(process.cwd(), resolved)
}
if (resolved !== '/' && resolved.slice(-1) === '/')
{
resolved = resolved.slice(0, -1)
}
return resolved
}
function is_absolute (arg: string): boolean
{
if (arg[0] === '!')
{
arg = arg.slice(1)
}
return path.isAbsolute(arg)
}