Skip to content

Commit

Permalink
feat($core): extra watching files
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Jan 29, 2019
1 parent 8a234bb commit 02cc268
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions packages/@vuepress/core/lib/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ async function prepareServer (sourceDir, cliOptions = {}, context) {

// setup watchers to update options and dynamically generated files
const update = (reason) => {
logger.debug(`Re-prepare due to ${chalk.cyan(reason)}`)
console.log(`Reload due to ${reason}`)
ctx.pluginAPI.options.updated.syncApply()
prepare(sourceDir, cliOptions, false /* isProd */).catch(err => {
console.error(logger.error(chalk.red(err.stack), false))
})
}

// Curry update handler by update type
const spawnUpdate = (updateType) =>
file => update(`${chalk.red(updateType)} ${chalk.cyan(file)}`)

// watch add/remove of files
const pagesWatcher = chokidar.watch([
'**/*.md',
Expand All @@ -45,21 +49,29 @@ async function prepareServer (sourceDir, cliOptions = {}, context) {
ignored: ['.vuepress/**/*.md', 'node_modules'],
ignoreInitial: true
})
pagesWatcher.on('add', () => update('add page'))
pagesWatcher.on('unlink', () => update('unlink page'))
pagesWatcher.on('addDir', () => update('addDir'))
pagesWatcher.on('unlinkDir', () => update('unlinkDir'))
pagesWatcher.on('add', spawnUpdate('add'))
pagesWatcher.on('unlink', spawnUpdate('unlink'))
pagesWatcher.on('addDir', spawnUpdate('addDir'))
pagesWatcher.on('unlinkDir', spawnUpdate('unlinkDir'))

// watch config file
const configWatcher = chokidar.watch([
const watchFiles = [
'.vuepress/config.js',
'.vuepress/config.yml',
'.vuepress/config.toml'
], {
].concat(
(
ctx.siteConfig.extraWatchFiles || []
).map(file => normalizeWatchFilePath(file, ctx.sourceDir))
)

logger.debug('watchFiles', watchFiles)

// watch config file
const configWatcher = chokidar.watch(watchFiles, {
cwd: sourceDir,
ignoreInitial: true
})
configWatcher.on('change', () => update('config change'))
configWatcher.on('change', spawnUpdate('change'))

// also listen for frontmatter changes from markdown files
frontmatterEmitter.on('update', () => update('frontmatter or headers change'))
Expand Down Expand Up @@ -177,3 +189,11 @@ async function resolvePort (port) {
port = await portfinder.getPortPromise()
return port
}

function normalizeWatchFilePath (filepath, baseDir) {
const { isAbsolute, relative } = require('path')
if (isAbsolute(filepath)) {
return relative(baseDir, filepath)
}
return filepath
}

0 comments on commit 02cc268

Please sign in to comment.