files_list.js

import fs from 'fs'
import path from 'path'

/**
 * Determines if a given file in a directory is a directory itself.
 *
 * @param {string} directoryItems - The directory path containing the file.
 * @param {string} file - The name of the file.
 * @returns {boolean} `true` if the file is a directory, `false` otherwise.
 */
function isDirectory (directoryItems, file) {
  return fs.statSync(path.join(directoryItems, file)).isDirectory()
}

/**
 * Recursively reads a directory and returns an array of all files.
 *
 * @param {string} dir - The directory path to read.
 * @returns {string[]} An array of file paths.
 */
function getDirectory (dir) {
  const directoryItems = fs.readdirSync(dir)
  const filelist = []

  directoryItems.forEach(file => {
    if (isDirectory(directoryItems, file)) {
      filelist.push(...readDir(path.join(directoryItems, file)))
    } else {
      filelist.push(path.join(directoryItems, file))
    }
  })
  return filelist
}

export { readDir, isDirectory }