import store from "../store";

export default {
  routes: () => store.state.apps.map(e => {
    return {
      ...e,
      component: () => import(`../views/apps/${e.entry}`)
    }
  }),
  init() {
    //约束正则式
    store.commit("cleanApps")
    // 自动化本工程应用
    this.loadApps()
  },
  loadApps() {
    //新App的自动化格式
    let files = require.context('../../packages', true, /\.(\/.+)\/App[^\/]+\.vue$/)
    files.keys().map(path => {
      if (files(path).default) {
        let {name, label} = files(path).default,
            addApp = {
              name, label: label || name,
              path: path.replace(/\.(\/.+\/App.+)\.vue$/, '$1'),
              entry: 'appEntry'
            }
        //命名规范入口文件必须以App开头
        store.commit("addApp", addApp)
      }
    })
    let cores = require.context('../../core', true, /\.(\/.+)\/App[^\/]+\.vue$/)
    cores.keys().map(path => {
      if (cores(path).default) {
        let {name, label} = cores(path).default,
            addApp = {
              name, label: label || name,
              path: path.replace(/\.(\/.+\/App.+)\.vue$/, '$1'),
              entry: 'appEntry'
            }
        //命名规范入口文件必须以App开头
        store.commit("addApp", addApp)
      }
    })
    let project = require.context('../../project', true, /\.(\/.+)\/App[^\/]+\.vue$/)
    project.keys().map(path => {
      if (project(path).default) {
        let {name, label} = project(path).default,
            addApp = {
              name, label: label || name,
              path: path.replace(/\.(\/.+\/App.+)\.vue$/, '$1'),
              entry: 'appEntry'
            }
        //命名规范入口文件必须以App开头
        store.commit("addApp", addApp)
      }
    })
  }
}
