-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add initial support for npm (#29)
- Loading branch information
Showing
4 changed files
with
58 additions
and
6 deletions.
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 |
---|---|---|
@@ -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) |
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