-
-
Notifications
You must be signed in to change notification settings - Fork 252
/
path-arg.ts
56 lines (49 loc) · 1.5 KB
/
path-arg.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
import { parse, resolve } from 'path'
import { inspect } from 'util'
import { RimrafAsyncOptions } from './index.js'
import platform from './platform.js'
const pathArg = (path: string, opt: RimrafAsyncOptions = {}) => {
const type = typeof path
if (type !== 'string') {
const ctor = path && type === 'object' && path.constructor
const received =
ctor && ctor.name ? `an instance of ${ctor.name}`
: type === 'object' ? inspect(path)
: `type ${type} ${path}`
const msg =
'The "path" argument must be of type string. ' + `Received ${received}`
throw Object.assign(new TypeError(msg), {
path,
code: 'ERR_INVALID_ARG_TYPE',
})
}
if (/\0/.test(path)) {
// simulate same failure that node raises
const msg = 'path must be a string without null bytes'
throw Object.assign(new TypeError(msg), {
path,
code: 'ERR_INVALID_ARG_VALUE',
})
}
path = resolve(path)
const { root } = parse(path)
if (path === root && opt.preserveRoot !== false) {
const msg = 'refusing to remove root directory without preserveRoot:false'
throw Object.assign(new Error(msg), {
path,
code: 'ERR_PRESERVE_ROOT',
})
}
if (platform === 'win32') {
const badWinChars = /[*|"<>?:]/
const { root } = parse(path)
if (badWinChars.test(path.substring(root.length))) {
throw Object.assign(new Error('Illegal characters in path.'), {
path,
code: 'EINVAL',
})
}
}
return path
}
export default pathArg