-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathpath.ts
94 lines (87 loc) · 1.91 KB
/
path.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
import path from "path"
import os from "os"
/**
* Joins all given path segments and converts
* @param paths A sequence of path segments
*/
export function joinPath(...paths: Array<string>): string {
const joinedPath = path.join(...paths)
if (os.platform() === `win32`) {
return joinedPath.replace(/\\/g, `\\\\`)
}
return joinedPath
}
// copied from https://runpkg.com/[email protected]/lib/nodePaths.js
// and added `^internal/` test
const nodePaths = [
/^_debugger.js$/,
/^_http_agent.js$/,
/^_http_client.js$/,
/^_http_common.js$/,
/^_http_incoming.js$/,
/^_http_outgoing.js$/,
/^_http_server.js$/,
/^_linklist.js$/,
/^_stream_duplex.js$/,
/^_stream_passthrough.js$/,
/^_stream_readable.js$/,
/^_stream_transform.js$/,
/^_stream_writable.js$/,
/^_tls_legacy.js$/,
/^_tls_wrap.js$/,
/^assert.js$/,
/^buffer.js$/,
/^child_process.js$/,
/^cluster.js$/,
/^console.js$/,
/^constants.js$/,
/^crypto.js$/,
/^dgram.js$/,
/^dns.js$/,
/^domain.js$/,
/^events.js$/,
/^freelist.js$/,
/^fs.js$/,
/^http.js$/,
/^https.js$/,
/^module.js$/,
/^net.js$/,
/^os.js$/,
/^path.js$/,
/^punycode.js$/,
/^querystring.js$/,
/^readline.js$/,
/^repl.js$/,
/^smalloc.js$/,
/^stream.js$/,
/^string_decoder.js$/,
/^sys.js$/,
/^timers.js$/,
/^tls.js$/,
/^tty.js$/,
/^url.js$/,
/^util.js$/,
/^vm.js$/,
/^zlib.js$/,
/^node.js$/,
/^internal[/\\]/,
]
/**
* Checks if the file name matches a node path
* @param fileName File name
*/
export const isNodeInternalModulePath = (fileName: string): boolean =>
nodePaths.some(regTest => regTest.test(fileName))
/**
* Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar
*
* @param path
* @return slashed path
*/
export function slash(path: string): string {
const isExtendedLengthPath = /^\\\\\?\\/.test(path)
if (isExtendedLengthPath) {
return path
}
return path.replace(/\\/g, `/`)
}