npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

scan-imports

v1.1.1

Published

Node.js package designed to extract and analyze import statements from TypeScript and TypeScript JSX files. It provides a simple and efficient way to scan your codebase for import declarations, making it useful for various code analysis and refactoring ta

Downloads

25

Readme

Import Scanner 🕵️‍♀️

The Import Scanner is a Node.js package designed to extract and analyze import statements from TypeScript and TypeScript JSX files. It provides a simple and efficient way to scan your codebase for import declarations, making it useful for various code analysis and refactoring tasks.

Installation

scan-imports does not require you to install locally to your node modules. Just run

npx scan-imports@latest scan -d <directory> -i <importModule> -ext <fileExtension>

Usage

| Argument | Description | Default Value | Required | | ----------------------- | ----------------------------------------------------------------------------------------------------------------------- | ------------- | -------- | | -d or --directory | The directory to scan for import statements. | - | Yes | | -i or --import | The import module to search for. | - | Yes | | -ext or --extension | The file extension to scan for. Separate by commas for multiple extensions | .ts | No | | -det or --details | Whether to show details of the import statements. | false | No | | -a or --alpha | By default results are sorted by count. But if you want it to sort alphabetically, add a --alpha flag statements. | false | No | | -f or --format | The format of the output. Currently only supports json and csv. Using this flag will automatically export to a file | - | No |

Example

If I run this command in the root directory of this project:

npx scan-imports@latest scan -d src -i fs -ext .tsx,.ts

I will get the following output:

Found 2 files with "fs" imports across directory /Users/aminroslan/Projects/scan-imports/src:
{
  "default": {
    "fs": 2
  },
  "named": {}
}

I can see that there are 2 files that import fs from the src directory.

If I need more information about each import statement, I can add the -det or --details flag to the command:

scan -d src -i fs -ext .tsx,.ts -det

This will give me the following output:

Found 2 files with "fs" imports across directory /Users/aminroslan/Projects/scan-imports/src:
{
  "default": {
    "fs": 2
  },
  "named": {}
}
[
  {
    "path": "/Users/aminroslan/Projects/scan-imports/src/utils/file-contains-import.ts",
    "statement": "import fs from 'fs'",
    "hasDefault": true,
    "hasNamed": false,
    "defaultImport": "fs",
    "namedImports": []
  },
  {
    "path": "/Users/aminroslan/Projects/scan-imports/src/utils/scan-directories.ts",
    "statement": "import fs from 'fs'",
    "hasDefault": true,
    "hasNamed": false,
    "defaultImport": "fs",
    "namedImports": []
  }
]

Now, I can see that both files import the default export from fs.

Exporting to a file

If -f or --format is specified, the output will be exported to a file. The output will look like this:

CSV

name, usage
fs, 4
appendFileSync, 1

JSON

{
	"fs": 4,
	"appendFileSync": 1
}

You can set -det or --details to true to get more details about the import statements, but the output will only be in JSON format.

Development

To execute the script locally, run:

# Install dependencies
yarn

# Run dev script to compile TypeScript and watch for changes
yarn dev

# Execute the script
node dist/index.js scan -d src -i fs -ext .tsx,.ts

Contributing

If you have any ideas for improvements or find any bugs, please feel free to open an issue or submit a pull request. 😀

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Todo

TBD.