import { CompPage } from './type' const context: any = require.context('@/views/pages/component-page/components', true) const map: Record = {} context.keys().map((k: string) => { const name = k.replace('./', '').replace(/\/\w+\.?\w+/g, '') initMap(name, map) apiHandler(k, name, map) summaryHandler(k, name, map) exampleHandler(k, name, map) }) console.log(map) export default Object.values(map) function apiHandler (key: string, name: string, map: Record>) { if (!key.endsWith('api.ts')) return const apis = (map[name] && map[name].apis) || [] apis.push(...context(key).default) } function summaryHandler (key: string, name: string, map: Record>) { if (!key.endsWith('summary.md')) return const summary: string = context(key) map[name].summary = summary const firstLine = summary.substr(0, summary.indexOf('')).replace(/]*>/, '') if (firstLine) { const [n, label] = firstLine.split(' ') map[name].name = n map[name].label = label } } function exampleHandler (key: string, name: string, map: Record>) { if (!key.endsWith('.vue')) return const example = { comp: context(key).default, source: '', title: '', desc: '' } const text = require('!raw-loader!' + '@/views/pages/component-page/components' + key.slice(1)).default example.source = text const tsi = text.indexOf('@title') + 6 const tei = text.indexOf('\n', tsi) example.title = text.substring(tsi, tei) const dsi = text.indexOf('@desc') + 5 const dei = text.indexOf('\n', dsi) example.desc = text.substring(dsi, dei) const examples = (map[name] && map[name].examples) || [] examples.push(example) } function initMap (name: string, map: Record>) { if (map[name]) return map[name] = { name: '', label: '', apis: [], summary: '', examples: [] } }