Guest User

Untitled

a guest
Oct 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. // @flow
  2. const fs = require('fs')
  3. const path = require('path')
  4. const { transform } = require('babel-core')
  5.  
  6. const walker = (inputPath, extension, list = []) => {
  7. if (fs.statSync(inputPath).isFile()) {
  8. list.push(inputPath)
  9. return
  10. }
  11.  
  12. fs.readdirSync(inputPath).forEach(target => {
  13. const src = path.join(inputPath, target)
  14. const stat = fs.statSync(src)
  15.  
  16. if (stat.isDirectory()) {
  17. walker(src, extension, list)
  18. } else {
  19. if (src.endsWith(extension)) {
  20. list.push(src)
  21. }
  22. }
  23. })
  24. }
  25.  
  26. const getAllImports = filepath => {
  27. const imports = []
  28. const content = fs.readFileSync(filepath)
  29.  
  30. transform(content, {
  31. plugins: [
  32. //'transform-runtime',
  33. //'transform-react-jsx',
  34. //'syntax-class-properties',
  35. 'transform-decorators-legacy',
  36. {
  37. visitor: {
  38. ImportDeclaration(path) {
  39. imports.push(path.node.source.value)
  40. }
  41. }
  42. }
  43. ],
  44. presets: ['react', 'es2015', 'stage-0']
  45. })
  46.  
  47. return imports
  48. }
  49.  
  50. const argv = process.argv
  51.  
  52. if (argv.length !== 3) {
  53. console.log(`
  54. Usage:
  55.  
  56. node dep.js <path>
  57.  
  58. for example:
  59. node dep.js ./src/examples
  60.  
  61. shows every file dependency using import
  62. `)
  63.  
  64. process.exit(0)
  65. }
  66.  
  67. const list = []
  68. walker(argv[2], '.js', list)
  69.  
  70. list.forEach(path => {
  71. console.log(path)
  72. getAllImports(path).forEach(path => {
  73. console.log(` ${path}`)
  74. })
  75. })
Add Comment
Please, Sign In to add comment