Last active
December 18, 2015 16:39
-
-
Save kpuputti/5813344 to your computer and use it in GitHub Desktop.
Print all string tokens in the given js file in format:
path/to/file.js:line:col: 'value'
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 esprima = require('esprima'); | |
var fs = require('fs'); | |
function die(err) { | |
process.stderr.write(err + '\n'); | |
process.exit(1); | |
} | |
function format(filename, obj) { | |
return filename + ':' + | |
obj.loc.start.line + ':' + obj.loc.start.column + ': ' + | |
obj.value; | |
} | |
function getStrings(contents) { | |
return esprima.parse(contents, { | |
loc: true, | |
tokens: true | |
}).tokens.filter(function (token) { | |
return token.type === 'String'; | |
}); | |
} | |
function main(filename) { | |
fs.readFile(filename, function (err, data) { | |
if (err) die(err); | |
var strings = getStrings(data); | |
strings.forEach(function (s) { | |
console.log(format(filename, s)); | |
}); | |
}); | |
} | |
if (process.argv.length > 2) { | |
process.argv.slice(2).forEach(main); | |
} else { | |
process.stderr.write('Usage: node print-strings.js [files]\n'); | |
process.exit(2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment