diff --git a/docs/userGuide/userQuickStart.md b/docs/userGuide/userQuickStart.md index 7a6ee1b8cb..9f27befc2b 100644 --- a/docs/userGuide/userQuickStart.md +++ b/docs/userGuide/userQuickStart.md @@ -102,6 +102,7 @@ Live reload is enabled to regenerate the site for changes, so you could see the | Options | Description | |---|---| +| `-f`, `--force-reload` | Force a full reload of all site files when a file is changed. | | `-p`, `--port ` | The port used for serving your website. | | --no-open | Don't open browser automatically. | diff --git a/index.js b/index.js index 9cb244225c..acdf742998 100755 --- a/index.js +++ b/index.js @@ -127,6 +127,7 @@ program program .command('serve [root]') .description('build then serve a website from a directory') + .option('-f, --force-reload', 'force a full reload of all site files when a file is changed') .option('-p, --port ', 'port for server to listen on (Default is 8080)') .option('--no-open', 'do not automatically open the site in browser') .action((root, options) => { @@ -134,7 +135,7 @@ program const logsFolder = path.join(rootFolder, '_markbind/logs'); const outputFolder = path.join(rootFolder, '_site'); - const site = new Site(rootFolder, outputFolder); + const site = new Site(rootFolder, outputFolder, options.forceReload); const addHandler = (filePath) => { logger.info(`[${new Date().toLocaleTimeString()}] Reload for file add: ${filePath}`); diff --git a/lib/Site.js b/lib/Site.js index 7fe7692af1..13f5a22a9d 100644 --- a/lib/Site.js +++ b/lib/Site.js @@ -99,7 +99,7 @@ const GENERATE_SITE_LOGGING_KEY = 'Generate Site'; const MARKBIND_WEBSITE_URL = 'https://markbind.github.io/markbind/'; const MARKBIND_LINK_HTML = `MarkBind ${CLI_VERSION}`; -function Site(rootPath, outputPath) { +function Site(rootPath, outputPath, forceReload = false) { const configPath = findUp.sync(SITE_CONFIG_NAME, { cwd: rootPath }); this.rootPath = configPath ? path.dirname(configPath) : rootPath; this.outputPath = outputPath; @@ -117,6 +117,7 @@ function Site(rootPath, outputPath) { // Other properties this.addressablePages = []; this.baseUrlMap = {}; + this.forceReload = forceReload; this.siteConfig = {}; this.userDefinedVariablesMap = {}; } @@ -443,7 +444,8 @@ Site.prototype.buildSourceFiles = function () { }; Site.prototype._rebuildAffectedSourceFiles = function (filePaths) { - const uniquePaths = _.uniq(filePaths); + const filePathArray = Array.isArray(filePaths) ? filePaths : [filePaths]; + const uniquePaths = _.uniq(filePathArray); logger.verbose(`Rebuild affected paths: ${uniquePaths}`); return new Promise((resolve, reject) => { this.regenerateAffectedPages(uniquePaths) @@ -464,7 +466,8 @@ Site.prototype.rebuildAffectedSourceFiles = delay(Site.prototype._rebuildAffectedSourceFiles, 1000); Site.prototype._buildMultipleAssets = function (filePaths) { - const uniquePaths = _.uniq(filePaths); + const filePathArray = Array.isArray(filePaths) ? filePaths : [filePaths]; + const uniquePaths = _.uniq(filePathArray); const ignoreConfig = this.siteConfig.ignore || []; const fileIgnore = ignore().add(ignoreConfig); const fileRelativePaths = uniquePaths.map(filePath => path.relative(this.rootPath, filePath)); @@ -482,7 +485,8 @@ Site.prototype.buildAsset = delay(Site.prototype._buildMultipleAssets, 1000); Site.prototype._removeMultipleAssets = function (filePaths) { - const uniquePaths = _.uniq(filePaths); + const filePathArray = Array.isArray(filePaths) ? filePaths : [filePaths]; + const uniquePaths = _.uniq(filePathArray); const fileRelativePaths = uniquePaths.map(filePath => path.relative(this.rootPath, filePath)); const filesToRemove = fileRelativePaths.map( fileRelativePath => path.join(this.outputPath, fileRelativePath)); @@ -578,9 +582,9 @@ Site.prototype.generatePages = function () { Site.prototype.regenerateAffectedPages = function (filePaths) { const builtFiles = {}; const processingFiles = []; - const shouldRebuildAllPages = this.collectUserDefinedVariablesMapIfNeeded(filePaths); + const shouldRebuildAllPages = this.collectUserDefinedVariablesMapIfNeeded(filePaths) || this.forceReload; if (shouldRebuildAllPages) { - logger.warn('Rebuilding all pages as variables file was changed'); + logger.warn('Rebuilding all pages as variables file was changed, or the --force-reload flag was set'); } this._setTimestampVariable(); this.pages.forEach((page) => {