/**
 * 文件相关操作在这里维护
 */
const fs = require('fs')
const fsEx = require('fs-extra')
const chalk = require('chalk')
const ejs = require('ejs')

/**
 * 动态渲染文件内容并写入到某文件
 * @param {string} src 模板绝对路径
 * @param {string} dist 写入到绝对路径
 * @param {object} data 模板参数
 * @returns
 */
function ejsWriteFile(src, dist, data) {
  return new Promise((resolve) => {
    ejs.renderFile(src, data, {}, (err, res) => {
      if (err) {
        console.log(chalk.red(`${err}`))
        resolve(false)
        return
      }
      fsEx.writeFileSync(dist, res)
      resolve(true)
    })
  })
}

// 判断是否是个目录
function isDirectory(path) {
  return fs.statSync(path).isDirectory()
}

module.exports = {
  ejsWriteFile,
  isDirectory,
}
