Skip to content

Instantly share code, notes, and snippets.

@rmarscher
Created October 18, 2013 17:17
Show Gist options
  • Save rmarscher/7044775 to your computer and use it in GitHub Desktop.
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.
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();
}
{
"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