Skip to content

Commit 45c8f7d

Browse files
authored
Add pretext init --system to reinstall pretext resources for current version (#1001)
* use shutil.which to find npm in mjsr npm install * add You are using the latest version of pretext, 2.19.4. PreTeXt-CLI version: 2.19.4 �[33mwarning: �[0mNo project.ptx manifest found in current workspace. Using global configuration specified in '~/.ptx/2.19.4/project.ptx'. Reinstalling system resources... Deleting existing resources at /home/oscar/.ptx/2.19.4 Installing core resources Installing templates Installing rs_cache files Installing pelican files Installing npm packages added 51 packages, and audited 52 packages in 1s 11 packages are looking for funding run `npm fund` for details found 0 vulnerabilities System resources reinstalled successfully. to reinstall pretext resources for current version * update changelog * try to fix types
1 parent 24e67a6 commit 45c8f7d

15 files changed

Lines changed: 65 additions & 13 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Instructions: Add a subsection under `[Unreleased]` for additions, fixes, change
99

1010
## [Unreleased]
1111

12+
### Added
13+
14+
- Sometimes something goes wrong with the pretext resources in `~/.ptx`. You can now run `pretext init --system` to reinstall these resources for the current version. This will also try to run `npm install` for the css themes.
15+
1216
## [2.19.3] - 2025-06-14
1317

1418
Includes updates to core through commit: [1b65b63](https://github.com/PreTeXtBook/pretext/commit/1b65b63c3a83292c22b46cf1e12795c4dd519cc0)

pretext/cli.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,14 @@ def new(template: str, directory: Path, url_template: str) -> None:
375375
multiple=True,
376376
type=click.Choice([r for r in constants.PROJECT_RESOURCES], case_sensitive=False),
377377
)
378+
@click.option(
379+
"-s",
380+
"--system",
381+
is_flag=True,
382+
help="Reinstall's the system's pretext resources, including the core pretext script, templates, and other resources (including npm installs). Useful if something is broken or after initial installation or upgrade.",
383+
)
378384
@nice_errors
379-
def init(refresh: bool, files: List[str]) -> None:
385+
def init(refresh: bool, files: List[str], system: bool) -> None:
380386
"""
381387
Generates/updates CLI-specific files for the current version of PreTeXt-CLI.
382388
This feature is mainly intended for updating existing PreTeXt projects to use this CLI,
@@ -388,6 +394,11 @@ def init(refresh: bool, files: List[str]) -> None:
388394
389395
Note: `pretext init -r` is does the same thing as `pretext update -f`.
390396
"""
397+
if system:
398+
log.info("Reinstalling system resources...")
399+
resources.install(reinstall=True, npm_install=True)
400+
log.info("System resources reinstalled successfully.")
401+
return
391402
project_path = utils.project_path()
392403
if project_path is None:
393404
project = Project()

pretext/resources/__init__.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import importlib.resources
22
import json
33
import logging
4+
import os
45
from pathlib import Path
56
import shutil
7+
import subprocess
68
import zipfile
79

810
from .. import VERSION, CORE_COMMIT
@@ -12,7 +14,7 @@
1214
_RESOURCE_BASE_PATH = Path.home() / ".ptx" / VERSION
1315

1416

15-
def install(reinstall: bool = False) -> None:
17+
def install(reinstall: bool = False, npm_install: bool = False) -> None:
1618
if _RESOURCE_BASE_PATH.exists():
1719
if reinstall:
1820
log.info(f"Deleting existing resources at {_RESOURCE_BASE_PATH}")
@@ -56,6 +58,23 @@ def install(reinstall: bool = False) -> None:
5658
with zipfile.ZipFile(static_zip, "r") as zip:
5759
zip.extractall(path=_RESOURCE_BASE_PATH / "pelican")
5860

61+
if npm_install:
62+
log.info("Installing npm packages")
63+
# Look for node_modules and install if we can. This is a simplified version of the more robust utils.css_node_modules_install, which we cannot use here due to circular imports.
64+
os.chdir(_RESOURCE_BASE_PATH / "core" / "script" / "cssbuilder")
65+
try:
66+
npm_cmd = shutil.which("npm")
67+
if npm_cmd is None:
68+
log.warning(
69+
"Cannot find npm. Will try to use prebuilt CSS files instead."
70+
)
71+
raise FileNotFoundError
72+
subprocess.run([npm_cmd, "install", "--engine-strict=true"])
73+
except Exception as e:
74+
log.warning(f"Unable to install npm packages for theme building: {e}")
75+
log.debug(e, exc_info=True)
76+
return
77+
5978

6079
def resource_base_path() -> Path:
6180
if not _RESOURCE_BASE_PATH.exists():

pretext/resources/resource_hash_table.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,5 +272,17 @@
272272
"installPandoc.sh": "023b47421a5c9e787649c75b1257d417b5e359c6a176e14621a7831d6f95d2ae",
273273
"installLatex.sh": "e2ff5b661ed273e82ea58b290978564015eebff3d632d3013321f805548d2887",
274274
"installSage.sh": "d167efbf07ea099aba56864e21eeb7c13462203d0a38cafbbef15e34379732de"
275+
},
276+
"2.19.4": {
277+
"project.ptx": "2dc2900025b5cd39b0f7c8890c1debc2cf2102875bed5b3634aa0e9b151b1711",
278+
"codechat_config.yaml": "89dc598ea7db1c91ec8882e2737aff9d683d7123187c288e16f1617a39f9d9e3",
279+
".gitignore": "721a3a2c412782db41cb72441eaef2b9bffdb6d927fde87f67ada1f44a78af90",
280+
"devcontainer.json": "5695d90f4194164b092038133b6675544c81d5ea2cf4c5eeec8075e26890998d",
281+
"pretext-cli.yml": "e2d6d3a23cd2a955c998ec5cb4005d4cd67cc3d3a2cafc6af618bf5ce0b64009",
282+
"pretext-deploy.yml": "4aa427a79fac24030f5a21f49a0a75c333f62f2c0bd44d2d403126eaca0e8ea3",
283+
"installPretext.sh": "99676bf6224335cd1dfd8efebd0835ce255366bf3aa10d04388d5fda8134a44e",
284+
"installPandoc.sh": "67f9a2418ca80b79fa796d31c77b6f230dba4a496021c6fcf39b67ddba35f013",
285+
"installLatex.sh": "51905c51e0b3c95121caab413de37b670aedf712f432bfcdd38c1fe8f312dbc6",
286+
"installSage.sh": "1c4b41cba70577b78552884755674fcc91cb2a039aafa63915fc6e7b7165345f"
275287
}
276288
}

pretext/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,13 @@ def mjsre_npm_install() -> None:
497497
):
498498
log.info("Attempting to install/update required node packages.")
499499
try:
500-
subprocess.run("npm install", shell=True)
500+
npm_cmd = shutil.which("npm")
501+
if npm_cmd is None:
502+
log.warning(
503+
"Cannot find npm. Will try to use prebuilt CSS files instead."
504+
)
505+
raise FileNotFoundError
506+
subprocess.run([npm_cmd, "install", "--engine-strict=true"])
501507
except Exception as e:
502508
log.critical(
503509
"Unable to install required npm packages. Please see the documentation."

templates/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# This file was automatically generated with PreTeXt 2.18.4.
1+
# This file was automatically generated with PreTeXt 2.19.4.
22
# If you modify this file, PreTeXt will no longer automatically update it.
33
#
44
# Boilerplate list of files in a PreTeXt project for git to ignore

templates/codechat_config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# This file was automatically generated with PreTeXt 2.18.4.
1+
# This file was automatically generated with PreTeXt 2.19.4.
22
# If you modify this file, PreTeXt will no longer automatically update it.
33
#
44
#############################################################

templates/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file was automatically generated with PreTeXt 2.18.4.
1+
// This file was automatically generated with PreTeXt 2.19.4.
22
// If you modify this file, PreTeXt will no longer automatically update it.
33
//
44
//////////////////////////////////////////////////////////////

templates/installLatex.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
# This file was automatically generated with PreTeXt 2.18.4.
3+
# This file was automatically generated with PreTeXt 2.19.4.
44
# If you modify this file, PreTeXt will no longer automatically update it.
55

66
# We use TinyTeX (https://yihui.org/tinytex/)

templates/installPandoc.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
# This file was automatically generated with PreTeXt 2.18.4.
3+
# This file was automatically generated with PreTeXt 2.19.4.
44
# If you modify this file, PreTeXt will no longer automatically update it.
55

66
wget https://github.com/jgm/pandoc/releases/download/3.6.4/pandoc-3.6.4-1-amd64.deb -O pandoc.deb

0 commit comments

Comments
 (0)