const fsExtra = require('fs-extra')
const path = require('path')
const {exec} = require('child_process')
const chalk = require('chalk')
const fs = require('fs')
/**
 * 将函数封装成promise
 */
const promisify = fn => {
  return function () {
    let args = arguments;
    return new Promise(function (resolve, reject) {
      [].push.call(args, function (err, result) {
        if (err) {
          console.log(err)
          reject(err);
        } else {
          resolve(result);
        }
      });
      fn.apply(null, args);
    });
  }
}
const readdir = promisify(fs.readdir)
const stat = promisify(fs.stat)
/**
 * 封装打印工具
 */
const {log} = console
const chalkTag = {
  info: msg => log([chalk.bgBlue.black(' INFO '), msg].join(' ')),
  done: msg => log([chalk.bgGreen.black(' DONE '), msg].join(' ')),
  warn: msg => log([chalk.bgYellow.black(' WARN '), msg].join(' ')),
  error: msg => log([chalk.bgRed.black(' ERROR '), msg].join(' ')),
}
/**
 * 遍历应用的方法
 */
const findApp = (dir, cb) => {
  return readdir(dir).then(apps => {
    return Promise.all(apps.map(e => {
      let cPath = path.join(dir, e)
      return stat(cPath).then(state => {
        if (state.isDirectory()) {
          return findApp(cPath, cb)
        } else if (state.isFile()) {
          cb && cb(cPath)
        }
      })
    }) || [])
  })
}
const start = () => {
  chalkTag.info("开始运行sass应用打包工具")
  let cores, apps, PROJECT = "sass"
  new Promise(resolve => {
    let install = path.join(__dirname, PROJECT, 'index.js')
    fsExtra.ensureFile(install)
    const coreLib = path.join(__dirname, PROJECT, 'core.import.json')

    fsExtra.readJson(coreLib, (err, data) => {
      chalkTag.info("加载核心库配置..")
      cores = []
      if (data) {
        findApp('core/apps', file => {
          let fileName = file.replace(/.*(App\w+)\.vue/g, '$1')
          if (Object.keys(data).includes(fileName)) {
            cores.push({name: fileName, component: file.replace(/\\/g, "/")})
            log(">>>正在打包核心库...%s", fileName)
          }
        }).then(() => {
          chalkTag.done('核心库打包完毕')
          resolve(PROJECT)
        })
      } else {
        chalkTag.done('核心库无打包')
        resolve(PROJECT)
      }
    })
  }).then(prj => {
    apps = []
    // return findApp("packages", file => {
    //   if (/.*\\(App[^\\]+)\.vue/g.test(file)) {
    //     let fileName = file.replace(/.*\\(App[^\\]+)\.vue/g, '$1')
    //     apps.push({name: fileName, component: file.replace(/\\/g, "/")})
    //     log(">>>正在打包业务应用...%s", fileName)
    //   }
    // }).then(() => {
    //   chalkTag.done('业务应用打包完毕')
    //   return prj
    // })
    return prj
  }).then(prj => new Promise(resolve => {
    chalkTag.info('正在生成打包文件...')
    let bin = path.join(__dirname, prj.toString(), 'index.js'),
        coreApps = cores.map(e => `{name:'${e.name}',component:require('../../${e.component}').default}`),
        bizApps = apps.map(e => `{name:'${e.name}',component:require('../../${e.component}').default}`),
        comps = [...coreApps, ...bizApps],
        content = `
        const apps = [${comps.toString()}]
        const install = function (Vue) {
          if (install.installed) return Promise.resolve()
          else{
            let contexts = require.context('.', true, /(\\/.+)\\/App[^\\/]+\\.vue$/)
            if (contexts) {
              contexts.keys().map(e => {
                if (contexts(e).default) {
                  let mod = apps.find(a=>a.name==contexts(e).default.name)
                  if(mod){
                    mod.component = contexts(e).default
                  }else{
                    apps.push({name:contexts(e).default.name,component:contexts(e).default})
                  }
                }
              })
            }
            apps.map(e=>{
               Vue.component(e.name,e.component)
            })
          }
        }
       // 判断是否是直接引入文件
        if (typeof window !== 'undefined' && window.Vue) {
          install(window.Vue)
        }
        export default {
            // 导出的对象必须具有 install，才能被 Vue.use() 方法安装
            install
         }
        `
    fsExtra.outputFile(bin, content, err => {
      if (err) {
        chalkTag.error(err)
      } else chalkTag.done('生成打包文件')
      resolve(prj)
    })
  })).then(project => new Promise(resolve => {
    fsExtra.readJson(path.join(__dirname, project.toString(), 'package.json'), (err, json) => {
      let cmd = `vue-cli-service build --target lib --dest project/${project}/dist project/${project}/index.js --name ${json.name}`
      chalkTag.info('正在压缩文件中...')
      exec(cmd, {
        cwd: path.join(__dirname, '..')
      }, (err, stdout) => {
        log(stdout);
        if (!err) {
          chalkTag.done('打包成功!')
          resolve(project)
        } else {
          chalkTag.error(err)
        }
      })
    })
  })).then(project => {
    chalkTag.info('正在发布中...')
    let cmd = `npm unpublish dvcp-sass-apps@1.0.0 --force&&npm publish`
    exec(cmd, {
      cwd: path.join(__dirname, project)
    }, (err, stdout) => {
      log(stdout);
      if (!err) {
        chalkTag.done('发布成功!')
      } else chalkTag.error(err)
    })
  })
}
start()
