Created
April 20, 2018 09:41
-
-
Save sandfox/1ac783e02835ea154ae4221fc2381529 to your computer and use it in GitHub Desktop.
finding some lodash imports
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
/** | |
* node import-assign.js path/to/folder/to/scan | |
*/ | |
const fs = require('fs') | |
const path = require('path') | |
const { loadFileSync } = require('babel-file-loader'); | |
const readdir = require('recursive-readdir'); | |
const { sortBy, sortedUniq, flowRight, identity } = require('lodash/fp') | |
const ignoreFunc = (filepath, stats) => { | |
switch (true) { | |
case stats.isFile() && path.extname(filepath) !== '.js': | |
case filepath.includes('/__tests__/'): | |
return true | |
} | |
return false | |
} | |
const main = async () => { | |
// target dir to start scanning from | |
const target = process.argv[2] | |
console.log(`scanning ${target} recursively for ".js" files`) | |
const files = await readdir(target, [ignoreFunc]) | |
console.log(`${files.length} files found`) | |
const imports = [] | |
console.log('parsing files for imports') | |
files.forEach( f => { | |
for (let item of loadFileSync(f).path.get('body')) { | |
if (item.isImportDeclaration() || (item.isExportDeclaration() && item.node.source)) { | |
if(item.node.source.value.startsWith('lodash/fp')) { | |
item.node.specifiers.forEach( specifier => { | |
if (specifier.type === 'ImportSpecifier') { | |
imports.push(specifier.imported.name) | |
} | |
} ) | |
} | |
} | |
} | |
}) | |
console.log(`found ${imports.length} total imports`) | |
const uniqueImports = flowRight(sortedUniq, sortBy(identity))(imports) | |
console.log(`found ${uniqueImports.length} unique imports`) | |
console.log(uniqueImports) | |
} | |
main(process.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment