Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // @flow
- const fs = require('fs')
- const path = require('path')
- const { transform } = require('babel-core')
- const walker = (inputPath, extension, list = []) => {
- if (fs.statSync(inputPath).isFile()) {
- list.push(inputPath)
- return
- }
- fs.readdirSync(inputPath).forEach(target => {
- const src = path.join(inputPath, target)
- const stat = fs.statSync(src)
- if (stat.isDirectory()) {
- walker(src, extension, list)
- } else {
- if (src.endsWith(extension)) {
- list.push(src)
- }
- }
- })
- }
- const getAllImports = filepath => {
- const imports = []
- const content = fs.readFileSync(filepath)
- transform(content, {
- plugins: [
- //'transform-runtime',
- //'transform-react-jsx',
- //'syntax-class-properties',
- 'transform-decorators-legacy',
- {
- visitor: {
- ImportDeclaration(path) {
- imports.push(path.node.source.value)
- }
- }
- }
- ],
- presets: ['react', 'es2015', 'stage-0']
- })
- return imports
- }
- const argv = process.argv
- if (argv.length !== 3) {
- console.log(`
- Usage:
- node dep.js <path>
- for example:
- node dep.js ./src/examples
- shows every file dependency using import
- `)
- process.exit(0)
- }
- const list = []
- walker(argv[2], '.js', list)
- list.forEach(path => {
- console.log(path)
- getAllImports(path).forEach(path => {
- console.log(` ${path}`)
- })
- })
Add Comment
Please, Sign In to add comment