Skip to content

Instantly share code, notes, and snippets.

@kpuputti
Last active December 18, 2015 16:39
Show Gist options
  • Save kpuputti/5813344 to your computer and use it in GitHub Desktop.
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'
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