docs_vue2/one/build/generator.js

117 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2021-09-25 15:17:57 +08:00
import { statSync, writeFileSync } from 'fs'
2020-08-13 11:47:56 +08:00
import { resolve, relative, extname, basename, sep } from 'path'
import readdirpSync from 'recursive-readdir-sync'
import rimraf from 'rimraf'
import { copyFileSync, replaceExtSync } from './util'
import { renderDocToPage } from './page'
import { get, removeFile } from './deps'
2021-09-25 15:17:57 +08:00
import { getChangelogData } from './changelog'
2020-08-13 11:47:56 +08:00
const DOCS_DIR = resolve(__dirname, '../docs')
const PAGES_DIR = resolve(__dirname, '../../pages')
const DEMOS_DIR = resolve(__dirname, '../../components/demos')
const ASSETS_DIR = resolve(__dirname, '../../assets')
export function generatePages (file, stats) {
if (!file) {
rimraf.sync(PAGES_DIR)
rimraf.sync(DEMOS_DIR)
rimraf.sync(resolve(__dirname, './deps.json'))
console.log('Regenerating all files...')
handleFile(DOCS_DIR)
2021-09-25 15:17:57 +08:00
handleChangelog()
2020-08-13 11:47:56 +08:00
console.log('...done.')
} else {
handleFile(file, stats)
}
}
2021-09-25 15:17:57 +08:00
function handleChangelog () {
const changelogData = getChangelogData()
writeFileSync(
resolve(ASSETS_DIR, 'data', 'changelog.json'),
JSON.stringify(changelogData, null, 2)
)
}
2020-08-13 11:47:56 +08:00
function handleFile (file, stats) {
let segments = relative(DOCS_DIR, file).split(sep)
2021-09-25 15:17:57 +08:00
if (
segments.some(segment => {
return segment.startsWith('_') || segment.startsWith('.')
})
) {
2020-08-13 11:47:56 +08:00
return
}
let remove = stats ? stats.remove : false
let dir = stats ? stats.dir : statSync(file).isDirectory()
if (dir) {
if (remove) {
rimraf.sync(file)
return
}
let children = readdirpSync(file)
children.forEach(child => {
handleFile(child, remove)
})
return
}
let ext = extname(file).toLowerCase()
/* eslint-disable indent */
/* There seems to be something wrong with FECS here */
switch (ext) {
case '.md': {
if (remove) {
let relDest = replaceExtSync(relative(DOCS_DIR, file), 'vue')
rimraf.sync(resolve(PAGES_DIR, relDest))
console.log(`[${relDest}] removed.`)
} else {
let dest = relative(DOCS_DIR, file)
renderDocToPage(dest)
console.log(`[${dest}] synced.`)
}
break
}
case '.json': {
if (basename(file) === 'nav.json') {
copyFileSync(file, resolve(ASSETS_DIR, 'data', 'nav.json'))
console.log('[nav.json] synced.')
}
break
}
default: {
let relDest = relative(DOCS_DIR, file)
2021-09-25 15:17:57 +08:00
let dest =
relDest.split(sep).indexOf('demo') === -1
? resolve(PAGES_DIR, relDest)
: resolve(DEMOS_DIR, relDest)
2020-08-13 11:47:56 +08:00
if (remove) {
rimraf.sync(dest)
console.log(`[${relDest}] removed.`)
} else {
copyFileSync(file, dest)
console.log(`[${relDest}] synced.`)
}
break
}
}
/* eslint-enable indent */
if (remove) {
removeFile(file)
return
}
let deps = get(file)
if (deps) {
Object.keys(deps).forEach(dep => {
handleFile(dep)
})
}
}