Created
July 5, 2016 18:14
-
-
Save sebastien/0ccb281ee8bc699266a7f2d0abf1af1b to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
import sys, re, json | |
__doc__ = """ | |
A basic tool that lists modules imported by require.js | |
""" | |
RE_REQUIRE_JS = re.compile("require\s*\(\s*\[(.+)\]", re.MULTILINE) | |
def list_imports_requirejs( text ): | |
"""Looks for modules imported in the given files.""" | |
matches = RE_REQUIRE_JS.findall(text) | |
return set(json.loads("[" + (",".join(matches)) + "]")) | |
def command(args, output=sys.stdout): | |
"""Writes the list of modules imported in the given files, one per line.""" | |
r = set () | |
for p in args: | |
r = r.union(list_imports_requirejs(open(p).read() if p != "--" else sys.stdin.read())) | |
for m in r: | |
output.write(m) | |
output.write("\n") | |
if __name__ == "__main__": | |
command(sys.argv[1:]) | |
# EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment