Skip to content

Commit

Permalink
puer
Browse files Browse the repository at this point in the history
  • Loading branch information
leeluolee committed Jan 23, 2014
1 parent d2629cd commit d8a317a
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 44 deletions.
58 changes: 31 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
#Puer
一个在当前(或指定目录)开启静态服务器的命令行工具,css刷新样式,其它刷新页面.是的,跟F5差不多
##Introduction

__新版本去除了socket.io的依赖, 改为使用[SSE](http://en.wikipedia.org/wiki/Server-sent_events)代替, 所以IE不支持了__
puer —— a easy-use static server with livereload function。

##Feature
1. easy-install : `npm install puer -g`
2. easy-usage: `puer` in 90%

##安装
`npm -g install puer`

当然你也可以fork一份自己折腾:
__puer have integretd with weinre now, you can pass `-i` to open inspect__

`git clone https://github.com/leeluolee/puer`
##install
`npm -g install puer`


##使用
##Usage

###命令行
__90%__的情况下, 你应该是这样用的...
###Command line
in most cases
```bash
cd path/to/your/static/dir #到你想去地方
puer #泡一杯普洱

cd path/to/your/static/dir
puer
```
或许你想更__深入__一点...

__full options__

```bash
luobo(master) ✗> puer --help

Usage: puer [options...]

Options:
-a,--addon <file> your addon's path
-p,--port <port> server's listen port, 8000 default
-d,--dir <dir> your customer working dir. default current dir
--no-reload close auto-reload feature,(not recommended)
--no-launch close the auto launch feature
-i,--ignored <regexp> ignored file under watching
-t,--time <ms> watching interval time (ms), default 500ms
-h,--help help list
help list
```
其中, __addon__代表你可以传入一个自己的脚本(通常是路由定义用以测试, 也可以拦截.less这些资源请求), 这个模块输出一个函数, 可以获得当前的express的app实例,以及命令行传入的options(一般没啥用)
__tips__: you can use addon javascript to support
```javascript
Expand Down Expand Up @@ -68,6 +61,7 @@ var path = require("path")
var http = require("http")
var puer = require("puer")
var app = connect()
var server = http.createServer(app)
// 可以配置三个参数, 以下为默认值
var options = {
Expand All @@ -76,13 +70,14 @@ var options = {
ignored: /(\/|^)\..*|node_modules/ //忽略的监听文件,默认忽略dotfile 和 node_modules
}
// app 为你的connect 实例 或者 express 实例
// server 为 httpServer 实例
// 这里的options就上面所示的三个参数
app.use(puer.connect(app, options)) //puer connect 中间件,要在static等可能发送请求的中间件之前
app.use(puer.connect(app, server ,options)) //puer connect 中间件,要在static等可能发送请求的中间件之前
app.use("/", connect.static(__dirname))
http.createServer(app).listen(8001, function(){
server.listen(8001, function(){
console.log("listen on 8001 port")
})
Expand All @@ -95,7 +90,16 @@ http.createServer(app).listen(8001, function(){
###Changlog
1. v0.0.6 可以作为connect中间件了, 改为使用更简单的HTML5的SSE实现推送, 解决了内存溢出的问题
*. v0.1.0 变动较大
1. 集成了weinre(参数`-i --inspect`默认端口9001,可在puer folder查看页跳转)
2. 加入了本地ip列表
3. 支持folder页的二维码扫描
*. v0.0.6 可以作为connect中间件了, 改为使用更简单的HTML5的SSE实现推送, 解决了内存溢出的问题
### TODO
增加Adobe Edge inspect 的scroll、form、navigate的同步功能。由于无法控制屏幕休眠这种同步可能意义并没有直接提供客户端那么大,好处是跨平台。
###LICENSE
MIT
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dependencies": {
"coffee-script": "1.4.x",
"express": "3.0.x",
"chokidar": "0.5.3",
"chokidar": "0.8.1",
"socket.io": "0.9.16",
"weinre": "*",
"qrcode-npm": "0.0.3"
Expand Down
4 changes: 4 additions & 0 deletions src/cli.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ exports.run = (args) =>
option.dir = sysPath.resolve pwd, dir if dir
parser.on "-i --inspect", "start weinre server and debug all puer page", (dir) ->
option.inspect = true
parser.on "-x --exclude", "exclude file under watching(must be a regexp), default: ''", (dir) ->
ignored = reg.replace /^\/|\/$/g, ""
option.ignored = new RegExp ignored
parser.on "-a --addon <file>", "your addon's path", (file) ->
option.addon = sysPath.resolve pwd, file

parser.on "--no-reload", "close auto-reload feature,(not recommended)", () ->
option.reload =false
parser.on "--no-launch", "close the auto launch feature", () ->
Expand Down
23 changes: 19 additions & 4 deletions src/connect-puer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,35 @@ cwd = do process.cwd
# middleware options:
#
# * dir: watcher dir (defaults process.cwd())
# * fileType: watched fileType(defaults 'js|css|html|xhtml')
# * filetype: watched fileType(defaults 'js|css|html|xhtml')

module.exports = (app, server, options) ->
app.use '/puer', express.static path.join __dirname, "../vendor"

options.fileType ?= 'js|css|html|xhtml'
options.filetype ?= 'js|css|html|xhtml'
options.reload ?= true
options.inject ?= []
options.ignored ?= /node_modules/


filetype = options.filetype.split('|')

if(!options.dir)
throw Error("dir option is need to watch")
if(options.reload)
options.inject.push '<script src="/puer/js/reload.js"></script>'
watcher = chokidar.watch options.dir,
ignored: new RegExp('\\.(?!(' + options.fileType + ')$)')
ignored: (filename)->
# first detect the ignored list
if options.ignored.test(filename)
return true
else
matched= /\.(\w+)$/.exec(filename)
if matched
ext = matched[1]
return filetype.indexOf(ext) == -1
else
return false
persistent: true

helper.log "watcher on!!"
Expand All @@ -58,7 +73,7 @@ module.exports = (app, server, options) ->
index = sockets.indexOf socket
if index != -1
sockets.splice index, 1


(req, res, next) ->
# proxy
Expand Down
12 changes: 8 additions & 4 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,19 @@ puer = module.exports = (options = {}) ->
boundHost: '-all-'
verbose:false
debug: false
readTimeout:5
deathTimeout: 5
readTimeout: 20
deathTimeout: 50
options.inject.push(
"""
<script>
var url = location.href.replace(/\:\\d+/, ':9001') + 'target/target-script-min.js#anonymous'
var url = 'http://'+location.host.replace(/\:\\d+/, '') +':9001'+ '/target/target-script-min.js#anonymous'
var script = document.createElement('script');
script.src = url;
document.head.appendChild(script);
var head = document.head;
if(head){
head.appendChild(script);
}
</script>
"""
)
Expand Down
10 changes: 6 additions & 4 deletions test/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ var path = require("path")
var http = require("http")

var app = connect()
var server = http.createServer(app)

app.use(require(path.join(__dirname,"../lib/index")).connect(app, {dir: __dirname}))
app.use(require(path.join(__dirname,"../lib/index")).connect(app, server ,{
dir: __dirname
}))
app.use("/", connect.static(__dirname));
app.use("/", connect.directory(__dirname));

app.use("/home", function(req, res, next){
res.setHeader("content-type", "text/html")
res.send('<!doctype html><html lang="en"><head><meta charset="UTF-8" /><title>Document</title></head><body></body></html>')
})

http.createServer(app).listen(8001, function(){
server.listen(8001, function(){
console.log("listen on 8001 port")
})


3 changes: 1 addition & 2 deletions test/fixtures/css/style.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
body {
padding: 50px;
background-color: #ccc;
background-color: #fff;
font: 21px "Lucida Grande", Helvetica, Arial, sans-serif;
}

Expand All @@ -10,7 +10,6 @@ body {
#session {
border-bottom: 1px solid #333;
}

a {
color: #00B7FF;
}
16 changes: 14 additions & 2 deletions vendor/js/folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
if(document.addEventListener){
document.addEventListener("touchstart", function(){}, true)
}
var slice = [].slice;

function _type(_o) {
return _o == null /*means null or undefined*/
Expand Down Expand Up @@ -40,6 +41,16 @@ var _extend = function(o1, o2, override){
}
return o1;
}
// function
_extend(Function.prototype, {
bind: function(context, args) {
args = slice.call(arguments, 1);
var fn = this;
return function() {
fn.apply(context, args.concat(slice.call(arguments)));
}
}
})

_XHR._process = {
json: function(_text, _xml) {
Expand Down Expand Up @@ -106,11 +117,12 @@ _extend(_XHR.prototype, {
_send: function(_data) {
var _xhr = this._xhr,_setting=this._setting;
_data = _data || _setting._data ||{};
_data = _serialize(_data)
this._setHeader(_setting._headers);
if(_setting._method.toLowerCase()=="get"){
_setting.url+="?"+_serialize(_data)
_setting.url+= _data? ('?'+data): '';
}
this._xhr.open(_setting._method.toUpperCase(), _setting.url, _setting._async)
_xhr.open(_setting._method.toUpperCase(), _setting.url, _setting._async)
_xhr.onreadystatechange = this._onStateChange.bind(this);

_xhr.send(_serialize(_data))
Expand Down

0 comments on commit d8a317a

Please sign in to comment.