Skip to content

Commit

Permalink
feat: add eval command (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar authored Mar 19, 2022
1 parent 528228b commit f613be7
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 30 deletions.
91 changes: 62 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,33 +351,34 @@ notes.
# CLI

```
Usage: dzx [script] [args...]
Version: 0.3.0
Description:
🦕 A custom deno runtime for fun.
Options:
-h, --help - Show this help.
-V, --version - Show the version number for this program.
-A, --allow-all - Allow all permissions. (Depends: --worker)
--allow-env - Allow environment access. (Depends: --worker)
--allow-hrtime - Allow high resolution time measurement. (Depends: --worker)
--allow-net - Allow network access. (Depends: --worker)
--allow-ffi - Allow loading dynamic libraries. (Depends: --worker)
--allow-read - Allow file system read access. (Depends: --worker)
--allow-run - Allow running subprocesses. (Depends: --worker)
--allow-write - Allow file system write access. (Depends: --worker)
-w, --worker - Run script in an isolated web worker with it's own permissions.
Commands:
bundle [script] - Bundle an dzx script to a standalone deno sript.
compile [compile-options...] [script] [script-options...] - Combile an dzx script to a standalone binary.
repl - Start a dzx repl
upgrade - Upgrade dzx executable to latest or given version.
Usage: dzx [script] [args...]
Version: 0.3.0
Description:
🦕 A custom deno runtime for fun.
Options:
-h, --help - Show this help.
-V, --version - Show the version number for this program.
-A, --allow-all - Allow all permissions. (Depends: --worker)
--allow-env - Allow environment access. (Depends: --worker)
--allow-hrtime - Allow high resolution time measurement. (Depends: --worker)
--allow-net - Allow network access. (Depends: --worker)
--allow-ffi - Allow loading dynamic libraries. (Depends: --worker)
--allow-read - Allow file system read access. (Depends: --worker)
--allow-run - Allow running subprocesses. (Depends: --worker)
--allow-write - Allow file system write access. (Depends: --worker)
-w, --worker - Run script in an isolated web worker with it's own permissions.
Commands:
bundle [script] - Bundle an dzx script to a standalone deno sript.
compile [compile-options...] [script] [script-options...] - Combile an dzx script to a standalone binary.
eval <code> - Evaluate a dzx script from the command line.
repl - Start a dzx repl.
upgrade - Upgrade dzx executable to latest or given version.
```

- **dzx** `[script] [...args]`: Run a local or remote dzx script (optional in a
Expand Down Expand Up @@ -412,9 +413,41 @@ notes.
> flag with your permissions to specify the output file name, otherwise the
> compiled file will be emitted with a random value as its name.
- **dzx repl**: Start a dzx repl (deno repl bootstrapped with dzx).
- **dzx eval**: Evaluate a dzx script from command line.
```shell
dzx eval "console.log($.shell)"
```
Eval can also read from stdin:

```shell
echo "console.log($.shell)" | dzx eval
```

- **dzx repl**: Start a dzx repl (Read eval print loop).

- **dzx upgrade**: Start a dzx repl.
The `repl` command starts a deno repl bootstrapped with the dzx runtime code.

- **dzx upgrade**: Upgrade the `dzx` executable to latest or given version.

Upgrade to latest version:

```shell
dzx upgrade
```

Upgrade to specific version:

```shell
dzx upgrade --version 3.0.0
```

List all available versions:

```shell
dzx upgrade --list-versions
```

## Contributing

Expand Down
33 changes: 33 additions & 0 deletions src/cli/eval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Command, ValidationError } from "./deps.ts";
import { bootstrapScript, importModule } from "./lib/bootstrap.ts";
import { getModuleFromStdin } from "./lib/stream.ts";

export function evalCommand() {
return new Command<void>()
.description(`Evaluate a dzx script from the command line.\n
dzx eval "console.log($.shell)"\n
Or read from stdin:\n
echo "console.log($.shell)" | dzx eval
`)
.arguments("<code:string>")
.useRawArgs()
.action(
async function (_: void, ...args: Array<string>) {
if (["-h", "--help"].includes(args[0])) {
this.showHelp();
Deno.exit(0);
}
const code = args.shift();

if (!code && Deno.isatty(Deno.stdin.rid)) {
throw new ValidationError(`Missing argument(s): script`);
}

const mainModule: string = code
? bootstrapScript(code)
: await getModuleFromStdin();

await importModule({ mainModule });
},
);
}
2 changes: 2 additions & 0 deletions src/cli/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ValidationError,
} from "./deps.ts";
import { addProtocol } from "../_utils.ts";
import { evalCommand } from "./eval.ts";
import { importModule } from "./lib/bootstrap.ts";
import { getModuleFromStdin } from "./lib/stream.ts";
import { getMarkdownModule } from "./lib/markdown.ts";
Expand Down Expand Up @@ -102,6 +103,7 @@ export function dzx() {
)
.command("bundle", bundleCommand())
.command("compile", compileCommand())
.command("eval", evalCommand())
.command("repl", replCommand())
.command(
"upgrade",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Command } from "./deps.ts";

export function replCommand() {
return new Command<void>()
.description("Start a dzx repl")
.description("Start a dzx repl.")
.option(
"--compat",
"Node compatibility mode. Currently only enables built-in node modules like 'fs' and globals like 'process'",
Expand Down

0 comments on commit f613be7

Please sign in to comment.