Skip to content

Commit

Permalink
Fix using global install
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Sep 29, 2023
1 parent c4a53dc commit e7c25eb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sphinx_js/analyzer_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}".'
Expand Down
14 changes: 14 additions & 0 deletions sphinx_js/call_typedoc.mjs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion sphinx_js/typedoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
18 changes: 18 additions & 0 deletions tests/test_paths.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import subprocess
from pathlib import Path

import pytest
Expand Down Expand Up @@ -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

0 comments on commit e7c25eb

Please sign in to comment.