-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3ccfb1
commit 07e353b
Showing
100 changed files
with
1,373 additions
and
1,013 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 |
---|---|---|
|
@@ -11,6 +11,7 @@ __pycache__ | |
/betty.egg-info | ||
/build | ||
/dist | ||
/prebuild | ||
*.pyc | ||
*.pyc.* | ||
|
||
|
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 +1 @@ | ||
betty/**/assets/betty.extension.npm._Npm/build/* | ||
prebuild/**/* |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
prune node_modules |
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
""" | ||
Provide tools to integrate extensions with `npm <https://www.npmjs.com/>`_. | ||
This module is internal. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
import logging | ||
import sys | ||
from asyncio import subprocess as aiosubprocess | ||
from pathlib import Path | ||
from typing import Sequence | ||
|
||
from betty.asyncio import wait_to_thread | ||
from betty.error import UserFacingError | ||
from betty.locale import Str, DEFAULT_LOCALIZER | ||
from betty.requirement import Requirement | ||
from betty.subprocess import run_process | ||
|
||
|
||
_NPM_UNAVAILABLE_MESSAGE = Str._( | ||
"npm (https://www.npmjs.com/) must be available for features that require Node.js packages to be installed. Ensure that the `npm` executable is available in your `PATH`." | ||
) | ||
|
||
|
||
class NpmUnavailable(UserFacingError, RuntimeError): | ||
def __init__(self): | ||
super().__init__(_NPM_UNAVAILABLE_MESSAGE) | ||
|
||
|
||
async def npm( | ||
arguments: Sequence[str], | ||
cwd: Path | None = None, | ||
) -> aiosubprocess.Process: | ||
""" | ||
Run an npm command. | ||
""" | ||
try: | ||
return await run_process( | ||
["npm", *arguments], | ||
cwd=cwd, | ||
# Use a shell on Windows so subprocess can find the executables it needs (see | ||
# https://bugs.python.org/issue17023). | ||
shell=sys.platform.startswith("win32"), | ||
) | ||
except FileNotFoundError: | ||
raise NpmUnavailable() | ||
|
||
|
||
class NpmRequirement(Requirement): | ||
def __init__(self): | ||
super().__init__() | ||
self._met: bool | ||
self._summary: Str | ||
self._details = _NPM_UNAVAILABLE_MESSAGE | ||
|
||
def _check(self) -> None: | ||
if hasattr(self, "_met"): | ||
return | ||
try: | ||
wait_to_thread(npm(["--version"])) | ||
except NpmUnavailable: | ||
self._met = False | ||
self._summary = Str._("`npm` is not available") | ||
else: | ||
self._met = True | ||
self._summary = Str._("`npm` is available") | ||
finally: | ||
logging.getLogger(__name__).debug(self._summary.localize(DEFAULT_LOCALIZER)) | ||
|
||
def is_met(self) -> bool: | ||
self._check() | ||
return self._met | ||
|
||
def summary(self) -> Str: | ||
self._check() | ||
return self._summary | ||
|
||
def details(self) -> Str: | ||
return self._details |
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
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
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
Oops, something went wrong.