aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/parse-log.ts18
1 files changed, 16 insertions, 2 deletions
diff --git a/scripts/parse-log.ts b/scripts/parse-log.ts
index 4e4f3df63..dccab0884 100755
--- a/scripts/parse-log.ts
+++ b/scripts/parse-log.ts
@@ -1,5 +1,5 @@
1import * as program from 'commander' 1import * as program from 'commander'
2import { createReadStream, readdirSync } from 'fs' 2import { createReadStream, readdirSync, statSync } from 'fs'
3import { join } from 'path' 3import { join } from 'path'
4import { createInterface } from 'readline' 4import { createInterface } from 'readline'
5import * as winston from 'winston' 5import * as winston from 'winston'
@@ -53,7 +53,7 @@ const logLevels = {
53} 53}
54 54
55const logFiles = readdirSync(CONFIG.STORAGE.LOG_DIR) 55const logFiles = readdirSync(CONFIG.STORAGE.LOG_DIR)
56const lastLogFile = logFiles[logFiles.length - 1] 56const lastLogFile = getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
57 57
58const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile) 58const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile)
59console.log('Opening %s.', path) 59console.log('Opening %s.', path)
@@ -77,3 +77,17 @@ function toTimeFormat (time: string) {
77 77
78 return new Date(timestamp).toISOString() 78 return new Date(timestamp).toISOString()
79} 79}
80
81// Thanks: https://stackoverflow.com/a/37014317
82function getNewestFile (files: string[], basePath: string) {
83 const out = []
84
85 files.forEach(file => {
86 const stats = statSync(basePath + '/' + file)
87 if (stats.isFile()) out.push({ file, mtime: stats.mtime.getTime() })
88 })
89
90 out.sort((a, b) => b.mtime - a.mtime)
91
92 return (out.length > 0) ? out[ 0 ].file : ''
93} \ No newline at end of file