Skip to content

Commit

Permalink
feat: copy public dir
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 2, 2020
1 parent ea4b21b commit ddc9d51
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
10 changes: 10 additions & 0 deletions src/build/build.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import path from 'path'
import { promises as fs } from 'fs'
import { bundle } from './bundle'
import { BuildOptions as ViteBuildOptions } from 'vite'
import { resolveConfig } from '../config'
import { renderPage } from './render'
import { exists, copyDir } from '../utils/fs'

export type BuildOptions = Pick<
ViteBuildOptions,
Expand All @@ -18,6 +20,14 @@ export async function build(buildOptions: BuildOptions = {}) {
for (const page of siteConfig.pages) {
await renderPage(siteConfig, page, result)
}

if (await exists(siteConfig.publicDir)) {
console.log('copying public dir...')
await copyDir(
siteConfig.publicDir,
path.join(siteConfig.outDir, 'public')
)
}
} finally {
await fs.rmdir(siteConfig.tempDir, { recursive: true })
}
Expand Down
21 changes: 7 additions & 14 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import path from 'path'
import chalk from 'chalk'
import globby from 'globby'
import { promises as fs } from 'fs'
import { createResolver, APP_PATH } from './utils/pathResolver'
import { Resolver } from 'vite'
import { Header } from './markdown/plugins/header'
import { exists } from './utils/fs'

const debug = require('debug')('vitepress:config')

Expand All @@ -26,6 +26,7 @@ export interface SiteConfig<ThemeConfig = any> {
site: SiteData<ThemeConfig>
configPath: string
themeDir: string
publicDir: string
outDir: string
tempDir: string
resolver: Resolver
Expand Down Expand Up @@ -57,20 +58,17 @@ export async function resolveConfig(

// resolve theme path
const userThemeDir = resolve(root, 'theme')
let themeDir: string
try {
await fs.stat(userThemeDir)
themeDir = userThemeDir
} catch (e) {
themeDir = path.join(__dirname, '../lib/theme-default')
}
const themeDir = (await exists(userThemeDir))
? userThemeDir
: path.join(__dirname, '../lib/theme-default')

const config: SiteConfig = {
root,
site,
themeDir,
pages: await globby(['**.md'], { cwd: root, ignore: ['node_modules'] }),
configPath: resolve(root, 'config.js'),
publicDir: resolve(root, 'public'),
outDir: resolve(root, 'dist'),
tempDir: path.resolve(APP_PATH, 'temp'),
resolver: createResolver(themeDir)
Expand All @@ -82,12 +80,7 @@ export async function resolveConfig(
export async function resolveSiteData(root: string): Promise<SiteData> {
// load user config
const configPath = resolve(root, 'config.js')
let hasUserConfig = false
try {
await fs.stat(configPath)
hasUserConfig = true
} catch (e) {}

const hasUserConfig = await exists(configPath)
// always delete cache first before loading config
delete require.cache[configPath]
const userConfig: UserConfig = hasUserConfig ? require(configPath) : {}
Expand Down
29 changes: 29 additions & 0 deletions src/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import path from 'path'
import { promises as fs } from 'fs'

export async function exists(path: string) {
try {
await fs.stat(path)
return true
} catch (e) {
return false
}
}

export async function copyDir(from: string, to: string) {
if (exists(to)) {
await fs.rmdir(to, { recursive: true })
}
await fs.mkdir(to, { recursive: true })
const content = await fs.readdir(from)
for (const entry of content) {
const fromPath = path.join(from, entry)
const toPath = path.join(to, entry)
const stat = await fs.stat(fromPath)
if (stat.isFile()) {
await fs.copyFile(fromPath, toPath)
} else if (stat.isDirectory()) {
await copyDir(fromPath, toPath)
}
}
}

0 comments on commit ddc9d51

Please sign in to comment.