Skip to content

Commit

Permalink
Add CLI command to directly execute a Sol command
Browse files Browse the repository at this point in the history
  • Loading branch information
tobeno committed Jul 16, 2023
1 parent 2e6fdbf commit 15a3dda
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ So you can use all of those:

The **final result** of a command / pipeline is printed on the shell (e.g. `['a', 'b'].join(', ')` will show `a, b`).

Inside the interactive Sol REPL, you can also use `something.await` instead of `await something` to avoid jumping back and forth.
Inside the interactive Sol REPL, you can also use `something.await` instead of `await something` to avoid jumping back
and forth.

Sol is based on the [NodeJS REPL](https://nodejs.org/api/repl.html),
so all REPL features are available.
Expand Down Expand Up @@ -203,9 +204,15 @@ After the creation you still need to load the extension as described above.

### CLI

#### Play
#### Exec

To directly execute a play script without the REPL, you can use `sol play <name>`.
You can use Sol to directly execute Sol commands on the command line using `sol exec <command>`.

Example:

```sh
sol exec 'text("test").uppercased'
```

#### Pipe

Expand All @@ -215,9 +222,12 @@ Example:

```sh
cat package.json | sol pipe '.json.get("dependencies").keys.join(", ")'

```

#### Play

To directly execute a play script without the REPL, you can use `sol play <name>`.

#### Upgrade

To upgrade Sol to the latest version, you can use `sol upgrade`.
Expand Down
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Command } from 'commander';
import { execCommand } from './commands/exec.command';
import { pipeCommand } from './commands/pipe.command';
import { playCommand } from './commands/play.command';
import { upgradeCommand } from './commands/upgrade.command';

export async function runCli(args: string[]): Promise<void> {
const program = new Command();

program.addCommand(execCommand());
program.addCommand(pipeCommand());
program.addCommand(playCommand());
program.addCommand(upgradeCommand());
Expand Down
18 changes: 18 additions & 0 deletions src/commands/exec.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Command } from 'commander';
import * as vm from 'vm';

export function execCommand(): Command {
return new Command('exec')
.description('Executed the given command')
.argument(
'<command>',
'Command to run (e.g. `text("test").uppercased` to uppercase "test")',
)
.action(async (command: string) => {
// Use VM to execute command in a sandbox
const output = vm.runInThisContext(command);

// Write output to stdout
log(output);
});
}

0 comments on commit 15a3dda

Please sign in to comment.