import fs from 'fs-extra'
import { join } from 'path'
import Bluebird from 'bluebird'

/**
 * Get the size of a folder or a file.
 *
 * This function returns the actual file size of the folder (size), not the allocated space on disk (size on disk).
 * For more details between the difference, check this link:
 * https://www.howtogeek.com/180369/why-is-there-a-big-difference-between-size-and-size-on-disk/
 *
 * @param {string} path path to the file or the folder.
 */
async function getSize (path: string): Promise<number> {
  const stat = await fs.lstat(path)

  if (stat.isDirectory()) {
    const list = await fs.readdir(path)

    return Bluebird.resolve(list).reduce(async (prev: number, curr: string) => {
      const currPath = join(path, curr)

      const s = await fs.lstat(currPath)

      if (s.isDirectory()) {
        return prev + await getSize(currPath)
      }

      return prev + s.size
    }, 0)
  }

  return stat.size
}

export default getSize
