forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a tool by @rix0rrr to help find cycles. Add instructions in CONTRIBUTING
- Loading branch information
Elad Ben-Israel
authored
Jul 17, 2018
1 parent
41b8f3b
commit 3ed832d
Showing
3 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env python | ||
import json | ||
import sys | ||
import collections | ||
import pprint | ||
|
||
def find(xs, x): | ||
for i, value in enumerate(xs): | ||
if x == value: | ||
return i | ||
return None | ||
|
||
filenames = sys.argv[1:] | ||
|
||
graph = collections.defaultdict(set) | ||
for filename in filenames: | ||
with file(filename) as f: | ||
package_json = json.load(f) | ||
|
||
for key in ['devDependencies', 'dependencies']: | ||
if key in package_json: | ||
graph[package_json['name']].update(package_json[key].keys()) | ||
|
||
|
||
checked = set() | ||
|
||
# Do a check for cycles for each package. This is slow but it works, | ||
# and it has the advantage that it can give good diagnostics. | ||
def check_for_cycles(package, path): | ||
i = find(path, package) | ||
if i is not None: | ||
cycle = path[i:] + [package] | ||
print 'Cycle: %s' % ' => '.join(cycle) | ||
return | ||
|
||
if package in checked: | ||
return | ||
|
||
checked.add(package) | ||
|
||
deps = graph.get(package, []) | ||
for dep in deps: | ||
check_for_cycles(dep, path + [package]) | ||
|
||
for package in graph.keys(): | ||
check_for_cycles(package, []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
scriptdir="$(cd $(dirname $0) && pwd)" | ||
python ${scriptdir}/find-cycles.py $(find . -name package.json | grep -v node_modules) |