From afebf8bcf422cd6b968b843157688b4a5da1646f Mon Sep 17 00:00:00 2001 From: checktheroads Date: Sun, 22 Mar 2020 17:28:54 -0700 Subject: [PATCH] re-initialize node_modules on any package.json changes --- hyperglass/util.py | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/hyperglass/util.py b/hyperglass/util.py index 0c13e23..faa94f7 100644 --- a/hyperglass/util.py +++ b/hyperglass/util.py @@ -380,6 +380,33 @@ async def node_initial(dev_mode=False): return "\n".join(all_messages) +async def read_package_json(): + """Import package.json as a python dict. + + Raises: + RuntimeError: Raised if unable to read package.json + + Returns: + {dict} -- NPM package.json as dict + """ + from pathlib import Path + import ujson + + package_json_file = Path(__file__).parent / "ui" / "package.json" + + try: + + with package_json_file.open("r") as file: + package_json = ujson.load(file) + + except Exception as e: + raise RuntimeError(f"Error reading package.json: {str(e)}") + + log.debug("package.json:\n{p}", p=package_json) + + return package_json + + async def build_frontend( # noqa: C901 dev_mode, dev_url, prod_url, params, app_path, force=False ): @@ -417,7 +444,13 @@ async def build_frontend( # noqa: C901 env_file = Path("/tmp/hyperglass.env.json") # noqa: S108 - env_vars = {"_HYPERGLASS_CONFIG_": params, "_HYPERGLASS_VERSION_": __version__} + package_json = await read_package_json() + + env_vars = { + "_HYPERGLASS_CONFIG_": params, + "_HYPERGLASS_VERSION_": __version__, + "_HYPERGLASS_PACKAGE_JSON_": package_json, + } # Set NextJS production/development mode and base URL based on # developer_mode setting. @@ -434,7 +467,8 @@ async def build_frontend( # noqa: C901 elif not initialized: log.debug("node_modules has not been initialized. Starting initialization...") node_setup = await node_initial(dev_mode) - log.debug(node_setup) + if node_setup == "": + log.debug("Re-initialized node_modules") try: env_json = json.dumps(env_vars) @@ -480,8 +514,14 @@ async def build_frontend( # noqa: C901 # While temporary file is still open, initiate UI build process. if not dev_mode or force: + initialize_result = await node_initial(dev_mode) build_result = await build_ui(app_path=app_path) + if initialize_result: + log.debug(initialize_result) + elif initialize_result == "": + log.debug("Re-initialized node_modules") + if build_result: log.debug("Completed UI build") elif dev_mode and not force: