From 5d4b7c0d43b7a1990454195d9fe9f2b368934301 Mon Sep 17 00:00:00 2001 From: Jamos Tay Date: Fri, 28 Sep 2018 20:48:23 +0800 Subject: [PATCH 1/3] Allow custom site json --- docs/userGuide/developingASite.md | 2 ++ docs/userGuide/userQuickStart.md | 1 + index.js | 3 ++- lib/Site.js | 13 ++++++------- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/docs/userGuide/developingASite.md b/docs/userGuide/developingASite.md index 26d4807653..50ad88f413 100644 --- a/docs/userGuide/developingASite.md +++ b/docs/userGuide/developingASite.md @@ -37,7 +37,9 @@ MarkBind will generate the site in a folder named `_site` in the current directo | 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. | +| `-s`, `--site-config ` | Specify a custom filepath for site.json. | | --no-open | Don't open browser automatically. | Note: Live reload is only supported for the following file types: diff --git a/docs/userGuide/userQuickStart.md b/docs/userGuide/userQuickStart.md index 9f27befc2b..fd3627839d 100644 --- a/docs/userGuide/userQuickStart.md +++ b/docs/userGuide/userQuickStart.md @@ -104,6 +104,7 @@ Live reload is enabled to regenerate the site for changes, so you could see the |---|---| | `-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. | +| `-s`, `--site-config ` | Specify a custom filepath for site.json. | | --no-open | Don't open browser automatically. | ### Build the static site diff --git a/index.js b/index.js index acdf742998..4c4df9a59c 100755 --- a/index.js +++ b/index.js @@ -129,13 +129,14 @@ program .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('-s, --site-config ', 'specify a custom filepath for site.json') .option('--no-open', 'do not automatically open the site in browser') .action((root, options) => { const rootFolder = path.resolve(root || process.cwd()); const logsFolder = path.join(rootFolder, '_markbind/logs'); const outputFolder = path.join(rootFolder, '_site'); - const site = new Site(rootFolder, outputFolder, options.forceReload); + const site = new Site(rootFolder, outputFolder, options.forceReload, options.siteConfig); const addHandler = (filePath) => { logger.info(`[${new Date().toLocaleTimeString()}] Reload for file add: ${filePath}`); diff --git a/lib/Site.js b/lib/Site.js index 2a963200de..18c6bd87cc 100644 --- a/lib/Site.js +++ b/lib/Site.js @@ -2,7 +2,6 @@ const cheerio = require('cheerio'); const ejs = require('ejs'); -const findUp = require('find-up'); const fs = require('fs-extra-promise'); const ghpages = require('gh-pages'); const ignore = require('ignore'); @@ -99,9 +98,8 @@ 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, forceReload = false) { - const configPath = findUp.sync(SITE_CONFIG_NAME, { cwd: rootPath }); - this.rootPath = configPath ? path.dirname(configPath) : rootPath; +function Site(rootPath, outputPath, forceReload = false, siteConfigPath = SITE_CONFIG_NAME) { + this.rootPath = rootPath; this.outputPath = outputPath; this.tempPath = path.join(rootPath, TEMP_FOLDER_NAME); @@ -119,6 +117,7 @@ function Site(rootPath, outputPath, forceReload = false) { this.baseUrlMap = {}; this.forceReload = forceReload; this.siteConfig = {}; + this.siteConfigPath = siteConfigPath; this.userDefinedVariablesMap = {}; } @@ -235,7 +234,7 @@ Site.initSite = function (rootPath) { Site.prototype.readSiteConfig = function (baseUrl) { return new Promise((resolve, reject) => { - const siteConfigPath = path.join(this.rootPath, SITE_CONFIG_NAME); + const siteConfigPath = path.join(this.rootPath, this.siteConfigPath); fs.readJsonAsync(siteConfigPath) .then((config) => { this.siteConfig = config; @@ -243,7 +242,7 @@ Site.prototype.readSiteConfig = function (baseUrl) { resolve(this.siteConfig); }) .catch((err) => { - reject(new Error('Failed to read the site config file \'site.json\' at' + reject(new Error(`Failed to read the site config file '${this.siteConfigPath}' at` + `${this.rootPath}:\n${err.message}\nPlease ensure the file exist or is valid`)); }); }); @@ -332,7 +331,7 @@ Site.prototype.collectAddressablePages = function () { Site.prototype.collectBaseUrl = function () { const candidates = walkSync(this.rootPath, { directories: false }) - .filter(x => x.endsWith(SITE_CONFIG_NAME)) + .filter(x => x.endsWith(this.siteConfigPath)) .map(x => path.resolve(this.rootPath, x)); this.baseUrlMap = candidates.reduce((pre, now) => { From c743f1f6f788226e5807553c5af3894a222b3621 Mon Sep 17 00:00:00 2001 From: Jamos Tay Date: Mon, 1 Oct 2018 11:12:39 +0800 Subject: [PATCH 2/3] Revert changes to path logic --- docs/userGuide/developingASite.md | 4 +++- docs/userGuide/userQuickStart.md | 2 +- index.js | 2 +- lib/Site.js | 4 +++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/userGuide/developingASite.md b/docs/userGuide/developingASite.md index 50ad88f413..cbbccd2e9b 100644 --- a/docs/userGuide/developingASite.md +++ b/docs/userGuide/developingASite.md @@ -39,9 +39,11 @@ MarkBind will generate the site in a folder named `_site` in the current directo |----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `-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. | -| `-s`, `--site-config ` | Specify a custom filepath for site.json. | +| `-s`, `--site-config ` | Specify the site config file (default: site.json) | | --no-open | Don't open browser automatically. | +Note: The site config file must be in the root folder. + Note: Live reload is only supported for the following file types: - `.html` diff --git a/docs/userGuide/userQuickStart.md b/docs/userGuide/userQuickStart.md index fd3627839d..015f1997f9 100644 --- a/docs/userGuide/userQuickStart.md +++ b/docs/userGuide/userQuickStart.md @@ -104,7 +104,7 @@ Live reload is enabled to regenerate the site for changes, so you could see the |---|---| | `-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. | -| `-s`, `--site-config ` | Specify a custom filepath for site.json. | +| `-s`, `--site-config ` | Specify the site config file (default: site.json) | | --no-open | Don't open browser automatically. | ### Build the static site diff --git a/index.js b/index.js index 4c4df9a59c..c37fb05c3b 100755 --- a/index.js +++ b/index.js @@ -129,7 +129,7 @@ program .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('-s, --site-config ', 'specify a custom filepath for site.json') + .option('-s, --site-config ', 'specify the site config file (default: site.json)') .option('--no-open', 'do not automatically open the site in browser') .action((root, options) => { const rootFolder = path.resolve(root || process.cwd()); diff --git a/lib/Site.js b/lib/Site.js index 18c6bd87cc..00d7f64a68 100644 --- a/lib/Site.js +++ b/lib/Site.js @@ -2,6 +2,7 @@ const cheerio = require('cheerio'); const ejs = require('ejs'); +const findUp = require('find-up'); const fs = require('fs-extra-promise'); const ghpages = require('gh-pages'); const ignore = require('ignore'); @@ -99,7 +100,8 @@ const MARKBIND_WEBSITE_URL = 'https://markbind.github.io/markbind/'; const MARKBIND_LINK_HTML = `MarkBind ${CLI_VERSION}`; function Site(rootPath, outputPath, forceReload = false, siteConfigPath = SITE_CONFIG_NAME) { - this.rootPath = rootPath; + const configPath = findUp.sync(SITE_CONFIG_NAME, { cwd: rootPath }); + this.rootPath = configPath ? path.dirname(configPath) : rootPath; this.outputPath = outputPath; this.tempPath = path.join(rootPath, TEMP_FOLDER_NAME); From b1646c3d5c558a76c3009dc9a9eedbc3b4b68c09 Mon Sep 17 00:00:00 2001 From: Jamos Tay Date: Wed, 10 Oct 2018 16:18:26 +0800 Subject: [PATCH 3/3] Replace SITE_CONFIG_NAME with siteConfigPath --- docs/userGuide/developingASite.md | 2 -- lib/Site.js | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/userGuide/developingASite.md b/docs/userGuide/developingASite.md index cbbccd2e9b..9920c1b1fa 100644 --- a/docs/userGuide/developingASite.md +++ b/docs/userGuide/developingASite.md @@ -42,8 +42,6 @@ MarkBind will generate the site in a folder named `_site` in the current directo | `-s`, `--site-config ` | Specify the site config file (default: site.json) | | --no-open | Don't open browser automatically. | -Note: The site config file must be in the root folder. - Note: Live reload is only supported for the following file types: - `.html` diff --git a/lib/Site.js b/lib/Site.js index 00d7f64a68..8efec354d5 100644 --- a/lib/Site.js +++ b/lib/Site.js @@ -100,7 +100,7 @@ const MARKBIND_WEBSITE_URL = 'https://markbind.github.io/markbind/'; const MARKBIND_LINK_HTML = `MarkBind ${CLI_VERSION}`; function Site(rootPath, outputPath, forceReload = false, siteConfigPath = SITE_CONFIG_NAME) { - const configPath = findUp.sync(SITE_CONFIG_NAME, { cwd: rootPath }); + const configPath = findUp.sync(siteConfigPath, { cwd: rootPath }); this.rootPath = configPath ? path.dirname(configPath) : rootPath; this.outputPath = outputPath; this.tempPath = path.join(rootPath, TEMP_FOLDER_NAME);