Created
July 7, 2017 05:54
-
-
Save lirantal/370680021e10adb5f1a6e7386a3416fe to your computer and use it in GitHub Desktop.
jscodemod-import-statistics
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
const fs = require('fs'); | |
const crypto = require('crypto'); | |
module.exports = function transformer(file, api, options) { | |
const j = api.jscodeshift; | |
const importSource = options.importSource || ''; | |
const importInstances = []; | |
j(file.source) | |
.find(j.ImportDeclaration) | |
.forEach(path => { | |
if ( | |
path.node.source.type === "Literal" && | |
path.node.source.value.indexOf(importSource) !== -1 | |
) { | |
path.node.specifiers.forEach(specifier => { | |
if (specifier.type === "ImportNamespaceSpecifier") { | |
importInstances.push('*'); | |
} | |
if (specifier.imported && specifier.imported.type === "Identifier") { | |
importInstances.push(specifier.imported.name); | |
} | |
if (specifier.type === "ImportDefaultSpecifier") { | |
importInstances.push(specifier.local.name); | |
} | |
}); | |
} | |
}); | |
const uniqId = crypto.randomBytes(16).toString('hex'); | |
fs.writeFileSync(`./out/out-${uniqId}.json`, JSON.stringify(importInstances)); | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment