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

quick-import-cycle-checker

v1.0.3

Published

Quick import cycle checker for ES6 imports in TypeScript projects

Downloads

10

Readme

quick-import-cycle-checker

The aim of this project is to provide a very fast way to check for cyclic imports in a typescript-project. There are already plenty of similar projects out there, however, most are relatively slow because they create an abstract syntax tree (AST) to validate the imports, which is rather expensive.

quick-import-cycle-checker has no dependencies, and will remain so.

Assumptions

In order to be fast and to be able to skip the creation of an AST, quick-import-cycle-checker makes a few assumptions:

  • There's at most one import-statement per line of code
  • You are using only relative imports (or imports from packages, which are ignored)

Currently, quick-import-cycle-checker does not support the following:

  • Typescript path-mapping; will be ignored
  • Imports relative to the baseUrl as described here. The imports must be relative, using ./ and ../, otherwise it will not work.
  • In short: Every import that does not start with a . is currently being ignored.
  • Dynamic imports are currently ignored.

Usage

Installation

Install via npm: npm install --save-dev quick-import-cycle-checker

Via CLI

The quick-import-cycle-checker exposes a bin-entry that e.g. can be used as follows in your package.json:

{
    "scripts": {
        "quick-import-cycle-checker": "quick-import-cycle-checker"
    }
}

npm run quick-import-cycle-checker will discover all *.ts-files in the current directory and all subdirectories, and then check if there are cycles. If there are any, it will exit non-zero and print the files that are part of the cycle. Otherwise, it will not print anything and exit with zero.

If you wish to overwrite the default settings you can pass the following parameters:

--checkDirs # list of relative or absolute paths
--root # absolute path
--exclusions # list of RegExp
# example
npm run quick-import-cycle-checker -- --checkDirs '/first/Path' '/Second/Path'

Programmatically

It can be used programmatically e.g. in node as follows:

const path = require('path');
const QuickImportCycleChecker = require('quick-import-cycle-checker').QuickImportCycleChecker;

QuickImportCycleChecker.forDirectories(
    // paths need to be absolute
    path.join(__dirname, './some/folder'),
    path.join(__dirname, './some/other/folder')
)
    .withExclusions([/[\\/]dist[\\/]/, /[\\/]generated[\\/]/]) // node_modules are ignored by default
    .withRootDirectory(__dirname)
    .createImportGraph()
    .searchForImportCycles()
    .reportCyclesAndExit(); // Note that reportCyclesAndExit returns a Promise<void>.

Alternatively, instead of reportCyclesAndExit, handle the promise yourself by using getCycleValidationResult.