diff --git a/docs/quickreference.md b/docs/quickreference.md index 70bf2c8..aad5963 100644 --- a/docs/quickreference.md +++ b/docs/quickreference.md @@ -22,6 +22,7 @@ git clone git@github.com:jaywcjlove/reference.git ```shell npm i # 安装依赖 npm run build # 编译输出 HTML +npm run start # 监听 md 文件编译输出 HTML ``` HTML 存放在仓库根目录下的 `dist` 目录中,将 `dist/index.html` 静态页面在浏览器中打开预览。 diff --git a/package.json b/package.json index 8f41d3a..e756fc2 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "private": false, "scripts": { "build": "node scripts/build.mjs", - "start": "node scripts/build.mjs" + "start": "node scripts/watch.mjs" }, "repository": { "type": "git", @@ -20,6 +20,7 @@ "keywords": [], "devDependencies": { "@wcj/markdown-to-html": "^2.0.15", + "chokidar": "^3.5.3", "fs-extra": "^10.1.0", "recursive-readdir-files": "^2.3.0", "rehype-document": "^6.1.0", diff --git a/scripts/index.mjs b/scripts/index.mjs index f51af5f..2fdd5bc 100644 --- a/scripts/index.mjs +++ b/scripts/index.mjs @@ -3,15 +3,15 @@ import path from 'path'; import recursiveReaddirFiles from 'recursive-readdir-files'; import { create } from './create.mjs'; -const OUTOUT = path.resolve(process.cwd(), 'dist'); -const DOCS = path.resolve(process.cwd(), 'docs'); -const CSSPATH = path.resolve(process.cwd(), 'scripts/style.css'); -const CSS_OUTPUT_PATH = path.resolve(OUTOUT, 'style/style.css'); +export const OUTOUT = path.resolve(process.cwd(), 'dist'); +export const DOCS = path.resolve(process.cwd(), 'docs'); +export const CSSPATH = path.resolve(process.cwd(), 'scripts/style.css'); +export const CSS_OUTPUT_PATH = path.resolve(OUTOUT, 'style/style.css'); -async function createHTML(files = [], num = 0) { +export async function createHTML(files = [], num = 0) { const dataFile = files[num]; if (!dataFile) { - console.log('\ndone!') + console.log(' \n done!\n') return; } ++num; diff --git a/scripts/watch.mjs b/scripts/watch.mjs new file mode 100644 index 0000000..ab91bb2 --- /dev/null +++ b/scripts/watch.mjs @@ -0,0 +1,19 @@ +import path from 'path'; +import chokidar from 'chokidar'; +import { getStat } from 'recursive-readdir-files'; +import { run, DOCS, createHTML } from './index.mjs'; + +;(async () => { + await run(); + const homeMdPath = path.relative(process.cwd(), 'README.md') + const watcher = chokidar.watch([DOCS, homeMdPath], { + ignored: /(^|[\/\\])\../, // ignore dotfiles + persistent: true + }); + + watcher.on('change', async (path) => { + const stats = await getStat(path) + createHTML([stats]); + }) + .on('error', error => console.log(`Watcher error: ${error}`)) +})(); \ No newline at end of file