refactor: start cli command

This commit is contained in:
endiliey 2018-08-08 00:45:58 +08:00
parent 7a69cff30c
commit 1150c45f9b

View file

@ -15,16 +15,16 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
const load = require('../load'); const load = require('../load');
const createDevConfig = require('../webpack/dev'); const createDevConfig = require('../webpack/dev');
async function getPort(port) { async function getPort(reqPort) {
portfinder.basePort = parseInt(port, 10) || 3000; portfinder.basePort = parseInt(reqPort, 10) || 3000;
return await portfinder.getPortPromise(); const port = await portfinder.getPortPromise();
return port;
} }
module.exports = async function start(siteDir, cliOptions = {}) { module.exports = async function start(siteDir, cliOptions = {}) {
// Preprocess whole files as a prop // Process all related files as a prop
const props = await load(siteDir); const props = await load(siteDir);
// (if enabled) live reload for any changes in file
if (!cliOptions.noWatch) { if (!cliOptions.noWatch) {
const reload = () => { const reload = () => {
load(siteDir).catch(err => { load(siteDir).catch(err => {
@ -67,8 +67,9 @@ module.exports = async function start(siteDir, cliOptions = {}) {
{ {
inject: false, inject: false,
hash: true, hash: true,
template: path.resolve(__dirname, '../core/index.html'), template: path.resolve(__dirname, '../core/devTemplate.ejs'),
filename: 'index.html' filename: 'index.html',
title: props.siteConfig.title
} }
]); ]);
@ -94,7 +95,7 @@ module.exports = async function start(siteDir, cliOptions = {}) {
}, },
logLevel: 'error', logLevel: 'error',
port, port,
add: (app, middleware, options) => { add: app => {
const staticDir = path.resolve(siteDir, 'static'); const staticDir = path.resolve(siteDir, 'static');
if (fs.existsSync(staticDir)) { if (fs.existsSync(staticDir)) {
app.use(mount(baseUrl, serveStatic(staticDir))); app.use(mount(baseUrl, serveStatic(staticDir)));