diff --git a/package.json b/package.json index 56776504..90065ad9 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "debug": "^4.1.1", "diacritics": "^1.3.0", "escape-html": "^1.0.3", + "fs-extra": "^9.0.0", "globby": "^11.0.0", "gray-matter": "^4.0.2", "lru-cache": "^5.1.1", @@ -68,6 +69,7 @@ "vue": "^3.0.0-beta.9" }, "devDependencies": { + "@types/fs-extra": "^8.1.0", "@types/lru-cache": "^5.1.0", "@types/markdown-it": "^10.0.1", "@types/node": "^13.13.4", diff --git a/src/build/build.ts b/src/build/build.ts index 290a46ba..5ff1a2b9 100644 --- a/src/build/build.ts +++ b/src/build/build.ts @@ -1,4 +1,4 @@ -import { promises as fs } from 'fs' +import fs from 'fs-extra' import { bundle } from './bundle' import { BuildOptions as ViteBuildOptions } from 'vite' import { resolveConfig } from '../config' @@ -6,7 +6,10 @@ import { renderPage } from './render' export type BuildOptions = Pick< ViteBuildOptions, - 'root' | 'rollupInputOptions' | 'rollupOutputOptions' + | 'root' + | 'rollupInputOptions' + | 'rollupOutputOptions' + | 'rollupPluginVueOptions' > export const ASSETS_DIR = '_assets/' @@ -20,7 +23,7 @@ export async function build(buildOptions: BuildOptions = {}) { await renderPage(siteConfig, page, clientResult) } } finally { - await fs.rmdir(siteConfig.tempDir, { recursive: true }) + await fs.remove(siteConfig.tempDir) } console.log('done.') } diff --git a/src/build/bundle.ts b/src/build/bundle.ts index b202bf5a..ebef37b4 100644 --- a/src/build/bundle.ts +++ b/src/build/bundle.ts @@ -1,6 +1,6 @@ import path from 'path' import slash from 'slash' -import { promises as fs } from 'fs' +import fs from 'fs-extra' import { APP_PATH, createResolver } from '../utils/pathResolver' import { BuildOptions, ASSETS_DIR } from './build' import { SiteConfig } from '../config' diff --git a/src/build/render.ts b/src/build/render.ts index 5e011f76..3f228431 100644 --- a/src/build/render.ts +++ b/src/build/render.ts @@ -1,5 +1,5 @@ import path from 'path' -import { promises as fs } from 'fs' +import fs from 'fs-extra' import { SiteConfig, HeadConfig } from '../config' import { BuildResult } from 'vite' import { renderToString } from '@vue/server-renderer' @@ -64,7 +64,7 @@ export async function renderPage( `.trim() 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) } diff --git a/src/config.ts b/src/config.ts index 4f3342d9..ca362d97 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,10 +1,10 @@ import path from 'path' +import fs from 'fs-extra' import chalk from 'chalk' import globby from 'globby' 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') @@ -57,7 +57,7 @@ export async function resolveConfig( // resolve theme path const userThemeDir = resolve(root, 'theme') - const themeDir = (await exists(userThemeDir)) + const themeDir = (await fs.pathExists(userThemeDir)) ? userThemeDir : path.join(__dirname, '../lib/theme-default') @@ -78,7 +78,7 @@ export async function resolveConfig( export async function resolveSiteData(root: string): Promise { // load user config const configPath = resolve(root, 'config.js') - const hasUserConfig = await exists(configPath) + const hasUserConfig = await fs.pathExists(configPath) // always delete cache first before loading config delete require.cache[configPath] const userConfig: UserConfig = hasUserConfig ? require(configPath) : {} diff --git a/src/utils/fs.ts b/src/utils/fs.ts deleted file mode 100644 index 59e6aecc..00000000 --- a/src/utils/fs.ts +++ /dev/null @@ -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) - } - } -} diff --git a/yarn.lock b/yarn.lock index 50f493e3..fb22f1c5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -179,6 +179,13 @@ "@types/qs" "*" "@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@*": version "1.5.1" resolved "https://registry.yarnpkg.com/@types/http-assert/-/http-assert-1.5.1.tgz#d775e93630c2469c2f980fc27e3143240335db3b"