-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
64 lines (54 loc) · 1.46 KB
/
index.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
'use strict'
const fs = require('fs')
const path = require('path')
let userHome = require('user-home')
if (process.platform === 'linux' && isRootUser(getUid())) {
userHome = path.resolve('/usr/local/share')
}
function getDirectory() {
let configDirectory
// use %LOCALAPPDATA%/Yarn on Windows
if (process.platform === 'win32' && process.env.LOCALAPPDATA) {
configDirectory = path.join(process.env.LOCALAPPDATA, 'Yarn', 'config')
} else {
// otherwise use ~/.config/yarn
configDirectory = path.join(userHome, '.config', 'yarn')
}
return path.join(configDirectory, 'global', 'node_modules')
}
function inDirectory(dir) {
return dir.indexOf(getDirectory()) !== -1
}
function getDependencies() {
try {
const dir = getDirectory()
return Object
.keys(require(path.join(dir, '../', 'package.json')).dependencies)
} catch (_) {
return []
}
}
function hasDependency(name) {
return getDependencies().indexOf(name) !== -1
}
function hasPackage(name) {
try {
return fs.existsSync(path.join(getDirectory(), name))
} catch (_) {
return false
}
}
function getUid() {
if (process.platform !== 'win32' && process.getuid) {
return process.getuid()
}
return null
}
function isRootUser(uid) {
return uid === 0
}
module.exports.getDirectory = getDirectory
module.exports.inDirectory = inDirectory
module.exports.getDependencies = getDependencies
module.exports.hasDependency = hasDependency
module.exports.hasPackage = hasPackage