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
.