[Feature] Ability to only detect & list cycles involving specific files
Closed this issue · 3 comments
Is your feature request related to a problem? Please describe.
It would be nice if we were able to run this tool together with lint-staged
to report dependency cycles for staged/modified files, without having to manually skip through cycles in the rest of the project.
Describe the solution you'd like
A new CLI flag (--only
maybe?) that tells dpdm to only return cycles that directly reference the file paths passed as arguments.
Describe alternatives you've considered
Running dpdm and then grepping kinda works when only passing a single file, but doesn't work as well for multiple files.
npx dpdm -T --skip-dynamic-imports circular --no-tree --no-warning $files | grep $files
Additional context
None
Checking for circular dependencies with lint-staged
is not recommended as it is very time-consuming. The recommended approach is to add it to the automated pipeline for checking, just like unit tests. But if you must do it, just use it like this:
{
"lint-staged": {
"*.{ts,tsx,js,jsx}": "npx dpdm --exit-code circular:1 --no-progress --no-tree --no-warning"
}
}
lint-staged
will auto put all the updated files to dpdm
.
I just tried it, and there seems to be a bug that cannot resolve absolute paths.
A temporary way is to add the following code directly to your husky pre-commit
:
# Your other lints
# npx lint-staged
# check circular dependencies
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep "[jt]sx\?$" || true)
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
npx dpdm --exit-code circular:1 --no-progress --no-tree --no-warning "${STAGED_FILES[@]}"