Fs.write not adding the file to my repo on build from netlify

Hi! so I wanted to use netlify cms for one of my projects and I am a netlify newbie. so my collections export in JSON format extension: JSON , format: JSON and obviously netlify cms create a file, and each entry to the file is stored as independent JSON files so I wanted to combine all this json files into one json file on the build . Now the problem I am facing is on build in the local server there is no problem the code combines all JSON files into a new JSON file but when building on netlify the file is not created in the git repo ad no error is also thrown.

folder structure:-

-products _( collection output  )_
   --product1.json
   --product2.json
   --product3.json
-public
   --admin
      --config.yml
      --index.html
   --images
   --index.html 
   --main.js _( code to combine all products to one product.json file )_
-src
   --shared
      --products.json _( after combining output file . it is produced on build in  local server but not on netlify  )_
-package.json
-etc.

main.js:-

var fs = require("fs");
var path = require("path");
let jsonfile = []; 
const dirPath = path.join(__dirname, "../products")
fs.readdir(dirPath, (err, fileNames) => {
fileNames.forEach(filename => {
    const filePath = path.join(dirPath,"/",filename)
    let jsonData = require(filePath);
    jsonfile.push(jsonData);  
});
 fs.writeFile("./src/Shared/products/products.json", JSON.stringify(jsonfile), err => { 
    // Checking for errors 
   if (err) throw err;  
   console.log("Done writing"); // Success 
 }); 
});

package.json

"build": "node ./public/main.js && react-scripts build",
well, the node script seems to run when I console logged it but nothing else happens .
Well, this works perfectly in the local server but on build in netlify there are no errors thrown yet the file is not created.
well I am also open any ideas where I can get a combined JSON file directly from the CMS (I have aleady tried list but it looks a bit messsy .)

config.yml

backend:
  name: git-gateway
  branch: master # Branch to update (optional; defaults to master)

# These lines should *not* be indented
media_folder: "public/images/products" 
public_folder: "/images/products" 

collections:
  - name: "products" # Used in routes, e.g., /admin/collections/blog
    label: "Products" # Used in the UI
    extension: json
    format: json
    folder: "products" # The path to the folder where the documents are stored
    create: true # Allow users to create new documents in this collection
    slug: "{{slug}}" # Filename template, e.g., YYYY-MM-DD-title.md
    fields: # The fields for each document, usually in front matter
      - {label: "Title", name: "title", widget: "string"}
      - {label: "Short Title", name: "stitle", widget: "string"}
      - {label: "ID", name: "id", widget: "hidden", default: '{{year}}_{{month}}_{{day}}_{{hour}}_{{second}}_{{minute}}_{{day}}_{{slug}}'}
      - widget: relation
        name: category
        label: Related category
        required: false
        multiple: false
        hint: >-
          Category of product (The category should already be present in Category Tab )
        collection: category
        valueField: title
        searchFields: ["title"]
        displayFields: ["title"]
      - {label: "Orginal Price", name: "oldprice", widget: "number"}
      - {label: "Discounted Price", name: "newprice", widget: "number"}
      - {label: "Discount ?", name: "discount", widget: "boolean", default: false}
      - {label: "Description", name: "description", widget: "text"}
      - {label: "Cover Image", name: "coverimg", widget: "image"}
      - {label: "Main Image", name: "mainimg", widget: "image"}
      - {label: "Product Image 1", name: "prdctimg1", widget: "image", required: false}
      - {label: "Product Image 2", name: "prdctimg2", widget: "image", required: false}
      - {label: "Product Image 3", name: "prdctimg3", widget: "image", required: false}
      - {label: "Product Image 4", name: "prdctimg4", widget: "image", required: false}
      - {label: "Product Image 5", name: "prdctimg5", widget: "image", required: false}
      - {label: "Slider", name: "slider", widget: "boolean", default: false}
      - {label: "Top Selling", name: "top", widget: "boolean", default: false}
      - {label: "Featured product", name: "featured", widget: "boolean", default: false}
      - {label: "New Products", name: "new", widget: "boolean", default: true}

  - name: "category" # Used in routes, e.g., /admin/collections/blog
    label: "Categories" # Used in the UI
    extension: json
    format: json
    folder: "category" # The path to the folder where the documents are stored
    create: true # Allow users to create new documents in this collection
    slug: "{{slug}}" # Filename template, e.g., YYYY-MM-DD-title.md
    fields: # The fields for each document, usually in front matter
      - {label: "Categories", name: "title", widget: "string" }

Hi @crimzonarc, I recommend switching to a async/await interface via:
GitHub - jprichardson/node-fs-extra: Node.js: extra methods for the fs object like copy(), remove(), mkdirs() or
File System | Node.js v10.24.1 Documentation

to make the code easier to follow.

Then add log message on every line to make sure the files you’re expecting to exist are actually there.