-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContextEnv.ts
More file actions
106 lines (100 loc) · 2.67 KB
/
ContextEnv.ts
File metadata and controls
106 lines (100 loc) · 2.67 KB
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
import { platform, arch, release } from 'node:os'
/**
* Context environment information provider.
* @description Gathers current time and OS system information for context.
*/
export class ContextEnv {
/**
* Gets current context information as a readable string.
* @description Returns formatted time, OS, and working directory information.
* @returns Formatted context string
*/
getContext(): string {
const timeInfo: string = this.getTimeInfo()
const osInfo: string = this.getOSInfo()
const pathInfo: string = this.getPathInfo()
return `- Time: ${timeInfo}\n- OS: ${osInfo}\n- Path: ${pathInfo}`
}
/**
* Gets formatted time information.
* @description Returns current date/time with timezone.
* @returns Formatted time string
*/
private getTimeInfo(): string {
const now: Date = new Date()
const timeString: string = now
.toLocaleString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
})
.replace(
/(\d{1,2})\/(\d{1,2})\/(\d{4}),?\s*(\d{1,2}):(\d{1,2}):(\d{1,2})/,
'$3-$1-$2 $4:$5:$6'
)
const timezoneOffset: string =
now
.toLocaleTimeString('en-US', {
timeZoneName: 'short'
})
.split(' ')
.pop() ?? ''
return `${timeString} ${timezoneOffset}`
}
/**
* Gets formatted OS information.
* @description Returns operating system details.
* @returns Formatted OS string
*/
private getOSInfo(): string {
const osName: string = this.getOSName()
const osVersion: string = release()
const architecture: string = arch()
return `${osName} ${osVersion} ${architecture}`
}
/**
* Gets formatted path information.
* @description Returns current working directory.
* @returns Formatted path string
*/
private getPathInfo(): string {
return process.cwd()
}
/**
* Gets human-readable OS name.
* @description Converts platform to readable OS name.
* @returns OS name string
*/
private getOSName(): string {
switch (platform()) {
case 'darwin':
return 'macOS'
case 'win32':
return 'Windows'
case 'linux':
return 'Linux'
case 'aix':
return 'AIX'
case 'android':
return 'Android'
case 'freebsd':
return 'FreeBSD'
case 'haiku':
return 'Haiku'
case 'openbsd':
return 'OpenBSD'
case 'sunos':
return 'SunOS'
case 'cygwin':
return 'Cygwin'
case 'netbsd':
return 'NetBSD'
default:
return platform()
}
}
}