Skip to content

Instantly share code, notes, and snippets.

@semeltheone
Created March 6, 2018 07:08
Show Gist options
  • Save semeltheone/16f0765793d56ab93115ee7c524e3322 to your computer and use it in GitHub Desktop.
Save semeltheone/16f0765793d56ab93115ee7c524e3322 to your computer and use it in GitHub Desktop.
Walks all js files in the source directory and finds strings using acorn.
var acorn = require('acorn-jsx');
let fs = require('fs');
function getAllStrings(fileName) {
let file = fs.readFileSync(fileName);
const options = {
sourceType: 'module',
ecmaVersion: 9,
plugins: {
jsx: { allowNamespacedObjects: true },
},
};
let tokens = [...acorn.tokenizer(file, options)];
return tokens.filter(t => t.type.label === 'string').map(t => t.value);
}
module.exports = { getAllStrings };
var walk = require('walk');
let { getAllStrings } = require('./acornTest.js');
var files = [];
var walker = walk.walk('./src', { followLinks: false });
walker.on('file', function(root, stat, next) {
files.push(root + '/' + stat.name);
next();
});
let strings = [];
walker.on('end', function() {
let jsFiles = files.filter(f => f.endsWith('.js'));
for (let index = 0; index < jsFiles.length; index++) {
try {
const file = jsFiles[index];
strings = strings.concat(getAllStrings(file));
} catch (error) {
console.log(error);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment