-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
Copy pathenv.js
261 lines (231 loc) · 6.12 KB
/
env.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const { execSync } = require('child_process')
const fs = require('fs')
const path = require('path')
const LRU = require('lru-cache')
const semver = require('semver')
const { Buffer } = require('buffer')
let _hasYarn
const _yarnProjects = new LRU({
max: 10,
maxAge: 1000
})
let _hasGit
const _gitProjects = new LRU({
max: 10,
maxAge: 1000
})
const DELIMITER = '\f'
// env detection
exports.hasYarn = () => {
if (process.env.VUE_CLI_TEST) {
return true
}
if (_hasYarn != null) {
return _hasYarn
}
try {
execSync('yarn --version', { stdio: 'ignore' })
return (_hasYarn = true)
} catch (e) {
return (_hasYarn = false)
}
}
exports.hasProjectYarn = (cwd) => {
if (_yarnProjects.has(cwd)) {
return checkYarn(_yarnProjects.get(cwd))
}
const lockFile = path.join(cwd, 'yarn.lock')
const result = fs.existsSync(lockFile)
_yarnProjects.set(cwd, result)
return checkYarn(result)
}
function checkYarn (result) {
if (result && !exports.hasYarn()) throw new Error(`The project seems to require yarn but it's not installed.`)
return result
}
exports.hasGit = () => {
if (process.env.VUE_CLI_TEST) {
return true
}
if (_hasGit != null) {
return _hasGit
}
try {
execSync('git --version', { stdio: 'ignore' })
return (_hasGit = true)
} catch (e) {
return (_hasGit = false)
}
}
exports.hasProjectGit = (cwd) => {
if (_gitProjects.has(cwd)) {
return _gitProjects.get(cwd)
}
let result
try {
execSync('git status', { stdio: 'ignore', cwd })
result = true
} catch (e) {
result = false
}
_gitProjects.set(cwd, result)
return result
}
let _hasPnpm
let _pnpmVersion
const _pnpmProjects = new LRU({
max: 10,
maxAge: 1000
})
function getPnpmVersion () {
if (_pnpmVersion != null) {
return _pnpmVersion
}
try {
_pnpmVersion = execSync('pnpm --version', {
stdio: ['pipe', 'pipe', 'ignore']
}).toString()
// there's a critical bug in pnpm 2
// https://github.com/pnpm/pnpm/issues/1678#issuecomment-469981972
// so we only support pnpm >= 3.0.0
_hasPnpm = true
} catch (e) {}
return _pnpmVersion || '0.0.0'
}
exports.hasPnpmVersionOrLater = (version) => {
if (process.env.VUE_CLI_TEST) {
return true
}
return semver.gte(getPnpmVersion(), version)
}
exports.hasPnpm3OrLater = () => {
return this.hasPnpmVersionOrLater('3.0.0')
}
exports.hasProjectPnpm = (cwd) => {
if (_pnpmProjects.has(cwd)) {
return checkPnpm(_pnpmProjects.get(cwd))
}
const lockFile = path.join(cwd, 'pnpm-lock.yaml')
const result = fs.existsSync(lockFile)
_pnpmProjects.set(cwd, result)
return checkPnpm(result)
}
function checkPnpm (result) {
if (result && !exports.hasPnpm3OrLater()) {
throw new Error(`The project seems to require pnpm${_hasPnpm ? ' >= 3' : ''} but it's not installed.`)
}
return result
}
const _npmProjects = new LRU({
max: 10,
maxAge: 1000
})
exports.hasProjectNpm = (cwd) => {
if (_npmProjects.has(cwd)) {
return _npmProjects.get(cwd)
}
const lockFile = path.join(cwd, 'package-lock.json')
const result = fs.existsSync(lockFile)
_npmProjects.set(cwd, result)
return result
}
// OS
exports.isWindows = process.platform === 'win32'
exports.isMacintosh = process.platform === 'darwin'
exports.isLinux = process.platform === 'linux'
const browsers = {}
let hasCheckedBrowsers = false
function tryRun (cmd) {
try {
return execSync(cmd, {
stdio: [0, 'pipe', 'ignore'],
timeout: 10000
}).toString().trim()
} catch (e) {
return ''
}
}
function getLinuxAppVersion (binary) {
return tryRun(`${binary} --version`).replace(/^.* ([^ ]*)/g, '$1')
}
function getMacAppVersion (bundleIdentifier) {
const bundlePath = tryRun(`mdfind "kMDItemCFBundleIdentifier=='${bundleIdentifier}'"`)
if (bundlePath) {
return tryRun(`/usr/libexec/PlistBuddy -c Print:CFBundleShortVersionString ${
bundlePath.replace(/(\s)/g, '\\ ')
}/Contents/Info.plist`)
}
}
exports.getInstalledBrowsers = () => {
if (hasCheckedBrowsers) {
return browsers
}
hasCheckedBrowsers = true
if (exports.isLinux) {
browsers.chrome = getLinuxAppVersion('google-chrome')
browsers.firefox = getLinuxAppVersion('firefox')
} else if (exports.isMacintosh) {
browsers.chrome = getMacAppVersion('com.google.Chrome')
browsers.firefox = getMacAppVersion('org.mozilla.firefox')
} else if (exports.isWindows) {
// get chrome stable version
// https://stackoverflow.com/a/51773107/2302258
const chromeQueryResult = tryRun(
'reg query "HKLM\\Software\\Google\\Update\\Clients\\{8A69D345-D564-463c-AFF1-A69D9E530F96}" /v pv /reg:32'
) || tryRun(
'reg query "HKCU\\Software\\Google\\Update\\Clients\\{8A69D345-D564-463c-AFF1-A69D9E530F96}" /v pv /reg:32'
)
if (chromeQueryResult) {
const matched = chromeQueryResult.match(/REG_SZ\s+(\S*)$/)
browsers.chrome = matched && matched[1]
}
// get firefox version
// https://community.spiceworks.com/topic/111518-how-to-determine-version-of-installed-firefox-in-windows-batchscript
const ffQueryResult = tryRun(
'reg query "HKLM\\Software\\Mozilla\\Mozilla Firefox" /v CurrentVersion'
)
if (ffQueryResult) {
const matched = ffQueryResult.match(/REG_SZ\s+(\S*)$/)
browsers.firefox = matched && matched[1]
}
}
return browsers
}
exports.getIpcPath = (id) => {
id = '/tmp/app.' + id
if (exports.isWindows) {
id = id.replace(/^\//, '')
id = id.replace(/\//g, '-')
id = `\\\\.\\pipe\\${id}`
}
return id
}
exports.encodeIpcData = (type, data) => {
if (!data && data !== false && data !== 0) {
data = {}
}
if (data._maxListeners) {
data = {}
}
const message = JSON.stringify({ type, data })
return Buffer.from(message + DELIMITER)
}
exports.decodeIpcData = (data) => {
if (data.slice(-1) !== DELIMITER || data.indexOf(DELIMITER) === -1) {
return
}
const messages = []
const lines = data.split(DELIMITER)
lines.pop()
for (const line of lines) {
try {
messages.push(JSON.parse(line))
} catch (error) {
messages.push({
type: 'error',
data: `Error handling data: ${error}`
})
}
}
return messages
}