Created
March 6, 2018 07:08
-
-
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.
This file contains 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 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 }; |
This file contains 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 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