Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 0da4b80b78848b9310cff45686064f26f10b76df @sebv committed Apr 24, 2012
@@ -0,0 +1,4 @@
+node_modules
+.DS_Store
+workdir
+*-debug.log
113 Cakefile
@@ -0,0 +1,113 @@
+###
+Cakefile
+###
+{execFile, spawn, exec} = require 'child_process'
+
+task 'compile', 'Compile all coffee files', ->
+ compileAll()
+
+task 'compile:watch', 'Compile all coffee files and watch for change', ->
+ compileAll true
+
+task 'clean', 'Remove all js files', ->
+ cleanAllJs()
+
+task 'test', 'Run all tests', ->
+ testSync (syncStatus) ->
+ if syncStatus is 0
+ testAsync (asyncStatus) ->
+ if(asyncStatus == 0)
+ console.log "\n\nAll Tests Succesful.\n\n"
+ else
+ console.warn "\n\n!!! Async tests failed.\n\n"
+ else
+ console.warn "\n\n!!! Sync tests failed.\n\n"
+
+task 'test:sync', 'Run all sync tests (using mocha)', ->
+ compileTestEnv()
+ testSync()
+
+task 'test:async', 'Run all async tests (using vows)', ->
+ testAsync()
+
+task 'grep:dirty', 'Lookup for debugger and console.log in code', ->
+ grepDirty()
+
+task 'kill:all:phantom', 'Kill all phantom process', ->
+ killAllPhantom()
+
+compileAll = (watch = false) ->
+ compileCoffee ['lib','test','workdir'], ['index.coffee'], watch
+
+cleanAllJs = ->
+ cleanJs ['lib','test','workdir'], ['index.js']
+
+compileCoffee = (dirs , files , watch = false) ->
+ params = ['--compile']
+ params.push('--watch') if watch
+ params = params.concat dirs
+ params = params.concat files
+ _spawn 'coffee', params
+
+compileTestEnv = () ->
+ compileCoffee [], ['test/testing-env.coffee'], false
+
+testDirWithMocha = (dir , done) ->
+ execFile 'find', [ dir ] , (err, stdout, stderr) ->
+ files = (stdout.split '\n').filter( (name) -> name.match /.+\.coffee/ )
+ params = ['-R', 'spec', '--colors'].concat files
+ _spawn 'mocha', params, false , done
+
+testDirWithVows = (dir , done) ->
+ execFile 'find', [ dir ] , (err, stdout, stderr) ->
+ files = (stdout.split '\n').filter( (name) -> name.match /.+\.coffee/ )
+ params = ['--spec'].concat files
+ _spawn 'vows', params, false, done
+
+testSync = (done) ->
+ testDirWithMocha 'test/sync', done
+
+testAsync = (done) ->
+ testDirWithVows 'test/async', done
+
+cleanJs = (dirs , files) ->
+ execFile 'find', dirs , (err, stdout, stderr) ->
+ _files = (stdout.split '\n').filter( (name) -> name.match /.+\.js/ )
+ files = files.concat _files
+ _spawn 'rm', files, false
+ compileTestEnv()
+
+grepDirty = (dirs , word) ->
+ execFile 'find', [ '.' ] , (err, stdout, stderr) ->
+ files = (stdout.split '\n')\
+ .filter( (name) -> not name.match /\/node_modules\//)\
+ .filter( (name) -> not name.match /\/workdir\//)\
+ .filter( (name) -> not name.match /\/\.git\//)\
+ .filter( (name) ->
+ ( name.match /\.js$/) or
+ (name.match /\.coffee$/ ) )
+ _spawn 'grep', (['console.log'].concat files), false
+ _spawn 'grep', (['debugger'].concat files), false
+
+killAllPhantom = ->
+ cmd = "kill -9 `ps -el | grep phantom | grep -v grep | awk '{ print $2 }'`"
+ console.log cmd
+ _exec cmd
+
+_spawn = (cmd,params,exitOnError=true, done) ->
+ proc = spawn cmd, params
+ proc.stdout.on 'data', (buffer) -> process.stdout.write buffer.toString()
+ proc.stderr.on 'data', (buffer) -> process.stderr.write buffer.toString()
+ proc.on 'exit', (status) ->
+ process.exit(1) if exitOnError and status != 0
+ done status if done?
+
+_exec = (cmd,exitOnError=true) ->
+ proc = exec cmd
+ proc.stdout.on 'data', (buffer) -> process.stdout.write buffer.toString()
+ proc.stderr.on 'data', (buffer) -> process.stderr.write buffer.toString()
+ proc.on 'exit', (status) ->
+ process.exit(1) if exitOnError and status != 0
+
+# unix kill all phantom
+
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2012 Seb Vincent
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,84 @@
+# phantomjs-node-sync
+
+This is a synchronous version of [phantomjs-node](http://github.com/sgentle/phantomjs-node)
+using [node-fibers](http://github.com/laverdet/node-fibers). There are different modes
+available, allowing the [PhantomJS](http://www.phantomjs.org/) API to be used synchronously,
+asynchronously, or a mix of both.
+
+## install
+```
+npm install phantom-sync
+```
+
+
+## simple usage (coffeescript)
+```coffeescript
+{Phantom,Sync} = (require 'phantom-sync')
+
+phantom = new Phantom
+
+Sync ->
+ ph = phantom.create()
+ page = ph.createPage()
+ status = page.open "http://www.google.com"
+ console.log "status=", status
+ title = page.evaluate ->
+ document.title
+ console.log "title=", title
+ ph.exit()
+```
+## modes
+Please refer to [node-make-sync](http://github.com/sebv/node-make-sync) for
+detailed mode description.
+
+```coffeescript
+# sync (default)
+phantom = new Phantom
+phantom = new Phantom mode:'sync'
+# async
+phantom = new Phantom mode:'async'
+# mixed-args (mixed default)
+phantom = new Phantom mode:'mixed'
+phantom = new Phantom mode:['mixed','args']
+# mixed-fibers
+phantom = new Phantom mode:['mixed','fibers']
+```
+
+# mixed mode example (coffeescript)
+```coffeescript
+{Phantom,Sync} = (require 'phantom-sync')
+
+phantom = new Phantom {mode:'mixed'}
+
+[page,ph] = []
+Sync ->
+ console.log "Step 1"
+ ph = phantom.create()
+ page = ph.createPage()
+ status = page.open "http://www.google.com"
+ console.log "status=", status
+ title = page.evaluate ->
+ document.title
+ console.log "title=", title
+
+# reusing the previous objects
+setTimeout ->
+ console.log "Step 2"
+ page.open "http://www.yahoo.com", (status) ->
+ console.log "status=", status
+ page.evaluate (-> document.title), (title) ->
+ console.log "title=", title
+ ph.exit()
+ # creating a new set of object
+ setTimeout ->
+ console.log "Step 3"
+ phantom.create (ph2) ->
+ ph2.createPage (page2) ->
+ page2.open "http://www.apple.com", (status) ->
+ console.log "status=", status
+ page2.evaluate (-> document.title), (title) ->
+ console.log "title=", title
+ ph2.exit()
+ , 5000
+, 5000
+```
@@ -0,0 +1 @@
+module.exports = require './lib/phantom-sync'

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,40 @@
+{Sync, MakeSync} = require('node-make-sync')
+phantom = require 'phantom'
+
+buildObjectOptions = (options) ->
+ ph:
+ mode: options.mode
+ exclude: [/^_/, 'exit']
+ num_of_args:
+ set: 2
+ page:
+ mode: options.mode
+ exclude: [/^_/, 'sendEvent']
+ num_of_args:
+ evaluate: 1
+ set: 2
+
+# Builds replacement for the phantom.create method
+createReplacement = (options) ->
+ objectOptions = buildObjectOptions options
+ MakeSync \
+ (args..., done) ->
+ # disassembling and rebuilding child object structure
+ # with sync version of objects
+ phantom.create args..., (ph) ->
+ _createPage = ph.createPage
+ ph.createPage = (args..., done) ->
+ _createPage args..., (page) ->
+ MakeSync page, objectOptions.page
+ done page
+ MakeSync ph, objectOptions.ph
+ done(ph)
+ , options
+
+class Phantom
+ constructor: (options) ->
+ options = {mode: 'sync'} unless options?
+ @create = createReplacement options
+
+exports.Phantom = Phantom
+exports.Sync= Sync

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,28 @@
+{
+ "name": "phantom-sync"
+ , "version": "0.0.1"
+ , "description": "sync version of node-phantom"
+ , "keywords": ["phantom", "phantomjs", "sync", "synchronous"]
+ , "author": "Seb Vincent <seb.vincent@gmail.com>"
+ , "repository": { "type": "git", "url": "git@github.com:sebv/node-phantomjs-sync.git" }
+ , "main": "./index"
+ , "engine": "node >= 0.6.1"
+ , "scripts": {
+ "test": "cake test"
+ }
+ , "dependencies": {
+ "phantom": "latest"
+ ,"node-make-sync" : "latest"
+ }
+ , "devDependencies" : {
+ "coffee-script" : "latest"
+ , "mocha" : "latest"
+ , "should": "latest"
+ , "vows": "latest"
+ , "assert": "latest"
+ , "ps-tree": "latest"
+ , "express": "latest"
+ , "temp": "latest"
+ , "underscore": "latest"
+ }
+}
Oops, something went wrong.

0 comments on commit 0da4b80

Please sign in to comment.