Skip to content

Commit 551c84b

Browse files
fredreandre
andauthored
add docs for exec cli (#1849)
Add some docs for the exec cli command for go and ts. Also deprecate the alpha version of the command and refer to using the non-alpha version. --------- Co-authored-by: André Eriksson <[email protected]>
1 parent 8d18c3d commit 551c84b

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed

cli/cmd/encore/exec.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ var execCmd = &cobra.Command{
2323
execScript(appRoot, wd, args)
2424
},
2525
}
26+
var execCmdAlpha = &cobra.Command{
27+
Use: "exec path/to/script [args...]",
28+
Short: "Runs executable scripts against the local Encore app",
29+
Hidden: true,
30+
Deprecated: "use \"encore exec\" instead",
31+
Run: func(cmd *cobra.Command, args []string) {
32+
if len(args) == 0 {
33+
args = []string{"."} // current directory
34+
}
35+
appRoot, wd := determineAppRoot()
36+
execScript(appRoot, wd, args)
37+
},
38+
}
2639

2740
func execScript(appRoot, relWD string, args []string) {
2841
interrupt := make(chan os.Signal, 1)
@@ -64,6 +77,6 @@ func init() {
6477

6578
func init() {
6679
execCmd.Flags().StringVarP(&nsName, "namespace", "n", "", "Namespace to use (defaults to active namespace)")
67-
alphaCmd.AddCommand(execCmd)
80+
alphaCmd.AddCommand(execCmdAlpha)
6881
rootCmd.AddCommand(execCmd)
6982
}

docs/go/cli/cli-reference.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ Checks your application for compile-time errors using Encore's compiler.
3434
$ encore check
3535
```
3636

37+
38+
#### Exec
39+
40+
Runs executable scripts against the local Encore app.
41+
42+
Compiles and runs a go script with the local Encore app environment setup.
43+
44+
```
45+
$ encore exec <path/to/script>
46+
```
47+
48+
##### Example
49+
50+
Run a database seed script
51+
52+
```
53+
$ encore exec cmd/seed
54+
```
55+
3756
## App
3857

3958
Commands to create and link Encore apps

docs/menu.cue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,11 @@
813813
text: "Multithreading"
814814
path: "/ts/develop/multithreading"
815815
file: "ts/develop/multithreading"
816+
},{
817+
kind: "basic"
818+
text: "Running Scripts"
819+
path: "/ts/develop/running-scripts"
820+
file: "ts/develop/running-scripts"
816821
}]
817822
},
818823
{

docs/ts/cli/cli-reference.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ Checks your application for compile-time errors using Encore's compiler.
3434
$ encore check
3535
```
3636

37+
#### Exec
38+
39+
Runs executable scripts against the local Encore app.
40+
41+
Takes a command that it will execute with the local Encore app environment setup.
42+
43+
```
44+
$ encore exec -- <command>
45+
```
46+
47+
##### Example
48+
49+
Run a database seed script
50+
51+
```
52+
$ encore exec -- npx tsx ./seed.ts
53+
```
54+
3755
## App
3856

3957
Commands to create and link Encore apps

docs/ts/develop/running-scripts.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
seotitle: How to use `encore exec` for running scripts
3+
seodesc: Learn how to use the `encore exec` command to run scripts like database seeding in your Encore app.
4+
title: Running Scripts
5+
subtitle: Run scripts with your application's infrastructure and runtime configured and initialized
6+
lang: ts
7+
---
8+
In local development, you may need to run scripts or commands, such as seeding a database with initial data.
9+
For that to work the database needs to be started, and the Encore runtime needs to be configured and initialized.
10+
11+
## Using `encore exec`
12+
13+
The `encore exec` command allows you to execute custom commands while leveraging Encore's infrastructure setup. This is particularly useful for tasks like database seeding, running scripts, or other one-off commands that require the app's environment to be initialized.
14+
15+
### How it works
16+
17+
The `encore exec` command initializes the required infrastructure for your Encore app and executes the specified command.
18+
This ensures that your commands run in the correct context with all dependencies properly configured.
19+
20+
### Example: Database Seeding
21+
22+
In this example, `npx tsx ./seed.ts` runs a TypeScript script (`seed.ts`) to populate the database with initial data
23+
24+
```bash
25+
encore exec -- npx tsx ./seed.ts
26+
```
27+
28+
Here’s what happens:
29+
1. Encore initializes the app infrastructure.
30+
2. The `npx tsx ./seed.ts` command is executed in the context of the initialized app.
31+
32+
### General Syntax
33+
34+
```bash
35+
encore exec -- <your-command>
36+
```
37+
38+
Substitute `<your-command>` with the specific command you wish to run.
39+
40+
### Use Cases
41+
42+
- **Database Seeding**: Populate your database with initial data using a script.
43+
- **Client Generation**: Generate a client for interacting with an external dependency.
44+
- **Custom Scripts**: Run any script that depends on the app's initialized environment.
45+
46+
### Notes
47+
48+
- Ensure that the command you provide is executable in your environment.
49+
- Use `--` to separate `encore exec` options from the command you want to run.
50+

0 commit comments

Comments
 (0)