Skip to content

Commit

Permalink
new file structure and added required files
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Parolisi committed Mar 21, 2013
1 parent 23adb02 commit fea7864
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 159 deletions.
35 changes: 35 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,35 @@
/*
* peon-gui
* Creates a local webserver GUI tool to run Grunt tasks
*
* Copyright (c) 2013 Mark Parolisi, contributors
* Licensed under the MIT license.
*/

/*global module */
module.exports = function (grunt) {
"use strict";
grunt.initConfig({
jshint: {
all: [
'Gruntfile.js',
'app/js/*.js',
'<%= nodeunit.tests %>'
],
options: { }
},
coffeelint: {
app: ['lib/*.coffee', 'tasks/*.coffee']
},
nodeunit: {
tests: ['tests/*_test.js']
}
});

grunt.loadTasks('tasks');

grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-coffeelint');
grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.registerTask('default', ['jshint', 'coffeelint', 'nodeunit']);
};
22 changes: 22 additions & 0 deletions LICENSE-MIT
@@ -0,0 +1,22 @@
Copyright (c) 2013 Mark Parolisi, contributors

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.
25 changes: 25 additions & 0 deletions lib/peon-gui-server.coffee
@@ -0,0 +1,25 @@
class PeonGUIServer
grunt: require('grunt')
connect: require('connect')
path: require('path')
server: false
startPort = 8080
endPort = 8088

constructor: (worker) ->
@worker = worker

run: () ->
ps = require('portscanner')
that = @
workerPort = @worker.getSocket()
ps.findAPortNotInUse(startPort, endPort, 'localhost', (err, port) ->
appPath = that.path.resolve(__dirname, '../app')
that.server = that.connect.createServer(that.connect.static(appPath))
that.server.listen(port)
that.grunt.log.writeln "GUI running on localhost:#{port}"
url = "http://localhost:#{port}/?socket=#{workerPort}"
that.grunt.log.writeln "Manage this project on #{url}"
)

module.exports = PeonGUIServer
115 changes: 115 additions & 0 deletions lib/peon-web-socket.coffee
@@ -0,0 +1,115 @@
class PeonWebSocket
grunt = require "grunt"
spawn = require("child_process").spawn
pkg: require(process.cwd() + '/package.json')
workers: []
projectPort: 0
server: require('http').createServer((request, response) ->
response.writeHead(404)
response.end()
)

constructor: () ->
process.on("uncaughtException", @killWorkers)
process.on("SIGINT", @killWorkers)
process.on("SIGTERM", @killWorkers)
@tasks = grunt.task._tasks
@removeTasks(['gui'])
@addConfigToTasks()
peonFile = __dirname.replace("tasks/peon-gui/tasks", "peon.coffee")
if grunt.file.exists(peonFile)
gruntFilePath = peonFile
else if grunt.file.exists("Gruntfile.cofee")
gruntFilePath = "Gruntfile.coffee"
else if grunt.file.exists("Gruntfile.js")
gruntFilePath = "Gruntfile.js"
else
grunt.fatal("No Gruntfile found")

addConfigToTasks: () ->
config = grunt.config.get()
that = @
grunt.util._.forEach(@tasks, (task, k)->
taskConfig = JSON.stringify(config[k], null, 4) || "No configuration"
that.tasks[k].config = taskConfig
)

removeTasks: (taskList) ->
that = @
grunt.util._.forEach(@tasks, (task, k)->
if grunt.util._.indexOf(taskList, task.name) > -1
delete that.tasks[k]
)

killWorkers: () ->
@workers.forEach((worker) ->
process.kill(worker)
)
process.exit()

getSocket: ()->
@projectPort

startWorker: ()->
ps = require('portscanner')
that = @
ps.findAPortNotInUse(61750, 61755, 'localhost', (err, port) ->
that.projectPort = port
pgs = require("./PeonGUIServer")
new pgs(that).run()
if that.projectPort
that.server.listen(port, () ->
grunt.log.writeln("WebSocket running on localhost:#{port}")
)
else
grunt.log.writeln("Too many Peon WebSockets open. Close one.")
)
@listen()

listen: () ->
WebSocketServer = require('websocket').server
wsServer = new WebSocketServer(
httpServer: @server
autoAcceptConnections: false
)
that = @
wsServer.on('request', (request) ->
connection = request.accept('echo-protocol', request.origin)
connection.on('message', (message) ->
if message.type is 'utf8'
msg = message.utf8Data
if msg is 'connect'
connection.sendUTF(JSON.stringify(
tasks: that.tasks
project: that.pkg.name
port: that.projectPort
action: "connected"
))
else if Object.keys(that.tasks).indexOf(msg) > -1
connection.send("Running Task: #{msg}")
args = [
'--gruntfile'
gruntFilePath
'--base'
'.'
msg
]
command = spawn('grunt', args)
that.workers.push(command)
command.stdout.on('data', (data) ->
if data then connection.send(data.toString())
)
command.stdout.on('end', (data) ->
connection.sendUTF(JSON.stringify({ action: 'done'}))
)
command.stderr.on('data', (stderr) ->
grunt.log.writeln stderr
if stderr then connection.send(stderr.toString())
)
)
connection.on('close', () ->

)
)

module.exports = PeonWebSocket
68 changes: 48 additions & 20 deletions package.json
@@ -1,22 +1,50 @@
{
"name": "peon-gui",
"description": "Web interface for Peon tasks",
"version": "0.1.0",
"engines": {
"node": ">= 0.8.0"
},
"devDependencies": {
"grunt-contrib-jshint": "~0.1.1",
"grunt-contrib-clean": "~0.4.0",
"grunt-contrib-nodeunit": "~0.1.2",
"grunt": "~0.4.0"
},
"peerDependencies": {
"grunt": "~0.4.0"
},
"dependencies": {
"websocket": "~1.0.8",
"portscanner": "~0.1.3",
"connect": "latest"
}
"name": "peon-gui",
"description": "Web interface for Grunt tasks",
"keywords": [
"gruntplugin",
"server",
"http",
"gui"
],
"version": "0.1.0",
"homepage": "https://github.com/",
"author": {
"name": "Mark Parolisi",
"url": "http://markparolisi.com/"
},
"repository": {
"type": "git",
"url": "git://github.com/"
},
"bugs": {
"url": "https://github.com/ /issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/ /blob/master/LICENSE-MIT"
}
],
"main": "Gruntfile.js",
"scripts": {
"test": "grunt test"
},
"engines": {
"node": ">= 0.8.0"
},
"devDependencies": {
"grunt-contrib-jshint": "latest",
"grunt-contrib-nodeunit": "latest",
"grunt-coffeelint" : "latest",
"grunt": "~0.4.0"
},
"peerDependencies": {
"grunt": "~0.4.0"
},
"dependencies": {
"websocket": "~1.0.8",
"portscanner": "~0.1.3",
"connect": "latest"
}
}
9 changes: 9 additions & 0 deletions tasks/peon-gui.coffee
@@ -0,0 +1,9 @@
module.exports = (grunt) ->

grunt.registerTask('gui', 'Launch a GUI web interface', () ->
@async()
peonWebSocket = require "../lib/peon-web-socket"
new peonWebSocket().startWorker()

)

0 comments on commit fea7864

Please sign in to comment.