Skip to content

Commit 6867394

Browse files
committed
misc fixes
1 parent 91f747b commit 6867394

File tree

11 files changed

+68
-47
lines changed

11 files changed

+68
-47
lines changed

docs/source/_exts/interactive_widget.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def run(self):
3131
enableWidgetButton.setAttribute("class", "enable-widget-button")
3232
3333
enableWidgetButton.addEventListener("click", () => {{
34-
import("/client/core_modules/layout.js").then((layout) => {{
34+
import("/client/src/layout.js").then((layout) => {{
3535
fadeOutAndThen(enableWidgetButton, () => {{
3636
mount.removeChild(enableWidgetButton);
3737
mount.setAttribute("class", "interactive widget-container");

idom/cli/commands.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import os
22
import json
33
from pathlib import Path
4-
from typing import Optional
4+
from typing import Optional, List
55

66
import typer
77

88
import idom
99
from idom.client import manage as manage_client
10-
from idom.client.build_config import find_build_config_item_in_python_file
10+
from idom.client.build_config import find_build_config_entry_in_python_file
1111

1212
from . import settings
1313
from .console import echo
@@ -18,6 +18,21 @@
1818
main.add_typer(show, name="show", short_help="Display information about IDOM")
1919

2020

21+
@main.command()
22+
def install(packages: List[str]):
23+
config = manage_client.build_config()
24+
with config.change_entry("__main__") as entry:
25+
entry_packages = entry.setdefault("js_dependencies", [])
26+
entry_packages.extend(
27+
[
28+
pkg
29+
for pkg in packages
30+
if not config.entry_has_dependency("__main__", pkg)
31+
]
32+
)
33+
manage_client.build()
34+
35+
2136
@main.command()
2237
def build(
2338
entrypoint: Optional[str] = typer.Option(
@@ -49,7 +64,7 @@ def build(
4964
manage_client.build()
5065
return None
5166

52-
config = find_build_config_item_in_python_file("__main__", Path.cwd() / entrypoint)
67+
config = find_build_config_entry_in_python_file("__main__", Path.cwd() / entrypoint)
5368
if config is None:
5469
echo(f"No build config found in {entrypoint!r}")
5570
manage_client.build()

idom/client/app/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<body>
99
<div id="app"></div>
1010
<script type="module">
11-
import { mountLayoutWithWebSocket } from "./core_modules/layout.js";
11+
import { mountLayoutWithWebSocket } from "./src/layout.js";
1212

1313
const uri = document.location.hostname + ":" + document.location.port;
1414
const url = (uri + document.location.pathname).split("/").slice(0, -1);

idom/client/app/package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
"repository": {
88
"url": "https://github.com/rmorshea/idom"
99
},
10-
"main": "core_modules/layout.js",
10+
"main": "src/layout.js",
1111
"files": [
12-
"core_modules/*.js"
12+
"src/*.js"
1313
],
1414
"scripts": {
1515
"build": "npx snowpack && npx snowpack build",
16-
"format": "npx prettier --write ./core_modules"
16+
"format": "npx prettier --write ./src"
1717
},
1818
"devDependencies": {
1919
"prettier": "2.1.1",
@@ -24,5 +24,10 @@
2424
"htm": "^3.0.3",
2525
"react": "^16.13.1",
2626
"react-dom": "^16.13.1"
27+
},
28+
"snowpack": {
29+
"installOptions": {
30+
"polyfillNode": true
31+
}
2732
}
2833
}

idom/client/build_config.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from copy import deepcopy
66
from hashlib import sha256
77
from pathlib import Path
8+
from contextlib import contextmanager
89
from importlib.machinery import SourceFileLoader
910
from pkgutil import iter_modules
1011
from typing import (
@@ -16,6 +17,7 @@
1617
TypeVar,
1718
Tuple,
1819
NamedTuple,
20+
Iterator,
1921
)
2022

2123
from jsonschema import validate as validate_schema
@@ -40,16 +42,18 @@ def __init__(self, path: Path) -> None:
4042
for name, entry in self.data["entries"].items()
4143
}
4244

43-
def get_entry(self, source_name: str) -> ConfigEntry:
44-
return self.data["entries"][source_name]
45-
46-
def get_entry_info(self, source_name: str) -> "ConfigEntryInfo":
47-
return self._entry_info[source_name]
48-
4945
def clear_entries(self) -> None:
5046
self.data["entries"].clear()
5147
self._entry_info.clear()
5248

49+
@contextmanager
50+
def change_entry(self, source_name: str) -> Iterator[ConfigEntry]:
51+
entry = self.data["entries"].setdefault(
52+
source_name, {"source_name": source_name}
53+
)
54+
yield entry
55+
self.update_entries([entry])
56+
5357
def update_entries(self, config_entries: Iterable[Dict[str, Any]]) -> None:
5458
# a useful debug assertion - the error which would result otherwise is confusing
5559
assert not isinstance(config_entries, dict), "expected list, not a dict"
@@ -68,11 +72,10 @@ def has_entry(self, source_name: str) -> bool:
6872

6973
def entry_has_dependency(self, source_name: str, dependency: str) -> bool:
7074
name, _ = split_package_name_and_version(dependency)
71-
entry_info = self.get_entry_info(source_name)
72-
return (
73-
name in entry_info.js_dependency_aliases
74-
or name == entry_info.js_package_def.name
75-
)
75+
if source_name not in self._entry_info:
76+
return False
77+
else:
78+
return name in self._entry_info[source_name].js_dependency_aliases
7679

7780
def resolve_js_dependency_name(
7881
self,

idom/client/manage.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import shutil
23
import subprocess
34
from pathlib import Path
@@ -89,7 +90,7 @@ def _build_and_save_config() -> None:
8990
snowpack_build = snowpack_config.setdefault("buildOptions", {})
9091
snowpack_build["clean"] = True
9192

92-
console.echo(f"Current config: {config.data}", debug=True)
93+
console.echo(f"IDOM build config: {config.data}", debug=True)
9394

9495
with console.spinner(
9596
f"Installing {len(packages_to_install)} dependencies"
@@ -98,6 +99,9 @@ def _build_and_save_config() -> None:
9899
):
99100
_npm_install(packages_to_install, temp_app_dir)
100101

102+
with package_json_path.open() as pkg_json_file:
103+
console.echo(f"Javascript package: {json.load(pkg_json_file)}", debug=True)
104+
101105
with console.spinner("Building client"):
102106
_npm_run_build(temp_app_dir)
103107

tests/conftest.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -276,30 +276,12 @@ def _clean_last_server_error(last_server_error) -> Iterator[None]:
276276
def install():
277277
def add_dependency(*packages):
278278
config = build_config()
279-
if not config.has_entry("tests"):
280-
build_client(
281-
[
282-
{
283-
"source_name": "tests",
284-
"js_dependencies": list(sorted(packages)),
285-
}
286-
]
287-
)
288-
else:
289-
to_install = set(packages)
290-
already_installed = config.get_entry("tests")["js_dependencies"]
291-
not_installed_pkgs = to_install.difference(already_installed)
292-
if not_installed_pkgs:
293-
build_client(
294-
[
295-
{
296-
"source_name": "tests",
297-
"js_dependencies": list(
298-
sorted(to_install.union(already_installed))
299-
),
300-
}
301-
]
302-
)
279+
with config.change_entry("tests") as entry:
280+
old_js_deps = entry.setdefault("js_dependencies", [])
281+
new_js_deps = set(packages).union(old_js_deps)
282+
entry["js_dependencies"] = list(new_js_deps)
283+
if old_js_deps != new_js_deps:
284+
build_client()
303285

304286
return add_dependency
305287

tests/test_cli/test_commands.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,16 @@ def test_show_environment():
6767
name, value = line.split("=")
6868
shown[name] = value
6969
assert shown == {name: os.environ[name] for name in settings.NAMES}
70+
71+
72+
def test_install():
73+
result = cli_runner.invoke(main, ["install", "jquery"])
74+
assert result.exit_code == 0
75+
assert build_config().has_entry("__main__")
76+
assert web_module_exists("__main__", "jquery")
77+
78+
result = cli_runner.invoke(main, ["install", "lodash"])
79+
assert result.exit_code == 0
80+
assert build_config().has_entry("__main__")
81+
assert web_module_exists("__main__", "jquery")
82+
assert web_module_exists("__main__", "lodash")

tests/test_client/test_manage.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ def test_web_module_exists(temp_build_config, victory_js):
2828
assert not web_module_exists("tests", "does/not/exist")
2929
assert web_module_exists("tests", "victory")
3030

31-
temp_build_config.get_entries("tests")["js_dependencies"].append(
32-
"module_not_installed_yet"
33-
)
31+
with temp_build_config.change_entry("tests") as entry:
32+
entry["js_dependencies"].append("module_not_installed_yet")
3433
assert not web_module_exists("tests", "module_not_installed_yet")
3534

3635

0 commit comments

Comments
 (0)