Source: FileSystem.js

const fs = require('fs')
const path = require('path')

/**
 * @module FileSystem
 */

/**
 * This function creates a new path if it does not exist.
 * @param {string} pathName
 */
exports.createPathSync = function (pathName) {
  if (!fs.existsSync(pathName)) {
    fs.mkdirSync(pathName)
  }
}

/**
 * This function copies all path and files from a source directory to
 * a destination directory in a sync way.
 * @param {string} sourcePath
 * @param {string} destPath
 */
exports.copyAllSync = function (sourcePath, destPath = './') {
  const exists = fs.existsSync(sourcePath)
  const stats = exists && fs.statSync(sourcePath)
  const isDirectory = exists && stats.isDirectory()

  if (exists && isDirectory) {
    if (!fs.existsSync(destPath)) {
      fs.mkdirSync(destPath)
    }

    fs.readdirSync(sourcePath).forEach((childItemName) => {
      this.copyAllSync(
        path.join(sourcePath, childItemName),
        path.join(destPath, childItemName)
      )
    })
  } else {
    fs.writeFileSync(destPath, fs.readFileSync(sourcePath))
  }
}