Skip to content

Instantly share code, notes, and snippets.

@jbclements
Created April 11, 2012 18:05
Show Gist options
  • Save jbclements/2361016 to your computer and use it in GitHub Desktop.
Save jbclements/2361016 to your computer and use it in GitHub Desktop.
Creel
/* A little error checking for input file */
if (process.argv.length < 3) {
throw new Error('Usage: node vars.js <example.js>');
}
/* variables and whatnot */
var parse = require('./parse-js.js');
var fs = require('fs');
var varList = [];
var getVars = function getVars(elm) {
for (var i = 0; i < elm[1].length; i++) {
varList.push(elm[1][i][0]);
}
};
/* Reads JS from the file and prints out a list of variables. */
fs.readFile(process.argv[2], function(err,data) {
if(err) {
console.error("Could not open file: %s", err);
process.exit(1);
}
var tree = parse.parse(data.toString('ascii'));
var mapfn = function mapfn(elm) {
if (Array.isArray(elm)) {
if (elm[0] == 'var') {
getVars(elm);
}
elm.map(mapfn);
}
};
tree.map(mapfn);
console.log(varList);
console.log(varList.length);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment