Skip to content

Commit 88f1cb1

Browse files
committed
add linux support
1 parent a6f5ace commit 88f1cb1

File tree

7 files changed

+196
-40
lines changed

7 files changed

+196
-40
lines changed

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"devDependencies": {
1111
"@types/node": "^22.13.1",
1212
"nodemon": "^3.1.9",
13+
"ts-node": "^10.9.2",
1314
"typescript": "^5.7.3"
1415
},
1516
"dependencies": {

example/pnpm-lock.yaml

Lines changed: 129 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { homedir } from 'os'
12
import { VrchatLogWatcher } from 'vrchat-log-watcher'
23

34
const log = new VrchatLogWatcher()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vrchat-log-watcher",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"description": "Tool to watch and parse VRChat log files in realtime",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
Instance as VrchatInstance,
1212
parseLocation,
1313
} from 'vrchat-location-parser'
14+
import { getVrchatLogDir } from './utils/getVrchatLogDir'
15+
import { getLatestLogfile, isVrchatLogFile } from './utils/getLatestLogfile'
1416

1517
export * from './interfaces/VrchatLogWatcherEventData'
1618

@@ -79,16 +81,9 @@ export class VrchatLogWatcher extends EventEmitter {
7981

8082
constructor() {
8183
super()
82-
this.vrchatLogDir = path.join(
83-
os.homedir(),
84-
'Appdata',
85-
'LocalLow',
86-
'VRChat',
87-
'VRChat',
88-
)
89-
84+
this.vrchatLogDir = getVrchatLogDir()
9085
// start logging on init
91-
this.currentLogFile = this.getLatestLogfile()
86+
this.currentLogFile = getLatestLogfile(this.vrchatLogDir)
9287
this.watchFile()
9388

9489
// watch new files in the loggin folder
@@ -98,10 +93,10 @@ export class VrchatLogWatcher extends EventEmitter {
9893
return
9994
}
10095
// is vrchat logfile
101-
if (!this.isVrchatLogFile(filename)) {
96+
if (!isVrchatLogFile(filename)) {
10297
return
10398
}
104-
const newLogFile = this.getLatestLogfile()
99+
const newLogFile = getLatestLogfile(this.vrchatLogDir)
105100
// is same logfile that current
106101
if (newLogFile === this.currentLogFile) {
107102
return
@@ -111,34 +106,6 @@ export class VrchatLogWatcher extends EventEmitter {
111106
})
112107
}
113108

114-
/**
115-
* check whether the filename could be a vrchat file
116-
* @param filename filename of a file
117-
* @returns whether the file could be a vrchat log file.
118-
*/
119-
private isVrchatLogFile = (filename: string) =>
120-
filename.startsWith('output_log_')
121-
122-
/**
123-
* get the latest log file path from vrchat folder
124-
* @returns the latest logfile path if found
125-
*/
126-
private getLatestLogfile = () => {
127-
const logFiles = fs
128-
.readdirSync(this.vrchatLogDir)
129-
.filter((x) => this.isVrchatLogFile(x))
130-
.map((x) => ({
131-
filename: x,
132-
createdAt: fs.statSync(path.join(this.vrchatLogDir, x)).ctime.getTime(),
133-
}))
134-
.sort((a, b) => b.createdAt - a.createdAt)
135-
136-
if (logFiles.length === 0) {
137-
return null
138-
}
139-
return path.join(this.vrchatLogDir, logFiles[0].filename)
140-
}
141-
142109
/**
143110
* Watches the current file
144111
*/
@@ -160,6 +127,9 @@ export class VrchatLogWatcher extends EventEmitter {
160127
this.emit('raw', data)
161128
this.parseLine(data)
162129
})
130+
this.tail.on('error', (error) => {
131+
this.tail?.unwatch()
132+
})
163133
}
164134

165135
/**

src/utils/getLatestLogfile.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as fs from 'fs'
2+
import * as path from 'path'
3+
4+
/**
5+
* check whether the filename could be a vrchat file
6+
* @param filename filename of a file
7+
* @returns whether the file could be a vrchat log file.
8+
*/
9+
export const isVrchatLogFile = (filename: string) =>
10+
filename.startsWith('output_log_')
11+
12+
/**
13+
* get the latest log file path from vrchat folder
14+
* @param logDir dir with vrchat log files
15+
* @returns the latest logfile path if found
16+
*/
17+
export const getLatestLogfile = (logDir: string) => {
18+
const logFiles = fs
19+
.readdirSync(logDir)
20+
.filter((x) => isVrchatLogFile(x))
21+
.map((x) => ({
22+
filename: x,
23+
createdAt: fs.statSync(path.join(logDir, x)).ctime.getTime(),
24+
}))
25+
.sort((a, b) => b.createdAt - a.createdAt)
26+
27+
if (logFiles.length === 0) {
28+
return null
29+
}
30+
return path.join(logDir, logFiles[0].filename)
31+
}

src/utils/getVrchatLogDir.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as path from 'path'
2+
import * as os from 'os'
3+
4+
export const getVrchatLogDir = () => {
5+
// windows
6+
if (process.platform == 'win32')
7+
return path.join(os.homedir(), 'Appdata', 'LocalLow', 'VRChat', 'VRChat')
8+
return path.join(
9+
os.homedir(),
10+
'.steam',
11+
'steam',
12+
'steamapps',
13+
'compatdata',
14+
'438100',
15+
'pfx',
16+
'drive_c',
17+
'users',
18+
'steamuser',
19+
'AppData',
20+
'LocalLow',
21+
'VRChat',
22+
'VRChat',
23+
)
24+
}

0 commit comments

Comments
 (0)