Last active
December 28, 2015 09:39
-
-
Save hallettj/7479891 to your computer and use it in GitHub Desktop.
Substack Rube Goldburg Challenge: runs some input through as many of substack's npm modules as possible.
This file contains hidden or 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 fs = require('fs'); | |
var exec = require('child_process').exec; | |
var templ_ = fs.readFileSync('package.json.template', { encoding: 'UTF-8' }); | |
var templ = JSON.parse(templ_); | |
var deps = {}; | |
exec("npm search substack | grep '=substack' | awk '{ print $1 }'", function(err, stdout) { | |
var lines = stdout.toString('UTF-8'); | |
lines.split("\n").forEach(function(line) { | |
if (line) { | |
deps[line] = "*"; | |
} | |
}); | |
templ.dependencies = deps; | |
fs.writeFileSync('package.json', JSON.stringify(templ), { encoding: 'UTF-8' }); | |
}); |
This file contains hidden or 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": "rube-substack", | |
"version": "1.0.0", | |
"description": "Automated usefulness", | |
"author": { | |
"name": "12 Event Horizon" | |
}, | |
"engines": { | |
"node": ">= 0.10.0" | |
}, | |
"dependencies": {} | |
} |
This file contains hidden or 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 fs = require('fs'); | |
var exec = require('child_process').exec; | |
var spawn = require('child_process').spawn; | |
var manifest = JSON.parse(fs.readFileSync('package.json')); | |
var deps = Object.keys(manifest.dependencies); | |
var input = {'hackerolympics': 'dope', 'chipotle': {'price': 'nope', 'originality':'nope'}}; | |
(function applyModules(deps, input, count) { | |
if (deps.length < 1) { | |
console.log("Ran through " + count + " modules: "); | |
console.log(input); | |
return; | |
} | |
install(deps[0], function(err) { | |
// console.log('applying ' + deps[0]); | |
var output = input, count_ = count; | |
var mod = {}; | |
var out; | |
console.log('trying', deps[0]); | |
try { | |
if (deps[0] !== 'browser-launcher') { | |
mod = err ? {} : require(deps[0]); | |
} | |
} | |
catch (_) { | |
console.log('could not load, '+ deps[0]); | |
} | |
var fs = extractFuncs(mod); | |
for (var i = 0; i < fs.length; i += 1) { | |
try { | |
out = fs[i](input); | |
} | |
catch (_) {} | |
if (!!out) { | |
console.log(count_ + 1, deps[0], out); | |
output = out; | |
count_ += 1; | |
break; | |
} | |
} | |
applyModules(deps.slice(1), output, count_); | |
}); | |
}(deps, input, 0)); | |
function extractFuncs(mod, input) { | |
return Object.keys(mod).map(function(k) { | |
if (mod.hasOwnProperty(k) && typeof mod[k] === 'function') { | |
return mod[k]; | |
} | |
}).filter(function(f) { | |
return !!f; | |
}); | |
} | |
function install(mod, fn) { | |
reset(function() { | |
var installer = spawn('npm', ['install', mod]); | |
installer.on('close', function(code) { | |
if (code == 0) { | |
fn(null, mod); | |
} | |
else { | |
fn(code); | |
} | |
}); | |
}); | |
} | |
function reset(fn) { | |
exec('rm -rf node_modules/', fn); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment