prepare.js 3.6 KB
Newer Older
E
Evan You 已提交
1 2 3 4 5 6 7 8 9 10 11
const fs = require('fs')
const path = require('path')
const globby = require('globby')
const yaml = require('yaml-front-matter')
const tempPath = path.resolve(__dirname, 'app/.temp')

module.exports = async function prepare (sourceDir) {
  // 1. load options
  const options = await resolveOptions(sourceDir)

  // 2. generate dynamic component registration file
E
tweaks  
Evan You 已提交
12
  const componentCode = await genComponentRegistrationFile(options)
E
Evan You 已提交
13 14

  // 3. generate routes
E
tweaks  
Evan You 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27
  const routesCode = await genRoutesFile(options)

  // 4. generate siteData
  const dataCode = `export const siteData = ${JSON.stringify(options.siteData, null, 2)}`

  fs.writeFileSync(
    path.join(tempPath, 'siteData.js'),
    [
      componentCode,
      routesCode,
      dataCode
    ].join('\n\n')
  )
E
Evan You 已提交
28 29 30 31 32 33 34 35 36 37 38 39

  return options
}

async function resolveOptions (sourceDir) {
  const configPath = path.resolve(sourceDir, 'vuepress.config.js')
  const siteConfig = fs.existsSync(configPath) ? require(configPath) : {}

  const options = {
    siteConfig,
    sourceDir,
    publicPath: siteConfig.baseUrl || '/',
E
tweaks  
Evan You 已提交
40 41
    themePath: path.resolve(__dirname, 'default-theme/Layout.vue'),
    notFoundPath: path.resolve(__dirname, 'default-theme/NotFound.vue'),
E
Evan You 已提交
42 43 44
    pages: await globby(['**/*.md'], { cwd: sourceDir })
  }

E
wip  
Evan You 已提交
45
  // resolve theme
E
tweaks  
Evan You 已提交
46
  const themePath = path.resolve(sourceDir, '_theme/Layout.vue')
E
wip  
Evan You 已提交
47 48
  if (fs.existsSync(themePath)) {
    options.themePath = themePath
E
Evan You 已提交
49 50
  }

E
tweaks  
Evan You 已提交
51 52 53 54 55 56
  const notFoundPath = path.resolve(sourceDir, '_theme/NotFound.vue')
  if (fs.existsSync(notFoundPath)) {
    options.notFoundPath = notFoundPath
  }

  const pagesData = options.pages.map(file => {
E
Evan You 已提交
57 58
    const name = file.replace(/\.md$/, '').replace(/\\/g, '/')
    const urlPath = '/' + (name === 'index' ? '' : (name + '.html'))
E
Evan You 已提交
59
    const content = fs.readFileSync(path.resolve(sourceDir, file), 'utf-8')
E
tweaks  
Evan You 已提交
60
    const data = {
E
Evan You 已提交
61 62
      path: urlPath
    }
E
Evan You 已提交
63 64

    // extract yaml frontmatter
E
Evan You 已提交
65
    const frontmatter = yaml.loadFront(content)
E
Evan You 已提交
66 67 68 69 70
    // infer title
    const titleMatch = frontmatter.__content.match(/^#+\s+(.*)/)
    if (titleMatch) {
      data.title = titleMatch[1]
    }
E
Evan You 已提交
71
    delete frontmatter.__content
E
Evan You 已提交
72 73
    if (Object.keys(frontmatter).length) {
      data.frontmatter = frontmatter
E
Evan You 已提交
74
    }
E
tweaks  
Evan You 已提交
75
    return data
E
Evan You 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  })

  options.siteData = Object.assign({}, siteConfig.data, {
    pages: pagesData
  })

  return options
}

async function genComponentRegistrationFile ({ sourceDir, pages }) {
  function genImport (file) {
    const name = toComponentName(file)
    const baseDir = /\.md$/.test(file)
      ? sourceDir
      : path.resolve(sourceDir, '_components')
    const absolutePath = path.resolve(baseDir, file)
    const code = `Vue.component(${JSON.stringify(name)}, () => import(${JSON.stringify(absolutePath)}))`
    return code
  }

  const components = (await resolveComponents(sourceDir)) || []
  const all = [...pages, ...components]
E
tweaks  
Evan You 已提交
98
  return `import Vue from 'vue'\n` + all.map(genImport).join('\n')
E
Evan You 已提交
99 100
}

E
Evan You 已提交
101 102 103 104 105 106 107 108 109 110
function toComponentName (file) {
  const isPage = /\.md$/.test(file)
  return (
    (isPage ? `page-` : ``) +
    file
      .replace(/\.(vue|md)$/, '')
      .replace(/\/|\\/g, '-')
  )
}

E
Evan You 已提交
111 112 113 114 115
async function resolveComponents (sourceDir) {
  const componentDir = path.resolve(sourceDir, '_components')
  if (!fs.existsSync(componentDir)) {
    return
  }
E
tweaks  
Evan You 已提交
116
  return await globby(['**/*.vue'], { cwd: componentDir })
E
Evan You 已提交
117 118
}

E
Evan You 已提交
119
async function genRoutesFile ({ sourceDir, siteData: { pages }}) {
E
tweaks  
Evan You 已提交
120
  function genRoute ({ path }) {
E
Evan You 已提交
121 122
    const code = `
    {
E
Evan You 已提交
123
      path: ${JSON.stringify(path)},
E
Evan You 已提交
124 125 126 127 128
      component: Theme
    }`
    return code
  }

E
tweaks  
Evan You 已提交
129
  return (
E
Evan You 已提交
130
    `import Theme from '~theme'\n` +
E
tweaks  
Evan You 已提交
131 132
    `export const routes = [${pages.map(genRoute).join(',')}\n]`
  )
E
Evan You 已提交
133
}