prepare.js 4.5 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

  return options
}

async function resolveOptions (sourceDir) {
E
Evan You 已提交
33 34 35
  const vuepressDir = path.resolve(sourceDir, '.vuepress')
  const configPath = path.resolve(vuepressDir, 'config.js')
  const siteConfig = fs.existsSync(configPath) ? require(configPath) : {}
E
Evan You 已提交
36 37

  const options = {
E
Evan You 已提交
38
    siteConfig,
E
Evan You 已提交
39
    sourceDir,
E
Evan You 已提交
40 41 42
    outDir: siteConfig.dest
      ? path.resolve(siteConfig.dest)
      : path.resolve(sourceDir, '.vuepress/dist'),
E
Evan You 已提交
43 44
    publicPath: siteConfig.baseUrl || '/',
    pageFiles: await globby(['**/*.md', '!.vuepress'], { cwd: sourceDir })
E
Evan You 已提交
45 46
  }

E
Evan You 已提交
47 48 49 50 51 52
  // resolve theme
  const hasTheme = (
    siteConfig.theme ||
    fs.existsSync(path.resolve(vuepressDir, 'theme'))
  )

E
Evan You 已提交
53 54 55 56 57
  if (!hasTheme) {
    // use default theme
    options.themePath = path.resolve(__dirname, 'default-theme/Layout.vue')
    options.notFoundPath = path.resolve(__dirname, 'default-theme/NotFound.vue')
  } else {
E
Evan You 已提交
58 59 60 61
    // resolve custom theme
    const themeDir = siteConfig.theme
      ? path.resolve(__dirname, `../../${siteConfig.theme}`)
      : path.resolve(sourceDir, 'theme')
E
Evan You 已提交
62

E
Evan You 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
    const themePath = path.resolve(themeDir, 'Layout.vue')
    if (fs.existsSync(themePath)) {
      options.themePath = themePath
    } else {
      throw new Error('[vuepress] Custom theme must have a Layout.vue file.')
    }

    const notFoundPath = path.resolve(themeDir, '/NotFound.vue')
    if (fs.existsSync(notFoundPath)) {
      options.notFoundPath = notFoundPath
    } else {
      throw new Error('[vuepress] Custom theme must have a NotFound.vue file.')
    }
E
tweaks  
Evan You 已提交
76 77
  }

E
Evan You 已提交
78 79
  // resolve pages
  const pagesData = options.pageFiles.map(file => {
E
Evan You 已提交
80 81 82
    const urlPath = isIndexFile(file)
      ? '/'
      : `/${file.replace(/\.md$/, '').replace(/\\/g, '/')}.html`
E
Evan You 已提交
83
    const content = fs.readFileSync(path.resolve(sourceDir, file), 'utf-8')
E
tweaks  
Evan You 已提交
84
    const data = {
E
Evan You 已提交
85 86
      path: urlPath
    }
E
Evan You 已提交
87 88

    // extract yaml frontmatter
E
Evan You 已提交
89
    const frontmatter = yaml.loadFront(content)
E
Evan You 已提交
90 91 92 93 94
    // infer title
    const titleMatch = frontmatter.__content.match(/^#+\s+(.*)/)
    if (titleMatch) {
      data.title = titleMatch[1]
    }
E
Evan You 已提交
95
    delete frontmatter.__content
E
Evan You 已提交
96 97
    if (Object.keys(frontmatter).length) {
      data.frontmatter = frontmatter
E
Evan You 已提交
98
    }
E
tweaks  
Evan You 已提交
99
    return data
E
Evan You 已提交
100 101
  })

E
Evan You 已提交
102 103
  // resolve site data
  options.siteData = Object.assign({}, siteConfig.data, {
E
Evan You 已提交
104 105 106 107 108 109
    pages: pagesData
  })

  return options
}

E
Evan You 已提交
110
async function genComponentRegistrationFile ({ sourceDir, pageFiles }) {
E
Evan You 已提交
111 112 113 114
  function genImport (file) {
    const name = toComponentName(file)
    const baseDir = /\.md$/.test(file)
      ? sourceDir
E
Evan You 已提交
115
      : path.resolve(sourceDir, '.vuepress/components')
E
Evan You 已提交
116 117 118 119 120 121
    const absolutePath = path.resolve(baseDir, file)
    const code = `Vue.component(${JSON.stringify(name)}, () => import(${JSON.stringify(absolutePath)}))`
    return code
  }

  const components = (await resolveComponents(sourceDir)) || []
E
Evan You 已提交
122
  const all = [...pageFiles, ...components]
E
tweaks  
Evan You 已提交
123
  return `import Vue from 'vue'\n` + all.map(genImport).join('\n')
E
Evan You 已提交
124 125
}

E
Evan You 已提交
126 127
function toComponentName (file) {
  const isPage = /\.md$/.test(file)
E
Evan You 已提交
128 129 130 131 132 133 134 135 136
  const isIndex = isIndexFile(file)
  const normalizedName = isIndex
    ? 'index'
    : file.replace(/\.(vue|md)$/, '').replace(/\/|\\/g, '-')
  return isPage ? `page-${normalizedName}` : normalizedName
}

function isIndexFile (file) {
  return /^(index|readme)\.md$/i.test(file)
E
Evan You 已提交
137 138
}

E
Evan You 已提交
139
async function resolveComponents (sourceDir) {
E
Evan You 已提交
140
  const componentDir = path.resolve(sourceDir, '.vuepress/components')
E
Evan You 已提交
141 142 143
  if (!fs.existsSync(componentDir)) {
    return
  }
E
tweaks  
Evan You 已提交
144
  return await globby(['**/*.vue'], { cwd: componentDir })
E
Evan You 已提交
145 146
}

E
Evan You 已提交
147
async function genRoutesFile ({ siteData: { pages }}) {
E
tweaks  
Evan You 已提交
148
  function genRoute ({ path }) {
E
Evan You 已提交
149 150
    const code = `
    {
E
Evan You 已提交
151
      path: ${JSON.stringify(path)},
E
Evan You 已提交
152 153 154 155 156
      component: Theme
    }`
    return code
  }

E
tweaks  
Evan You 已提交
157
  return (
E
Evan You 已提交
158
    `import Theme from '~theme'\n` +
E
tweaks  
Evan You 已提交
159 160
    `export const routes = [${pages.map(genRoute).join(',')}\n]`
  )
E
Evan You 已提交
161
}