Skip to content

Commit

Permalink
feat: migrate from brickyard to be standalone package
Browse files Browse the repository at this point in the history
  • Loading branch information
e-cloud committed Jul 5, 2016
1 parent daba21a commit 4e0c346
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
37 changes: 37 additions & 0 deletions index.js
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)
}
23 changes: 16 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
"name": "package",
"author": "John Doe",
"version": "0.0.0",
"description": "nothing.",
"name": "brickyard-command-run",
"version": "0.0.2",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "e-cloud",
"license": "ISC",
"devDependencies": {
"conventional-changelog": "^1.1.0",
"eslint": "^2.8.0",
Expand All @@ -11,10 +16,14 @@
"ghooks": "^1.2.1",
"validate-commit-msg": "^2.6.1"
},
"scripts": {
"test": "echo 'no test!'"
"dependencies": {
"express": "^4.0.0",
"http-proxy-middleware": "^0.14.0",
"lodash": "^4.6.1",
"log4js": "^0.6.33",
"morgan": "^1.7.0",
"opn": "^3.0.0"
},
"license": "ISC",
"config": {
"validate-commit-msg": {
"types": [
Expand Down
44 changes: 44 additions & 0 deletions static-proxy-server.js
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)
})
}

0 comments on commit 4e0c346

Please sign in to comment.