-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate from brickyard to be standalone package
- Loading branch information
Showing
3 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Created by scott on 16-3-31. | ||
*/ | ||
'use strict' | ||
|
||
const sps = require('./static-proxy-server') | ||
|
||
module.exports = { | ||
register, | ||
run | ||
} | ||
|
||
/** | ||
* | ||
* @param {Command} cmd | ||
* @param {function(Object)} optionsCallback | ||
*/ | ||
function register(cmd, optionsCallback) { | ||
cmd | ||
.description('run a program on the target release dir') | ||
.arguments('<dir>') | ||
.usage('<dir> [options]') | ||
.option('--program <program...>', 'specify the program to read config', function (param) { | ||
return param.split(',').map(function (program) { | ||
return program.trim() | ||
}) | ||
}) | ||
.option('--no-browse', 'do not open the browser automatically') | ||
.option('--https', 'use https protocol to serve the resources') | ||
.action(function (dir) { | ||
optionsCallback(Object.assign({ dir: dir }, this.opts())) | ||
}) | ||
} | ||
|
||
function run(runtime) { | ||
sps(runtime.config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Created by scott on 16-5-10. | ||
*/ | ||
|
||
'use strict' | ||
|
||
const _ = require('lodash') | ||
const express = require('express') | ||
const proxy = require('http-proxy-middleware') | ||
const morgan = require('morgan') | ||
const path = require("path") | ||
const open = require('opn') | ||
const url = require('url') | ||
|
||
const logger = require('log4js').getLogger('SPS') | ||
|
||
module.exports = function createServer(config) { | ||
const server = express() | ||
const staticPath = path.resolve(process.cwd(), config.dir) | ||
|
||
server.use(express.static(staticPath)) | ||
server.use(morgan('dev')) | ||
|
||
if (config.apiProxy) { | ||
server.use(proxy(config.apiProxy.address, { | ||
target: config.apiProxy.host, | ||
ws: true, | ||
changeOrigin: true | ||
})) | ||
} | ||
|
||
server.listen(config.port, config.host, function () { | ||
logger.info(`\nserving file from ${staticPath}\nstatic-proxy-server is running...`) | ||
|
||
const serverUrl = url.format({ | ||
protocol: config.protocol, | ||
hostname: config.host, | ||
port: config.port | ||
}) | ||
|
||
logger.info(`opening ${serverUrl}`) | ||
open(serverUrl) | ||
}) | ||
} |