'Is there any way to find functions inside a JavaScript file read into a string using fs?

Ok, so this might be an awkward situation.

I have a JavaScript file called functions.js which has some functions inside of it:

function functionA () {
  console.log('This is the functionA being executed')
}

const functionB = () => {
  console.log('This is the functionB being executed')
}

I have another file where I read the content of functions.js and convert all of its content using the package fs to a string, something like:

const fs = require('fs')

const readFileContent = ({ functionName, handler }) => {
  const fileContent = fs.readFileSync('./functions.js', 'utf8')
  console.log(fileContent)
}

I want to locate the functions inside the string fileContent. Is there any easy way for doing that? Or any better approach?

My goal is to write a brand-new file for each function containing only the content of it. I.e.:

// functionA.js
function functionA () {
  console.log('This is the functionA being executed')
}

// functionB.js
const functionB = () => {
  console.log('This is the functionB being executed')
}


Sources

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

Source: Stack Overflow

Solution Source