-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathcache.ts
59 lines (49 loc) · 1.53 KB
/
cache.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
import {readFileSync} from 'node:fs'
import {join} from 'node:path'
import {Config} from './config/config'
import {OclifConfiguration, Plugin} from './interfaces'
type CacheContents = {
rootPlugin: Plugin
config: Config
exitCodes: OclifConfiguration['exitCodes']
'@oclif/core': OclifCoreInfo
}
type OclifCoreInfo = {name: string; version: string}
type ValueOf<T> = T[keyof T]
/**
* A simple cache for storing values that need to be accessed globally.
*/
export default class Cache extends Map<keyof CacheContents, ValueOf<CacheContents>> {
static instance: Cache
public constructor() {
super()
this.set('@oclif/core', this.getOclifCoreMeta())
}
static getInstance(): Cache {
if (!Cache.instance) {
Cache.instance = new Cache()
}
return Cache.instance
}
public get(_key: 'config'): Config | undefined
public get(_key: '@oclif/core'): OclifCoreInfo
public get(_key: 'rootPlugin'): Plugin | undefined
public get(_key: 'exitCodes'): OclifConfiguration['exitCodes'] | undefined
public get(key: keyof CacheContents): ValueOf<CacheContents> | undefined {
return super.get(key)
}
private getOclifCoreMeta(): OclifCoreInfo {
try {
return {name: '@oclif/core', version: require('@oclif/core/package.json').version}
} catch {
try {
return {
name: '@oclif/core',
version: JSON.parse(readFileSync(join(__dirname, '..', 'package.json'), 'utf8')).version,
}
} catch {
return {name: '@oclif/core', version: 'unknown'}
}
}
}
}