diff --git a/README.md b/README.md index 137613d..091544f 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,27 @@ Installation of cyjs is pretty simplistic and you can get the released version f pip install cyjs ``` +## Running as a Standalone script + +Currently there is a new standalone script if you need +to run javascript directly like with quickjs although +it currently is in it's early phases. It currently uses +[typer](https://typer.tiangolo.com/) as a backend (which was developed by the same people who wrote fastapi which is another popular python package). + +``` +pip install cyjs[cli] +``` + +### Using the cli + +It should not be difficult although cyjs is still not very mature yet but hopefully the commandline will help to patch a few bugs or make it easier to test different aspects of quickjs-ng. + +``` +py -m cyjs -e "console.log('Hello Vizonex');" +Hello Vizonex +``` + + ### Using Unreleased Development Versions If your planning to contribute or want in on anything newly added you can clone the github repo there is however one diff --git a/cyjs/__main__.py b/cyjs/__main__.py new file mode 100644 index 0000000..544ef3c --- /dev/null +++ b/cyjs/__main__.py @@ -0,0 +1,33 @@ +from pathlib import Path +from typing import Annotated + +from typer import Argument, Option, Typer + +from ._cyjs import Context + +app = Typer() + +@app.command() +def cli( + args:Annotated[list[str] | None, Argument(help="Arguments to pass to quickjs script")] = None, + script: Annotated[Path | None, Option("-C", "--script", help="load module as a script", exists=True)] = None, + eval_code: Annotated[str | None, Option("-e", "--eval", metavar="EVAL", help= "evaluate EXPR")] = None, + strict: bool = False, + backtrace_barrier: bool = False, + promise: bool = False +) -> None: + """A Simplistic script runner for testing code with cyjs""" + + ctx = Context() + if args: + ctx.set_script_arguments(*args) + ctx.add_std_print_handlers() + + if script: + code = script.read_bytes() + ctx.eval_module(code, script.as_posix(), strict, backtrace_barrier, promise) + elif eval_code: + ctx.eval(eval_code, strict=strict, backtrace_barrier=backtrace_barrier, promise=promise) + +if __name__ == "__main__": + app() diff --git a/pyproject.toml b/pyproject.toml index fa7d1f4..5efd2fb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,6 @@ authors = [ { name = "Vizonex", email = "VizonexBusiness@gmail.com" } ] requires-python = ">=3.10" -dependencies = [] [tool.cibuildwheel] @@ -28,3 +27,8 @@ version = {attr = "cyjs.__version__"} [project.urls] repository = "https://github.com/Vizonex/cyjs.git" + +[project.optional-dependencies] +cli = [ + "typer>=0.27.2", +]