Skip to content

Commit

Permalink
feat: Add initial support for npm (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Apr 6, 2021
1 parent b5385a7 commit 3dc227c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/mk/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
50 changes: 49 additions & 1 deletion src/mk/tools/npm.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion src/mk/tools/pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/mk/tools/tox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 3dc227c

Please sign in to comment.