From 2b5ee3e8697f9a12b1f9e97daa19c352433beea3 Mon Sep 17 00:00:00 2001 From: endiliey Date: Thu, 30 Aug 2018 01:03:38 +0800 Subject: [PATCH] feat: enable custom theme with eject --- bin/munseo.js | 9 ++++++++- lib/commands/eject.js | 16 ++++++++++++++++ lib/index.js | 2 ++ lib/load/config.js | 1 - lib/load/index.js | 18 +++++++++++++----- package.json | 1 + 6 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 lib/commands/eject.js diff --git a/bin/munseo.js b/bin/munseo.js index 8a1e3aa22d..fafbb8095b 100644 --- a/bin/munseo.js +++ b/bin/munseo.js @@ -4,7 +4,7 @@ const chalk = require('chalk'); const semver = require('semver'); const path = require('path'); const program = require('commander'); -const {build, init, start} = require('../lib'); +const {build, eject, init, start} = require('../lib'); const requiredVersion = require('../package.json').engines.node; if (!semver.satisfies(process.version, requiredVersion)) { @@ -42,6 +42,13 @@ program wrapCommand(build)(path.resolve(siteDir), {skipImageCompression}); }); +program + .command('eject [siteDir]') + .description('copy the default theme into website folder for customization.') + .action((siteDir = '.') => { + wrapCommand(eject)(path.resolve(siteDir)); + }); + program .command('init [projectDir]') .description('Initialize website') diff --git a/lib/commands/eject.js b/lib/commands/eject.js new file mode 100644 index 0000000000..42c630747d --- /dev/null +++ b/lib/commands/eject.js @@ -0,0 +1,16 @@ +const fs = require('fs-extra'); +const chalk = require('chalk'); +const path = require('path'); + +module.exports = async function eject(siteDir) { + const defaultTheme = path.resolve(__dirname, '..', 'theme'); + const customTheme = path.resolve(siteDir, 'theme'); + await fs.copy(defaultTheme, customTheme); + + const relativeDir = path.relative(process.cwd(), customTheme); + console.log( + `\n${chalk.green('Success!')} Copied default theme files to ${chalk.cyan( + relativeDir + )}.\n` + ); +}; diff --git a/lib/index.js b/lib/index.js index 7594a1260b..03c0a0dab0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,9 +1,11 @@ const build = require('./commands/build'); const init = require('./commands/init'); const start = require('./commands/start'); +const eject = require('./commands/eject'); module.exports = { build, + eject, init, start }; diff --git a/lib/load/config.js b/lib/load/config.js index 92540265e4..c106e9e0ff 100644 --- a/lib/load/config.js +++ b/lib/load/config.js @@ -20,7 +20,6 @@ module.exports = function loadConfig(siteDir, deleteCache = true) { ]; const optionalFields = [ 'customDocsPath', - 'themePath', 'highlight', 'markdownPlugins' ]; diff --git a/lib/load/index.js b/lib/load/index.js index 7ce9b4540a..f31e13eeb5 100644 --- a/lib/load/index.js +++ b/lib/load/index.js @@ -34,11 +34,19 @@ module.exports = async function load(siteDir) { const outDir = path.resolve(siteDir, 'build'); // resolve the theme - const themePath = - siteConfig.themePath && - fs.existsSync(path.resolve(siteDir, siteConfig.themePath)) - ? siteConfig.themePath - : path.resolve(__dirname, '../theme'); + const customThemePath = path.resolve(siteDir, 'theme'); + const themePath = fs.existsSync(customThemePath) + ? customThemePath + : path.resolve(__dirname, '../theme'); + + const themeComponents = ['Docs', 'Loading', 'NotFound']; + themeComponents.forEach(component => { + if (!require.resolve(path.join(themePath, component))) { + throw new Error( + `Failed to load ${themePath}/${component}. It does not exist.` + ); + } + }); const baseUrl = siteConfig.baseUrl || '/'; diff --git a/package.json b/package.json index d67af924e5..01d9b0898d 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "munseo": "node bin/munseo", "start": "node bin/munseo start website", "build": "node bin/munseo build website", + "eject": "node bin/munseo eject website", "prettier": "prettier --config .prettierrc --write \"lib/**/*.js\" \"bin/**/*.js\" \"test/**/*.js\"", "lint": "eslint --cache \"lib/**/*.js\" \"bin/**/*.js\" \"test/**/*.js\"", "test": "jest --config test/jest.config.js"