Skip to content

Instantly share code, notes, and snippets.

@BafS
Created August 15, 2014 17:28
Show Gist options
  • Save BafS/8672d4bae03b66cfca10 to your computer and use it in GitHub Desktop.
Save BafS/8672d4bae03b66cfca10 to your computer and use it in GitHub Desktop.
Yo cmd
{
"name": "yocmd",
"version": "0.1.0",
"description": "Execute a command and send a yo",
"bin": {
"yo": "yo.js"
},
"main": "yo.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"yo"
],
"author": "Fabien Sa",
"license": "MIT",
"preferGlobal": "true"
}
#!/usr/bin/env node
/*
* This script first executes the command passed as argument, then, when the command
* is done, it sends you a yo
*
* All you need to do is define the YO_USER and YO_TOKEN environment variables with:
* YO_USER: your Yo username
* YO_TOKEN: the Yo API token corresponding to the Yo API account you want to receive
* Yos from, don't forget to subscribe to your accounts. (http://dev.justyo.co/)
*
* Uses:
* yo ls -al
* yo npm update -g
* yo make
* yo ENV_VAR=FOO make
* ...
*
* Installation: npm install -g yocmd
*
* Original python source https://gist.github.com/ChrisJamesC/884be774f6f8725bcc84
*/
var http = require('http'),
childProcess = require('child_process');
var arg = process.argv.slice(2).join(' ');
/**
* Send a yo
*/
var sendYo = function() {
// Sends a Yo to the user defined in the envrionement variable YO_USER with the
// Yo account corresponding to the token defined in the environement variable
// YO_TOKEN.
var user = process.env.YO_USER;
var token = process.env.YO_TOKEN;
if(!user)
console.warn('YO_USER undefined');
if(!token)
console.warn('YO_TOKEN undefined');
var data = {
'api_token': token,
'username': user
}
var dataStr = require("querystring").stringify(data);
var options = {
host: 'api.justyo.co',
path: '/yo',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(dataStr)
}
};
var req = http.request(options, function(res) {
var rep = '';
res.on('data', function(chunk) {
rep += chunk;
});
res.on('end', function() {
if(rep !== '{"result": "OK"}')
console.warn('Yo error: ' + rep);
});
});
req.write(dataStr);
req.end();
};
var exec = childProcess.exec,
child = exec(arg); // Execute the command
child.stdout.on('data', function(data) {
process.stdout.write(data);
});
child.stderr.on('data', function(data) {
process.stderr.write(data);
});
// Wait for the command execution to finish
child.on('exit', function() {
sendYo(); // Send a yo
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment