got bored one night and decided to write a cli script to randomly show a new place to work. It's got to be from a pool of selections (see places.json
for the barebones). It definitely looks like it's only been worked on for 90 minutes, but whatever. (if you're using it, you'll need to remove the newjob.
prefix from the files)
Created
February 17, 2015 00:53
-
-
Save rococodogs/12a496754ec7c48404c5 to your computer and use it in GitHub Desktop.
sometimes you just need to see what's out there
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
#!/usr/bin/env iojs | |
var path = require('path') | |
, fs = require('fs') | |
, jsonPath = path.join(__dirname, 'places.json') | |
, jobs = require(jsonPath) | |
, places = jobs.places | |
, spawn = require('child_process').spawn | |
, argv = require('minimist')(process.argv.slice(2)) | |
, prompt = require('prompt') | |
, rand = (function() { return places[Math.floor(Math.random() * places.length)]; })() | |
, adjectives = [ 'great', 'pretty cool', 'fun', 'exciting', 'neat', 'rad', 'cool', 'pretty great' ] | |
, randAdj = (function() { return adjectives[Math.floor(Math.random() * adjectives.length)]; })() | |
; | |
// picker helper | |
Array.prototype.findFirst = function(cb) { | |
var result, i = 0, len = this.length; | |
for(;i < len; i++) { | |
if ( result ) { return result; } | |
else { | |
if ( cb(this[i], i, this) ) { | |
result = this[i]; | |
} | |
} | |
} | |
return result; | |
} | |
if ( argv._.indexOf('help') > -1 || argv.h || argv.help ) { | |
usage(); | |
} | |
if ( argv._.indexOf('add') > -1 ) { | |
runNew(); | |
} | |
if ( argv._.indexOf('list') > -1 ) { | |
listPlaces(argv.v || argv.verbose); | |
} | |
if ( argv._.indexOf('at') === 0 ) { | |
argv._.shift(); | |
if ( argv._.length > 0 ) { | |
var name = argv._[0] | |
, thisplace = places.findFirst(function(place) { | |
return place.name.toLowerCase() === name; | |
}) | |
, msg | |
; | |
if ( thisplace ) { | |
openURL(thisplace.url, thisplace.name + '! Good call; I hear they\'re ' + randAdj + '!'); | |
} else { | |
console.warn('Uh oh! ' + name + ' isn\'t a place I have on file.'); | |
console.info('If you want to add a place, try "' | |
+ path.basename(process.argv[1]) | |
+ ' add"'); | |
process.exit(1); | |
} | |
} | |
} | |
if ( argv._.length === 0 ) { | |
openURL(rand.url, 'How about ' + rand.name + '? I hear they\'re ' + randAdj + '!'); | |
} | |
function listPlaces(verbose) { | |
places.forEach(function(place) { | |
console.log(place.name + (verbose ? (':\t' + place.url) : '')); | |
}); | |
process.exit(0); | |
} | |
function openURL(url, message) { | |
spawn('open', [ url ]).on('exit', function() { | |
console.log(message); | |
}); | |
} | |
function runNew() { | |
var name, url; | |
prompt.message = prompt.delimiter = ''; | |
prompt.start(); | |
prompt.get({ | |
properties: { | |
name: { | |
description: 'What place is it? '.green, | |
required: true | |
}, | |
url: { | |
description: 'How about a URL? '.green, | |
required: true | |
} | |
} | |
}, function(err, result) { | |
places.push({ | |
"name": result.name, | |
"url": result.url | |
}); | |
fs.writeFile(jsonPath, JSON.stringify(jobs), function() { | |
console.log('Added ' + result.name + ' to the list of places! ^_^'); | |
process.exit(0); | |
}); | |
}); | |
} | |
function usage() { | |
console.log('Someimes you just need to see what else is out there'); | |
console.log('----------------'); | |
console.log('usage: ' + path.basename(process.argv[1]) + ' ([action])'); | |
console.log(' help, -h, --help this screen'); | |
console.log(' at <name> open jobs page for <name>'); | |
console.log(' add prompt to add a place'); | |
console.log(' list (-v, --verbose) list places (verbose shows urls)'); | |
console.log(); | |
console.log('using no action opens a page at random'); | |
process.exit(0); | |
} |
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
{ | |
"places": [] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment