Created
October 18, 2013 17:17
-
-
Save rmarscher/7044775 to your computer and use it in GitHub Desktop.
Invoke a shotgun.js shell from command-line without opening prompt. Put these files in a directory, run npm install, then run `node . help` to see it work.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var readline = require('readline'), | |
shotgun = require('shotgun'), | |
shell = new shotgun.Shell(), | |
context = {}; // Declare empty context object. | |
// Create interface that reads from console and outputs to console. | |
var rl = readline.createInterface(process.stdin, process.stdout); | |
// Configure shotgun. | |
shell | |
// Handle the onContextSave callback and save the context to our custom variable. | |
.onContextSave(function (updatedContext) { | |
context = updatedContext; | |
}) | |
// This callback is fired every time shotgun sends data back to your application. | |
.onData(function (data) { | |
if (data.clearDisplay) { | |
console.log('\u001B[2J\u001B[0;0f'); | |
} | |
if (data.line) { | |
console[data.line.type](data.line.text); | |
} | |
if (data.exit) { | |
rl.close(); | |
process.exit(); | |
} | |
}); | |
rl.on('line', function (cmdStr) { | |
shell.execute(cmdStr, context); | |
rl.prompt(); | |
}); | |
if (process.argv.length > 2) { | |
// trying to invoke a command directly | |
var cmdStr = process.argv.slice(2).join(" "); | |
shell.execute(cmdStr, context); | |
if (process.argv.indexOf('--prompt') !== -1) { | |
rl.prompt(); | |
} else { | |
process.exit(); | |
} | |
} else { | |
rl.prompt(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "shotgun-test", | |
"version": "0.1.0", | |
"decription": "Trying out shotgun.", | |
"dependencies": { | |
"shotgun": "~5.2.24", | |
"readline": "0.0.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment