diff --git a/.changeset/wise-cameras-yawn.md b/.changeset/wise-cameras-yawn.md new file mode 100644 index 00000000000..8543a436cab --- /dev/null +++ b/.changeset/wise-cameras-yawn.md @@ -0,0 +1,5 @@ +--- +"@pnpm/file-reporter": minor +--- + +Logs will be added as they are created instead of all at once at the end. Prevents memory exceeded errors if there are too many logs. diff --git a/packages/file-reporter/src/index.ts b/packages/file-reporter/src/index.ts index fd7dc4b25db..589600d2041 100644 --- a/packages/file-reporter/src/index.ts +++ b/packages/file-reporter/src/index.ts @@ -4,49 +4,40 @@ import fs from 'graceful-fs' const LOG_FILENAME = 'node_modules/.pnpm-debug.log' export default function (streamParser: Object) { - const logs: 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)) { - logs.push(logObj) + 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 - try { - fs.unlinkSync(LOG_FILENAME) - } catch (err) {} - return + if (fs.existsSync(LOG_FILENAME)) fs.unlinkSync(LOG_FILENAME) + if (fs.existsSync(path.basename(LOG_FILENAME))) fs.unlinkSync(path.basename(LOG_FILENAME)) } - - const prettyLogs = getPrettyLogs() - const jsonLogs = JSON.stringify(prettyLogs, null, 2) - // Don't create a node_modules directory if it does not exist - const dest = fs.existsSync(path.dirname(LOG_FILENAME)) - ? LOG_FILENAME - : path.basename(LOG_FILENAME) - fs.writeFileSync(dest, jsonLogs, 'utf-8') }) - function getPrettyLogs () { - const prettyLogs = {} - logs.forEach((logObj, i) => { - // eslint-disable-next-line - const key = `${i} ${logObj['level']} ${logObj['name']}` - const msgobj = getMessageObj(logObj) - prettyLogs[key] = prettify(msgobj) - }) - return prettyLogs - } - function getMessageObj (logobj: Object): Object { - const msgobj = {} + 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 } diff --git a/packages/file-reporter/test/fixture/1/stdout b/packages/file-reporter/test/fixture/1/stdout index d3d9966885a..c9f8815d4d6 100644 --- a/packages/file-reporter/test/fixture/1/stdout +++ b/packages/file-reporter/test/fixture/1/stdout @@ -1,7 +1,9 @@ { - "0 info pnpm": "foo", - "1 warn pnpm": { - "foo": 1, - "bar": 2 - } -} \ No newline at end of file + "message": "foo", + "key": "0 info pnpm" +} +{ + "foo": 1, + "bar": 2, + "key": "1 warn pnpm" +}