/**
 *@author       孙冬冬
 *@date         2020/3/4 10:38
 *@Copyright    天源迪科信息技术股份有限公司
 *@Description
 */
const fs = require('fs')
const chalk = require('chalk')
const path = require('path')
const fileSave = require('file-save')
const render = require('json-templater/string')

function getIndexWxss() {
  const files = fs.readdirSync(path.resolve(__dirname, '../src/components'))
  const indexWxssPath = path.resolve(__dirname, '../src/themes/default')
  let wxssGroup = []
  if (Array.isArray(files)) {
    wxssGroup = files.filter((item) => item.includes('ht-'))
  }
  wxssGroup = wxssGroup.map((item) => `@import '../../components/${item}/index.wxss';`)

  wxssGroup = wxssGroup.join('\n')

  const wxssTpl = fs.readFileSync(path.resolve(__dirname, './tpl/theme.wxss.tpl'), 'utf8')
  const content = render(wxssTpl, {
    componentCss: wxssGroup
  })
  const filePath = path.join(indexWxssPath, 'index.wxss')
  fileSave(filePath).write(content, 'utf8')
  console.log(chalk.green(`创建默认主题文件文件:${filePath}`))
}
exports.getIndexWxss = getIndexWxss
