From 76c1a5fd0430f5891cdd9b69993d274b348f93e0 Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Tue, 18 Jul 2023 17:46:18 +0200 Subject: [PATCH] =?UTF-8?q?#1:=20katex-server=20si=20nyn=C3=AD=20s=C3=A1m?= =?UTF-8?q?=20vol=C3=A1=20`npm=20install`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Po poradě s kamarády pythonáři se to asi tak má dělat, i když je to trochu eh --- src/formatitko/katex.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/formatitko/katex.py b/src/formatitko/katex.py index fc9ace4..4470e7f 100644 --- a/src/formatitko/katex.py +++ b/src/formatitko/katex.py @@ -16,7 +16,19 @@ class KatexClient: self._temp_dir = tempfile.TemporaryDirectory(prefix='formatitko') self._socket_file = self._temp_dir.name + "/katex-socket" - self._server_process = subprocess.Popen(["node", os.path.dirname(os.path.realpath(__file__)) + "/katex-server/index.mjs", self._socket_file]) + srcdir = os.path.dirname(os.path.realpath(__file__)) + + # Test if `node_modules` directory exists and if not, run `npm install` + if not os.path.isdir(srcdir + "/katex-server/node_modules"): + print("Installing node dependencies for the first time...") + try: + subprocess.run(["npm", "install"], cwd=srcdir+"/katex-server", check=True) + except subprocess.CalledProcessError as e: + if e.returncode == 127: + raise Exception("npm not found. Node.js is required to use KaTeX.") + else: + raise e + self._server_process = subprocess.Popen(["node", srcdir + "/katex-server/index.mjs", self._socket_file]) self._client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)