-
Notifications
You must be signed in to change notification settings - Fork 6
/
vite-plugin-log-start.ts
71 lines (59 loc) · 1.57 KB
/
vite-plugin-log-start.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
import fs from 'fs';
import path from 'path';
import dayjs from 'dayjs';
import { ViteDevServer } from 'vite';
interface PluginOptions {
filePath?: string;
includeUser?: boolean;
}
interface LogEntry {
timestamp: string;
user?: string | null;
}
export default function vitePluginLogStart(options: PluginOptions = {}) {
const {
filePath = path.resolve(process.cwd(), './dev-start-log.json'),
includeUser = false,
} = options;
const getUserName = (): string | null => {
return includeUser
? process.env.USER || process.env.USERNAME || 'Unknown User'
: null;
};
const writeLog = (): void => {
const logEntry: LogEntry = {
timestamp: dayjs().format(),
};
if (includeUser) {
logEntry.user = getUserName();
}
let logs: LogEntry[] = [];
if (fs.existsSync(filePath)) {
try {
const data = fs.readFileSync(filePath, 'utf-8');
logs = JSON.parse(data);
if (!Array.isArray(logs)) {
logs = [];
}
} catch (error) {
console.error("Log faylini o'qishda xato:", error);
logs = [];
}
}
logs.push(logEntry);
try {
fs.writeFileSync(filePath, JSON.stringify(logs, null, 2), 'utf-8');
console.log(`\n✅ Dev server is running and log write file: ${filePath}`);
} catch (error) {
console.error('Error for your data write:', error);
}
};
return {
name: 'vite-plugin-log-start',
configureServer(server: ViteDevServer) {
server.httpServer?.once('listening', () => {
writeLog();
});
},
};
}