diff --git a/src/mk/tools/__init__.py b/src/mk/tools/__init__.py index b5fc364..b809f34 100644 --- a/src/mk/tools/__init__.py +++ b/src/mk/tools/__init__.py @@ -2,23 +2,27 @@ class Action: + + # pylint: disable=too-many-arguments def __init__( self, name: str, tool: "Tool", description: Optional[str] = None, + cwd: Optional[str] = None, args: Optional[List[Any]] = [], ) -> None: self.name = name self.description: str = (description or "...") + f" (from {tool})" self.tool = tool + self.cwd = cwd self.args = args # Python does not allow writing __doc__ and this is what click uses # for storing command names. # self.run.__doc__ = "bleah!" def run(self) -> None: - self.tool.run(self.name) + self.tool.run(self) def __str__(self) -> str: return self.name @@ -58,7 +62,7 @@ def is_present(self, path: str) -> bool: def actions(self) -> List[Action]: return [] - def run(self, action: Optional[str] = None): + def run(self, action: Optional[Action] = None): pass def __repr__(self): diff --git a/src/mk/tools/npm.py b/src/mk/tools/npm.py index 1cb01a9..f4370ef 100644 --- a/src/mk/tools/npm.py +++ b/src/mk/tools/npm.py @@ -1,5 +1,53 @@ -from mk.tools import Tool +import json +import subprocess +from typing import List + +from mk.tools import Action, Tool class NpmTool(Tool): name = "npm" + + def __init__(self, path=".") -> None: + super().__init__(path=path) + cmd = ("git", "ls-files", "**/package.json") + result = subprocess.run( + cmd, + stdout=subprocess.PIPE, + universal_newlines=True, + check=False, + ) + if result.returncode != 0 or not result.stdout: + self.present = False + return + self.present = True + self._actions: List[Action] = [] + for line in result.stdout.split(): + cwd = line.split("/")[0] + with open(line, "r") as package_json: + x = json.load(package_json)["scripts"] + for k in x.keys(): + self._actions.append( + Action( + name=k, + tool=self, + # description=cp[section]["description"], + args=[k], + cwd=cwd, + ) + ) + + def is_present(self, path: str) -> bool: + return self.present + + def actions(self) -> List[Action]: + return self._actions + + def run(self, action: Action = None) -> None: + if not action: + cmd = ["npm", "run"] + cwd = None + else: + cmd = ["npm", "run", action.name] + cwd = action.cwd + subprocess.run(cmd, check=True, cwd=cwd) diff --git a/src/mk/tools/pre_commit.py b/src/mk/tools/pre_commit.py index 2d425c5..bf62043 100644 --- a/src/mk/tools/pre_commit.py +++ b/src/mk/tools/pre_commit.py @@ -8,7 +8,7 @@ class PreCommitTool(Tool): name = "pre-commit" - def run(self, action: Optional[str] = None): + def run(self, action: Optional[Action] = None): subprocess.run(["pre-commit", "run", "-a"], check=True) def is_present(self, path: str) -> bool: diff --git a/src/mk/tools/tox.py b/src/mk/tools/tox.py index b484bc4..56632a4 100644 --- a/src/mk/tools/tox.py +++ b/src/mk/tools/tox.py @@ -36,9 +36,9 @@ def actions(self) -> List[Action]: return actions - def run(self, action: Optional[str] = None) -> None: + def run(self, action: Optional[Action] = None) -> None: if not action: cmd = ["tox"] else: - cmd = ["tox", "-e", action] + cmd = ["tox", "-e", action.name] subprocess.run(cmd, check=True)