diff --git a/sphinx_js/analyzer_utils.py b/sphinx_js/analyzer_utils.py index b6c302cd..0143ccee 100644 --- a/sphinx_js/analyzer_utils.py +++ b/sphinx_js/analyzer_utils.py @@ -36,7 +36,7 @@ def search_node_modules(cmdname: str, cmdpath: str, dir: str | Path) -> str: # perhaps it's globally installed result = shutil.which(cmdname) if result: - return result + return str(Path(result).resolve()) raise SphinxError( f'{cmdname} was not found. Install it using "npm install {cmdname}".' diff --git a/sphinx_js/call_typedoc.mjs b/sphinx_js/call_typedoc.mjs index 711a0ce7..cf9fdd0c 100644 --- a/sphinx_js/call_typedoc.mjs +++ b/sphinx_js/call_typedoc.mjs @@ -1,5 +1,15 @@ import { writeFile } from "fs/promises"; +const ExitCodes = { + Ok: 0, + OptionError: 1, + CompileError: 3, + ValidationError: 4, + OutputError: 5, + ExceptionThrown: 6, + Watching: 7, +}; + let td; // Locate the kind IDs, look up the corresponding kindStrings, and add them to @@ -36,6 +46,10 @@ async function main() { // Most of this stuff is copied from typedoc/src/lib/cli.ts const start = Date.now(); let app = await bootstrapAppTypedoc0_25(); + if (app.options.getValue("version")) { + console.log(app.toString()); + return ExitCodes.Ok; + } const project = await app.convert(); const preValidationWarnCount = app.logger.warningCount; diff --git a/sphinx_js/typedoc.py b/sphinx_js/typedoc.py index 0f749fe0..1477cc94 100644 --- a/sphinx_js/typedoc.py +++ b/sphinx_js/typedoc.py @@ -30,7 +30,10 @@ @cache def typedoc_version_info(typedoc: str) -> tuple[tuple[int, ...], tuple[int, ...]]: result = subprocess.run( - [typedoc, "--version"], capture_output=True, encoding="utf8" + [typedoc, "--version"], + capture_output=True, + encoding="utf8", + check=True, ) lines = result.stdout.strip().splitlines() m = re.search(r"TypeDoc ([0-9]+\.[0-9]+\.[0-9]+)", lines[0]) diff --git a/tests/test_paths.py b/tests/test_paths.py index 32be4c45..40f6fcfc 100644 --- a/tests/test_paths.py +++ b/tests/test_paths.py @@ -1,3 +1,5 @@ +import os +import subprocess from pathlib import Path import pytest @@ -86,3 +88,19 @@ def test_err(): match='my_program was not found. Install it using "npm install my_program"', ): search_node_modules("my_program", my_prog_path, "/a/b/c") + + +def test_global_install(tmp_path_factory): + tmpdir = tmp_path_factory.mktemp("global_root") + tmpdir2 = tmp_path_factory.mktemp("blah") + os.environ["npm_config_prefix"] = str(tmpdir) + subprocess.run(["npm", "i", "-g", "typedoc"]) + typedoc = search_node_modules("typedoc", "typedoc/bin/typedoc", str(tmpdir2)) + os.environ["TYPEDOC_NODE_MODULES"] = str(Path(typedoc).parents[2]) + res = subprocess.run( + ["node", Path(__file__).parents[1] / "sphinx_js/call_typedoc.mjs", "--version"], + check=True, + capture_output=True, + encoding="utf8", + ) + assert "TypeDoc 0.25" in res.stdout