Skip to content

Commit

Permalink
Feat lavas start (#158)
Browse files Browse the repository at this point in the history
合并 lavas-static 进入 lavas-start
  • Loading branch information
easonyq committed May 9, 2018
1 parent 98a83b6 commit 2bbbc1d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"lint": "fecs ./ --rule --type 'vue,js,css'",
"test": "rimraf packages/lavas-core-vue/test/temp && nyc ava -v",
"nyc:report": "nyc report --reporter=html",
"publish:core": "lerna publish --scope=lavas-core-vue"
"publish:lavas-core-vue": "lerna publish --scope=lavas-core-vue"
},
"bin": {
"lavas": "./packages/lavas-cli/bin/dev-lavas-cli.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/lavas-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lavas",
"version": "2.2.7",
"version": "2.2.8",
"description": "Lavas solution cli tool",
"main": "dist/index.js",
"files": [
Expand Down
131 changes: 79 additions & 52 deletions packages/lavas-cli/src/commander/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,81 +83,108 @@ module.exports = function (program) {
// lavas start
program
.command('start')
.description(locals.START_PROD)
.description(locals.START)
.option('-p, --port <port>', locals.START_PORT)
.action(async ({port}) => {
let serverScriptPath = path.resolve(utils.getLavasProjectRoot(), DEFAULT_PROD_SERVER_SCRIPT);
.action(({port}) => {
let serverScriptPath = path.resolve(process.cwd(), DEFAULT_PROD_SERVER_SCRIPT);

let isServerScriptExist = await fs.pathExists(serverScriptPath);
if (!isServerScriptExist) {
log.warn(`${locals.START_NO_FILE} ${serverScriptPath}`);
// Start SSR project
if (fs.pathExistsSync(serverScriptPath)) {
log.info(locals.START_SSR + '...');
process.env.NODE_ENV = 'production';

if (port) {
process.env.PORT = Number(port);
}

fork(serverScriptPath);
return;
}

process.env.NODE_ENV = 'production';

if (port) {
process.env.PORT = Number(port);
port = port || 8000;
let routesJsonPath = path.resolve(process.cwd(), 'lavas/routes.json');
let message;
// Start SPA project
if (fs.pathExistsSync(routesJsonPath)) {
startSPA(routesJsonPath);
message = locals.START_SPA + '...';
}
// Start a normal static server
else {
startStatic();
message = locals.START_STATIC + '...';
}

fork(serverScriptPath);
app.listen(port, () => log.info(`${message} localhost:${port}`));

});

// lavas static
program.
command('static')
.description(locals.START_STATIC)
.option('-p, --port <port>', locals.START_PORT)
.action(async ({port = 8000}) => {
.action(({port = 8000}) => {
log.info(locals.START_STATIC + '...');

let routesJsonPath = path.resolve(process.cwd(), 'lavas/routes.json');
let message;

// start static server with lavas routes configured
if (await fs.pathExists(routesJsonPath)) {
try {
let baseUrl = require(routesJsonPath).base;
if (!baseUrl || baseUrl === '/') {
// redirect all requests to '/index.html'
app.use(historyMiddleware({
htmlAcceptHeaders: ['text/html'],
disableDotRule: false // ignore paths with dot inside
}));

app.use(express.static('.'));
}
else {
// fix trailing '/'
// @see https://lavas.baidu.com/guide/v2/advanced/multi-lavas#express-%E5%A4%84%E7%90%86-spa-%E8%B7%AF%E7%94%B1%E7%9A%84%E5%B0%8F%E9%97%AE%E9%A2%98-%E6%89%A9%E5%B1%95
if (!baseUrl.endsWith('/')) {
baseUrl += '/';
}

app.use('/', (req, res, next) => {
let requestUrl = req.url.replace(/\?.+?$/, '');

if (requestUrl === baseUrl.substring(0, baseUrl.length - 1)) {
req.url = requestUrl + '/';
}

next();
});

app.use(baseUrl, historyMiddleware({
htmlAcceptHeaders: ['text/html'],
disableDotRule: false // ignore paths with dot inside
}));

app.use(baseUrl, express.static('.'));
}
}
catch (e) {}
if (fs.pathExistsSync(routesJsonPath)) {
startSPA(routesJsonPath);
message = locals.START_SPA + '...';
}
// start a normal static server
else {
app.use(express.static('.'));
startStatic();
message = locals.START_STATIC + '...';
}

app.listen(port, () => log.info(`Static server start at localhost:${port}`));
app.listen(port, () => log.info(`${message} localhost:${port}`));
});
};

function startSPA(routesJsonPath) {
try {
let baseUrl = require(routesJsonPath).base;
if (!baseUrl || baseUrl === '/') {
// redirect all requests to '/index.html'
app.use(historyMiddleware({
htmlAcceptHeaders: ['text/html'],
disableDotRule: false // ignore paths with dot inside
}));

app.use(express.static('.'));
}
else {
// fix trailing '/'
// @see https://lavas.baidu.com/guide/v2/advanced/multi-lavas#express-%E5%A4%84%E7%90%86-spa-%E8%B7%AF%E7%94%B1%E7%9A%84%E5%B0%8F%E9%97%AE%E9%A2%98-%E6%89%A9%E5%B1%95
if (!baseUrl.endsWith('/')) {
baseUrl += '/';
}

app.use('/', (req, res, next) => {
let requestUrl = req.url.replace(/\?.+?$/, '');

if (requestUrl === baseUrl.substring(0, baseUrl.length - 1)) {
req.url = requestUrl + '/';
}

next();
});

app.use(baseUrl, historyMiddleware({
htmlAcceptHeaders: ['text/html'],
disableDotRule: false // ignore paths with dot inside
}));

app.use(baseUrl, express.static('.'));
}
}
catch (e) {}
}

function startStatic() {
app.use(express.static('.'));
}
7 changes: 4 additions & 3 deletions packages/lavas-cli/src/locals/en/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ module.exports = {
PLEASE_SEE: 'Please check ',
START_BUILD: 'Start building ',
START_DEV: 'Start Lavas development environment server ',
START_PROD: 'Start Lavas production environment server ',
START_STATIC: 'Start Lavas static server ',
START_DEV_SERVER: 'Launching Lavas debug server ',
START_PROD_SERVER: 'Launching Lavas production server ',
START: 'Start Lavas Server',
START_SSR: 'Start Lavas SSR Server',
START_SPA: 'Start Lavas SPA Server',
START_STATIC: 'Start Lavas static server ',
START_PORT: 'Specify a port ',
START_SCRIPT: 'Specify the development environment server-side script ',
START_NO_FILE: 'There is no file in project root path - ',
Expand Down
7 changes: 4 additions & 3 deletions packages/lavas-cli/src/locals/zh_CN/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ module.exports = {
PLEASE_SEE: '请查看',
START_BUILD: '开始构建',
START_DEV: '启动 Lavas 开发环境服务器',
START_PROD: '启动 Lavas 生产环境服务器',
START_STATIC: '启动 Lavas 静态服务器',
START_DEV_SERVER: '正在启动 Lavas 调试服务器',
START_PROD_SERVER: '正在启动 Lavas 正式服务器',
START: '启动 Lavas 服务器',
START_SSR: '正在启动 Lavas SSR 服务器',
START_SPA: '正在启动 Lavas SPA 服务器',
START_STATIC: '启动 Lavas 静态服务器',
START_PORT: '指定 port',
START_SCRIPT: '指定开发环境服务端脚本',
START_NO_FILE: 'Lavas 没有检测到项目根目录下含有文件',
Expand Down

0 comments on commit 2bbbc1d

Please sign in to comment.