'How to run cmd command in each patch with NodeJS

I'm trying to make script for compiling all scss files in sub-folders to css which are too many. I managed to create array of all paths but still not working properly. Where I'm wrong?

const fs = require('fs')
const path = require('path')
const { exec } = require('child_process')

//Recursive take all scss files

const getAllFiles = function (dirPath, arrayOfFiles) {
  files = fs.readdirSync(dirPath)

  arrayOfFiles = arrayOfFiles || []

  files.forEach(function (file) {
    if (fs.statSync(dirPath + '/' + file).isDirectory()) {
      arrayOfFiles = getAllFiles(dirPath + '/' + file, arrayOfFiles)
    } else {
      arrayOfFiles.push(path.join(__dirname, dirPath, '/', file))
    }
  })

  return arrayOfFiles
}
const all = getAllFiles('./')
const scss = all.filter((file) => {
  const extension = file.split('.')[1]

  if (extension == 'scss') {
    return extension
  }
})

//here I'm trying to run the command sass --watch style.scss style.css --style compressed

scss.forEach((path) => {
  console.log(path)
  exec(
    `sass --watch style.scss style.css --style compressed`,
    { cwd: path },
    (error, stdout, stderr) => {
      if (error) {
        console.log(`error: ${error.message}`)
        return
      }
      if (stderr) {
        console.log(`stderr: ${stderr}`)
        return
      }
      console.log(`stdout: ${stdout}`)
    },
  )
})


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source