write-log-file.js 775 B

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. const os = require("os");
  3. const path = require("path");
  4. const log = require("npmlog");
  5. const writeFileAtomic = require("write-file-atomic");
  6. module.exports.writeLogFile = writeLogFile;
  7. function writeLogFile(cwd) {
  8. let logOutput = "";
  9. log.record.forEach((m) => {
  10. let pref = [m.id, m.level];
  11. if (m.prefix) {
  12. pref.push(m.prefix);
  13. }
  14. pref = pref.join(" ");
  15. m.message
  16. .trim()
  17. .split(/\r?\n/)
  18. .map((line) => `${pref} ${line}`.trim())
  19. .forEach((line) => {
  20. logOutput += line + os.EOL;
  21. });
  22. });
  23. // this must be synchronous because it is called before process exit
  24. writeFileAtomic.sync(path.join(cwd, "lerna-debug.log"), logOutput);
  25. // truncate log after writing
  26. log.record.length = 0;
  27. }