Coder Social home page Coder Social logo

parse-import's Introduction

parse-import

Build & Test Tag & Release

A tool to help parse imports and all the dependencies associated with a file.

Usage

Basic usage

$ go run main.go -f /path/to/file.ts
2019/12/25 16:26:45 Parsing imports for: [/path/to/file.ts]
2019/12/25 16:26:45 Imports detected: 537
2019/12/25 16:26:45 Writing output to: ./imports.json
2019/12/25 16:26:45 Done

Features

Supports multiple languages

Currently, parse-import supports ts and python and is open to contributions for other languages. :)

Recursively parses through the local imports

Use entrypoint flag to recursively parse local imports in Python

go run main.go -f /path/to/main.py -l py -entryPoint /path/to/main.py
2020/01/25 17:11:21 Parsing using entry point file: /path/to/main.py
2020/01/25 17:11:21 Parsing imports for: [/path/to/main.py]
2020/01/25 17:11:21 Imports detected: 5
2020/01/25 17:11:21 Writing output to: ./imports.json
2020/01/25 17:11:21 Done
Here is how the `./imports.json` looks
{
  "/path/to/baseDir/main.py": {
    "IsLocal": true,
    "IsEntrypoint": true,
    "Path": "/path/to/baseDir/main.py",
    "Info": {
      "Path": "/path/to/baseDir/main.py",
      "IsDir": false,
      "Imports": [
        "/path/to/baseDir/ablah/abc.py",
        "/path/to/baseDir/utils/blahhah/something/",
        "/path/to/baseDir/utils/blahhah/uss.py",
        "/path/to/baseDir/utils/utils.py",
      ]
      "Importers": null
    }
  },
  "/path/to/baseDir/ablah/abc.py": {
    "IsLocal": true,
    "IsEntrypoint": false,
    "Path": "/path/to/baseDir/ablah/abc.py",
    "Info": {
      "Path": "/path/to/baseDir/ablah/abc.py",
      "IsDir": false,
      "Imports": ["/path/to/baseDir/utils/utils.py"],
      "Importers": [
        {
          "Name": "some2",
          "Module": "ablah.abc",
          "Path": "/path/to/baseDir/main.py"
        }
      ]
    }
  },
  "/path/to/baseDir/utils/blahhah/something/": {
    "IsLocal": true,
    "IsEntrypoint": false,
    "Path": "/path/to/baseDir/utils/blahhah/something/",
    "Info": {
      "Path": "/path/to/baseDir/utils/blahhah/something/",
      "IsDir": true,
      "Imports": null,
      "Importers": [
        {
          "Name": "ess",
          "Module": "utils.blahhah.something",
          "Path": "/path/to/baseDir/main.py"
        }
      ]
    }
  },
  "/path/to/baseDir/utils/blahhah/uss.py": {
    "IsLocal": true,
    "IsEntrypoint": false,
    "Path": "/path/to/baseDir/utils/blahhah/uss.py",
    "Info": {
      "Path": "/path/to/baseDir/utils/blahhah/uss.py",
      "IsDir": false,
      "Imports": [
        "datetime",
        "/path/to/baseDir/utils/utils.py"
      ],
      "Importers": [
        {
          "Name": "some3",
          "Module": "utils.blahhah.uss",
          "Path": "/path/to/baseDir/main.py"
        }
      ]
    }
  },
  "/path/to/baseDir/utils/utils.py": {
    "IsLocal": true,
    "IsEntrypoint": false,
    "Path": "/path/to/baseDir/utils/utils.py",
    "Info": {
      "Path": "/path/to/baseDir/utils/utils.py",
      "IsDir": false,
      "Imports": []
      "Importers": [
        {
          "Name": "some",
          "Module": "utils.utils",
          "Path": "/path/to/baseDir/ablah/abc.py"
        },
        {
          "Name": "some",
          "Module": "utils.utils",
          "Path": "/path/to/baseDir/utils/blahhah/uss.py"
        },
        {
          "Name": "some",
          "Module": "utils.utils",
          "Path": "/path/to/baseDir/main.py"
        }
      ]
    }
  },
  "datetime": {
    "IsLocal": false,
    "IsEntrypoint": false,
    "Path": "datetime",
    "Info": {
      "Path": "datetime",
      "IsDir": false,
      "Imports": null,
      "Importers": [
        {
          "Name": "datetime",
          "Module": "datetime",
          "Path": "/path/to/baseDir/utils/blahhah/uss.py"
        }
      ]
    }
  }
}

Note: Supports relative imports in typescript/javascript by default.

Supports tsconfig baseUrl property in Typescript

$ go run main.go -tsconfig /path/to/tsconfig.json -f /path/to/src/index.tsx
2020/01/25 17:12:55 Parsing using tsconfig file: /path/to/tsconfig.json
2020/01/25 17:12:55 Parsing imports for: [/path/to/src/index.tsx]
2020/01/25 17:12:55 Imports detected: 1295
2020/01/25 17:12:55 Writing output to: ./imports.json
2020/01/25 17:12:55 Done

Supports custom output file path

$ go run main.go -tsconfig /path/to/tsconfig.json -f /path/to/file.ts -o yoyo.json
2019/12/25 18:35:53 Parsing using tsconfig file: /path/to/tsconfig.json
2019/12/25 16:26:45 Parsing imports for: [/path/to/file.ts]
2019/12/25 16:26:45 Imports detected: 598
2019/12/25 16:26:45 Writing output to: yoyo.json
2019/12/25 16:26:45 Done

You can now install parse-import in your TS/JS project with package.json ๐ŸŽ‰

Simply run the install command to get the latest released version.

# With yarn
yarn add SafalPandey/parse-import

# With npm
npm install SafalPandey/parse-import

Note: You can run yarn add SafalPandey/parse-import#<version-tag> to get a specific version.

Test if installation is successful.

$ yarn run parse-import -h

Usage of /path/to/node_modules/.bin/parse-import:
  -entry-point string
        Path to main.py
  -f string
        File to parse
  -h    Show usage
  -l string
        Language to parse (default "ts")
  -o string
        Output file path (default "./imports.json")
  -tsconfig string
        Path to tsconfig file

Output

Outputs a json file of following format:

  1. Now shows details for each importer if something is imported multiple times.

    "redux-persist": {
        "IsLocal": false,
        "IsEntrypoint": false,
        "Path": "redux-persist",
        "Info": {
            "Path": "redux-persist",
            "IsDir": false,
            "Importers": [
                {
                "Name": "{ Persistor }",
                "Module": "'redux-persist';",
                "Path": "/path/to/src/app/utils/crossTabSync.ts"
                },
                {
                "Name": "{ persistStore, autoRehydrate }",
                "Module": "'redux-persist';",
                "Path": "/path/to/src/store.ts"
                }
            ]
        }
    }
  2. Local imports json are now consistent with non local imports. No more nested maps.

    "/path/to/src/components/home/common/CalendarIcon.tsx": {
        "IsLocal": true,
        "IsEntrypoint": false,
        "Path": "/path/to/src/components/home/common/CalendarIcon.tsx",
        "Info": {
            "Path": "/path/to/src/components/home/common/CalendarIcon.tsx",
            "IsDir": false,
            "Importers": [
                {
                "Name": "CalendarIcon",
                "Module": "'components/home/common/CalendarIcon';",
                "Path": "/path/to/src/components/home/accountability/time-and-attendance/UpdateTodo.tsx"
                },
                {
                "Name": "CalendarIcon",
                "Module": "'../CalendarIcon';",
                "Path": "/path/to/src/components/home/common/fields/CreateTodo.tsx"
                }
            ]
        }
    }

Others

For the visualization of imports data generated by parse-import, see visualize-import.

parse-import's People

Contributors

dependabot[bot] avatar safalpandey avatar

Stargazers

 avatar  avatar

Watchers

 avatar

parse-import's Issues

[Security] Workflow go.yml is using vulnerable action actions/checkout

The workflow go.yml is referencing action actions/checkout using references v1. However this reference is missing the commit a6747255bd19d7a757dbdda8c654a9f84db19839 which may contain fix to the some vulnerability.
The vulnerability fix that is missing by actions version could be related to:
(1) CVE fix
(2) upgrade of vulnerable dependency
(3) fix to secret leak and others.
Please consider to update the reference to the action.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.