Skip to content

Commit

Permalink
Merge pull request #29 from jtpio/npm-cleanup
Browse files Browse the repository at this point in the history
More Changes Needed to Support Lumino
  • Loading branch information
blink1073 authored May 21, 2021
2 parents 9a791b4 + c7da61c commit 6499aa1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 37 deletions.
18 changes: 4 additions & 14 deletions jupyter_releaser/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,19 +295,14 @@ def build_python(dist_dir):

@main.command()
@add_options(dist_dir_options)
@click.option(
"--test-cmd",
envvar="RH_PY_TEST_COMMAND",
help="The command to run in the test venvs",
)
@use_checkout_dir()
def check_python(dist_dir, test_cmd):
def check_python(dist_dir):
"""Check Python dist files"""
for dist_file in glob(f"{dist_dir}/*"):
if Path(dist_file).suffix not in [".gz", ".whl"]:
util.log(f"Skipping non-python dist file {dist_file}")
continue
python.check_dist(dist_file, test_cmd=test_cmd)
python.check_dist(dist_file)


@main.command()
Expand All @@ -324,18 +319,13 @@ def build_npm(package, dist_dir):

@main.command()
@add_options(dist_dir_options)
@click.option(
"--test-cmd",
envvar="RH_NPM_TEST_COMMAND",
help="The command to run in isolated install.",
)
@use_checkout_dir()
def check_npm(dist_dir, test_cmd):
def check_npm(dist_dir):
"""Check npm package"""
if not osp.exists("./package.json"):
util.log("Skipping check-npm since there is no package.json file")
return
npm.check_dist(dist_dir, test_cmd=test_cmd)
npm.check_dist(dist_dir)


@main.command()
Expand Down
42 changes: 20 additions & 22 deletions jupyter_releaser/npm.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def build_dist(package, dist_dir):

if "workspaces" in data:
all_data = dict()
packages = data["workspaces"].get("packages", [])
for pattern in packages:
for pattern in _get_workspace_packages(data):
for path in glob(osp.join(basedir, pattern), recursive=True):
path = Path(path)
package_json = path / "package.json"
Expand Down Expand Up @@ -91,16 +90,18 @@ def extract_dist(dist_dir, target):
tar.extractall(target)
tar.close()

if "main" in data:
main = osp.join(target, "package", data["main"])
if not osp.exists(main):
raise ValueError(f"{name} is missing 'main' file {data['main']}")

shutil.move(str(target / "package"), str(pkg_dir))

return names


def check_dist(dist_dir, test_cmd=None):
def check_dist(dist_dir):
"""Check npm dist file(s) in a dist dir"""
if not test_cmd:
test_cmd = "node index.js"

tmp_dir = Path(TemporaryDirectory().name)
os.makedirs(tmp_dir)

Expand All @@ -114,11 +115,6 @@ def check_dist(dist_dir, test_cmd=None):

util.run(f"npm install {install_str}", cwd=tmp_dir)

text = "\n".join([f'require("{name}")' for name in names])
tmp_dir.joinpath("index.js").write_text(text, encoding="utf-8")

util.run(test_cmd, cwd=tmp_dir)

shutil.rmtree(str(tmp_dir), ignore_errors=True)


Expand Down Expand Up @@ -152,15 +148,7 @@ def get_package_versions(version):
message += f'\nnpm version: {data["name"]}: {data["version"]}'
if "workspaces" in data:
message += "\nnpm workspace versions:"

if isinstance(data["workspaces"], dict):
packages = []
for value in data["workspaces"].values():
packages.extend(value)
else:
packages = data["workspaces"]

for pattern in packages:
for pattern in _get_workspace_packages(data):
for path in glob(pattern, recursive=True):
text = Path(path).joinpath("package.json").read_text()
data = json.loads(text)
Expand All @@ -178,8 +166,7 @@ def tag_workspace_packages():
if not "workspaces" in data:
return

packages = data["workspaces"].get("packages", [])
for pattern in packages:
for pattern in _get_workspace_packages(data):
for path in glob(pattern, recursive=True):
sub_package_json = Path(path) / "package.json"
sub_data = json.loads(sub_package_json.read_text(encoding="utf-8"))
Expand All @@ -188,3 +175,14 @@ def tag_workspace_packages():
util.log(f"Skipping existing tag {tag_name}")
else:
util.run(f"git tag {tag_name}")


def _get_workspace_packages(data):
"""Get the workspace packages for a package given package data"""
if isinstance(data["workspaces"], dict):
packages = []
for value in data["workspaces"].values():
packages.extend(value)
else:
packages = data["workspaces"]
return packages
2 changes: 2 additions & 0 deletions jupyter_releaser/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ def workspace_package(npm_package):
pkg_json = new_dir / "package.json"
sub_data = json.loads(pkg_json.read_text(encoding="utf-8"))
sub_data["dependencies"] = dict(bar="*")
sub_data["main"] = "index.js"
pkg_json.write_text(json.dumps(sub_data), encoding="utf-8")
elif name == "baz":
pkg_json = new_dir / "package.json"
sub_data = json.loads(pkg_json.read_text(encoding="utf-8"))
sub_data["dependencies"] = dict(foo="*")
sub_data["main"] = "index.js"
pkg_json.write_text(json.dumps(sub_data), encoding="utf-8")
os.chdir(prev_dir)
util.run("git add .")
Expand Down
1 change: 0 additions & 1 deletion jupyter_releaser/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ def test_list_envvars(runner):
repo: RH_REPOSITORY
resolve-backports: RH_RESOLVE_BACKPORTS
since: RH_SINCE
test-cmd: RH_NPM_TEST_COMMAND
twine-cmd: TWINE_COMMAND
username: GITHUB_ACTOR
version-cmd: RH_VERSION_COMMAND
Expand Down

0 comments on commit 6499aa1

Please sign in to comment.