|
| 1 | +const fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | +const globby = require('globby') |
| 4 | +const yaml = require('yaml-front-matter') |
| 5 | +const tempPath = path.resolve(__dirname, 'app/.temp') |
| 6 | + |
| 7 | +module.exports = async function prepare (sourceDir) { |
| 8 | + // 1. load options |
| 9 | + const options = await resolveOptions(sourceDir) |
| 10 | + |
| 11 | + // 2. generate dynamic component registration file |
| 12 | + await genComponentRegistrationFile(options) |
| 13 | + |
| 14 | + // 3. generate routes |
| 15 | + await genRoutesFile(options) |
| 16 | + |
| 17 | + return options |
| 18 | +} |
| 19 | + |
| 20 | +async function resolveOptions (sourceDir) { |
| 21 | + const configPath = path.resolve(sourceDir, 'vuepress.config.js') |
| 22 | + const siteConfig = fs.existsSync(configPath) ? require(configPath) : {} |
| 23 | + |
| 24 | + const options = { |
| 25 | + siteConfig, |
| 26 | + sourceDir, |
| 27 | + publicPath: siteConfig.baseUrl || '/', |
| 28 | + themePath: path.resolve(__dirname, 'default-theme/App.vue'), |
| 29 | + templatePath: path.resolve(__dirname, 'default-theme/index.html'), |
| 30 | + pages: await globby(['**/*.md'], { cwd: sourceDir }) |
| 31 | + } |
| 32 | + |
| 33 | + // resolve theme & index template |
| 34 | + const themeDir = path.resolve(sourceDir, '_theme') |
| 35 | + if (fs.existsSync(themeDir)) { |
| 36 | + const template = path.resolve(themeDir, 'index.html') |
| 37 | + if (fs.existsSync(template)) { |
| 38 | + options.templatePath = template |
| 39 | + } |
| 40 | + |
| 41 | + const app = path.resolve(themeDir, 'App.vue') |
| 42 | + if (fs.existsSync(app)) { |
| 43 | + options.themePath = app |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + const pagesData = {} |
| 48 | + options.pages.forEach(file => { |
| 49 | + const name = file.replace(/\.md$/, '') |
| 50 | + const urlPath = '/' + (name === 'index' ? '' : name) |
| 51 | + const componentName = toComponentName(file) |
| 52 | + const content = fs.readFileSync(path.resolve(sourceDir, file), 'utf-8') |
| 53 | + const frontmatter = yaml.loadFront(content) |
| 54 | + delete frontmatter.__content |
| 55 | + pagesData[urlPath] = { |
| 56 | + name, |
| 57 | + path: urlPath, |
| 58 | + componentName, |
| 59 | + frontmatter |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + options.siteData = Object.assign({}, siteConfig.data, { |
| 64 | + pages: pagesData |
| 65 | + }) |
| 66 | + |
| 67 | + return options |
| 68 | +} |
| 69 | + |
| 70 | +function toComponentName (file) { |
| 71 | + const isPage = /\.md$/.test(file) |
| 72 | + return ( |
| 73 | + (isPage ? `page-` : ``) + |
| 74 | + file |
| 75 | + .replace(/\.(vue|md)$/, '') |
| 76 | + .replace(/\/|\\/, '-') |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +async function genComponentRegistrationFile ({ sourceDir, pages }) { |
| 81 | + function genImport (file) { |
| 82 | + const name = toComponentName(file) |
| 83 | + const baseDir = /\.md$/.test(file) |
| 84 | + ? sourceDir |
| 85 | + : path.resolve(sourceDir, '_components') |
| 86 | + const absolutePath = path.resolve(baseDir, file) |
| 87 | + const code = `Vue.component(${JSON.stringify(name)}, () => import(${JSON.stringify(absolutePath)}))` |
| 88 | + return code |
| 89 | + } |
| 90 | + |
| 91 | + const components = (await resolveComponents(sourceDir)) || [] |
| 92 | + const all = [...pages, ...components] |
| 93 | + const file = `import Vue from 'vue'\n` + all.map(genImport).join('\n') |
| 94 | + fs.writeFileSync(path.join(tempPath, 'register-components.js'), file) |
| 95 | +} |
| 96 | + |
| 97 | +async function resolveComponents (sourceDir) { |
| 98 | + const componentDir = path.resolve(sourceDir, '_components') |
| 99 | + if (!fs.existsSync(componentDir)) { |
| 100 | + return |
| 101 | + } |
| 102 | + return await globby(['*.vue'], { cwd: componentDir }) |
| 103 | +} |
| 104 | + |
| 105 | +async function genRoutesFile ({ sourceDir, pages }) { |
| 106 | + function genRoute (file) { |
| 107 | + const name = file.replace(/\.md$/, '') |
| 108 | + const code = ` |
| 109 | + { |
| 110 | + path: ${JSON.stringify('/' + (name === 'index' ? '' : name))}, |
| 111 | + component: Theme |
| 112 | + }` |
| 113 | + return code |
| 114 | + } |
| 115 | + |
| 116 | + const file = |
| 117 | + `import Theme from '~theme'\n` + |
| 118 | + `export default [${pages.map(genRoute).join(',')}\n]` |
| 119 | + fs.writeFileSync(path.join(tempPath, 'routes.js'), file) |
| 120 | +} |
0 commit comments