-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathutil.js
167 lines (142 loc) · 4.31 KB
/
util.js
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
163
164
165
166
167
import * as fs from "fs"
import * as Path from "path"
import * as os from "os"
import { performance } from "perf_hooks"
import { stdoutStyle } from "./termstyle"
import { inspect } from "util"
export const json = (val, pretty, showHidden) => JSON.stringify(val, showHidden, pretty)
export const clock = () => performance.now()
// running on Windows?
export const isWindows = process.platform.startsWith("win")
// generic symbols
export const TYPE = Symbol("TYPE")
// runtimeRequire(id :string) :any
export function runtimeRequire(id) {
// _runtimeRequire is defined at compile time by build.js (== require)
try { return _runtimeRequire(id) } catch { return null }
}
runtimeRequire.resolve = id => {
try { return _runtimeRequire.resolve(id) } catch { return "" }
}
// isCLI is true if estrella is invoked directly and not imported as a module
export const isCLI = module.id == "." || process.mainModule.filename == __filename
export function repr(val, prettyOrOptions) {
let options = {
colors: stdoutStyle.ncolors > 0,
}
if (typeof prettyOrOptions == "object") {
options = { ...prettyOrOptions }
} else if (prettyOrOptions !== undefined) {
options.compact = !prettyOrOptions
}
return inspect(val, options)
}
export function resolveModulePackageFile(moduleSpec) {
const mainfile = runtimeRequire.resolve(moduleSpec)
let dir = Path.dirname(Path.resolve(mainfile))
let lastdir = Path.sep // lastdir approach to support Windows (not just check for "/")
while (dir != lastdir) {
let pfile = Path.join(dir, "package.json")
if (fs.existsSync(pfile)) {
return pfile
}
dir = Path.dirname(dir)
}
throw new Error(`package.json not found for module ${moduleSpec}`)
}
export function getModulePackageJSON(moduleSpec) {
const pfile = resolveModulePackageFile(moduleSpec)
return jsonparseFile(pfile)
}
let _tmpdir = ""
export function tmpdir() {
if (!_tmpdir) {
// Some systems return paths with symlinks.
// esbuild does "realpath" on some pathnames and thus reporting with esbuild's metafile
// may be incorrect if this is not canonical.
_tmpdir = fs.realpathSync.native(os.tmpdir())
}
return _tmpdir
}
export function fmtDuration(ms) {
return (
ms >= 59500 ? (ms/60000).toFixed(0) + "min" :
ms >= 999.5 ? (ms/1000).toFixed(1) + "s" :
ms.toFixed(2) + "ms"
)
}
export function fmtByteSize(bytes) {
return (
bytes >= 1024*1000 ? (bytes/(1024*1000)).toFixed(1) + "MB" :
bytes >= 1000 ? (bytes/1024).toFixed(1) + "kB" :
bytes + "B"
)
}
export function findInPATH(executableName) {
const exeFileMode = isWindows ? 0xFFFFFFFF : fs.constants.X_OK
const PATH = new Set((process.env.PATH || "").split(Path.delimiter))
for (let dir of PATH) {
let path = Path.join(Path.resolve(dir), executableName)
if (isWindows) {
path += ".cmd"
}
while (true) {
try {
let st = fs.statSync(path)
if (st.isSymbolicLink()) {
path = fs.realpathSync.native(path)
continue // try again
} else if (st.isFile() && (st.mode & exeFileMode)) {
return path
}
} catch (_) {
if (isWindows && path.endsWith(".cmd")) {
path = Path.join(Path.resolve(dir), executableName) + ".exe"
continue // try with .exe extension
}
}
break
}
}
return null
}
// jsonparse parses "relaxed" JSON which can be in JavaScript format
export function jsonparse(jsonText, filename /*optional*/) {
try {
return JSON.parse(json)
} catch (err) {
return require("vm").runInNewContext(
'(' + jsonText + ')',
{ /* sandbox */ },
{ filename, displayErrors: true }
)
}
}
export function jsonparseFile(filename) {
const json = fs.readFileSync(filename, "utf8")
try {
return jsonparse(json)
} catch (err) {
throw new Error(`failed to parse ${filename}: ${err.message || err}`)
}
}
// ~/hello => /home/user/hello
export function expandTildePath(path) {
const homedir = os.homedir()
if (path == "~") {
return homedir
}
if (path.startsWith("~" + Path.sep)) {
return homedir + path.substr(1)
}
return path
}
// /home/user/hello => ~/hello
export function tildePath(path) {
const s = Path.resolve(path)
const homedir = os.homedir()
if (s.startsWith(homedir)) {
return "~" + s.substr(homedir.length)
}
return s
}