Skip to content
This repository has been archived by the owner on Feb 4, 2018. It is now read-only.

Commit

Permalink
added binder-control integration
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewosh committed Mar 25, 2016
1 parent da0a36d commit 058a31c
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
15 changes: 15 additions & 0 deletions conf/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"port": 3000,
"build": {
"host": "localhost",
"port": 8082
},
"registry": {
"host": "localhost",
"port": 8082
},
"deploy": {
"host": "localhost",
"port": 8084
}
}
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var server = require('./server/index.js')
var cli = require('./server/cli.js')

module.exports = {
server: server,
cli: cli
}


116 changes: 116 additions & 0 deletions server/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
var _ = require('lodash')
var fs = require('fs')
var shell = require('shelljs')
var path = require('path')

var start = require('./index')
var startWithPM2 = require('binder-utils').startWithPM2

/**
* The argument-parsing and action components of a CLI command are separated so that the CLI
* can both be imported from other modules and launched via PM2
*/
function Command (name, cli, action) {
if (!(this instanceof Command)) {
return new Command(name, cli, action)
}
this.name = name
this.cli = cli
this.action = action
}

/**
* Add an action for this command to the main program
*
* @param {Program} program - main commander program
*/
Command.prototype.makeAction = function (program) {
program.command(this.name)
this.cli(program)
program.action(this.action)
return program
}

var makeCommands = function () {
var commands = [
Command('start', function (program) {
program
.description('Start the binder-web server')
.option('-a, --api-key <key>', 'Binder API key')
.option('-c, --config <file>', 'module configuration file')
.option('-p, --port <port>', 'binder-web server port')
return program
}, function (options) {
console.log('Starting the binder-web server...')
var opts = {}
if (options.config) {
_.merge(opts, JSON.parse(fs.readFileSync(path.resolve(options.config))))
} if (options.port) {
opts.port = options.port
} if (process.env['BINDER_API_KEY']) {
opts.apiKey = process.env['BINDER_API_KEY']
} if (options.apiKey) {
opts.apiKey = options.apiKey
}
start(opts)
}),
Command('stop', function (program) {
program
.description('Stop the binder-web server')
return program
}, function (options) {
console.log('Stopping the binder-web server...')
shell.exec('pm2 delete binder-web-start')
})
// TODO: add any module-specific commands here
]
return commands
}

var setupProgram = function (commands, program) {
_.forEach(commands, function (cmd) {
var command = program.command(cmd.name)
command = cmd.cli(command)
command.action(cmd.action)
})
}

var pm2CLI = function (program) {
if (!program) {
program = require('commander')
}
// Replace all actions with PM2 subprocess creation
var pm2Commands = _.map(makeCommands(), function (cmd) {
if (cmd.name === 'start') {
cmd.action = function (options) {
console.log('Starting the binder-web server...')
startWithPM2({
name: 'binder-web-' + cmd.name,
script: path.join(__dirname, 'cli.js'),
args: process.argv.slice(2)
})
}
}
return cmd
})
setupProgram(pm2Commands, program)
return program
}

var standaloneCLI = function (program) {
if (!program) {
program = require('commander')
}
setupProgram(makeCommands(), program)
return program
}

if (require.main === module) {
var program = standaloneCLI()
program.parse(process.argv)
} else {
module.exports = {
standaloneCLI: standaloneCLI,
pm2CLI: pm2CLI
}
}
17 changes: 17 additions & 0 deletions server/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var path = require('path')
var fs = require('fs')
var jsonfile = require('jsonfile')

var optsPath = path.join(process.env['HOME'], '.binder-web.conf')
var defaultOptsPath = path.join(__dirname, '../conf/main.json')
var packagePath = path.join(__dirname, '../package.json')

if (fs.existsSync(optsPath)) {
var fullSettings = jsonfile.readFileSync(optsPath)
} else {
var fullSettings = jsonfile.readFileSync(defaultOptsPath)
}
var packageJson = jsonfile.readFileSync(packagePath)
fullSettings.name = packageJson.name

module.exports = fullSettings
12 changes: 12 additions & 0 deletions start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var program = require('commander')
var cli = require('./server/cli.js')

program
.version('1.0.0')

cli.standaloneCLI(program)
console.log('process.argv: ' + process.argv)
if (process.argv.length === 2) program.help()
program.parse(process.argv)


0 comments on commit 058a31c

Please sign in to comment.