forked from pnpm/pnpm
/
index.ts
59 lines (51 loc) · 1.95 KB
/
index.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
import path from 'path'
import fs from 'graceful-fs'
const LOG_FILENAME = 'node_modules/.pnpm-debug.log'
export default function (streamParser: Object) {
let logNum = 0
// Clean up previous log files
if (fs.existsSync(LOG_FILENAME)) fs.unlinkSync(LOG_FILENAME)
if (fs.existsSync(path.basename(LOG_FILENAME))) fs.unlinkSync(path.basename(LOG_FILENAME))
streamParser['on']('data', function (logObj: Object) {
if (isUsefulLog(logObj) && global['writeDebugLogFile'] !== false) {
const msgobj = getMessageObj(logObj)
const prettyLogs = prettify(msgobj)
const jsonLogs = JSON.stringify(prettyLogs, null, 2) + '\n'
const dest = fs.existsSync(path.dirname(LOG_FILENAME)) ? LOG_FILENAME : path.basename(LOG_FILENAME)
fs.appendFileSync(dest, jsonLogs)
logNum++
}
})
process.on('exit', (code: number) => {
if (code === 0 || global['writeDebugLogFile'] === false) {
// it might not exist, so it is OK if it fails
if (fs.existsSync(LOG_FILENAME)) fs.unlinkSync(LOG_FILENAME)
if (fs.existsSync(path.basename(LOG_FILENAME))) fs.unlinkSync(path.basename(LOG_FILENAME))
}
})
function getMessageObj (logobj: Object): Object {
const msgobj: { [key: string]: string } = {}
for (const key in logobj) {
if (['time', 'hostname', 'pid', 'level', 'name'].includes(key)) continue
msgobj[key] = logobj[key]
}
const logLevel: string = logobj['level']
const logName: string = logobj['name']
msgobj.key = `${logNum} ${logLevel} ${logName}`
return msgobj
}
function prettify (obj: Object): string | Object {
if (obj instanceof Error) {
let logMsg = obj.toString()
if (obj.stack) {
logMsg += `\n${obj.stack}`
}
return logMsg
}
if (Object.keys(obj).length === 1 && obj['message']) return obj['message']
return obj
}
}
function isUsefulLog (logObj: Object) {
return logObj['name'] !== 'pnpm:progress' || logObj['status'] !== 'downloading'
}