refactor: use fs-extra to ensure node 10 compat

pull/14/head
Evan You 5 years ago
parent c9fcea8a93
commit 7beb172b00

@ -53,6 +53,7 @@
"debug": "^4.1.1", "debug": "^4.1.1",
"diacritics": "^1.3.0", "diacritics": "^1.3.0",
"escape-html": "^1.0.3", "escape-html": "^1.0.3",
"fs-extra": "^9.0.0",
"globby": "^11.0.0", "globby": "^11.0.0",
"gray-matter": "^4.0.2", "gray-matter": "^4.0.2",
"lru-cache": "^5.1.1", "lru-cache": "^5.1.1",
@ -68,6 +69,7 @@
"vue": "^3.0.0-beta.9" "vue": "^3.0.0-beta.9"
}, },
"devDependencies": { "devDependencies": {
"@types/fs-extra": "^8.1.0",
"@types/lru-cache": "^5.1.0", "@types/lru-cache": "^5.1.0",
"@types/markdown-it": "^10.0.1", "@types/markdown-it": "^10.0.1",
"@types/node": "^13.13.4", "@types/node": "^13.13.4",

@ -1,4 +1,4 @@
import { promises as fs } from 'fs' import fs from 'fs-extra'
import { bundle } from './bundle' import { bundle } from './bundle'
import { BuildOptions as ViteBuildOptions } from 'vite' import { BuildOptions as ViteBuildOptions } from 'vite'
import { resolveConfig } from '../config' import { resolveConfig } from '../config'
@ -6,7 +6,10 @@ import { renderPage } from './render'
export type BuildOptions = Pick< export type BuildOptions = Pick<
ViteBuildOptions, ViteBuildOptions,
'root' | 'rollupInputOptions' | 'rollupOutputOptions' | 'root'
| 'rollupInputOptions'
| 'rollupOutputOptions'
| 'rollupPluginVueOptions'
> >
export const ASSETS_DIR = '_assets/' export const ASSETS_DIR = '_assets/'
@ -20,7 +23,7 @@ export async function build(buildOptions: BuildOptions = {}) {
await renderPage(siteConfig, page, clientResult) await renderPage(siteConfig, page, clientResult)
} }
} finally { } finally {
await fs.rmdir(siteConfig.tempDir, { recursive: true }) await fs.remove(siteConfig.tempDir)
} }
console.log('done.') console.log('done.')
} }

@ -1,6 +1,6 @@
import path from 'path' import path from 'path'
import slash from 'slash' import slash from 'slash'
import { promises as fs } from 'fs' import fs from 'fs-extra'
import { APP_PATH, createResolver } from '../utils/pathResolver' import { APP_PATH, createResolver } from '../utils/pathResolver'
import { BuildOptions, ASSETS_DIR } from './build' import { BuildOptions, ASSETS_DIR } from './build'
import { SiteConfig } from '../config' import { SiteConfig } from '../config'

@ -1,5 +1,5 @@
import path from 'path' import path from 'path'
import { promises as fs } from 'fs' import fs from 'fs-extra'
import { SiteConfig, HeadConfig } from '../config' import { SiteConfig, HeadConfig } from '../config'
import { BuildResult } from 'vite' import { BuildResult } from 'vite'
import { renderToString } from '@vue/server-renderer' import { renderToString } from '@vue/server-renderer'
@ -64,7 +64,7 @@ export async function renderPage(
</body> </body>
</html>`.trim() </html>`.trim()
const htmlFileName = path.join(config.outDir, page.replace(/\.md$/, '.html')) const htmlFileName = path.join(config.outDir, page.replace(/\.md$/, '.html'))
await fs.mkdir(path.dirname(htmlFileName), { recursive: true }) await fs.ensureDir(path.dirname(htmlFileName))
await fs.writeFile(htmlFileName, html) await fs.writeFile(htmlFileName, html)
} }

@ -1,10 +1,10 @@
import path from 'path' import path from 'path'
import fs from 'fs-extra'
import chalk from 'chalk' import chalk from 'chalk'
import globby from 'globby' import globby from 'globby'
import { createResolver, APP_PATH } from './utils/pathResolver' import { createResolver, APP_PATH } from './utils/pathResolver'
import { Resolver } from 'vite' import { Resolver } from 'vite'
import { Header } from './markdown/plugins/header' import { Header } from './markdown/plugins/header'
import { exists } from './utils/fs'
const debug = require('debug')('vitepress:config') const debug = require('debug')('vitepress:config')
@ -57,7 +57,7 @@ export async function resolveConfig(
// resolve theme path // resolve theme path
const userThemeDir = resolve(root, 'theme') const userThemeDir = resolve(root, 'theme')
const themeDir = (await exists(userThemeDir)) const themeDir = (await fs.pathExists(userThemeDir))
? userThemeDir ? userThemeDir
: path.join(__dirname, '../lib/theme-default') : path.join(__dirname, '../lib/theme-default')
@ -78,7 +78,7 @@ export async function resolveConfig(
export async function resolveSiteData(root: string): Promise<SiteData> { export async function resolveSiteData(root: string): Promise<SiteData> {
// load user config // load user config
const configPath = resolve(root, 'config.js') const configPath = resolve(root, 'config.js')
const hasUserConfig = await exists(configPath) const hasUserConfig = await fs.pathExists(configPath)
// always delete cache first before loading config // always delete cache first before loading config
delete require.cache[configPath] delete require.cache[configPath]
const userConfig: UserConfig = hasUserConfig ? require(configPath) : {} const userConfig: UserConfig = hasUserConfig ? require(configPath) : {}

@ -1,29 +0,0 @@
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)
}
}
}

@ -179,6 +179,13 @@
"@types/qs" "*" "@types/qs" "*"
"@types/serve-static" "*" "@types/serve-static" "*"
"@types/fs-extra@^8.1.0":
version "8.1.0"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-8.1.0.tgz#1114834b53c3914806cd03b3304b37b3bd221a4d"
integrity sha512-UoOfVEzAUpeSPmjm7h1uk5MH6KZma2z2O7a75onTGjnNvAvMVrPzPL/vBbT65iIGHWj6rokwfmYcmxmlSf2uwg==
dependencies:
"@types/node" "*"
"@types/http-assert@*": "@types/http-assert@*":
version "1.5.1" version "1.5.1"
resolved "https://registry.yarnpkg.com/@types/http-assert/-/http-assert-1.5.1.tgz#d775e93630c2469c2f980fc27e3143240335db3b" resolved "https://registry.yarnpkg.com/@types/http-assert/-/http-assert-1.5.1.tgz#d775e93630c2469c2f980fc27e3143240335db3b"

Loading…
Cancel
Save