feat: enable custom theme with eject

This commit is contained in:
endiliey 2018-08-30 01:03:38 +08:00
parent 8c6bc6dd38
commit 2b5ee3e869
6 changed files with 40 additions and 7 deletions

View file

@ -4,7 +4,7 @@ const chalk = require('chalk');
const semver = require('semver'); const semver = require('semver');
const path = require('path'); const path = require('path');
const program = require('commander'); const program = require('commander');
const {build, init, start} = require('../lib'); const {build, eject, init, start} = require('../lib');
const requiredVersion = require('../package.json').engines.node; const requiredVersion = require('../package.json').engines.node;
if (!semver.satisfies(process.version, requiredVersion)) { if (!semver.satisfies(process.version, requiredVersion)) {
@ -42,6 +42,13 @@ program
wrapCommand(build)(path.resolve(siteDir), {skipImageCompression}); 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 program
.command('init [projectDir]') .command('init [projectDir]')
.description('Initialize website') .description('Initialize website')

16
lib/commands/eject.js Normal file
View file

@ -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`
);
};

View file

@ -1,9 +1,11 @@
const build = require('./commands/build'); const build = require('./commands/build');
const init = require('./commands/init'); const init = require('./commands/init');
const start = require('./commands/start'); const start = require('./commands/start');
const eject = require('./commands/eject');
module.exports = { module.exports = {
build, build,
eject,
init, init,
start start
}; };

View file

@ -20,7 +20,6 @@ module.exports = function loadConfig(siteDir, deleteCache = true) {
]; ];
const optionalFields = [ const optionalFields = [
'customDocsPath', 'customDocsPath',
'themePath',
'highlight', 'highlight',
'markdownPlugins' 'markdownPlugins'
]; ];

View file

@ -34,11 +34,19 @@ module.exports = async function load(siteDir) {
const outDir = path.resolve(siteDir, 'build'); const outDir = path.resolve(siteDir, 'build');
// resolve the theme // resolve the theme
const themePath = const customThemePath = path.resolve(siteDir, 'theme');
siteConfig.themePath && const themePath = fs.existsSync(customThemePath)
fs.existsSync(path.resolve(siteDir, siteConfig.themePath)) ? customThemePath
? siteConfig.themePath : path.resolve(__dirname, '../theme');
: 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 || '/'; const baseUrl = siteConfig.baseUrl || '/';

View file

@ -10,6 +10,7 @@
"munseo": "node bin/munseo", "munseo": "node bin/munseo",
"start": "node bin/munseo start website", "start": "node bin/munseo start website",
"build": "node bin/munseo build website", "build": "node bin/munseo build website",
"eject": "node bin/munseo eject website",
"prettier": "prettier --config .prettierrc --write \"lib/**/*.js\" \"bin/**/*.js\" \"test/**/*.js\"", "prettier": "prettier --config .prettierrc --write \"lib/**/*.js\" \"bin/**/*.js\" \"test/**/*.js\"",
"lint": "eslint --cache \"lib/**/*.js\" \"bin/**/*.js\" \"test/**/*.js\"", "lint": "eslint --cache \"lib/**/*.js\" \"bin/**/*.js\" \"test/**/*.js\"",
"test": "jest --config test/jest.config.js" "test": "jest --config test/jest.config.js"