-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
54 lines (46 loc) · 1.23 KB
/
utils.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
import path from 'path'
import { execaSync } from 'execa'
import globalDirs from 'global-dirs'
export interface GlobalPackagesDirs {
npm?: string
yarn?: string
pnpm?: string
}
/**
* @borrowed from: https://github.com/sindresorhus/is-path-inside/blob/main/index.js
*/
export function isPathInside(childPath: string, parentPath?: string) {
if (!parentPath)
return false
const relation = path.relative(parentPath, childPath)
return Boolean(
relation
&& relation !== '..'
&& !relation.startsWith(`..${path.sep}`)
&& relation !== path.resolve(childPath),
)
}
export function getGlobalPackagesDirs(): GlobalPackagesDirs {
const npmGlobalDir = globalDirs.npm.packages
const yarnGlobalDir = globalDirs.yarn.packages
let pnpmGlobalDir
try {
// TODO: SO SLOW!!!!!
const { stdout } = execaSync('pnpm', ['root', '-g'])
pnpmGlobalDir = stdout.trim()
}
catch { }
return {
npm: npmGlobalDir,
yarn: yarnGlobalDir,
pnpm: pnpmGlobalDir,
}
}
export function isInstalledGlobally(globalDirs?: GlobalPackagesDirs) {
const gd = globalDirs || getGlobalPackagesDirs()
return (
isPathInside(__dirname, gd.npm)
|| isPathInside(__dirname, gd.yarn)
|| isPathInside(__dirname, gd.pnpm)
)
}