Skip to content

Commit

Permalink
Refactor with client as re-usable module.
Browse files Browse the repository at this point in the history
  • Loading branch information
smblott-github committed Sep 7, 2016
1 parent a72e8b3 commit 2b99a0d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,5 +1,5 @@

src = utils.coffee extension/utils.coffee server.coffee client.coffee
src = utils.coffee extension/utils.coffee chromix-too.coffee server.coffee client.coffee
js = $(src:.coffee=.js)

build: $(js)
Expand Down
32 changes: 32 additions & 0 deletions chromix-too.coffee
@@ -0,0 +1,32 @@

# Usage:
# see examples in ./client.coffee.

utils = require "./utils"
utils.extend global, utils

module.exports = (sock = config.sock) ->

# This sends a single request to chrome, unpacks the response, and calls any callbacks with the response as
# argument(s).
chromix:
(path, request, extra_arguments...) ->
extend request, {path}
request.args ?= []
callbacks = []

# Extra arguments which are functions are callbacks (usually just one); all other arguments are added to the
# list of arguments.
for arg in extra_arguments
(if typeof(arg) == "function" then callbacks else request.args).push arg

client = require("net").connect sock, ->
client.write JSON.stringify request

client.on "data", (data) ->
response = JSON.parse data.toString "utf8"
if response.error
console.error "error: #{response.error}"
process.exit 1
callback response.response... for callback in callbacks
client.destroy()
25 changes: 2 additions & 23 deletions client.coffee
@@ -1,6 +1,6 @@
`#!/usr/bin/env node
`
utils = require "./utils.js"
utils = require "./utils"
utils.extend global, utils

optimist = require "optimist"
Expand All @@ -13,28 +13,7 @@ if args.help
optimist.showHelp()
process.exit(0)

# This sends a single request to chrome, unpacks the response, and calls any callbacks with the response as
# argument(s).
chromix = (path, request, extra_arguments...) ->
extend request, {path}
request.args ?= []
callbacks = []

# Extra arguments which are functions are callbacks (usually just one); all other arguments are added to the
# list of arguments.
for arg in extra_arguments
(if typeof(arg) == "function" then callbacks else request.args).push arg

client = require("net").connect args.sock, ->
client.write JSON.stringify request

client.on "data", (data) ->
response = JSON.parse data.toString "utf8"
if response.error
console.error "error: #{response.error}"
process.exit 1
callback response.response... for callback in callbacks
client.destroy()
chromix = require("./chromix-too")(args.sock).chromix

[ commandName, commandArgs ] =
if 2 < process.argv.length then [ process.argv[2], process.argv[3...] ] else [ "ping", [] ]
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -9,6 +9,7 @@
"engines" : { "node" : ">=0.4" },
"keywords": [ "chrome", "chromix", "extension", "cli", "command", "line", "linux" ],
"dependencies": { "ws" : "*", "optimist" : "*" },
"main": "chromix-too.js",
"bin": {
"chromix-too-server": "./server.js",
"chromix-too": "./client.js"
Expand Down
2 changes: 1 addition & 1 deletion utils.coffee
@@ -1,5 +1,5 @@

utils = require "./extension/utils.js"
utils = require "./extension/utils"
utils.extend exports, utils

utils.extend exports.config,
Expand Down

0 comments on commit 2b99a0d

Please sign in to comment.