Created
July 19, 2018 11:49
-
-
Save kwoktung/53969b64b0fbcf4bb87255d0d140ece4 to your computer and use it in GitHub Desktop.
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
const babel = require('babel-core'); | |
const fs = require('fs'); | |
const glob = require("glob") | |
const traverse = require("babel-traverse").default; | |
const i18n = {} | |
function get(properties) { | |
if(Array.isArray(properties)) { | |
let key, value | |
properties.forEach(property => { | |
if (property.key.name === 'id') { | |
key = property.value.value | |
} else if (property.key.name === 'defaultMessage') { | |
value = property.value.value | |
} | |
}) | |
return { [key]: value } | |
} | |
} | |
glob("./src/**/*.js?(x)", {}, function (er, files) { | |
files.forEach((file) => { | |
const { ast } = babel.transformFileSync(file) | |
traverse(ast, { | |
CallExpression(path) { | |
if (path.node.callee.type == 'MemberExpression' && path.node.callee.object.name === 'intl' && path.node.callee.property.name === 'formatMessage') { | |
Object.assign(i18n, get(path.node.arguments[0].properties)) | |
} else if ( | |
path.node.callee.type == 'MemberExpression' && | |
path.node.callee.property.name === 'createElement' | |
) { | |
const firstArgs = path.node.arguments[0] | |
if (firstArgs.type === 'MemberExpression' && firstArgs.object.name === '_reactIntl' && firstArgs.property.name === 'FormattedMessage') { | |
Object.assign(i18n, get(path.node.arguments[1].properties)) | |
} | |
} | |
} | |
}) | |
}) | |
console.log(i18n) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment