+import { registerTSPaths } from '../server/helpers/register-ts-paths'
+registerTSPaths()
+
import * as program from 'commander'
-import { createReadStream, readdirSync, statSync } from 'fs'
+import { createReadStream, readdir } from 'fs-extra'
import { join } from 'path'
import { createInterface } from 'readline'
import * as winston from 'winston'
import { labelFormatter } from '../server/helpers/logger'
-import { CONFIG } from '../server/initializers/constants'
+import { CONFIG } from '../server/initializers/config'
+import { mtimeSortFilesDesc } from '../shared/core-utils/logs/logs'
program
.option('-l, --level [level]', 'Level log (debug/info/warn/error)')
debug: logger.debug.bind(logger)
}
-const logFiles = readdirSync(CONFIG.STORAGE.LOG_DIR)
-const lastLogFile = getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
+run()
+ .then(() => process.exit(0))
+ .catch(err => console.error(err))
-const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile)
-console.log('Opening %s.', path)
+function run () {
+ return new Promise(async res => {
+ const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
+ const lastLogFile = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
-const rl = createInterface({
- input: createReadStream(path)
-})
+ const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile)
+ console.log('Opening %s.', path)
-rl.on('line', line => {
- const log = JSON.parse(line)
- // Don't know why but loggerFormat does not remove splat key
- Object.assign(log, { splat: undefined })
+ const stream = createReadStream(path)
- logLevels[log.level](log)
-})
+ const rl = createInterface({
+ input: stream
+ })
-function toTimeFormat (time: string) {
- const timestamp = Date.parse(time)
+ rl.on('line', line => {
+ const log = JSON.parse(line)
+ // Don't know why but loggerFormat does not remove splat key
+ Object.assign(log, { splat: undefined })
- if (isNaN(timestamp) === true) return 'Unknown date'
+ logLevels[ log.level ](log)
+ })
- return new Date(timestamp).toISOString()
+ stream.once('close', () => res())
+ })
}
// Thanks: https://stackoverflow.com/a/37014317
-function getNewestFile (files: string[], basePath: string) {
- const out = []
+async function getNewestFile (files: string[], basePath: string) {
+ const sorted = await mtimeSortFilesDesc(files, basePath)
- files.forEach(file => {
- const stats = statSync(basePath + '/' + file)
- if (stats.isFile()) out.push({ file, mtime: stats.mtime.getTime() })
- })
+ return (sorted.length > 0) ? sorted[ 0 ].file : ''
+}
+
+function toTimeFormat (time: string) {
+ const timestamp = Date.parse(time)
- out.sort((a, b) => b.mtime - a.mtime)
+ if (isNaN(timestamp) === true) return 'Unknown date'
- return (out.length > 0) ? out[ 0 ].file : ''
+ return new Date(timestamp).toISOString()
}