You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
980 B

11 months ago
  1. const path = require('node:path');
  2. const fs = require('node:fs');
  3. const { parentPort } = require('node:worker_threads');
  4. const documentsPath =
  5. process.env.NODE_ENV === "development"
  6. ? path.resolve(__dirname, `../../storage/documents`)
  7. : path.resolve(process.env.STORAGE_DIR, `documents`);
  8. function log(stringContent = '') {
  9. if (parentPort) parentPort.postMessage(`\x1b[33m[${process.pid}]\x1b[0m: ${stringContent}`); // running as worker
  10. else process.send(`\x1b[33m[${process.ppid}:${process.pid}]\x1b[0m: ${stringContent}`); // running as child_process
  11. }
  12. function conclude() {
  13. if (parentPort) parentPort.postMessage('done');
  14. else process.exit(0);
  15. }
  16. function updateSourceDocument(docPath = null, jsonContent = {}) {
  17. const destinationFilePath = path.resolve(documentsPath, docPath);
  18. fs.writeFileSync(destinationFilePath, JSON.stringify(jsonContent, null, 4), {
  19. encoding: "utf-8",
  20. });
  21. }
  22. module.exports = {
  23. log,
  24. conclude,
  25. updateSourceDocument,
  26. }