Skip to content

Commit

Permalink
git
Browse files Browse the repository at this point in the history
  • Loading branch information
lancejpollard committed Nov 10, 2011
1 parent 1fad161 commit fb1bddf
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 52 deletions.
5 changes: 1 addition & 4 deletions package.json
Expand Up @@ -26,12 +26,9 @@
"engines": { "node": ">= 0.4.0" },
"bin": { "design.io": "./bin/design.io" },
"dependencies": {
"shift": ">= 0.1.4",
"watch-node": ">= 0.1.5",
"commander": ">= 0.3.2",
"socket.io": ">= 0.8.6",
"connect": ">= 1.7.2",
"request": ">= 2.1.1",
"express": ">= 2.5.0"
"request": ">= 2.1.1"
}
}
41 changes: 23 additions & 18 deletions src/design.io/client.coffee
Expand Up @@ -17,11 +17,11 @@ class window.DesignIO
socket.on 'connect', ->
socket.emit 'userAgent', self.userAgent()
socket.on 'watch', (data) ->
self.watch(data)
self.watch JSON.parse(data, @reviver)
socket.on 'exec', (data) ->
self.exec(data)
self.exec JSON.parse(data, @reviver)

# on "ready"
# on "create"
on: (name, callback) ->
@callbacks[name] = callback

Expand All @@ -30,22 +30,11 @@ class window.DesignIO
true

watch: (data) ->
watchers = data.body
actions = ["create", "update", "delete"]

for watcher in watchers
watcher.match = eval(watcher.match)

for action in actions
watcher[action] = eval(watcher[action]) if watcher.hasOwnProperty(action)

for pattern, i in watcher.patterns
watcher.patterns[i] = new RegExp(pattern.source, pattern.options)

@watchers = watchers
@watchers = data.body

exec: (data) ->
watchers = @watchers

for watcher in watchers
if watcher.match(data.path)
watcher[data.action].call(@, data) if watcher.hasOwnProperty(data.action)
Expand All @@ -56,9 +45,25 @@ class window.DesignIO
if typeof(data) == "object"
data.userAgent = window.navigator.userAgent
data.url = window.location.href

@socket.emit 'log', data
@socket.emit 'log', JSON.stringify(data, @replacer)

userAgent: ->
userAgent: window.navigator.userAgent
url: window.location.href

replacer: (key, value) ->
if typeof value == "function"
"(#{value})"
else
value

reviver: (key, value) ->
if typeof value == "string" &&
# match start of function or regexp
!!value.match(/^(\(?:function\s*\(\)\s*\{|\(\/)/) &&
# match end of function or regexp
!!value.match(/(?:\}\s*\)|\/\w*\))$/)
eval(value)
else
value
5 changes: 2 additions & 3 deletions src/design.io/extensions/stylesheets.coffee
Expand Up @@ -40,10 +40,9 @@ module.exports = ->

# this should get better so it knows how to map template files to browser files
update: (data) ->
stylesheets = @stylesheets
stylesheets[data.id].remove() if stylesheets[data.id]?
@stylesheets[data.id].remove() if @stylesheets[data.id]?
node = $("<style id='#{data.id}' type='text/css'>#{data.body}</style>")
stylesheets[data.id] = node
@stylesheets[data.id] = node
$("body").append(node)

delete: (data) ->
Expand Down
11 changes: 6 additions & 5 deletions src/design.io/process.coffee
Expand Up @@ -3,10 +3,11 @@
command = new (require("#{__dirname}/command"))(process.argv)
command.run()

server = spawn "node", ["#{__dirname}/server", "--watchfile", command.program.watchfile, "--directory", command.program.directory, "--port", command.program.port]
server = spawn "node", [
"#{__dirname}/server",
"--watchfile", command.program.watchfile,
"--directory", command.program.directory,
"--port", command.program.port
]
server.stdout.on 'data', (data) -> console.log data.toString().trim()
server.stderr.on 'data', (data) -> console.log data.toString().trim()

#watcher = spawn "node", ["#{__dirname}/design.io/watcher", "--directory", program.directory, "--watch", program.watch]
#watcher.stdout.on 'data', (data) -> console.log data.toString().trim()
#watcher.stderr.on 'data', (data) -> console.log data.toString().trim()
21 changes: 11 additions & 10 deletions src/design.io/server.coffee
@@ -1,15 +1,16 @@
connect = require('connect')
express = require('express')
designIO = require('../design.io')
command = new (require("./command"))(process.argv)
command.run()

app = express.createServer()
designer = require('./connection')(require('socket.io').listen(app))
app.listen(command.program.port)
app = require("http").createServer (request, response) ->
action = request.url.split("/")
action = action[action.length - 1]

designer.emit action, request.body

response.writeHead 200, "Content-Type": "application/json"
response.write action
response.end()

app.use connect.bodyParser()
designer = require('./connection')(require('socket.io').listen(app))

app.post '/design.io/:action', (request, response) ->
designer.emit request.params.action, request.body
response.send request.params.action
app.listen(command.program.port)
34 changes: 22 additions & 12 deletions src/design.io/watcher.coffee
Expand Up @@ -28,6 +28,7 @@ class Watcher
function() {
var watch = this.watch;
var ignorePaths = this.ignorePaths;
var watcher = this.watcher;
global.Watcher = require('./watcher');
#{result}
delete global.Watcher
Expand Down Expand Up @@ -61,6 +62,22 @@ class Watcher

data

@replacer: (key, value) ->
if typeof value == "function"
"(#{value})"
else
value

@reviver: (key, value) ->
if typeof value == "string" &&
# match start of function or regexp
!!value.match(/^(\(?:function\s*\(\)\s*\{|\(\/)/) &&
# match end of function or regexp
!!value.match(/(?:\}\s*\)|\/\w*\))$/)
eval(value)
else
value

@exec: (path, action, timestamp) ->
watchers = @all()

Expand All @@ -82,7 +99,7 @@ class Watcher
params =
url: "#{@url}/design.io/#{action}"
method: "POST"
body: JSON.stringify(data)
body: JSON.stringify(data, @replacer)
headers:
"Content-Type": "application/json"

Expand Down Expand Up @@ -145,22 +162,15 @@ class Watcher
@constructor.broadcast action, data

toJSON: ->
data = patterns: []

for pattern in @patterns
options = []
options.push "m" if pattern.multiline
options.push "i" if pattern.ignoreCase
options.push "g" if pattern.global
data.patterns.push source: pattern.source, options: options.join("")
data =
patterns: @patterns
match: @match

data.match = "(#{@match.toString()})"

if @hasOwnProperty("client")
actions = ["create", "update", "delete"]
client = @client
for action in actions
data[action] = "(#{client[action].toString()})" if client.hasOwnProperty(action)
data[action] = client[action] if client.hasOwnProperty(action)

data

Expand Down

0 comments on commit fb1bddf

Please sign in to comment.