Skip to content

Instantly share code, notes, and snippets.

@lirantal
Created July 7, 2017 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lirantal/370680021e10adb5f1a6e7386a3416fe to your computer and use it in GitHub Desktop.
Save lirantal/370680021e10adb5f1a6e7386a3416fe to your computer and use it in GitHub Desktop.
jscodemod-import-statistics
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