From 02c85d18fc2ac530004e668d072bc93059b88d2c Mon Sep 17 00:00:00 2001
From: MohammadHoseinPaymard <talacemi@gmail.com>
Date: Mon, 26 May 2025 18:18:14 +0330
Subject: [PATCH] update index.js and util.js

---
 index.js |  5 +++--
 util.js  | 35 ++++++++++++++++++++++++++++++++++-
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/index.js b/index.js
index 0995459..d197652 100644
--- a/index.js
+++ b/index.js
@@ -1,7 +1,7 @@
 const path = require('path');
 const fs = require('fs');
 
-const { sleep } = require('./util');
+const { sleep, saveLargeObject } = require('./util');
 const { getAllWorkflows } = require('./workflow');
 const { getAllProcesses, getProcessPaths, getProcessPathsSubFlow } = require('./process');
 
@@ -106,7 +106,8 @@ require(path.join(process.cwd(), "..", "server", "dist", "database.js"));
     }
 
     // console.log("allWorkFlowInfo", JSON.stringify(allWorkFlowInfo, null, 2));
-    fs.writeFileSync("./result-pretty.json", JSON.stringify(allWorkFlowInfo, null, 2));
+    // fs.writeFileSync("./result-pretty.json", JSON.stringify(allWorkFlowInfo, null, 2));
+    saveLargeObject(allWorkFlowInfo,"./result-pretty.json")
     fs.writeFileSync("./result.json", JSON.stringify(allWorkFlowInfo));
     console.timeEnd("WorkflowTester");
     console.log("DONE");
diff --git a/util.js b/util.js
index 3c3da41..0ff367f 100644
--- a/util.js
+++ b/util.js
@@ -1,3 +1,5 @@
+const fs = require('fs');
+
 const sleep = (ms) => {
     return new Promise(resolve => {
         console.log(`${(ms / 1000).toFixed(2)}s SHOULD WAIT`);
@@ -383,6 +385,36 @@ function isValidRegex(str) {
       return false;
   }
 }
+
+async function saveLargeObject(obj, filename) {
+  const ws = fs.createWriteStream(filename, { encoding: 'utf8' });
+  ws.write('{\n');
+
+  const entries = Object.entries(obj);
+  for (let i = 0; i < entries.length; i++) {
+    const [key, value] = entries[i];
+    // stringify مقدار
+    const chunk = JSON.stringify(value, null, 2)
+      .split('\n')
+      .map((line, idx) => idx === 0
+        ? `  "${key}": ${line}`
+        : `     ${line}`)
+      .join('\n');
+    // اگر اولین نیست، قبلش کاما بگذار
+    ws.write((i > 0 ? ',\n' : '') + chunk);
+  }
+
+  ws.write('\n}\n');
+  ws.end();
+
+  await new Promise((res, rej) => {
+    ws.on('finish', res);
+    ws.on('error', rej);
+  });
+  console.log(`فایل ${filename} ذخیره شد.`);
+}
+
+
 module.exports = {
     sleep,
     convertStringToJson,
@@ -396,5 +428,6 @@ module.exports = {
     isBoolean,
     checkingConditionTypes,
     checkingOptions,
-    checkCondition
+    checkCondition,
+    saveLargeObject
 }
\ No newline at end of file