-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.ts
130 lines (106 loc) · 2.77 KB
/
helpers.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
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
import path from 'node:path'
import { CWD, cjsRequire, isPkgAvailable, tryFile } from '@pkgr/core'
import isGlob from 'is-glob'
import { SCRIPT_RUNNERS, SCRIPT_EXECUTORS } from './constants.js'
export const tryRequirePkg = <T>(pkg: string): T | undefined => {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return cjsRequire(pkg)
} catch {}
}
export const isTsAvailable = () => isPkgAvailable('typescript')
export const isAngularAvailable = () => isPkgAvailable('@angular/core')
export const isMdxAvailable = () =>
isPkgAvailable('@mdx-js/mdx') || isPkgAvailable('@mdx-js/react')
export const isReactAvailable = () => isPkgAvailable('react')
export const isSvelteAvailable = () => isPkgAvailable('svelte')
export const isVueAvailable = () => isPkgAvailable('vue')
export const tryGlob = (
paths: string[],
options:
| string
| {
absolute?: boolean
baseDir?: string
ignore?: [string]
} = {},
) => {
const {
absolute = true,
baseDir = CWD,
ignore = ['**/node_modules/**'],
} = typeof options === 'string' ? { baseDir: options } : options
return paths.reduce<string[]>(
(acc, pkg) =>
[
...acc,
...(isGlob(pkg)
? tryRequirePkg<typeof import('fast-glob')>('fast-glob')!
.sync(pkg, {
cwd: baseDir,
ignore,
onlyFiles: false,
})
// https://github.com/mrmlnc/fast-glob/issues/379
.map(file => (absolute ? path.resolve(baseDir, file) : file))
: [tryFile(path.resolve(baseDir, pkg), true)]),
].filter(Boolean),
[],
)
}
/**
* type guard for non-empty values
*/
export const identify = <T>(
_: T,
): _ is Exclude<
T,
'' | (T extends boolean ? false : boolean) | null | undefined
> => !!_
/**
* flat array and remove nullish values
*/
export const arrayify = <
T,
R = T extends Array<infer S> ? NonNullable<S> : NonNullable<T>,
>(
...args: Array<R | R[]>
) =>
args.reduce<R[]>((arr, curr) => {
if (curr != null) {
arr.push(
...(Array.isArray(curr) ? curr.filter(it => it != null) : [curr]),
)
}
return arr
}, [])
export const getPackageManager = () => {
const execPath = process.env.npm_execpath
if (!execPath) {
return
}
if (/\byarn\b/.test(execPath)) {
return 'yarn'
}
if (/\bpnpm\b/.test(execPath)) {
return 'pnpm'
}
if (/\bnpm\b/.test(execPath)) {
return 'npm'
}
console.warn('unknown package manager:', execPath)
}
export const getScriptRunner = () => {
const pm = getPackageManager()
if (!pm) {
return
}
return SCRIPT_RUNNERS[pm]
}
export const getScriptExecutor = () => {
const pm = getPackageManager()
if (!pm) {
return
}
return SCRIPT_EXECUTORS[pm]
}