/**
 *@author       孙冬冬
 *@date         2020/3/11 9:42
 *@Copyright    天源迪科信息技术股份有限公司
 *@Description
 */
const fs = require('fs')
const chalk = require('chalk')
const path = require('path')
const fileSave = require('file-save')

function createRouteToApp() {
  const files = fs.readdirSync(path.resolve(__dirname, '../tools/demo/pages'))
  let wechatRoute = []
  if (Array.isArray(files)) {
    wechatRoute = files.filter((item) => item.includes('ht-'))
  }
  wechatRoute = wechatRoute.map((item) => `pages/${item}/index`)

  const navigate = ['pages/navigate/index']
  wechatRoute = navigate.concat(wechatRoute)
  fs.readFile(path.resolve(__dirname, '../tools/demo/app.json'), function (err, data) {
    if (err) {
      return console.error(err)
    }
    let app = data.toString()
    app = JSON.parse(app)
    app.pages = wechatRoute

    const str = JSON.stringify(app, null, 2)
    fs.writeFile(path.resolve(__dirname, '../tools/demo/app.json'), str, function (err) {
      if (err) {
        console.error(err)
      }
      console.log(chalk.green('组件路由已集成到app.json'))
    })
  })
}
exports.createRouteToApp = createRouteToApp
