Skip to content

Commit f613be7

Browse files
authored
feat: add eval command (#23)
1 parent 528228b commit f613be7

File tree

4 files changed

+98
-30
lines changed

4 files changed

+98
-30
lines changed

README.md

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -351,33 +351,34 @@ notes.
351351
# CLI
352352

353353
```
354-
Usage: dzx [script] [args...]
355-
Version: 0.3.0
356-
357-
Description:
358-
359-
🦕 A custom deno runtime for fun.
360-
361-
Options:
362-
363-
-h, --help - Show this help.
364-
-V, --version - Show the version number for this program.
365-
-A, --allow-all - Allow all permissions. (Depends: --worker)
366-
--allow-env - Allow environment access. (Depends: --worker)
367-
--allow-hrtime - Allow high resolution time measurement. (Depends: --worker)
368-
--allow-net - Allow network access. (Depends: --worker)
369-
--allow-ffi - Allow loading dynamic libraries. (Depends: --worker)
370-
--allow-read - Allow file system read access. (Depends: --worker)
371-
--allow-run - Allow running subprocesses. (Depends: --worker)
372-
--allow-write - Allow file system write access. (Depends: --worker)
373-
-w, --worker - Run script in an isolated web worker with it's own permissions.
374-
375-
Commands:
376-
377-
bundle [script] - Bundle an dzx script to a standalone deno sript.
378-
compile [compile-options...] [script] [script-options...] - Combile an dzx script to a standalone binary.
379-
repl - Start a dzx repl
380-
upgrade - Upgrade dzx executable to latest or given version.
354+
Usage: dzx [script] [args...]
355+
Version: 0.3.0
356+
357+
Description:
358+
359+
🦕 A custom deno runtime for fun.
360+
361+
Options:
362+
363+
-h, --help - Show this help.
364+
-V, --version - Show the version number for this program.
365+
-A, --allow-all - Allow all permissions. (Depends: --worker)
366+
--allow-env - Allow environment access. (Depends: --worker)
367+
--allow-hrtime - Allow high resolution time measurement. (Depends: --worker)
368+
--allow-net - Allow network access. (Depends: --worker)
369+
--allow-ffi - Allow loading dynamic libraries. (Depends: --worker)
370+
--allow-read - Allow file system read access. (Depends: --worker)
371+
--allow-run - Allow running subprocesses. (Depends: --worker)
372+
--allow-write - Allow file system write access. (Depends: --worker)
373+
-w, --worker - Run script in an isolated web worker with it's own permissions.
374+
375+
Commands:
376+
377+
bundle [script] - Bundle an dzx script to a standalone deno sript.
378+
compile [compile-options...] [script] [script-options...] - Combile an dzx script to a standalone binary.
379+
eval <code> - Evaluate a dzx script from the command line.
380+
repl - Start a dzx repl.
381+
upgrade - Upgrade dzx executable to latest or given version.
381382
```
382383

383384
- **dzx** `[script] [...args]`: Run a local or remote dzx script (optional in a
@@ -412,9 +413,41 @@ notes.
412413
> flag with your permissions to specify the output file name, otherwise the
413414
> compiled file will be emitted with a random value as its name.
414415
415-
- **dzx repl**: Start a dzx repl (deno repl bootstrapped with dzx).
416+
- **dzx eval**: Evaluate a dzx script from command line.
417+
418+
```shell
419+
dzx eval "console.log($.shell)"
420+
```
421+
422+
Eval can also read from stdin:
423+
424+
```shell
425+
echo "console.log($.shell)" | dzx eval
426+
```
427+
428+
- **dzx repl**: Start a dzx repl (Read eval print loop).
416429

417-
- **dzx upgrade**: Start a dzx repl.
430+
The `repl` command starts a deno repl bootstrapped with the dzx runtime code.
431+
432+
- **dzx upgrade**: Upgrade the `dzx` executable to latest or given version.
433+
434+
Upgrade to latest version:
435+
436+
```shell
437+
dzx upgrade
438+
```
439+
440+
Upgrade to specific version:
441+
442+
```shell
443+
dzx upgrade --version 3.0.0
444+
```
445+
446+
List all available versions:
447+
448+
```shell
449+
dzx upgrade --list-versions
450+
```
418451

419452
## Contributing
420453

src/cli/eval.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Command, ValidationError } from "./deps.ts";
2+
import { bootstrapScript, importModule } from "./lib/bootstrap.ts";
3+
import { getModuleFromStdin } from "./lib/stream.ts";
4+
5+
export function evalCommand() {
6+
return new Command<void>()
7+
.description(`Evaluate a dzx script from the command line.\n
8+
dzx eval "console.log($.shell)"\n
9+
Or read from stdin:\n
10+
echo "console.log($.shell)" | dzx eval
11+
`)
12+
.arguments("<code:string>")
13+
.useRawArgs()
14+
.action(
15+
async function (_: void, ...args: Array<string>) {
16+
if (["-h", "--help"].includes(args[0])) {
17+
this.showHelp();
18+
Deno.exit(0);
19+
}
20+
const code = args.shift();
21+
22+
if (!code && Deno.isatty(Deno.stdin.rid)) {
23+
throw new ValidationError(`Missing argument(s): script`);
24+
}
25+
26+
const mainModule: string = code
27+
? bootstrapScript(code)
28+
: await getModuleFromStdin();
29+
30+
await importModule({ mainModule });
31+
},
32+
);
33+
}

src/cli/mod.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ValidationError,
1010
} from "./deps.ts";
1111
import { addProtocol } from "../_utils.ts";
12+
import { evalCommand } from "./eval.ts";
1213
import { importModule } from "./lib/bootstrap.ts";
1314
import { getModuleFromStdin } from "./lib/stream.ts";
1415
import { getMarkdownModule } from "./lib/markdown.ts";
@@ -102,6 +103,7 @@ export function dzx() {
102103
)
103104
.command("bundle", bundleCommand())
104105
.command("compile", compileCommand())
106+
.command("eval", evalCommand())
105107
.command("repl", replCommand())
106108
.command(
107109
"upgrade",

src/cli/repl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Command } from "./deps.ts";
33

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

0 commit comments

Comments
 (0)