Skip to content

Instantly share code, notes, and snippets.

@sebastien
Created July 5, 2016 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastien/0ccb281ee8bc699266a7f2d0abf1af1b to your computer and use it in GitHub Desktop.
Save sebastien/0ccb281ee8bc699266a7f2d0abf1af1b to your computer and use it in GitHub Desktop.
#!/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