/
deps.ts
75 lines (66 loc) · 1.89 KB
/
deps.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
import module from 'node:module'
export type InstalledPackage = 'vue-i18n' | 'petite-vue-i18n'
const _require = module.createRequire(import.meta.url)
// eslint-disable-next-line @typescript-eslint/ban-types
export function checkInstallPackage(
pkg: string,
debug: Function
): InstalledPackage {
let installedVueI18n = false
try {
debug(`vue-i18n load path: ${_require.resolve('vue-i18n')}`)
installedVueI18n = true
} catch (e) {
debug(`cannot find 'vue-i18n'`, e)
}
let installedPetiteVueI18n = false
try {
debug(`petite-vue-i18n load path: ${_require.resolve('petite-vue-i18n')}`)
installedPetiteVueI18n = true
} catch (e) {
debug(`cannot find 'petite-vue-i18n'`, e)
}
if (installedVueI18n) {
return 'vue-i18n'
}
if (installedPetiteVueI18n) {
return 'petite-vue-i18n'
}
throw new Error(
`${pkg} requires 'vue-i18n' or 'petite-vue-i18n' to be present in the dependency tree.`
)
}
// eslint-disable-next-line @typescript-eslint/ban-types
export function checkVueI18nBridgeInstallPackage(debug: Function): boolean {
let ret = false
try {
debug(`vue-i18n-bridge load path: ${_require.resolve('vue-i18n-bridge')}`)
ret = true
} catch (e) {
debug(`cannot find 'vue-i18n-bridge'`, e)
}
return ret
}
type VueI18nVersion = '8' | '9' | 'unknown' | ''
export function getVueI18nVersion(debug: Function): VueI18nVersion {
const VueI18n = loadModule('vue-i18n', debug)
if (VueI18n == null) {
return ''
}
if (VueI18n.version && VueI18n.version.startsWith('8.')) {
return '8'
}
if (VueI18n.VERSION && VueI18n.VERSION.startsWith('9.')) {
return '9'
}
return 'unknown'
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function loadModule(moduleName: string, debug: Function): any {
try {
return _require(moduleName)
} catch (e) {
debug(`cannot load '${moduleName}'`, e)
return null
}
}